diff --git a/src/sql/workbench/browser/scriptingUtils.ts b/src/sql/workbench/browser/scriptingUtils.ts index 7cfbdc6a64..30102a1622 100644 --- a/src/sql/workbench/browser/scriptingUtils.ts +++ b/src/sql/workbench/browser/scriptingUtils.ts @@ -45,7 +45,7 @@ export async function scriptSelect(connectionProfile: IConnectionProfile, metada let paramDetails: azdata.ScriptingParamDetails = getScriptingParamDetails(connectionService, connectionResult, metadata); const result = await scriptingService.script(connectionResult, metadata, ScriptOperation.Select, paramDetails); if (result && result.script) { - const owner = await queryEditorService.newSqlEditor(result.script); + const owner = await queryEditorService.newSqlEditor({ initalContent: result.script }); // Connect our editor to the input connection let options: IConnectionCompletionOptions = { params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.executeQuery, input: owner }, @@ -127,7 +127,7 @@ export async function script(connectionProfile: IConnectionProfile, metadata: az if (script) { let description = (metadata.schema && metadata.schema !== '') ? `${metadata.schema}.${metadata.name}` : metadata.name; - const owner = await queryEditorService.newSqlEditor(script, connectionProfile.providerName, undefined, description); + const owner = await queryEditorService.newSqlEditor({ initalContent: script, description }); // Connect our editor to the input connection let options: IConnectionCompletionOptions = { params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.none, input: owner }, diff --git a/src/sql/workbench/browser/taskUtilities.ts b/src/sql/workbench/browser/taskUtilities.ts index 155a0a8336..19f8f10e6e 100644 --- a/src/sql/workbench/browser/taskUtilities.ts +++ b/src/sql/workbench/browser/taskUtilities.ts @@ -79,10 +79,10 @@ export function getCurrentGlobalConnection(objectExplorerService: IObjectExplore if (activeInput) { // dashboard Connection if (activeInput instanceof DashboardInput && activeInput.uri) { - connection = connectionManagementService.getConnectionProfile(activeInput.uri.toString()); + connection = connectionManagementService.getConnectionProfile(activeInput.uri); } else if (activeInput.resource) { // editor Connection - connection = connectionManagementService.getConnectionProfile(activeInput.resource.toString()); + connection = connectionManagementService.getConnectionProfile(activeInput.resource.toString(true)); } } diff --git a/src/sql/workbench/contrib/query/browser/queryActions.ts b/src/sql/workbench/contrib/query/browser/queryActions.ts index 5911b4d668..c05cf92bbf 100644 --- a/src/sql/workbench/contrib/query/browser/queryActions.ts +++ b/src/sql/workbench/contrib/query/browser/queryActions.ts @@ -120,7 +120,7 @@ export function openNewQuery(accessor: ServicesAccessor, profile?: IConnectionPr if (!profile) { profile = getCurrentGlobalConnection(objectExplorerService, connectionManagementService, editorService); } - return queryEditorService.newSqlEditor(initalContent).then((owner: IConnectableInput) => { + return queryEditorService.newSqlEditor({ initalContent }).then((owner: IConnectableInput) => { // Connect our editor to the input connection let options: IConnectionCompletionOptions = { params: { connectionType: ConnectionType.editor, runQueryOnCompletion: onConnection, input: owner }, diff --git a/src/sql/workbench/contrib/query/browser/queryInputFactory.ts b/src/sql/workbench/contrib/query/browser/queryInputFactory.ts index 524e16c7e5..b06a46f456 100644 --- a/src/sql/workbench/contrib/query/browser/queryInputFactory.ts +++ b/src/sql/workbench/contrib/query/browser/queryInputFactory.ts @@ -42,7 +42,7 @@ export class QueryEditorLanguageAssociation implements ILanguageAssociation { queryEditorInput = this.instantiationService.createInstance(FileQueryEditorInput, '', activeEditor, queryResultsInput); } else if (activeEditor instanceof UntitledTextEditorInput) { const content = (await activeEditor.resolve()).textEditorModel.getValue(); - queryEditorInput = (this.queryEditorService.newSqlEditor(content) as any) as UntitledQueryEditorInput; + queryEditorInput = await this.queryEditorService.newSqlEditor({ open: false, initalContent: content }) as UntitledQueryEditorInput; } else { return undefined; } diff --git a/src/sql/workbench/contrib/tasks/common/tasksAction.ts b/src/sql/workbench/contrib/tasks/common/tasksAction.ts index 4692c51007..b96e2d47c4 100644 --- a/src/sql/workbench/contrib/tasks/common/tasksAction.ts +++ b/src/sql/workbench/contrib/tasks/common/tasksAction.ts @@ -60,7 +60,7 @@ export class ScriptAction extends Action { public async run(element: TaskNode): Promise { if (element instanceof TaskNode) { if (element.script && element.script !== '') { - this._queryEditorService.newSqlEditor(element.script); + this._queryEditorService.newSqlEditor({ initalContent: element.script }); } } return true; diff --git a/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts b/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts index 51d7b31222..bb36f26c9c 100644 --- a/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts +++ b/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts @@ -6,7 +6,7 @@ import { QueryResultsInput } from 'sql/workbench/common/editor/query/queryResultsInput'; import { EditDataInput } from 'sql/workbench/browser/editData/editDataInput'; import { IConnectableInput } from 'sql/platform/connection/common/connectionManagement'; -import { IQueryEditorService } from 'sql/workbench/services/queryEditor/common/queryEditorService'; +import { IQueryEditorService, INewSqlEditorOptions } from 'sql/workbench/services/queryEditor/common/queryEditorService'; import { UntitledQueryEditorInput } from 'sql/workbench/common/editor/query/untitledQueryEditorInput'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; @@ -18,6 +18,11 @@ import { EditDataResultsInput } from 'sql/workbench/browser/editData/editDataRes import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService'; import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/untitledTextEditorInput'; import { UntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel'; +import { mixin } from 'vs/base/common/objects'; + +const defaults: INewSqlEditorOptions = { + open: true +}; /** * Service wrapper for opening and creating SQL documents as sql editor inputs @@ -39,36 +44,30 @@ export class QueryEditorService implements IQueryEditorService { /** * Creates new untitled document for SQL query and opens in new editor tab */ - public newSqlEditor(sqlContent?: string, connectionProviderName?: string, isDirty?: boolean, objectName?: string): Promise { - return new Promise(async (resolve, reject) => { - try { - // Create file path and file URI - let filePath = await this.createUntitledSqlFilePath(); - let docUri: URI = URI.from({ scheme: Schemas.untitled, path: filePath }); + public async newSqlEditor(options: INewSqlEditorOptions = {}): Promise { + options = mixin(options, defaults, false); + // Create file path and file URI + let filePath = await this.createUntitledSqlFilePath(); + let docUri: URI = URI.from({ scheme: Schemas.untitled, path: filePath }); - // Create a sql document pane with accoutrements - const fileInput = this._editorService.createEditorInput({ forceUntitled: true, resource: docUri, mode: 'sql' }) as UntitledTextEditorInput; - let untitledEditorModel = await fileInput.resolve() as UntitledTextEditorModel; - if (sqlContent) { - untitledEditorModel.textEditorModel.setValue(sqlContent); - if (isDirty === false || (isDirty === undefined && !this._configurationService.getValue('sql.promptToSaveGeneratedFiles'))) { - untitledEditorModel.setDirty(false); - } - } - - const queryResultsInput: QueryResultsInput = this._instantiationService.createInstance(QueryResultsInput, docUri.toString()); - let queryInput = this._instantiationService.createInstance(UntitledQueryEditorInput, objectName, fileInput, queryResultsInput); - - this._editorService.openEditor(queryInput, { pinned: true }) - .then((editor) => { - resolve(editor.input as UntitledQueryEditorInput); - }, (error) => { - reject(error); - }); - } catch (error) { - reject(error); + // Create a sql document pane with accoutrements + const fileInput = this._editorService.createEditorInput({ forceUntitled: true, resource: docUri, mode: 'sql' }) as UntitledTextEditorInput; + let untitledEditorModel = await fileInput.resolve() as UntitledTextEditorModel; + if (options.initalContent) { + untitledEditorModel.textEditorModel.setValue(options.initalContent); + if (options.dirty === false || (options.dirty === undefined && !this._configurationService.getValue('sql.promptToSaveGeneratedFiles'))) { + untitledEditorModel.setDirty(false); } - }); + } + + const queryResultsInput: QueryResultsInput = this._instantiationService.createInstance(QueryResultsInput, docUri.toString()); + let queryInput = this._instantiationService.createInstance(UntitledQueryEditorInput, options.description, fileInput, queryResultsInput); + + if (options.open) { + await this._editorService.openEditor(queryInput, { pinned: true }); + } + + return queryInput; } /** diff --git a/src/sql/workbench/services/queryEditor/common/queryEditorService.ts b/src/sql/workbench/services/queryEditor/common/queryEditorService.ts index 164d4c7eb5..8a64e5c4ac 100644 --- a/src/sql/workbench/services/queryEditor/common/queryEditorService.ts +++ b/src/sql/workbench/services/queryEditor/common/queryEditorService.ts @@ -16,12 +16,25 @@ export interface IQueryEditorOptions extends IEditorOptions { export const IQueryEditorService = createDecorator('QueryEditorService'); +export interface INewSqlEditorOptions { + initalContent?: string; + /** + * Defaults based on user configuration + */ + dirty?: boolean; + description?: string; + /** + * defaults to true + */ + open?: boolean; +} + export interface IQueryEditorService { _serviceBrand: undefined; // Creates new untitled document for SQL queries and opens it in a new editor tab - newSqlEditor(sqlContent?: string, connectionProviderName?: string, isDirty?: boolean, objectName?: string): Promise; + newSqlEditor(options?: INewSqlEditorOptions): Promise; // Creates new edit data session newEditDataEditor(schemaName: string, tableName: string, queryString: string): Promise; diff --git a/src/sql/workbench/services/queryEditor/test/common/testQueryEditorService.ts b/src/sql/workbench/services/queryEditor/test/common/testQueryEditorService.ts index 98ac4c64b8..24344b53e4 100644 --- a/src/sql/workbench/services/queryEditor/test/common/testQueryEditorService.ts +++ b/src/sql/workbench/services/queryEditor/test/common/testQueryEditorService.ts @@ -3,7 +3,7 @@ * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IQueryEditorService } from 'sql/workbench/services/queryEditor/common/queryEditorService'; +import { IQueryEditorService, INewSqlEditorOptions } from 'sql/workbench/services/queryEditor/common/queryEditorService'; import { IConnectableInput } from 'sql/platform/connection/common/connectionManagement'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { UntitledQueryEditorInput } from 'sql/workbench/common/editor/query/untitledQueryEditorInput'; @@ -17,10 +17,9 @@ export class TestQueryEditorService implements IQueryEditorService { constructor( @IInstantiationService private readonly instantiationService: IInstantiationService, @IEditorService private readonly editorService: IEditorService) { - } - newSqlEditor(sqlContent?: string, connectionProviderName?: string, isDirty?: boolean, objectName?: string): Promise { + newSqlEditor(options?: INewSqlEditorOptions): Promise { const base = this.editorService.createEditorInput({ forceUntitled: true }) as UntitledTextEditorInput; return Promise.resolve(this.instantiationService.createInstance(UntitledQueryEditorInput, '', base, new QueryResultsInput(base.resource.toString(true)))); } diff --git a/src/sql/workbench/services/tasks/common/tasksService.ts b/src/sql/workbench/services/tasks/common/tasksService.ts index d761180a98..2215785ea4 100644 --- a/src/sql/workbench/services/tasks/common/tasksService.ts +++ b/src/sql/workbench/services/tasks/common/tasksService.ts @@ -216,7 +216,7 @@ export class TaskService implements ITaskService { if ((task.status === TaskStatus.Succeeded || task.status === TaskStatus.SucceededWithWarning) && eventArgs.script && eventArgs.script !== '') { if (task.taskExecutionMode === TaskExecutionMode.script) { - this.queryEditorService.newSqlEditor(eventArgs.script); + this.queryEditorService.newSqlEditor({ initalContent: eventArgs.script }); } else if (task.taskExecutionMode === TaskExecutionMode.executeAndScript) { task.script = eventArgs.script; }