Skip to content

Commit 834e742

Browse files
authored
allow contributed range of higlight for code actions (#202197)
* groundwork for segttings * laying groundowrk for new API * more groundwork, api fixed * cleanup * more cleanup * added updates * fixed spacing
1 parent 1db9db3 commit 834e742

File tree

7 files changed

+31
-1
lines changed

7 files changed

+31
-1
lines changed

Diff for: src/vs/editor/common/languages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ export interface CodeAction {
749749
isPreferred?: boolean;
750750
isAI?: boolean;
751751
disabled?: string;
752+
ranges?: IRange[];
752753
}
753754

754755
export const enum CodeActionTriggerType {

Diff for: src/vs/editor/contrib/codeAction/browser/codeActionController.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ export class CodeActionController extends Disposable implements IEditorContribut
279279
return { canPreview: !!action.action.edit?.edits.length };
280280
},
281281
onFocus: (action: CodeActionItem | undefined) => {
282-
if (action && action.action.diagnostics) {
282+
// If provider contributes ranges, then highlight contributed range over diagnostic range.
283+
if (action && action.action.ranges) {
284+
currentDecorations.clear();
285+
const decorations: IModelDeltaDecoration[] = action.action.ranges.map(range => ({ range, options: CodeActionController.DECORATION }));
286+
currentDecorations.set(decorations);
287+
} else if (action && action.action.diagnostics) {
283288
currentDecorations.clear();
284289
const decorations: IModelDeltaDecoration[] = action.action.diagnostics.map(diagnostic => ({ range: diagnostic, options: CodeActionController.DECORATION }));
285290
currentDecorations.set(decorations);

Diff for: src/vs/monaco.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7102,6 +7102,7 @@ declare namespace monaco.languages {
71027102
isPreferred?: boolean;
71037103
isAI?: boolean;
71047104
disabled?: string;
7105+
ranges?: IRange[];
71057106
}
71067107

71077108
export enum CodeActionTriggerType {

Diff for: src/vs/workbench/api/common/extHost.protocol.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,7 @@ export interface ICodeActionDto {
19921992
isPreferred?: boolean;
19931993
isAI?: boolean;
19941994
disabled?: string;
1995+
ranges?: IRange[];
19951996
}
19961997

19971998
export interface ICodeActionListDto {

Diff for: src/vs/workbench/api/common/extHostLanguageFeatures.ts

+4
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ class CodeActionAdapter {
474474
}
475475
}
476476

477+
// Ensures that this is either a Range[] or an empty array so we don't get Array<Range | undefined>
478+
const range = candidate.ranges ?? [];
479+
477480
// new school: convert code action
478481
actions.push({
479482
cacheId: [cacheId, i],
@@ -484,6 +487,7 @@ class CodeActionAdapter {
484487
kind: candidate.kind && candidate.kind.value,
485488
isPreferred: candidate.isPreferred,
486489
isAI: isProposedApiEnabled(this._extension, 'codeActionAI') ? candidate.isAI : false,
490+
ranges: isProposedApiEnabled(this._extension, 'codeActionRanges') ? coalesce(range.map(typeConvert.Range.from)) : undefined,
487491
disabled: candidate.disabled?.reason
488492
});
489493
}

Diff for: src/vs/workbench/services/extensions/common/extensionsApiProposals.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const allApiProposals = Object.freeze({
1717
chatRequestAccess: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatRequestAccess.d.ts',
1818
chatTab: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatTab.d.ts',
1919
codeActionAI: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.codeActionAI.d.ts',
20+
codeActionRanges: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.codeActionRanges.d.ts',
2021
codiconDecoration: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.codiconDecoration.d.ts',
2122
commentReactor: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.commentReactor.d.ts',
2223
commentsDraftState: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.commentsDraftState.d.ts',

Diff for: src/vscode-dts/vscode.proposed.codeActionRanges.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
declare module 'vscode' {
7+
8+
export interface CodeAction {
9+
/**
10+
*
11+
* The range to which this Code Action applies to, which will be highlighted.
12+
*
13+
* Ex: A refactoring action will highlight the range of text that will be affected.
14+
*/
15+
ranges?: Range[];
16+
}
17+
}

0 commit comments

Comments
 (0)