Skip to content

Commit 1b6bf15

Browse files
authored
Allow negative numbers for go to match (microsoft#180479)
Fixes microsoft#180475 Also fixes the case where there are zero matches, which previously resulted in a non-functional quick input
1 parent b81fd85 commit 1b6bf15

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/vs/editor/contrib/find/browser/findController.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { ContextKeyExpr, IContextKey, IContextKeyService } from 'vs/platform/con
2626
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
2727
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
2828
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
29-
import { INotificationService } from 'vs/platform/notification/common/notification';
29+
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
3030
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
3131
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
3232
import { IThemeService, themeColorFromId } from 'vs/platform/theme/common/themeService';
@@ -759,22 +759,46 @@ export class MoveToMatchFindAction extends EditorAction {
759759

760760
public run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void | Promise<void> {
761761
const controller = CommonFindController.get(editor);
762-
763762
if (!controller) {
764763
return;
765764
}
766765

766+
const matchesCount = controller.getState().matchesCount;
767+
if (matchesCount < 1) {
768+
const notificationService = accessor.get(INotificationService);
769+
notificationService.notify({
770+
severity: Severity.Warning,
771+
message: nls.localize('findMatchAction.noResults', "No matches. Try searching for something else.")
772+
});
773+
return;
774+
}
775+
767776
const quickInputService = accessor.get(IQuickInputService);
768777
const inputBox = quickInputService.createInputBox();
769-
inputBox.placeholder = nls.localize('findMatchAction.inputPlaceHolder', "Type a number to go to a specific match (between 1 and {0})", controller.getState().matchesCount);
778+
inputBox.placeholder = nls.localize('findMatchAction.inputPlaceHolder', "Type a number to go to a specific match (between 1 and {0})", matchesCount);
770779

771-
const updatePickerAndEditor = (value: string) => {
780+
const toFindMatchIndex = (value: string): number | undefined => {
772781
const index = parseInt(value);
782+
if (isNaN(index)) {
783+
return undefined;
784+
}
785+
786+
const matchCount = controller.getState().matchesCount;
787+
if (index > 0 && index <= matchCount) {
788+
return index - 1; // zero based
789+
} else if (index < 0 && index >= -matchCount) {
790+
return matchCount + index;
791+
}
773792

774-
if (!isNaN(index) && index > 0 && index <= controller.getState().matchesCount) {
793+
return undefined;
794+
};
795+
796+
const updatePickerAndEditor = (value: string) => {
797+
const index = toFindMatchIndex(value);
798+
if (typeof index === 'number') {
775799
// valid
776800
inputBox.validationMessage = undefined;
777-
controller.goToMatch(index - 1);
801+
controller.goToMatch(index);
778802
const currentMatch = controller.getState().currentMatch;
779803
if (currentMatch) {
780804
this.addDecorations(editor, currentMatch);
@@ -789,9 +813,9 @@ export class MoveToMatchFindAction extends EditorAction {
789813
});
790814

791815
inputBox.onDidAccept(() => {
792-
const index = parseInt(inputBox.value);
793-
if (!isNaN(index) && index > 0 && index <= controller.getState().matchesCount) {
794-
controller.goToMatch(index - 1);
816+
const index = toFindMatchIndex(inputBox.value);
817+
if (typeof index === 'number') {
818+
controller.goToMatch(index);
795819
inputBox.hide();
796820
} else {
797821
inputBox.validationMessage = nls.localize('findMatchAction.inputValidationMessage', "Please type a number between 1 and {0}", controller.getState().matchesCount);

0 commit comments

Comments
 (0)