Skip to content

Commit 70b0382

Browse files
committed
update statusbar with "real" keybindings
1 parent d413a30 commit 70b0382

2 files changed

Lines changed: 51 additions & 29 deletions

File tree

src/vs/editor/contrib/suggest/suggestController.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,8 @@ const SuggestCommand = EditorCommand.bindToContribution<SuggestController>(Sugge
528528
registerEditorCommand(new SuggestCommand({
529529
id: 'acceptSelectedSuggestion',
530530
precondition: SuggestContext.Visible,
531-
handler(x, args) {
532-
const alternative: boolean = typeof args === 'object' && typeof args.alternative === 'boolean'
533-
? args.alternative
534-
: false;
535-
x.acceptSelectedSuggestion(true, alternative);
531+
handler(x) {
532+
x.acceptSelectedSuggestion(true, false);
536533
}
537534
}));
538535

@@ -552,16 +549,23 @@ KeybindingsRegistry.registerKeybindingRule({
552549
weight
553550
});
554551

552+
// todo@joh control enablement via context key
555553
// shift+enter and shift+tab use the alternative-flag so that the suggest controller
556554
// is doing the opposite of the editor.suggest.overwriteOnAccept-configuration
557-
KeybindingsRegistry.registerKeybindingRule({
558-
id: 'acceptSelectedSuggestion',
559-
when: ContextKeyExpr.and(SuggestContext.Visible, EditorContextKeys.textInputFocus),
560-
primary: KeyMod.Shift | KeyCode.Tab,
561-
secondary: [KeyMod.Shift | KeyCode.Enter],
562-
args: { alternative: true },
563-
weight
564-
});
555+
registerEditorCommand(new SuggestCommand({
556+
id: 'acceptAlternativeSelectedSuggestion',
557+
precondition: ContextKeyExpr.and(SuggestContext.Visible, EditorContextKeys.textInputFocus),
558+
kbOpts: {
559+
weight: weight,
560+
kbExpr: EditorContextKeys.textInputFocus,
561+
primary: KeyMod.Shift | KeyCode.Enter,
562+
secondary: [KeyMod.Shift | KeyCode.Tab],
563+
},
564+
handler(x) {
565+
x.acceptSelectedSuggestion(false, true);
566+
},
567+
}));
568+
565569

566570
// continue to support the old command
567571
CommandsRegistry.registerCommandAlias('acceptSelectedSuggestionOnEnter', 'acceptSelectedSuggestion');

src/vs/editor/contrib/suggest/suggestWidget.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ import { FileKind } from 'vs/platform/files/common/files';
4242
import { MarkdownString } from 'vs/base/common/htmlContent';
4343
import { flatten } from 'vs/base/common/arrays';
4444
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
45+
import { Position } from 'vs/editor/common/core/position';
4546

4647
const expandSuggestionDocsByDefault = false;
4748

48-
const READ_MORE_TEXT = nls.localize('suggestWidget.readMore', 'Read more... (⌃Space)');
49-
const READ_LESS_TEXT = nls.localize('suggestWidget.readLess', 'Read less... (⌃Space)');
50-
const INSERT_REPLACE_TEXT = nls.localize('suggestWidget.insertOrReplace', 'Enter to insert, Tab to replace');
51-
5249
interface ISuggestionTemplateData {
5350
root: HTMLElement;
5451

@@ -306,7 +303,7 @@ class SuggestionDetails {
306303
private readonly widget: SuggestWidget,
307304
private readonly editor: ICodeEditor,
308305
private readonly markdownRenderer: MarkdownRenderer,
309-
private readonly triggerKeybindingLabel: string,
306+
private readonly kbToggleDetails: string,
310307
) {
311308
this.disposables = new DisposableStore();
312309

@@ -321,7 +318,7 @@ class SuggestionDetails {
321318

322319
this.header = append(this.body, $('.header'));
323320
this.close = append(this.header, $('span.codicon.codicon-close'));
324-
this.close.title = nls.localize('readLess', "Read less...{0}", this.triggerKeybindingLabel);
321+
this.close.title = nls.localize('readLess', "Read less...{0}", this.kbToggleDetails);
325322
this.type = append(this.header, $('p.type'));
326323

327324
this.docs = append(this.body, $('p.docs'));
@@ -473,6 +470,9 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
473470
readonly allowEditorOverflow = true;
474471
readonly suppressMouseDown = false;
475472

473+
private readonly msgDetailMore: string;
474+
private readonly msgDetailsLess: string;
475+
476476
private state: State | null = null;
477477
private isAuto: boolean = false;
478478
private loadingTimeout: IDisposable = Disposable.None;
@@ -525,18 +525,20 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
525525
constructor(
526526
private readonly editor: ICodeEditor,
527527
@ITelemetryService private readonly telemetryService: ITelemetryService,
528+
@IKeybindingService private readonly keybindingService: IKeybindingService,
528529
@IContextKeyService contextKeyService: IContextKeyService,
529530
@IThemeService themeService: IThemeService,
530531
@IStorageService storageService: IStorageService,
531-
@IKeybindingService keybindingService: IKeybindingService,
532532
@IModeService modeService: IModeService,
533533
@IOpenerService openerService: IOpenerService,
534534
@IInstantiationService instantiationService: IInstantiationService,
535535
) {
536-
const kb = keybindingService.lookupKeybinding('editor.action.triggerSuggest');
537-
const triggerKeybindingLabel = !kb ? '' : ` (${kb.getLabel()})`;
538536
const markdownRenderer = this.toDispose.add(new MarkdownRenderer(editor, modeService, openerService));
539537

538+
const kbToggleDetails = keybindingService.lookupKeybinding('toggleSuggestionDetails')?.getLabel() ?? '';
539+
this.msgDetailsLess = nls.localize('detail.less', "{0} for less...", kbToggleDetails);
540+
this.msgDetailMore = nls.localize('detail.more', "{0} for more...", kbToggleDetails);
541+
540542
this.isAuto = false;
541543
this.focusedItem = null;
542544
this.storageService = storageService;
@@ -558,15 +560,15 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
558560
this.statusBarLeftSpan = append(this.statusBarElement, $('span'));
559561
this.statusBarRightSpan = append(this.statusBarElement, $('span'));
560562

561-
this.setStatusBarLeftText(INSERT_REPLACE_TEXT);
563+
this.setStatusBarLeftText('');
562564
this.setStatusBarRightText('');
563565

564-
this.details = instantiationService.createInstance(SuggestionDetails, this.element, this, this.editor, markdownRenderer, triggerKeybindingLabel);
566+
this.details = instantiationService.createInstance(SuggestionDetails, this.element, this, this.editor, markdownRenderer, kbToggleDetails);
565567

566568
const applyIconStyle = () => toggleClass(this.element, 'no-icons', !this.editor.getOption(EditorOption.suggest).showIcons);
567569
applyIconStyle();
568570

569-
let renderer = instantiationService.createInstance(ItemRenderer, this, this.editor, triggerKeybindingLabel);
571+
let renderer = instantiationService.createInstance(ItemRenderer, this, this.editor, kbToggleDetails);
570572

571573
this.list = new List('SuggestWidget', this.listElement, this, [renderer], {
572574
useShadows: false,
@@ -730,6 +732,22 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
730732
this.firstFocusInCurrentList = !this.focusedItem;
731733
if (item !== this.focusedItem) {
732734

735+
// update statusbar
736+
// todo@joh,pine -> this should a toolbar with actions so that these things become
737+
// mouse clickable and fit for accessibility...
738+
const wantsInsert = this.editor.getOption(EditorOption.suggest).insertMode === 'insert';
739+
const kbAccept = this.keybindingService.lookupKeybinding('acceptSelectedSuggestion')?.getLabel();
740+
const kbAcceptAlt = this.keybindingService.lookupKeybinding('acceptAlternativeSelectedSuggestion')?.getLabel();
741+
if (!Position.equals(item.editInsertEnd, item.editReplaceEnd)) {
742+
// insert AND replace
743+
if (wantsInsert) {
744+
this.setStatusBarLeftText(nls.localize('insert', "{0} to insert, {1} to replace", kbAccept, kbAcceptAlt));
745+
} else {
746+
this.setStatusBarLeftText(nls.localize('replace', "{0} to replace, {1} to insert", kbAccept, kbAcceptAlt));
747+
}
748+
} else {
749+
this.setStatusBarLeftText(nls.localize('accept', "{0} to accept", kbAccept));
750+
}
733751

734752
if (this.currentSuggestionDetails) {
735753
this.currentSuggestionDetails.cancel();
@@ -767,9 +785,9 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
767785

768786
if (canExpandCompletionItem(this.focusedItem)) {
769787
if (this.expandDocsSettingFromStorage()) {
770-
this.setStatusBarRightText(READ_LESS_TEXT);
788+
this.setStatusBarRightText(this.msgDetailsLess);
771789
} else {
772-
this.setStatusBarRightText(READ_MORE_TEXT);
790+
this.setStatusBarRightText(this.msgDetailMore);
773791
}
774792
} else {
775793
this.statusBarRightSpan.innerText = '';
@@ -1044,7 +1062,7 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
10441062
removeClass(this.element, 'docs-side');
10451063
removeClass(this.element, 'docs-below');
10461064
this.editor.layoutContentWidget(this);
1047-
this.setStatusBarRightText(READ_MORE_TEXT);
1065+
this.setStatusBarRightText(this.msgDetailMore);
10481066
this.telemetryService.publicLog2('suggestWidget:collapseDetails');
10491067
} else {
10501068
if (this.state !== State.Open && this.state !== State.Details && this.state !== State.Frozen) {
@@ -1053,7 +1071,7 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<Compl
10531071

10541072
this.updateExpandDocsSetting(true);
10551073
this.showDetails(false);
1056-
this.setStatusBarRightText(READ_LESS_TEXT);
1074+
this.setStatusBarRightText(this.msgDetailsLess);
10571075
this.telemetryService.publicLog2('suggestWidget:expandDetails');
10581076
}
10591077
}

0 commit comments

Comments
 (0)