Skip to content

Commit d7b9d87

Browse files
committed
editors - make closing a non-resolved editor on file deletes work with any editor
1 parent 2000f36 commit d7b9d87

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/vs/workbench/services/editor/browser/editorService.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/ur
3636
import { IModelService } from 'vs/editor/common/services/modelService';
3737
import { ILogService } from 'vs/platform/log/common/log';
3838
import { ContributedEditorPriority, DEFAULT_EDITOR_ASSOCIATION, IEditorOverrideService } from 'vs/workbench/services/editor/common/editorOverrideService';
39+
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
3940

4041
type CachedEditorInput = ResourceEditorInput | IFileEditorInput | UntitledTextEditorInput;
4142
type OpenInEditorGroup = IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE;
@@ -75,6 +76,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
7576
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
7677
@ILogService private readonly logService: ILogService,
7778
@IEditorOverrideService private readonly editorOverrideService: IEditorOverrideService,
79+
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService
7880
) {
7981
super();
8082

@@ -318,11 +320,11 @@ export class EditorService extends Disposable implements EditorServiceImpl {
318320
}
319321

320322
// Handle deletes in opened editors depending on:
321-
// - the user has not disabled the setting `closeOnFileDelete`
322-
// - the file change is local
323-
// - the input is a file that is not resolved (we need to dispose because
324-
// we cannot restore otherwise since we do not have the contents)
325-
if (this.closeOnFileDelete || !isExternal || (this.fileEditorInputFactory.isFileEditorInput(editor) && !editor.isResolved())) {
323+
// - we close any editor when `closeOnFileDelete: true`
324+
// - we close any editor when the delete occured from within VSCode
325+
// - we close any editor without resolved working copy assuming that
326+
// this editor could not be opened after the file is gone
327+
if (this.closeOnFileDelete || !isExternal || !this.workingCopyService.has(resource)) {
326328

327329
// Do NOT close any opened editor that matches the resource path (either equal or being parent) of the
328330
// resource we move to (movedTo). Otherwise we would close a resource that has been renamed to the same

src/vs/workbench/services/workingCopy/common/workingCopyService.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export interface IWorkingCopyService {
9090
*/
9191
registerWorkingCopy(workingCopy: IWorkingCopy): IDisposable;
9292

93+
/**
94+
* Whether a working copy with the given resource exists.
95+
*
96+
* @param resource the resource of the working copy to check
97+
*/
98+
has(resource: URI): boolean;
99+
93100
//#endregion
94101
}
95102

@@ -175,6 +182,10 @@ export class WorkingCopyService extends Disposable implements IWorkingCopyServic
175182
}
176183
}
177184

185+
has(resource: URI): boolean {
186+
return this.mapResourceToWorkingCopies.has(resource);
187+
}
188+
178189
//#endregion
179190

180191

src/vs/workbench/services/workingCopy/test/common/workingCopyService.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ suite('WorkingCopyService', () => {
3333

3434
// resource 1
3535
const resource1 = URI.file('/some/folder/file.txt');
36+
assert.strictEqual(service.has(resource1), false);
3637
const copy1 = new TestWorkingCopy(resource1);
3738
const unregister1 = service.registerWorkingCopy(copy1);
3839

@@ -42,6 +43,7 @@ suite('WorkingCopyService', () => {
4243
assert.strictEqual(onDidRegister[0], copy1);
4344
assert.strictEqual(service.dirtyCount, 0);
4445
assert.strictEqual(service.isDirty(resource1), false);
46+
assert.strictEqual(service.has(resource1), true);
4547
assert.strictEqual(service.hasDirty, false);
4648

4749
copy1.setDirty(true);
@@ -75,6 +77,7 @@ suite('WorkingCopyService', () => {
7577
assert.strictEqual(onDidUnregister.length, 1);
7678
assert.strictEqual(onDidUnregister[0], copy1);
7779
assert.strictEqual(service.workingCopies.length, 0);
80+
assert.strictEqual(service.has(resource1), false);
7881

7982
// resource 2
8083
const resource2 = URI.file('/some/folder/file-dirty.txt');

0 commit comments

Comments
 (0)