From a645a09f42055cde18f20926822a977c336c39c5 Mon Sep 17 00:00:00 2001 From: Udeesha Gautam <46980425+udeeshagautam@users.noreply.github.com> Date: Tue, 13 Aug 2019 18:41:25 -0700 Subject: [PATCH] Bug/restore query editor tabs (#6654) * trial run for query editor temp file fix * Fixing restore tabs bug with right layering * Moving FileService change from vscode to our code --- .../parts/query/common/queryInput.ts | 13 +++++++- .../query/test/browser/queryEditor.test.ts | 1 + .../test/common/taskUtilities.test.ts | 2 +- .../browser/parts/editor/editorGroupView.ts | 2 ++ src/vs/workbench/common/editor/editorGroup.ts | 33 ++++++++++++------- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/sql/workbench/parts/query/common/queryInput.ts b/src/sql/workbench/parts/query/common/queryInput.ts index 8367ae17a8..6ac56eb05c 100644 --- a/src/sql/workbench/parts/query/common/queryInput.ts +++ b/src/sql/workbench/parts/query/common/queryInput.ts @@ -10,6 +10,7 @@ import { URI } from 'vs/base/common/uri'; import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput'; import { EditorInput, ConfirmResult, EncodingMode, IEncodingSupport } from 'vs/workbench/common/editor'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { IFileService } from 'vs/platform/files/common/files'; import { IConnectionManagementService, IConnectableInput, INewConnectionParams, RunQueryOnConnectionMode } from 'sql/platform/connection/common/connectionManagement'; import { QueryResultsInput } from 'sql/workbench/parts/query/common/queryResultsInput'; @@ -18,6 +19,7 @@ import { IQueryModelService } from 'sql/platform/query/common/queryModel'; import { ISelectionData, ExecutionPlanOptions } from 'azdata'; import { UntitledEditorModel } from 'vs/workbench/common/editor/untitledEditorModel'; import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService'; +import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; const MAX_SIZE = 13; @@ -117,7 +119,8 @@ export class QueryInput extends EditorInput implements IEncodingSupport, IConnec private _connectionProviderName: string, @IConnectionManagementService private _connectionManagementService: IConnectionManagementService, @IQueryModelService private _queryModelService: IQueryModelService, - @IConfigurationService private _configurationService: IConfigurationService + @IConfigurationService private _configurationService: IConfigurationService, + @IFileService private _fileService: IFileService ) { super(); this._updateSelection = new Emitter(); @@ -218,6 +221,14 @@ export class QueryInput extends EditorInput implements IEncodingSupport, IConnec return false; } + public matchInputInstanceType(inputType: any): boolean { + return (this._sql instanceof inputType); + } + + public inputFileExists(): Promise { + return this._fileService.exists(this.getResource()); + } + public getName(longForm?: boolean): string { if (this._configurationService.getValue('sql.showConnectionInfoInTitle')) { let profile = this._connectionManagementService.getConnectionProfile(this.uri); diff --git a/src/sql/workbench/parts/query/test/browser/queryEditor.test.ts b/src/sql/workbench/parts/query/test/browser/queryEditor.test.ts index 9fbe720fdc..904f1975f3 100644 --- a/src/sql/workbench/parts/query/test/browser/queryEditor.test.ts +++ b/src/sql/workbench/parts/query/test/browser/queryEditor.test.ts @@ -294,6 +294,7 @@ suite('SQL QueryEditor Tests', () => { undefined, connectionManagementService.object, queryModelService.object, + undefined, undefined ); }); diff --git a/src/sql/workbench/test/common/taskUtilities.test.ts b/src/sql/workbench/test/common/taskUtilities.test.ts index 1c55b404e8..4908f77abc 100644 --- a/src/sql/workbench/test/common/taskUtilities.test.ts +++ b/src/sql/workbench/test/common/taskUtilities.test.ts @@ -76,7 +76,7 @@ suite('TaskUtilities', function () { // Mock the workbench service to return the active tab connection let tabConnectionUri = 'file://test_uri'; let editorInput = new UntitledEditorInput(URI.parse(tabConnectionUri), false, undefined, undefined, undefined, undefined, undefined, undefined); - let queryInput = new QueryInput(undefined, editorInput, undefined, undefined, undefined, undefined, undefined); + let queryInput = new QueryInput(undefined, editorInput, undefined, undefined, undefined, undefined, undefined, undefined); mockConnectionManagementService.setup(x => x.getConnectionProfile(tabConnectionUri)).returns(() => tabProfile); // If I call getCurrentGlobalConnection, it should return the expected profile from the active tab diff --git a/src/vs/workbench/browser/parts/editor/editorGroupView.ts b/src/vs/workbench/browser/parts/editor/editorGroupView.ts index da7adbac9d..5f95de1de7 100644 --- a/src/vs/workbench/browser/parts/editor/editorGroupView.ts +++ b/src/vs/workbench/browser/parts/editor/editorGroupView.ts @@ -426,6 +426,8 @@ export class EditorGroupView extends Themable implements IEditorGroupView { } private async restoreEditors(from: IEditorGroupView | ISerializedEditorGroup): Promise { + await this._group.removeNonExitingEditor(); // {{SQL CARBON EDIT}} @udeeshagautam perform async correction for non-existing files + if (this._group.count === 0) { return; // nothing to show } diff --git a/src/vs/workbench/common/editor/editorGroup.ts b/src/vs/workbench/common/editor/editorGroup.ts index 989c25a2d2..4b3b94e4d7 100644 --- a/src/vs/workbench/common/editor/editorGroup.ts +++ b/src/vs/workbench/common/editor/editorGroup.ts @@ -19,8 +19,6 @@ import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorIn import * as CustomInputConverter from 'sql/workbench/common/customInputConverter'; import { NotebookInput } from 'sql/workbench/parts/notebook/common/models/notebookInput'; import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; -import * as path from 'vs/base/common/path'; -import * as os from 'os'; const EditorOpenPositioning = { LEFT: 'left', @@ -666,15 +664,6 @@ export class EditorGroup extends Disposable { && !this.configurationService.getValue('sql.promptToSaveGeneratedFiles')) { return; } - // Do not add generated files from Temp if file is not dirty - if (e instanceof FileEditorInput && !e.isDirty()) { - let filePath = e.getResource() ? e.getResource().fsPath : undefined; - let tempPath = os.tmpdir(); - if (filePath && tempPath && - filePath.toLocaleLowerCase().includes(path.join(tempPath.toLocaleLowerCase(), 'mssql_definition'))) { - return; - } - } // {{SQL CARBON EDIT}} - End const value = factory.serialize(e); @@ -740,4 +729,26 @@ export class EditorGroup extends Disposable { this.preview = this.editors[data.preview]; } } + + // {{SQL CARBON EDIT}} + async removeNonExitingEditor(): Promise { + let n = 0; + while (n < this.editors.length) { + let editor = this.editors[n]; + if (editor instanceof QueryInput && editor.matchInputInstanceType(FileEditorInput) && !editor.isDirty() && await editor.inputFileExists() === false && this.editors.length > 1) { + // remove from editors list so that they do not get restored + this.editors.splice(n, 1); + let index = this.mru.findIndex(e => e.matches(editor)); + + // remove from MRU list otherwise later if we try to close them it leaves a sticky active editor with no data + this.mru.splice(index, 1); + this.active = this.isActive(editor) ? this.editors[0] : this.active; + editor.close(); + } + else { + n++; + } + } + } + // {{SQL CARBON EDIT}} }