From 35ecd669cad3ce22591a51aef526248b6bd324fe Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Thu, 9 Mar 2023 13:10:23 -0800 Subject: [PATCH] Select target line when showing editor gutter context menu (#176667) * Select target line when showing editor gutter context menu * Only set selection for clicks on line numbers --- .../codeEditor/browser/editorLineNumberMenu.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/vs/workbench/contrib/codeEditor/browser/editorLineNumberMenu.ts b/src/vs/workbench/contrib/codeEditor/browser/editorLineNumberMenu.ts index d7e5aff76ef6..4baeaca8412e 100644 --- a/src/vs/workbench/contrib/codeEditor/browser/editorLineNumberMenu.ts +++ b/src/vs/workbench/contrib/codeEditor/browser/editorLineNumberMenu.ts @@ -12,6 +12,7 @@ import { IEditorContribution } from 'vs/editor/common/editorCommon'; import { IMenuService, MenuId } from 'vs/platform/actions/common/actions'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IContextMenuService } from 'vs/platform/contextview/browser/contextView'; +import { TextEditorSelectionSource } from 'vs/platform/editor/common/editor'; import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { Registry } from 'vs/platform/registry/common/platform'; @@ -90,6 +91,22 @@ export class EditorLineNumberContextMenu extends Disposable implements IEditorCo const menuActions = menu.getActions({ arg: { lineNumber, uri: model.uri }, shouldForwardArgs: true }); actions.push(...menuActions.map(a => a[1])); + // if the current editor selections do not contain the target line number, + // set the selection to the clicked line number + if (e.target.type === MouseTargetType.GUTTER_LINE_NUMBERS) { + const currentSelections = this.editor.getSelections(); + const containsSelection = currentSelections?.some(selection => selection.containsPosition({ lineNumber, column: 1 })); + if (!containsSelection) { + const selection = { + startLineNumber: lineNumber, + endLineNumber: lineNumber, + startColumn: 1, + endColumn: model.getLineLength(lineNumber) + 1 + }; + this.editor.setSelection(selection, TextEditorSelectionSource.PROGRAMMATIC); + } + } + this.contextMenuService.showContextMenu({ getAnchor: () => anchor, getActions: () => Separator.join(...actions),