Skip to content

Commit 211fdbc

Browse files
author
Benjamin Pasero
committed
debt - more async/await adoption
1 parent 0f32f0e commit 211fdbc

23 files changed

Lines changed: 402 additions & 386 deletions

File tree

src/vs/workbench/browser/parts/editor/editorGroupView.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,17 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
279279
});
280280

281281
// Toolbar actions
282-
const removeGroupAction = this._register(new Action(CLOSE_EDITOR_GROUP_COMMAND_ID, localize('closeGroupAction', "Close"), 'close-editor-group', true, () => { this.accessor.removeGroup(this); return Promise.resolve(true); }));
282+
const removeGroupAction = this._register(new Action(
283+
CLOSE_EDITOR_GROUP_COMMAND_ID,
284+
localize('closeGroupAction', "Close"),
285+
'close-editor-group',
286+
true,
287+
() => {
288+
this.accessor.removeGroup(this);
289+
290+
return Promise.resolve(true);
291+
}));
292+
283293
const keybinding = this.keybindingService.lookupKeybinding(removeGroupAction.id);
284294
containerToolbar.push(removeGroupAction, { icon: true, label: false, keybinding: keybinding ? keybinding.getLabel() : undefined });
285295
}
@@ -402,7 +412,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
402412

403413
private async restoreEditors(from: IEditorGroupView | ISerializedEditorGroup): Promise<void> {
404414
if (this._group.count === 0) {
405-
return Promise.resolve(); // nothing to show
415+
return; // nothing to show
406416
}
407417

408418
// Determine editor options
@@ -415,7 +425,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
415425

416426
const activeEditor = this._group.activeEditor;
417427
if (!activeEditor) {
418-
return Promise.resolve();
428+
return;
419429
}
420430

421431
options.pinned = this._group.isPinned(activeEditor); // preserve pinned state
@@ -1102,7 +1112,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
11021112

11031113
private async handleDirty(editors: EditorInput[]): Promise<boolean /* veto */> {
11041114
if (!editors.length) {
1105-
return Promise.resolve(false); // no veto
1115+
return false; // no veto
11061116
}
11071117

11081118
const editor = editors.shift()!;
@@ -1135,7 +1145,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
11351145
this.accessor.groups.some(groupView => groupView !== this && groupView.group.contains(editor, true /* support side by side */)) || // editor is opened in other group
11361146
editor instanceof SideBySideEditorInput && this.isOpened(editor.master) // side by side editor master is still opened
11371147
) {
1138-
return Promise.resolve(false);
1148+
return false;
11391149
}
11401150

11411151
// Switch to editor that we want to handle and confirm to save/revert

src/vs/workbench/browser/parts/editor/editorStatus.ts

Lines changed: 116 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as nls from 'vs/nls';
88
import { $, append, runAtThisOrScheduleAtNextAnimationFrame } from 'vs/base/browser/dom';
99
import { format } from 'vs/base/common/strings';
1010
import { extname, basename } from 'vs/base/common/resources';
11-
import { areFunctions, withNullAsUndefined } from 'vs/base/common/types';
11+
import { areFunctions, withNullAsUndefined, withUndefinedAsNull } from 'vs/base/common/types';
1212
import { URI } from 'vs/base/common/uri';
1313
import { IStatusbarItem } from 'vs/workbench/browser/parts/statusbar/statusbar';
1414
import { Action } from 'vs/base/common/actions';
@@ -893,7 +893,7 @@ export class ChangeModeAction extends Action {
893893

894894
// Compute mode
895895
let currentModeId: string | undefined;
896-
let modeId: string;
896+
let modeId: string | undefined;
897897
if (textModel) {
898898
modeId = textModel.getLanguageIdentifier().language;
899899
currentModeId = this.modeService.getLanguageName(modeId) || undefined;
@@ -933,9 +933,9 @@ export class ChangeModeAction extends Action {
933933
}
934934

935935
// Offer action to configure via settings
936-
let configureModeAssociations: IQuickPickItem;
937-
let configureModeSettings: IQuickPickItem;
938-
let galleryAction: Action;
936+
let configureModeAssociations: IQuickPickItem | undefined;
937+
let configureModeSettings: IQuickPickItem | undefined;
938+
let galleryAction: Action | undefined;
939939
if (hasLanguageSupport && resource) {
940940
const ext = extname(resource) || basename(resource);
941941

@@ -959,56 +959,55 @@ export class ChangeModeAction extends Action {
959959
picks.unshift(autoDetectMode);
960960
}
961961

962-
return this.quickInputService.pick(picks, { placeHolder: nls.localize('pickLanguage', "Select Language Mode"), matchOnDescription: true }).then(pick => {
963-
if (!pick) {
964-
return;
965-
}
962+
const pick = await this.quickInputService.pick(picks, { placeHolder: nls.localize('pickLanguage', "Select Language Mode"), matchOnDescription: true });
963+
if (!pick) {
964+
return;
965+
}
966966

967-
if (pick === galleryAction) {
968-
galleryAction.run();
969-
return;
970-
}
967+
if (pick === galleryAction) {
968+
galleryAction.run();
969+
return;
970+
}
971971

972-
// User decided to permanently configure associations, return right after
973-
if (pick === configureModeAssociations) {
974-
if (resource) {
975-
this.configureFileAssociation(resource);
976-
}
977-
return;
972+
// User decided to permanently configure associations, return right after
973+
if (pick === configureModeAssociations) {
974+
if (resource) {
975+
this.configureFileAssociation(resource);
978976
}
977+
return;
978+
}
979979

980-
// User decided to configure settings for current language
981-
if (pick === configureModeSettings) {
982-
this.preferencesService.configureSettingsForLanguage(modeId);
983-
return;
984-
}
980+
// User decided to configure settings for current language
981+
if (pick === configureModeSettings) {
982+
this.preferencesService.configureSettingsForLanguage(withUndefinedAsNull(modeId));
983+
return;
984+
}
985985

986-
// Change mode for active editor
987-
const activeEditor = this.editorService.activeEditor;
988-
if (activeEditor) {
989-
const modeSupport = toEditorWithModeSupport(activeEditor);
990-
if (modeSupport) {
991-
992-
// Find mode
993-
let languageSelection: ILanguageSelection | undefined;
994-
if (pick === autoDetectMode) {
995-
if (textModel) {
996-
const resource = toResource(activeEditor, { supportSideBySide: SideBySideEditor.MASTER });
997-
if (resource) {
998-
languageSelection = this.modeService.createByFilepathOrFirstLine(resource.fsPath, textModel.getLineContent(1));
999-
}
986+
// Change mode for active editor
987+
const activeEditor = this.editorService.activeEditor;
988+
if (activeEditor) {
989+
const modeSupport = toEditorWithModeSupport(activeEditor);
990+
if (modeSupport) {
991+
992+
// Find mode
993+
let languageSelection: ILanguageSelection | undefined;
994+
if (pick === autoDetectMode) {
995+
if (textModel) {
996+
const resource = toResource(activeEditor, { supportSideBySide: SideBySideEditor.MASTER });
997+
if (resource) {
998+
languageSelection = this.modeService.createByFilepathOrFirstLine(resource.fsPath, textModel.getLineContent(1));
1000999
}
1001-
} else {
1002-
languageSelection = this.modeService.createByLanguageName(pick.label);
10031000
}
1001+
} else {
1002+
languageSelection = this.modeService.createByLanguageName(pick.label);
1003+
}
10041004

1005-
// Change mode
1006-
if (typeof languageSelection !== 'undefined') {
1007-
modeSupport.setMode(languageSelection.languageIdentifier.language);
1008-
}
1005+
// Change mode
1006+
if (typeof languageSelection !== 'undefined') {
1007+
modeSupport.setMode(languageSelection.languageIdentifier.language);
10091008
}
10101009
}
1011-
});
1010+
}
10121011
}
10131012

10141013
private configureFileAssociation(resource: URI): void {
@@ -1171,7 +1170,7 @@ export class ChangeEncodingAction extends Action {
11711170
super(actionId, actionLabel);
11721171
}
11731172

1174-
run(): Promise<any> {
1173+
async run(): Promise<any> {
11751174
if (!getCodeEditor(this.editorService.activeTextEditorWidget)) {
11761175
return this.quickInputService.pick([{ label: nls.localize('noEditor', "No text editor active at this time") }]);
11771176
}
@@ -1196,93 +1195,88 @@ export class ChangeEncodingAction extends Action {
11961195
reopenWithEncodingPick = { label: nls.localize('reopenWithEncoding', "Reopen with Encoding"), detail: 'Reopen with Encoding' };
11971196
}
11981197

1199-
let pickActionPromise: Promise<IQuickPickItem>;
1198+
let action: IQuickPickItem;
12001199
if (encodingSupport instanceof UntitledEditorInput) {
1201-
pickActionPromise = Promise.resolve(saveWithEncodingPick);
1200+
action = saveWithEncodingPick;
12021201
} else if (!isWritableBaseEditor(activeControl)) {
1203-
pickActionPromise = Promise.resolve(reopenWithEncodingPick);
1202+
action = reopenWithEncodingPick;
12041203
} else {
1205-
pickActionPromise = this.quickInputService.pick([reopenWithEncodingPick, saveWithEncodingPick], { placeHolder: nls.localize('pickAction', "Select Action"), matchOnDetail: true });
1204+
action = await this.quickInputService.pick([reopenWithEncodingPick, saveWithEncodingPick], { placeHolder: nls.localize('pickAction', "Select Action"), matchOnDetail: true });
12061205
}
12071206

1208-
return pickActionPromise.then(action => {
1209-
if (!action) {
1210-
return undefined;
1211-
}
1207+
if (!action) {
1208+
return;
1209+
}
12121210

1213-
const resource = toResource(activeControl!.input, { supportSideBySide: SideBySideEditor.MASTER });
1211+
await timeout(50); // quick open is sensitive to being opened so soon after another
12141212

1215-
return timeout(50 /* quick open is sensitive to being opened so soon after another */)
1216-
.then(() => {
1217-
if (!resource || !this.fileService.canHandleResource(resource)) {
1218-
return Promise.resolve(null); // encoding detection only possible for resources the file service can handle
1219-
}
1213+
const resource = toResource(activeControl!.input, { supportSideBySide: SideBySideEditor.MASTER });
1214+
if (!resource || !this.fileService.canHandleResource(resource)) {
1215+
return null; // encoding detection only possible for resources the file service can handle
1216+
}
12201217

1221-
return this.textFileService.read(resource, { autoGuessEncoding: true, acceptTextOnly: true }).then(content => content.encoding, err => null);
1222-
})
1223-
.then((guessedEncoding: string) => {
1224-
const isReopenWithEncoding = (action === reopenWithEncodingPick);
1225-
1226-
const configuredEncoding = this.textResourceConfigurationService.getValue(withNullAsUndefined(resource), 'files.encoding');
1227-
1228-
let directMatchIndex: number | undefined;
1229-
let aliasMatchIndex: number | undefined;
1230-
1231-
// All encodings are valid picks
1232-
const picks: QuickPickInput[] = Object.keys(SUPPORTED_ENCODINGS)
1233-
.sort((k1, k2) => {
1234-
if (k1 === configuredEncoding) {
1235-
return -1;
1236-
} else if (k2 === configuredEncoding) {
1237-
return 1;
1238-
}
1239-
1240-
return SUPPORTED_ENCODINGS[k1].order - SUPPORTED_ENCODINGS[k2].order;
1241-
})
1242-
.filter(k => {
1243-
if (k === guessedEncoding && guessedEncoding !== configuredEncoding) {
1244-
return false; // do not show encoding if it is the guessed encoding that does not match the configured
1245-
}
1246-
1247-
return !isReopenWithEncoding || !SUPPORTED_ENCODINGS[k].encodeOnly; // hide those that can only be used for encoding if we are about to decode
1248-
})
1249-
.map((key, index) => {
1250-
if (key === encodingSupport.getEncoding()) {
1251-
directMatchIndex = index;
1252-
} else if (SUPPORTED_ENCODINGS[key].alias === encodingSupport.getEncoding()) {
1253-
aliasMatchIndex = index;
1254-
}
1255-
1256-
return { id: key, label: SUPPORTED_ENCODINGS[key].labelLong, description: key };
1257-
});
1258-
1259-
const items = picks.slice() as IQuickPickItem[];
1260-
1261-
// If we have a guessed encoding, show it first unless it matches the configured encoding
1262-
if (guessedEncoding && configuredEncoding !== guessedEncoding && SUPPORTED_ENCODINGS[guessedEncoding]) {
1263-
picks.unshift({ type: 'separator' });
1264-
picks.unshift({ id: guessedEncoding, label: SUPPORTED_ENCODINGS[guessedEncoding].labelLong, description: nls.localize('guessedEncoding', "Guessed from content") });
1265-
}
1218+
const content = await this.textFileService.read(resource, { autoGuessEncoding: true, acceptTextOnly: true });
1219+
const guessedEncoding = content.encoding;
12661220

1267-
return this.quickInputService.pick(picks, {
1268-
placeHolder: isReopenWithEncoding ? nls.localize('pickEncodingForReopen', "Select File Encoding to Reopen File") : nls.localize('pickEncodingForSave', "Select File Encoding to Save with"),
1269-
activeItem: items[typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : -1]
1270-
}).then(encoding => {
1271-
if (!encoding) {
1272-
return;
1273-
}
1221+
const isReopenWithEncoding = (action === reopenWithEncodingPick);
12741222

1275-
const activeControl = this.editorService.activeControl;
1276-
if (!activeControl) {
1277-
return;
1278-
}
1223+
const configuredEncoding = this.textResourceConfigurationService.getValue(withNullAsUndefined(resource), 'files.encoding');
12791224

1280-
const encodingSupport = toEditorWithEncodingSupport(activeControl.input);
1281-
if (typeof encoding.id !== 'undefined' && encodingSupport && encodingSupport.getEncoding() !== encoding.id) {
1282-
encodingSupport.setEncoding(encoding.id, isReopenWithEncoding ? EncodingMode.Decode : EncodingMode.Encode); // Set new encoding
1283-
}
1284-
});
1285-
});
1225+
let directMatchIndex: number | undefined;
1226+
let aliasMatchIndex: number | undefined;
1227+
1228+
// All encodings are valid picks
1229+
const picks: QuickPickInput[] = Object.keys(SUPPORTED_ENCODINGS)
1230+
.sort((k1, k2) => {
1231+
if (k1 === configuredEncoding) {
1232+
return -1;
1233+
} else if (k2 === configuredEncoding) {
1234+
return 1;
1235+
}
1236+
1237+
return SUPPORTED_ENCODINGS[k1].order - SUPPORTED_ENCODINGS[k2].order;
1238+
})
1239+
.filter(k => {
1240+
if (k === guessedEncoding && guessedEncoding !== configuredEncoding) {
1241+
return false; // do not show encoding if it is the guessed encoding that does not match the configured
1242+
}
1243+
1244+
return !isReopenWithEncoding || !SUPPORTED_ENCODINGS[k].encodeOnly; // hide those that can only be used for encoding if we are about to decode
1245+
})
1246+
.map((key, index) => {
1247+
if (key === encodingSupport.getEncoding()) {
1248+
directMatchIndex = index;
1249+
} else if (SUPPORTED_ENCODINGS[key].alias === encodingSupport.getEncoding()) {
1250+
aliasMatchIndex = index;
1251+
}
1252+
1253+
return { id: key, label: SUPPORTED_ENCODINGS[key].labelLong, description: key };
1254+
});
1255+
1256+
const items = picks.slice() as IQuickPickItem[];
1257+
1258+
// If we have a guessed encoding, show it first unless it matches the configured encoding
1259+
if (guessedEncoding && configuredEncoding !== guessedEncoding && SUPPORTED_ENCODINGS[guessedEncoding]) {
1260+
picks.unshift({ type: 'separator' });
1261+
picks.unshift({ id: guessedEncoding, label: SUPPORTED_ENCODINGS[guessedEncoding].labelLong, description: nls.localize('guessedEncoding', "Guessed from content") });
1262+
}
1263+
1264+
const encoding = await this.quickInputService.pick(picks, {
1265+
placeHolder: isReopenWithEncoding ? nls.localize('pickEncodingForReopen', "Select File Encoding to Reopen File") : nls.localize('pickEncodingForSave', "Select File Encoding to Save with"),
1266+
activeItem: items[typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : -1]
12861267
});
1268+
1269+
if (!encoding) {
1270+
return;
1271+
}
1272+
1273+
if (!this.editorService.activeControl) {
1274+
return;
1275+
}
1276+
1277+
const activeEncodingSupport = toEditorWithEncodingSupport(this.editorService.activeControl.input);
1278+
if (typeof encoding.id !== 'undefined' && activeEncodingSupport && activeEncodingSupport.getEncoding() !== encoding.id) {
1279+
activeEncodingSupport.setEncoding(encoding.id, isReopenWithEncoding ? EncodingMode.Decode : EncodingMode.Encode); // Set new encoding
1280+
}
12871281
}
12881282
}

src/vs/workbench/browser/parts/editor/sideBySideEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class SideBySideEditor extends BaseEditor {
169169
}
170170

171171
if (!this.detailsEditor || !this.masterEditor) {
172-
return Promise.resolve();
172+
return;
173173
}
174174

175175
await Promise.all([

src/vs/workbench/browser/parts/editor/textResourceEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
7070

7171
// Assert Model instance
7272
if (!(resolvedModel instanceof BaseTextEditorModel)) {
73-
return Promise.reject(new Error('Unable to open file as text'));
73+
throw new Error('Unable to open file as text');
7474
}
7575

7676
// Set Editor Model

0 commit comments

Comments
 (0)