Skip to content

Commit a464701

Browse files
author
Benjamin Pasero
committed
debt - fix toResource() with schemes
1 parent 061a3f1 commit a464701

8 files changed

Lines changed: 28 additions & 53 deletions

File tree

src/vs/workbench/browser/parts/titlebar/titlebarPart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class TitlebarPart extends Part implements ITitleService {
179179
}
180180

181181
private updateRepresentedFilename(): void {
182-
const file = toResource(this.editorService.activeEditor, { supportSideBySide: SideBySideEditor.MASTER, filter: 'file' });
182+
const file = toResource(this.editorService.activeEditor, { supportSideBySide: SideBySideEditor.MASTER, filterByScheme: 'file' });
183183
const path = file ? file.fsPath : '';
184184

185185
// Apply to window

src/vs/workbench/common/editor.ts

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

66
import { Event, Emitter } from 'vs/base/common/event';
7-
import * as objects from 'vs/base/common/objects';
8-
import * as types from 'vs/base/common/types';
7+
import { assign } from 'vs/base/common/objects';
8+
import { isUndefinedOrNull, withUndefinedAsNull } from 'vs/base/common/types';
99
import { URI } from 'vs/base/common/uri';
1010
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
1111
import { IEditor as ICodeEditor, IEditorViewState, ScrollType, IDiffEditor } from 'vs/editor/common/editorCommon';
@@ -14,7 +14,6 @@ import { IInstantiationService, IConstructorSignature0, ServicesAccessor } from
1414
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1515
import { Registry } from 'vs/platform/registry/common/platform';
1616
import { ITextModel } from 'vs/editor/common/model';
17-
import { Schemas } from 'vs/base/common/network';
1817
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
1918
import { ICompositeControl } from 'vs/workbench/common/composite';
2019
import { ActionRunner, IAction } from 'vs/base/common/actions';
@@ -571,7 +570,7 @@ export class SideBySideEditorInput extends EditorInput {
571570
getTelemetryDescriptor(): object {
572571
const descriptor = this.master.getTelemetryDescriptor();
573572

574-
return objects.assign(descriptor, super.getTelemetryDescriptor());
573+
return assign(descriptor, super.getTelemetryDescriptor());
575574
}
576575

577576
private registerListeners(): void {
@@ -827,7 +826,7 @@ export class TextEditorOptions extends EditorOptions {
827826
* Returns if this options object has objects defined for the editor.
828827
*/
829828
hasOptionsDefined(): boolean {
830-
return !!this.editorViewState || (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn));
829+
return !!this.editorViewState || (!isUndefinedOrNull(this.startLineNumber) && !isUndefinedOrNull(this.startColumn));
831830
}
832831

833832
/**
@@ -875,10 +874,10 @@ export class TextEditorOptions extends EditorOptions {
875874
}
876875

877876
// Otherwise check for selection
878-
else if (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn)) {
877+
else if (!isUndefinedOrNull(this.startLineNumber) && !isUndefinedOrNull(this.startColumn)) {
879878

880879
// Select
881-
if (!types.isUndefinedOrNull(this.endLineNumber) && !types.isUndefinedOrNull(this.endColumn)) {
880+
if (!isUndefinedOrNull(this.endLineNumber) && !isUndefinedOrNull(this.endColumn)) {
882881
const range = {
883882
startLineNumber: this.startLineNumber,
884883
startColumn: this.startColumn,
@@ -986,43 +985,28 @@ export enum SideBySideEditor {
986985

987986
export interface IResourceOptions {
988987
supportSideBySide?: SideBySideEditor;
989-
filter?: string | string[];
988+
filterByScheme?: string | string[];
990989
}
991990

992991
export function toResource(editor: IEditorInput | null | undefined, options?: IResourceOptions): URI | null {
993992
if (!editor) {
994993
return null;
995994
}
996995

997-
// Check for side by side if we are asked to
998996
if (options && options.supportSideBySide && editor instanceof SideBySideEditorInput) {
999997
editor = options.supportSideBySide === SideBySideEditor.MASTER ? editor.master : editor.details;
1000998
}
1001999

10021000
const resource = editor.getResource();
1003-
if (!options || !options.filter) {
1004-
return types.withUndefinedAsNull(resource); // return early if no filter is specified
1001+
if (!resource || !options || !options.filterByScheme) {
1002+
return withUndefinedAsNull(resource);
10051003
}
10061004

1007-
if (!resource) {
1008-
return null;
1009-
}
1010-
1011-
let includeFiles: boolean;
1012-
let includeUntitled: boolean;
1013-
if (Array.isArray(options.filter)) {
1014-
includeFiles = (options.filter.indexOf(Schemas.file) >= 0);
1015-
includeUntitled = (options.filter.indexOf(Schemas.untitled) >= 0);
1016-
} else {
1017-
includeFiles = (options.filter === Schemas.file);
1018-
includeUntitled = (options.filter === Schemas.untitled);
1019-
}
1020-
1021-
if (includeFiles && resource.scheme === Schemas.file) {
1005+
if (Array.isArray(options.filterByScheme) && options.filterByScheme.some(scheme => resource.scheme === scheme)) {
10221006
return resource;
10231007
}
10241008

1025-
if (includeUntitled && resource.scheme === Schemas.untitled) {
1009+
if (options.filterByScheme === resource.scheme) {
10261010
return resource;
10271011
}
10281012

src/vs/workbench/contrib/files/browser/fileCommands.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { ILabelService } from 'vs/platform/label/common/label';
4040
import { onUnexpectedError } from 'vs/base/common/errors';
4141
import { basename } from 'vs/base/common/resources';
4242
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
43-
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
4443

4544
// Commands
4645

@@ -318,15 +317,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
318317
// Dispose once no more diff editor is opened with the scheme
319318
if (registerEditorListener) {
320319
providerDisposables.push(editorService.onDidVisibleEditorsChange(() => {
321-
if (!editorService.editors.some(editor => {
322-
if (editor instanceof DiffEditorInput) {
323-
const originalResource = toResource(editor.originalInput);
324-
325-
return !!(originalResource && originalResource.scheme === COMPARE_WITH_SAVED_SCHEMA);
326-
}
327-
328-
return false;
329-
})) {
320+
if (!editorService.editors.some(editor => !!toResource(editor, { supportSideBySide: SideBySideEditor.DETAILS, filterByScheme: COMPARE_WITH_SAVED_SCHEMA }))) {
330321
providerDisposables = dispose(providerDisposables);
331322
}
332323
}));

src/vs/workbench/contrib/files/common/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class OpenEditor implements IEditorIdentifier {
231231
}
232232

233233
public isUntitled(): boolean {
234-
return !!toResource(this.editor, { supportSideBySide: SideBySideEditor.MASTER, filter: Schemas.untitled });
234+
return !!toResource(this.editor, { supportSideBySide: SideBySideEditor.MASTER, filterByScheme: Schemas.untitled });
235235
}
236236

237237
public isDirty(): boolean {

src/vs/workbench/contrib/themes/test/electron-browser/themes.test.contribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ CommandsRegistry.registerCommand('_workbench.captureSyntaxTokens', function (acc
241241

242242
if (!resource) {
243243
const editorService = accessor.get(IEditorService);
244-
const file = editorService.activeEditor ? toResource(editorService.activeEditor, { filter: 'file' }) : null;
244+
const file = editorService.activeEditor ? toResource(editorService.activeEditor, { filterByScheme: 'file' }) : null;
245245
if (file) {
246246
process(file).then(result => {
247247
console.log(result);

src/vs/workbench/services/configurationResolver/browser/configurationResolverService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ConfigurationResolverService extends AbstractVariableResolverServic
5656
if (activeEditor instanceof DiffEditorInput) {
5757
activeEditor = activeEditor.modifiedInput;
5858
}
59-
const fileResource = toResource(activeEditor, { filter: Schemas.file });
59+
const fileResource = toResource(activeEditor, { filterByScheme: Schemas.file });
6060
if (!fileResource) {
6161
return undefined;
6262
}

src/vs/workbench/services/history/browser/history.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,17 +955,17 @@ export class HistoryService extends Disposable implements IHistoryService {
955955
return undefined;
956956
}
957957

958-
getLastActiveFile(schemeFilter: string): URI | undefined {
958+
getLastActiveFile(filterByScheme: string): URI | undefined {
959959
const history = this.getHistory();
960960
for (const input of history) {
961961
let resource: URI | null;
962962
if (input instanceof EditorInput) {
963-
resource = toResource(input, { filter: schemeFilter });
963+
resource = toResource(input, { filterByScheme });
964964
} else {
965965
resource = (input as IResourceInput).resource;
966966
}
967967

968-
if (resource && resource.scheme === schemeFilter) {
968+
if (resource && resource.scheme === filterByScheme) {
969969
return resource;
970970
}
971971
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,25 @@ suite('Workbench editor', () => {
6161

6262
assert.equal(toResource(untitled)!.toString(), untitled.getResource().toString());
6363
assert.equal(toResource(untitled, { supportSideBySide: SideBySideEditor.MASTER })!.toString(), untitled.getResource().toString());
64-
assert.equal(toResource(untitled, { filter: Schemas.untitled })!.toString(), untitled.getResource().toString());
65-
assert.equal(toResource(untitled, { filter: [Schemas.file, Schemas.untitled] })!.toString(), untitled.getResource().toString());
66-
assert.ok(!toResource(untitled, { filter: Schemas.file }));
64+
assert.equal(toResource(untitled, { filterByScheme: Schemas.untitled })!.toString(), untitled.getResource().toString());
65+
assert.equal(toResource(untitled, { filterByScheme: [Schemas.file, Schemas.untitled] })!.toString(), untitled.getResource().toString());
66+
assert.ok(!toResource(untitled, { filterByScheme: Schemas.file }));
6767

6868
const file = new FileEditorInput(URI.file('/some/path.txt'));
6969

7070
assert.equal(toResource(file)!.toString(), file.getResource().toString());
7171
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER })!.toString(), file.getResource().toString());
72-
assert.equal(toResource(file, { filter: Schemas.file })!.toString(), file.getResource().toString());
73-
assert.equal(toResource(file, { filter: [Schemas.file, Schemas.untitled] })!.toString(), file.getResource().toString());
74-
assert.ok(!toResource(file, { filter: Schemas.untitled }));
72+
assert.equal(toResource(file, { filterByScheme: Schemas.file })!.toString(), file.getResource().toString());
73+
assert.equal(toResource(file, { filterByScheme: [Schemas.file, Schemas.untitled] })!.toString(), file.getResource().toString());
74+
assert.ok(!toResource(file, { filterByScheme: Schemas.untitled }));
7575

7676
const diffEditorInput = new DiffEditorInput('name', 'description', untitled, file);
7777

7878
assert.ok(!toResource(diffEditorInput));
79-
assert.ok(!toResource(diffEditorInput, { filter: Schemas.file }));
79+
assert.ok(!toResource(diffEditorInput, { filterByScheme: Schemas.file }));
8080

8181
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER })!.toString(), file.getResource().toString());
82-
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER, filter: Schemas.file })!.toString(), file.getResource().toString());
83-
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER, filter: [Schemas.file, Schemas.untitled] })!.toString(), file.getResource().toString());
82+
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER, filterByScheme: Schemas.file })!.toString(), file.getResource().toString());
83+
assert.equal(toResource(file, { supportSideBySide: SideBySideEditor.MASTER, filterByScheme: [Schemas.file, Schemas.untitled] })!.toString(), file.getResource().toString());
8484
});
8585
});

0 commit comments

Comments
 (0)