Skip to content

Commit aca0074

Browse files
lramos15bpasero
andauthored
Better matches for untyped editors (microsoft#158493)
* Fix microsoft#158480 * use instantiation service in tests cc @lramos15 * align matches methods Co-authored-by: Benjamin Pasero <[email protected]>
1 parent be5af93 commit aca0074

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

src/vs/workbench/common/editor/textResourceEditorInput.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { DEFAULT_EDITOR_ASSOCIATION, GroupIdentifier, IRevertOptions, IUntypedEditorInput } from 'vs/workbench/common/editor';
6+
import { DEFAULT_EDITOR_ASSOCIATION, GroupIdentifier, IRevertOptions, isResourceEditorInput, IUntypedEditorInput } from 'vs/workbench/common/editor';
77
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
88
import { AbstractResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
99
import { URI } from 'vs/base/common/uri';
@@ -182,14 +182,18 @@ export class TextResourceEditorInput extends AbstractTextResourceEditorInput imp
182182
}
183183

184184
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
185-
if (super.matches(otherInput)) {
185+
if (this === otherInput) {
186186
return true;
187187
}
188188

189189
if (otherInput instanceof TextResourceEditorInput) {
190190
return isEqual(otherInput.resource, this.resource);
191191
}
192192

193+
if (isResourceEditorInput(otherInput)) {
194+
return super.matches(otherInput);
195+
}
196+
193197
return false;
194198
}
195199

src/vs/workbench/contrib/files/browser/editors/fileEditorInput.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { URI } from 'vs/base/common/uri';
7-
import { IFileEditorInput, Verbosity, GroupIdentifier, IMoveResult, EditorInputCapabilities, IEditorDescriptor, IEditorPane, IUntypedEditorInput, DEFAULT_EDITOR_ASSOCIATION, IUntypedFileEditorInput, findViewStateForEditor, isResourceMergeEditorInput } from 'vs/workbench/common/editor';
7+
import { IFileEditorInput, Verbosity, GroupIdentifier, IMoveResult, EditorInputCapabilities, IEditorDescriptor, IEditorPane, IUntypedEditorInput, DEFAULT_EDITOR_ASSOCIATION, IUntypedFileEditorInput, findViewStateForEditor, isResourceEditorInput } from 'vs/workbench/common/editor';
88
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
99
import { AbstractTextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
1010
import { ITextResourceEditorInput } from 'vs/platform/editor/common/editor';
@@ -421,18 +421,18 @@ export class FileEditorInput extends AbstractTextResourceEditorInput implements
421421
}
422422

423423
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
424-
if (isResourceMergeEditorInput(otherInput)) {
425-
return false;
426-
}
427-
428-
if (super.matches(otherInput)) {
424+
if (this === otherInput) {
429425
return true;
430426
}
431427

432428
if (otherInput instanceof FileEditorInput) {
433429
return isEqual(otherInput.resource, this.resource);
434430
}
435431

432+
if (isResourceEditorInput(otherInput)) {
433+
return super.matches(otherInput);
434+
}
435+
436436
return false;
437437
}
438438

src/vs/workbench/services/untitled/common/untitledTextEditorInput.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { URI } from 'vs/base/common/uri';
7-
import { DEFAULT_EDITOR_ASSOCIATION, findViewStateForEditor, GroupIdentifier, IUntitledTextResourceEditorInput, IUntypedEditorInput, Verbosity } from 'vs/workbench/common/editor';
7+
import { DEFAULT_EDITOR_ASSOCIATION, findViewStateForEditor, GroupIdentifier, isUntitledResourceEditorInput, IUntitledTextResourceEditorInput, IUntypedEditorInput, Verbosity } from 'vs/workbench/common/editor';
88
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
99
import { AbstractTextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
1010
import { IUntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
@@ -158,14 +158,18 @@ export class UntitledTextEditorInput extends AbstractTextResourceEditorInput imp
158158
}
159159

160160
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
161-
if (super.matches(otherInput)) {
161+
if (this === otherInput) {
162162
return true;
163163
}
164164

165165
if (otherInput instanceof UntitledTextEditorInput) {
166166
return isEqual(otherInput.resource, this.resource);
167167
}
168168

169+
if (isUntitledResourceEditorInput(otherInput)) {
170+
return super.matches(otherInput);
171+
}
172+
169173
return false;
170174
}
171175

src/vs/workbench/test/browser/parts/editor/editorInput.test.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,43 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'assert';
7+
import { DisposableStore } from 'vs/base/common/lifecycle';
8+
import { Schemas } from 'vs/base/common/network';
79
import { URI } from 'vs/base/common/uri';
8-
import { isEditorInput, isResourceDiffEditorInput, isResourceEditorInput, isResourceMergeEditorInput, isResourceSideBySideEditorInput, isUntitledResourceEditorInput } from 'vs/workbench/common/editor';
10+
import { IResourceEditorInput, ITextResourceEditorInput } from 'vs/platform/editor/common/editor';
11+
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
12+
import { DEFAULT_EDITOR_ASSOCIATION, IResourceDiffEditorInput, IResourceMergeEditorInput, IResourceSideBySideEditorInput, isEditorInput, isResourceDiffEditorInput, isResourceEditorInput, isResourceMergeEditorInput, isResourceSideBySideEditorInput, isUntitledResourceEditorInput, IUntitledTextResourceEditorInput } from 'vs/workbench/common/editor';
913
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
10-
import { TestEditorInput } from 'vs/workbench/test/browser/workbenchTestServices';
14+
import { TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
15+
import { FileEditorInput } from 'vs/workbench/contrib/files/browser/editors/fileEditorInput';
16+
import { MergeEditorInput, MergeEditorInputData } from 'vs/workbench/contrib/mergeEditor/browser/mergeEditorInput';
17+
import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/untitledTextEditorInput';
18+
import { TestEditorInput, TestServiceAccessor, workbenchInstantiationService } from 'vs/workbench/test/browser/workbenchTestServices';
1119

1220
suite('EditorInput', () => {
1321

22+
let instantiationService: IInstantiationService;
23+
let accessor: TestServiceAccessor;
24+
let disposables: DisposableStore;
25+
26+
const testResource: URI = URI.from({ scheme: 'random', path: '/path' });
27+
const untypedResourceEditorInput: IResourceEditorInput = { resource: testResource, options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
28+
const untypedTextResourceEditorInput: ITextResourceEditorInput = { resource: testResource, options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
29+
const untypedResourceSideBySideEditorInput: IResourceSideBySideEditorInput = { primary: untypedResourceEditorInput, secondary: untypedResourceEditorInput, options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
30+
const untypedUntitledResourceEditorinput: IUntitledTextResourceEditorInput = { resource: URI.from({ scheme: Schemas.untitled, path: '/path' }), options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
31+
const untypedResourceDiffEditorInput: IResourceDiffEditorInput = { original: untypedResourceEditorInput, modified: untypedResourceEditorInput, options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
32+
const untypedResourceMergeEditorInput: IResourceMergeEditorInput = { base: untypedResourceEditorInput, input1: untypedResourceEditorInput, input2: untypedResourceEditorInput, result: untypedResourceEditorInput, options: { override: DEFAULT_EDITOR_ASSOCIATION.id } };
33+
34+
setup(() => {
35+
disposables = new DisposableStore();
36+
instantiationService = workbenchInstantiationService(undefined, disposables);
37+
accessor = instantiationService.createInstance(TestServiceAccessor);
38+
});
39+
40+
teardown(() => {
41+
disposables.dispose();
42+
});
43+
1444
class MyEditorInput extends EditorInput {
1545
readonly resource = undefined;
1646

@@ -62,4 +92,58 @@ suite('EditorInput', () => {
6292
assert.ok(!testInput.matches(testUntypedInputWrong));
6393

6494
});
95+
96+
test('Untpyed inputs properly match TextResourceEditorInput', () => {
97+
const textResourceEditorInput = instantiationService.createInstance(TextResourceEditorInput, testResource, undefined, undefined, undefined, undefined);
98+
99+
assert.ok(textResourceEditorInput.matches(untypedResourceEditorInput));
100+
assert.ok(textResourceEditorInput.matches(untypedTextResourceEditorInput));
101+
assert.ok(!textResourceEditorInput.matches(untypedResourceSideBySideEditorInput));
102+
assert.ok(!textResourceEditorInput.matches(untypedUntitledResourceEditorinput));
103+
assert.ok(!textResourceEditorInput.matches(untypedResourceDiffEditorInput));
104+
assert.ok(!textResourceEditorInput.matches(untypedResourceMergeEditorInput));
105+
106+
textResourceEditorInput.dispose();
107+
});
108+
109+
test('Untyped inputs properly match FileEditorInput', () => {
110+
const fileEditorInput = instantiationService.createInstance(FileEditorInput, testResource, undefined, undefined, undefined, undefined, undefined, undefined);
111+
112+
assert.ok(fileEditorInput.matches(untypedResourceEditorInput));
113+
assert.ok(fileEditorInput.matches(untypedTextResourceEditorInput));
114+
assert.ok(!fileEditorInput.matches(untypedResourceSideBySideEditorInput));
115+
assert.ok(!fileEditorInput.matches(untypedUntitledResourceEditorinput));
116+
assert.ok(!fileEditorInput.matches(untypedResourceDiffEditorInput));
117+
assert.ok(!fileEditorInput.matches(untypedResourceMergeEditorInput));
118+
119+
fileEditorInput.dispose();
120+
});
121+
122+
test('Untyped inputs properly match MergeEditorInput', () => {
123+
const mergeData: MergeEditorInputData = { uri: testResource, description: undefined, detail: undefined, title: undefined };
124+
const mergeEditorInput = instantiationService.createInstance(MergeEditorInput, testResource, mergeData, mergeData, testResource);
125+
126+
assert.ok(!mergeEditorInput.matches(untypedResourceEditorInput));
127+
assert.ok(!mergeEditorInput.matches(untypedTextResourceEditorInput));
128+
assert.ok(!mergeEditorInput.matches(untypedResourceSideBySideEditorInput));
129+
assert.ok(!mergeEditorInput.matches(untypedUntitledResourceEditorinput));
130+
assert.ok(!mergeEditorInput.matches(untypedResourceDiffEditorInput));
131+
assert.ok(mergeEditorInput.matches(untypedResourceMergeEditorInput));
132+
133+
mergeEditorInput.dispose();
134+
});
135+
136+
test('Untyped inputs properly match UntitledTextEditorInput', () => {
137+
const untitledModel = accessor.untitledTextEditorService.create({ associatedResource: { authority: '', path: '/path', fragment: '', query: '' } });
138+
const untitledTextEditorInput: UntitledTextEditorInput = instantiationService.createInstance(UntitledTextEditorInput, untitledModel);
139+
140+
assert.ok(!untitledTextEditorInput.matches(untypedResourceEditorInput));
141+
assert.ok(!untitledTextEditorInput.matches(untypedTextResourceEditorInput));
142+
assert.ok(!untitledTextEditorInput.matches(untypedResourceSideBySideEditorInput));
143+
assert.ok(untitledTextEditorInput.matches(untypedUntitledResourceEditorinput));
144+
assert.ok(!untitledTextEditorInput.matches(untypedResourceDiffEditorInput));
145+
assert.ok(!untitledTextEditorInput.matches(untypedResourceMergeEditorInput));
146+
147+
untitledTextEditorInput.dispose();
148+
});
65149
});

0 commit comments

Comments
 (0)