Skip to content

Commit e4af7dc

Browse files
committed
💄 binary editor picking
//cc @lramos15
1 parent 03b15ac commit e4af7dc

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
2424
import { IThemeService } from 'vs/platform/theme/common/themeService';
2525
import { ICodeEditorViewState, ScrollType } from 'vs/editor/common/editorCommon';
2626
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
27-
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
27+
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
2828
import { CancellationToken } from 'vs/base/common/cancellation';
2929
import { IErrorWithActions } from 'vs/base/common/errorMessage';
3030
import { EditorActivation, ITextEditorOptions } from 'vs/platform/editor/common/editor';
@@ -212,7 +212,8 @@ export class TextFileEditor extends BaseTextEditor<ICodeEditorViewState> {
212212

213213
private openAsBinary(input: FileEditorInput, options: ITextEditorOptions | undefined): void {
214214
const defaultBinaryEditor = this.configurationService.getValue<string | undefined>('workbench.editor.defaultBinaryEditor');
215-
const groupToOpen = this.group ?? this.editorGroupService.activeGroup;
215+
const group = this.group ?? this.editorGroupService.activeGroup;
216+
216217
let editorOptions = {
217218
...options,
218219
// Make sure to not steal away the currently active group
@@ -222,26 +223,43 @@ export class TextFileEditor extends BaseTextEditor<ICodeEditorViewState> {
222223
activation: EditorActivation.PRESERVE
223224
};
224225

225-
// If we the user setting specifies a default binary editor we use that
226+
// Check configuration and determine whether we open the binary
227+
// file input in a different editor or going through the same
228+
// editor.
229+
// Going through the same editor is debt, and a better solution
230+
// would be to introduce a real editor for the binary case
231+
// and avoid enforcing binary or text on the file editor input.
232+
226233
if (defaultBinaryEditor && defaultBinaryEditor !== '' && defaultBinaryEditor !== DEFAULT_EDITOR_ASSOCIATION.id) {
227-
this.editorService.replaceEditors([{
228-
editor: input,
229-
replacement: { resource: input.resource, options: { ...editorOptions, override: defaultBinaryEditor } }
230-
}], groupToOpen);
231-
// Replace is completed, don't do any further text input options as it's no longer text.
232-
return;
233-
} else if (defaultBinaryEditor === DEFAULT_EDITOR_ASSOCIATION.id) {
234-
input.setForceOpenAsText();
235-
// Distinguish between plain text and plain text binary
236-
input.setPreferredLanguageId(BINARY_TEXT_FILE_MODE);
237-
// Same pane and same input, must force reload to clear cached state
238-
editorOptions = { ...editorOptions, forceReload: true };
234+
this.doOpenAsBinaryInDifferentEditor(group, defaultBinaryEditor, input, editorOptions);
239235
} else {
240-
input.setForceOpenAsBinary();
236+
this.doOpenAsBinaryInSameEditor(group, defaultBinaryEditor, input, editorOptions);
237+
}
238+
}
239+
240+
private doOpenAsBinaryInDifferentEditor(group: IEditorGroup, editorId: string | undefined, editor: FileEditorInput, editorOptions: ITextEditorOptions): void {
241+
this.editorService.replaceEditors([{
242+
editor,
243+
replacement: { resource: editor.resource, options: { ...editorOptions, override: editorId } }
244+
}], group);
245+
}
246+
247+
private doOpenAsBinaryInSameEditor(group: IEditorGroup, editorId: string | undefined, editor: FileEditorInput, editorOptions: ITextEditorOptions): void {
248+
249+
// Open binary as text
250+
if (editorId === DEFAULT_EDITOR_ASSOCIATION.id) {
251+
editor.setForceOpenAsText();
252+
editor.setPreferredLanguageId(BINARY_TEXT_FILE_MODE); // https://github.com/microsoft/vscode/issues/131076
253+
254+
editorOptions = { ...editorOptions, forceReload: true }; // Same pane and same input, must force reload to clear cached state
255+
}
256+
257+
// Open as binary
258+
else {
259+
editor.setForceOpenAsBinary();
241260
}
242261

243-
// Resolver wasn't needed proceed to oepn the editor as normal
244-
groupToOpen.openEditor(input, editorOptions);
262+
group.openEditor(editor, editorOptions);
245263
}
246264

247265
private async openAsFolder(input: FileEditorInput): Promise<void> {

0 commit comments

Comments
 (0)