diff --git a/src/sql/workbench/contrib/query/browser/queryInputFactory.ts b/src/sql/workbench/contrib/query/browser/queryInputFactory.ts index 2c15b31357..3f18079a10 100644 --- a/src/sql/workbench/contrib/query/browser/queryInputFactory.ts +++ b/src/sql/workbench/contrib/query/browser/queryInputFactory.ts @@ -43,7 +43,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 = await this.queryEditorService.newSqlEditor({ open: false, initalContent: content }) as UntitledQueryEditorInput; + queryEditorInput = await this.queryEditorService.newSqlEditor({ resource: this.editorService.isOpen(activeEditor) ? activeEditor.resource : undefined, open: false, initalContent: content }) as UntitledQueryEditorInput; } else { return undefined; } diff --git a/src/sql/workbench/contrib/query/test/browser/queryInputFactory.test.ts b/src/sql/workbench/contrib/query/test/browser/queryInputFactory.test.ts index f8e18aed2f..5cfd2ea575 100644 --- a/src/sql/workbench/contrib/query/test/browser/queryInputFactory.test.ts +++ b/src/sql/workbench/contrib/query/test/browser/queryInputFactory.test.ts @@ -25,6 +25,7 @@ import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/commo import { isThenable } from 'vs/base/common/async'; import { IQueryEditorService } from 'sql/workbench/services/queryEditor/common/queryEditorService'; import { QueryResultsInput } from 'sql/workbench/common/editor/query/queryResultsInput'; +import { extUri } from 'vs/base/common/resources'; suite('Query Input Factory', () => { @@ -104,7 +105,7 @@ suite('Query Input Factory', () => { const response = queryEditorLanguageAssociation.convertInput(input); assert(isThenable(response)); await response; - assert(newsqlEditorStub.calledWithExactly({ open: false, initalContent: '' })); + assert(newsqlEditorStub.calledWithExactly({ resource: undefined, open: false, initalContent: '' })); assert(connectionManagementService.numberConnects === 1, 'Convert input should have called connect when active editor connection exists'); }); @@ -134,6 +135,27 @@ suite('Query Input Factory', () => { assert(connectionManagementService.numberConnects === 0, 'Convert input should not have been called connect when no global connections exist'); }); + test('uses existing resource if provided', async () => { + const instantiationService = workbenchInstantiationService(); + const editorService = new MockEditorService(instantiationService); + instantiationService.stub(IEditorService, editorService); + const queryEditorLanguageAssociation = instantiationService.createInstance(QueryEditorLanguageAssociation); + const untitledService = instantiationService.invokeFunction(accessor => accessor.get(IUntitledTextEditorService)); + const queryeditorservice = instantiationService.invokeFunction(accessor => accessor.get(IQueryEditorService)); + const input = instantiationService.createInstance(UntitledTextEditorInput, untitledService.create()); + sinon.stub(editorService, 'isOpen', (editor: IEditorInput) => extUri.isEqual(editor.resource, input.resource)); + const newsqlEditorStub = sinon.stub(queryeditorservice, 'newSqlEditor', () => { + const untitledInput = instantiationService.createInstance(UntitledTextEditorInput, untitledService.create()); + const queryResultsInput: QueryResultsInput = instantiationService.createInstance(QueryResultsInput, untitledInput.resource.toString()); + let queryInput = instantiationService.createInstance(UntitledQueryEditorInput, '', untitledInput, queryResultsInput); + return queryInput; + }); + const response = queryEditorLanguageAssociation.convertInput(input); + assert(isThenable(response)); + await response; + assert(newsqlEditorStub.calledWithExactly({ resource: input.resource, open: false, initalContent: '' })); + }); + }); class ServiceAccessor { diff --git a/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts b/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts index 1ec009cce8..f44151d625 100644 --- a/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts +++ b/src/sql/workbench/services/queryEditor/browser/queryEditorService.ts @@ -48,8 +48,7 @@ export class QueryEditorService implements IQueryEditorService { 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 }); + let docUri: URI = options.resource ?? URI.from({ scheme: Schemas.untitled, path: await this.createUntitledSqlFilePath() }); // Create a sql document pane with accoutrements const fileInput = this._editorService.createEditorInput({ forceUntitled: true, resource: docUri, mode: 'sql' }) as UntitledTextEditorInput; diff --git a/src/sql/workbench/services/queryEditor/common/queryEditorService.ts b/src/sql/workbench/services/queryEditor/common/queryEditorService.ts index 8a64e5c4ac..84b01701d4 100644 --- a/src/sql/workbench/services/queryEditor/common/queryEditorService.ts +++ b/src/sql/workbench/services/queryEditor/common/queryEditorService.ts @@ -6,6 +6,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { IConnectableInput } from 'sql/platform/connection/common/connectionManagement'; import { IEditorOptions } from 'vs/platform/editor/common/editor'; +import { URI } from 'vs/base/common/uri'; export interface IQueryEditorOptions extends IEditorOptions { @@ -27,6 +28,10 @@ export interface INewSqlEditorOptions { * defaults to true */ open?: boolean; + /** + * use an existing resource, if this matches a resource already open that resource will be opened instead + */ + resource?: URI } export interface IQueryEditorService {