mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 04:49:35 +00:00
Squashed commit of the following:
commitbc4dc881ba
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 13:07:39 2023 +0100 adding one more possibility which is lightbulb sparkle and auto-fix commita5dfa1f620
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 12:55:44 2023 +0100 removing one because should be using zero based ranges commit78b29c0b56
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 12:49:50 2023 +0100 using the navTree request instead commitba1b8fef77
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 12:20:38 2023 +0100 polishing the code commit5ae767f43a
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 12:14:35 2023 +0100 polishing the code commitfd347bb0e2
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 11:03:04 2023 +0100 remembering the class array that was previously set, so not removing the whole class array commitfd5fd11bfc
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 10:54:18 2023 +0100 using the appropriate icon commitdf29259136
Merge:5285e1d97e
633efd8c55
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Tue Nov 21 10:47:18 2023 +0100 Merge branch 'main' into aiday/differentLightBulbDependingOnCodeAction commit5285e1d97e
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 18:24:45 2023 +0100 making the filtering function synchronous commita86c36c6b0
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 17:42:11 2023 +0100 adding code in order to also include move to a new file only when on the range of interest commit2cedc2ffb4
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 17:11:13 2023 +0100 not showing move to file all the time commit8f4ade1d3d
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 14:50:56 2023 +0100 removing the code which does the selection check commit2e0e6c534e
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 14:31:55 2023 +0100 adding code in order to execute the AI code action when it is unique commita037c7e8e9
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Mon Nov 20 14:05:32 2023 +0100 adding code in order to be able to detect when different code actions partitions appear commitabd005b78b
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Fri Nov 17 18:07:26 2023 +0100 changing the heuristic of the move to code actions commite4886eab7a
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Fri Nov 17 17:25:57 2023 +0100 removing model commit79102a983b
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Fri Nov 17 17:25:48 2023 +0100 making lightbub icon appear on empty lines commitee8bb3475b
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Fri Nov 17 17:09:15 2023 +0100 directly resolving the code action when it is an ai code action commit4d4bcb9b34
Author: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com> Date: Fri Nov 17 17:02:34 2023 +0100 wip allow lightbulb menu to specify different icons depending on the code actions
This commit is contained in:
parent
4328415767
commit
5b297c488c
|
@ -12,7 +12,7 @@ import * as PConst from '../tsServer/protocol/protocol.const';
|
|||
import * as typeConverters from '../typeConverters';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
|
||||
const getSymbolKind = (kind: string): vscode.SymbolKind => {
|
||||
export const getSymbolKind = (kind: string): vscode.SymbolKind => {
|
||||
switch (kind) {
|
||||
case PConst.Kind.module: return vscode.SymbolKind.Module;
|
||||
case PConst.Kind.class: return vscode.SymbolKind.Class;
|
||||
|
@ -33,7 +33,7 @@ const getSymbolKind = (kind: string): vscode.SymbolKind => {
|
|||
return vscode.SymbolKind.Variable;
|
||||
};
|
||||
|
||||
class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
||||
export class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
||||
|
||||
public constructor(
|
||||
private readonly client: ITypeScriptServiceClient,
|
||||
|
|
|
@ -21,6 +21,7 @@ import { nulToken } from '../utils/cancellation';
|
|||
import FormattingOptionsManager from './fileConfigurationManager';
|
||||
import { conditionalRegistration, requireSomeCapability } from './util/dependentRegistration';
|
||||
import { EditorChatFollowUp, EditorChatFollowUp_Args, CompositeCommand } from './util/copilot';
|
||||
import { getSymbolKind } from './documentSymbol';
|
||||
|
||||
function toWorkspaceEdit(client: ITypeScriptServiceClient, edits: readonly Proto.FileCodeEdits[]): vscode.WorkspaceEdit {
|
||||
const workspaceEdit = new vscode.WorkspaceEdit();
|
||||
|
@ -435,6 +436,48 @@ class MoveToFileCodeAction extends vscode.CodeAction {
|
|||
arguments: [<MoveToFileRefactorCommand.Args>{ action, document, range }]
|
||||
};
|
||||
}
|
||||
|
||||
private static readonly _scopesOfInterest = [
|
||||
vscode.SymbolKind.File,
|
||||
vscode.SymbolKind.Module,
|
||||
vscode.SymbolKind.Namespace,
|
||||
vscode.SymbolKind.Package,
|
||||
vscode.SymbolKind.Class,
|
||||
vscode.SymbolKind.Interface,
|
||||
];
|
||||
|
||||
private static _findSmallestNavTreeContaining(
|
||||
navigationTree: Proto.NavigationTree,
|
||||
range: vscode.Range
|
||||
): Proto.NavigationTree {
|
||||
const childTrees = navigationTree.childItems;
|
||||
if (!childTrees) {
|
||||
return navigationTree;
|
||||
}
|
||||
for (const childTree of childTrees) {
|
||||
if (MoveToFileCodeAction._navTreeContainsRange(childTree, range) && MoveToFileCodeAction._scopesOfInterest.includes(getSymbolKind(childTree.kind))) {
|
||||
return this._findSmallestNavTreeContaining(childTree, range);
|
||||
}
|
||||
}
|
||||
return navigationTree;
|
||||
}
|
||||
|
||||
private static _navTreeContainsRange(navigationTree: Proto.NavigationTree, range: vscode.Range): boolean {
|
||||
return navigationTree.spans.some(span => typeConverters.Range.fromTextSpan(span).contains(range));
|
||||
}
|
||||
|
||||
public static shouldIncludeMoveToAction(
|
||||
navigationTree: Proto.NavigationTree | undefined,
|
||||
range: vscode.Range
|
||||
): boolean {
|
||||
if (!navigationTree || !MoveToFileCodeAction._navTreeContainsRange(navigationTree, range)) {
|
||||
return false;
|
||||
}
|
||||
const smallestScopeContaining = MoveToFileCodeAction._findSmallestNavTreeContaining(navigationTree, range);
|
||||
return !!(smallestScopeContaining
|
||||
&& smallestScopeContaining.spans[0].start.line - 1 === range.start.line
|
||||
&& smallestScopeContaining.spans[smallestScopeContaining.spans.length - 1].end.line - 1 === range.end.line);
|
||||
}
|
||||
}
|
||||
|
||||
class SelectCodeAction extends vscode.CodeAction {
|
||||
|
@ -516,7 +559,8 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
|
|||
return undefined;
|
||||
}
|
||||
|
||||
const actions = Array.from(this.convertApplicableRefactors(document, response.body, rangeOrSelection)).filter(action => {
|
||||
const applicableRefactors = await this.convertApplicableRefactors(document, response.body, rangeOrSelection);
|
||||
const actions = await Promise.all(applicableRefactors.map(async (action) => {
|
||||
if (this.client.apiVersion.lt(API.v430)) {
|
||||
// Don't show 'infer return type' refactoring unless it has been explicitly requested
|
||||
// https://github.com/microsoft/TypeScript/issues/42993
|
||||
|
@ -524,8 +568,15 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
|
|||
return false;
|
||||
}
|
||||
}
|
||||
if (action.kind?.value === Move_NewFile.kind.value) {
|
||||
const navigationTree = await this.client.execute('navtree', { file: document.uri.path }, nulToken);
|
||||
if (navigationTree.type !== 'response') {
|
||||
return;
|
||||
}
|
||||
return MoveToFileCodeAction.shouldIncludeMoveToAction(navigationTree.body, rangeOrSelection);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
})).then((mappedRefactors) => applicableRefactors.filter((_, index) => mappedRefactors[index]));
|
||||
|
||||
if (!context.only) {
|
||||
return actions;
|
||||
|
@ -547,31 +598,44 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
|
|||
return context.triggerKind === vscode.CodeActionTriggerKind.Invoke ? 'invoked' : 'implicit';
|
||||
}
|
||||
|
||||
private *convertApplicableRefactors(
|
||||
private async convertApplicableRefactors(
|
||||
document: vscode.TextDocument,
|
||||
refactors: readonly Proto.ApplicableRefactorInfo[],
|
||||
rangeOrSelection: vscode.Range | vscode.Selection
|
||||
): Iterable<TsCodeAction> {
|
||||
): Promise<Array<TsCodeAction>> {
|
||||
const actions: Array<TsCodeAction> = [];
|
||||
for (const refactor of refactors) {
|
||||
if (refactor.inlineable === false) {
|
||||
yield new SelectCodeAction(refactor, document, rangeOrSelection);
|
||||
actions.push(new SelectCodeAction(refactor, document, rangeOrSelection));
|
||||
} else {
|
||||
for (const action of refactor.actions) {
|
||||
yield this.refactorActionToCodeAction(document, refactor, action, rangeOrSelection, refactor.actions);
|
||||
const refactorAction = await this.refactorActionToCodeAction(document, refactor, action, rangeOrSelection, refactor.actions);
|
||||
if (refactorAction) {
|
||||
actions.push(refactorAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
|
||||
private refactorActionToCodeAction(
|
||||
private async refactorActionToCodeAction(
|
||||
document: vscode.TextDocument,
|
||||
refactor: Proto.ApplicableRefactorInfo,
|
||||
action: Proto.RefactorActionInfo,
|
||||
rangeOrSelection: vscode.Range | vscode.Selection,
|
||||
allActions: readonly Proto.RefactorActionInfo[],
|
||||
): TsCodeAction {
|
||||
): Promise<TsCodeAction | undefined> {
|
||||
let codeAction: TsCodeAction;
|
||||
if (action.name === 'Move to file') {
|
||||
const navigationTree = await this.client.execute('navtree', { file: document.uri.path }, nulToken);
|
||||
if (navigationTree.type !== 'response') {
|
||||
return;
|
||||
}
|
||||
const shouldIncludeMoveToAction = MoveToFileCodeAction.shouldIncludeMoveToAction(navigationTree.body, rangeOrSelection);
|
||||
if (!shouldIncludeMoveToAction) {
|
||||
return;
|
||||
}
|
||||
codeAction = new MoveToFileCodeAction(document, action, rangeOrSelection);
|
||||
} else {
|
||||
let copilotRename: ((info: Proto.RefactorEditInfo) => vscode.Command) | undefined;
|
||||
|
|
Loading…
Reference in a new issue