From 172e1cf3bf87ae892ce39964c6a670790ffab7d0 Mon Sep 17 00:00:00 2001 From: Aditya Bist Date: Tue, 31 Jul 2018 16:27:11 -0700 Subject: [PATCH] put parse syntax in command palette instead of editor (#2103) --- .../parts/query/common/query.contribution.ts | 11 ++- src/sql/parts/query/editor/queryEditor.ts | 7 +- .../query/execution/keyboardQueryActions.ts | 68 +++++++++++++++++++ src/sql/parts/query/execution/queryActions.ts | 50 -------------- 4 files changed, 80 insertions(+), 56 deletions(-) diff --git a/src/sql/parts/query/common/query.contribution.ts b/src/sql/parts/query/common/query.contribution.ts index f1ac06cf39..965fade9ef 100644 --- a/src/sql/parts/query/common/query.contribution.ts +++ b/src/sql/parts/query/common/query.contribution.ts @@ -25,7 +25,7 @@ import { EditDataEditor } from 'sql/parts/editData/editor/editDataEditor'; import { EditDataInput } from 'sql/parts/editData/common/editDataInput'; import { RunQueryKeyboardAction, RunCurrentQueryKeyboardAction, CancelQueryKeyboardAction, RefreshIntellisenseKeyboardAction, ToggleQueryResultsKeyboardAction, - RunQueryShortcutAction, RunCurrentQueryWithActualPlanKeyboardAction, FocusOnCurrentQueryKeyboardAction + RunQueryShortcutAction, RunCurrentQueryWithActualPlanKeyboardAction, FocusOnCurrentQueryKeyboardAction, ParseSyntaxAction } from 'sql/parts/query/execution/keyboardQueryActions'; import * as gridActions from 'sql/parts/grid/views/gridActions'; import * as gridCommands from 'sql/parts/grid/views/gridCommands'; @@ -154,6 +154,15 @@ actionRegistry.registerWorkbenchAction( FocusOnCurrentQueryKeyboardAction.LABEL ); +actionRegistry.registerWorkbenchAction( + new SyncActionDescriptor( + ParseSyntaxAction, + ParseSyntaxAction.ID, + ParseSyntaxAction.LABEL + ), + ParseSyntaxAction.LABEL +); + // Grid actions actionRegistry.registerWorkbenchAction( diff --git a/src/sql/parts/query/editor/queryEditor.ts b/src/sql/parts/query/editor/queryEditor.ts index 8619622ff5..f7c71b1fa9 100644 --- a/src/sql/parts/query/editor/queryEditor.ts +++ b/src/sql/parts/query/editor/queryEditor.ts @@ -42,7 +42,7 @@ import { Taskbar, ITaskbarContent } from 'sql/base/browser/ui/taskbar/taskbar'; import { RunQueryAction, CancelQueryAction, ListDatabasesAction, ListDatabasesActionItem, ConnectDatabaseAction, ToggleConnectDatabaseAction, EstimatedQueryPlanAction, - ActualQueryPlanAction, ParseSyntaxAction + ActualQueryPlanAction } from 'sql/parts/query/execution/queryActions'; import { IQueryModelService } from 'sql/parts/query/execution/queryModel'; import { IEditorDescriptorService } from 'sql/parts/query/editor/editorDescriptorService'; @@ -88,7 +88,6 @@ export class QueryEditor extends BaseEditor { private _listDatabasesAction: ListDatabasesAction; private _estimatedQueryPlanAction: EstimatedQueryPlanAction; private _actualQueryPlanAction: ActualQueryPlanAction; - private _parseSyntaxAction: ParseSyntaxAction; private _savedViewStates = new Map(); private _resultViewStateChangeEmitters = new Map; onRestoreViewState: Emitter }>(); @@ -456,7 +455,6 @@ export class QueryEditor extends BaseEditor { this._listDatabasesAction = this._instantiationService.createInstance(ListDatabasesAction, this); this._estimatedQueryPlanAction = this._instantiationService.createInstance(EstimatedQueryPlanAction, this); this._actualQueryPlanAction = this._instantiationService.createInstance(ActualQueryPlanAction, this); - this._parseSyntaxAction = this._instantiationService.createInstance(ParseSyntaxAction, this); // Create HTML Elements for the taskbar let separator = Taskbar.createTaskbarSeparator(); @@ -470,8 +468,7 @@ export class QueryEditor extends BaseEditor { { action: this._changeConnectionAction }, { action: this._listDatabasesAction }, { element: separator }, - { action: this._estimatedQueryPlanAction }, - { action: this._parseSyntaxAction } + { action: this._estimatedQueryPlanAction } ]; this._taskbar.setContent(content); } diff --git a/src/sql/parts/query/execution/keyboardQueryActions.ts b/src/sql/parts/query/execution/keyboardQueryActions.ts index abba8c849c..e84836f0a0 100644 --- a/src/sql/parts/query/execution/keyboardQueryActions.ts +++ b/src/sql/parts/query/execution/keyboardQueryActions.ts @@ -20,6 +20,7 @@ import * as WorkbenchUtils from 'sql/workbench/common/sqlWorkbenchUtils'; import * as Constants from 'sql/parts/query/common/constants'; import * as ConnectionConstants from 'sql/parts/connection/common/constants'; import { EditDataEditor } from 'sql/parts/editData/editor/editDataEditor'; +import { INotificationService, Severity } from 'vs/platform/notification/common/notification'; const singleQuote = '\''; @@ -387,4 +388,71 @@ export class RunQueryShortcutAction extends Action { let info = this._connectionManagementService.getConnectionInfo(editor.uri); return info.connectionProfile.databaseName; } +} + +/** + * Action class that parses the query string in the current SQL text document. + */ +export class ParseSyntaxAction extends Action { + + public static ID = 'parseQueryAction'; + public static LABEL = nls.localize('parseSyntaxLabel', 'Parse Query'); + + constructor( + id: string, + label: string, + @IConnectionManagementService private _connectionManagementService: IConnectionManagementService, + @IQueryManagementService private _queryManagementService: IQueryManagementService, + @IWorkbenchEditorService private _editorService: IWorkbenchEditorService, + @INotificationService private _notificationService: INotificationService + ) { + super(id, label); + this.enabled = true; + } + + public run(): TPromise { + let editor = this._editorService.getActiveEditor(); + if (editor && editor instanceof QueryEditor) { + let queryEditor: QueryEditor = editor; + if (!queryEditor.isSelectionEmpty()) { + if (this.isConnected(queryEditor)) { + let text = queryEditor.getSelectionText(); + if (text === '') { + text = queryEditor.getAllText(); + } + this._queryManagementService.parseSyntax(queryEditor.connectedUri, text).then(result => { + if (result && result.parseable) { + this._notificationService.notify({ + severity: Severity.Info, + message: nls.localize('queryActions.parseSyntaxSuccess', 'Commands completed successfully') + }); + } else if (result && result.errors.length > 0) { + let errorMessage = nls.localize('queryActions.parseSyntaxFailure', 'Command failed: '); + this._notificationService.error(`${errorMessage}${result.errors[0]}`); + + } + }); + } else { + this._notificationService.notify({ + severity: Severity.Error, + message: nls.localize('queryActions.notConnected', 'Please connect to a server') + }); + } + } + + } + + return TPromise.as(null); + } + + /** + * Returns the URI of the given editor if it is not undefined and is connected. + * Public for testing only. + */ + private isConnected(editor: QueryEditor): boolean { + if (!editor || !editor.currentQueryInput) { + return false; + } + return this._connectionManagementService.isConnected(editor.currentQueryInput.uri); + } } \ No newline at end of file diff --git a/src/sql/parts/query/execution/queryActions.ts b/src/sql/parts/query/execution/queryActions.ts index 189647b815..f680764561 100644 --- a/src/sql/parts/query/execution/queryActions.ts +++ b/src/sql/parts/query/execution/queryActions.ts @@ -25,7 +25,6 @@ import { QueryEditor } from 'sql/parts/query/editor/queryEditor'; import { IQueryModelService } from 'sql/parts/query/execution/queryModel'; import { INotificationService } from 'vs/platform/notification/common/notification'; import Severity from 'vs/base/common/severity'; -import { IQueryManagementService } from 'sql/parts/query/common/queryManagement'; /** * Action class that query-based Actions will extend. This base class automatically handles activating and @@ -418,55 +417,6 @@ export class ListDatabasesAction extends QueryTaskbarAction { } } -/** - * Action class that parses the query string in the current SQL text document. - */ -export class ParseSyntaxAction extends QueryTaskbarAction { - - public static EnabledClass = ''; - public static ID = 'parseQueryAction'; - - constructor( - editor: QueryEditor, - @IConnectionManagementService connectionManagementService: IConnectionManagementService, - @IQueryManagementService private _queryManagementService: IQueryManagementService, - @INotificationService private _notificationService: INotificationService, - ) { - super(connectionManagementService, editor, ParseSyntaxAction.ID, ParseSyntaxAction.EnabledClass); - this.enabled = true; - this.label = nls.localize('parseSyntaxLabel', 'Parse Query'); - } - - public run(): TPromise { - if (!this.editor.isSelectionEmpty()) { - if (this.isConnected(this.editor)) { - let text = this.editor.getSelectionText(); - if (text === '') { - text = this.editor.getAllText(); - } - this._queryManagementService.parseSyntax(this.editor.connectedUri, text).then(result => { - if (result && result.parseable) { - this._notificationService.notify({ - severity: Severity.Info, - message: nls.localize('queryActions.parseSyntaxSuccess', 'Commands completed successfully') - }); - } else if (result && result.errors.length > 0) { - let errorMessage = nls.localize('queryActions.parseSyntaxFailure', 'Command failed: '); - this._notificationService.error(`${errorMessage}${result.errors[0]}`); - - } - }); - } else { - this._notificationService.notify({ - severity: Severity.Error, - message: nls.localize('queryActions.notConnected', 'Please connect to a server') - }); - } - } - return TPromise.as(null); - } -} - /* * Action item that handles the dropdown (combobox) that lists the available databases. * Based off StartDebugActionItem.