Fix lints

This commit is contained in:
Nathan Shively-Sanders
2023-09-14 09:16:39 -07:00
parent 3328d54c89
commit b1b350626f
3 changed files with 115 additions and 63 deletions

View File

@@ -321,7 +321,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
action: Proto.CodeFixAction
): VsCodeCodeAction[] {
const actions: VsCodeCodeAction[] = [];
let codeAction = new VsCodeCodeAction(action, action.description, vscode.CodeActionKind.QuickFix);
const codeAction = new VsCodeCodeAction(action, action.description, vscode.CodeActionKind.QuickFix);
codeAction.edit = getEditForCodeAction(this.client, action);
codeAction.diagnostics = [diagnostic];
codeAction.command = {
@@ -332,14 +332,14 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
actions.push(codeAction);
if (vscode.workspace.getConfiguration('typescript').get('experimental.aiQuickFix')) {
let message: string | undefined
let expand: Expand | undefined
let message: string | undefined;
let expand: Expand | undefined;
if(action.fixName === fixNames.classIncorrectlyImplementsInterface) {
if (action.fixName === fixNames.classIncorrectlyImplementsInterface) {
message = `Implement the stubbed-out class members for ${document.getText(diagnostic.range)} with a useful implementation.`;
expand = { kind: 'code-action', action }
expand = { kind: 'code-action', action };
}
else if(action.fixName === fixNames.fixClassDoesntImplementInheritedAbstractMember) {
else if (action.fixName === fixNames.fixClassDoesntImplementInheritedAbstractMember) {
message = `Implement the stubbed-out class members for ${document.getText(diagnostic.range)} with a useful implementation.`;
expand = { kind: 'code-action', action };
}
@@ -363,18 +363,18 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
actions.push(inferFromBody);
}
else if (action.fixName === fixNames.addNameToNamelessParameter) {
const newText = action.changes.map(change => change.textChanges.map(textChange => textChange.newText).join('')).join('')
message = `Rename the parameter ${newText} with a more meaningful name.`,
const newText = action.changes.map(change => change.textChanges.map(textChange => textChange.newText).join('')).join('');
message = `Rename the parameter ${newText} with a more meaningful name.`;
expand = {
kind: 'navtree-function',
pos: diagnostic.range.start
};
}
if (expand && message != null) {
if (expand && message !== undefined) {
codeAction.command = {
command: CompositeCommand.ID,
title: '',
arguments: [codeAction.command, {
arguments: [codeAction.command, {
command: EditorChatFollowUp.ID,
title: '',
arguments: [<EditorChatFollowUp.Args>{
@@ -383,7 +383,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
document
}],
}],
}
};
}
}
return actions;

View File

@@ -573,7 +573,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
if (action.name === 'Move to file') {
codeAction = new MoveToFileCodeAction(document, action, rangeOrSelection);
} else {
let copilotRename: ((info: Proto.RefactorEditInfo) => vscode.Command) | undefined
let copilotRename: ((info: Proto.RefactorEditInfo) => vscode.Command) | undefined;
if (vscode.workspace.getConfiguration('typescript', null).get('experimental.aiQuickFix')) {
if (Extract_Constant.matches(action)
|| Extract_Function.matches(action)
@@ -582,22 +582,22 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
|| action.name.startsWith('Infer function return')) {
const newName = Extract_Constant.matches(action) ? 'newLocal'
: Extract_Function.matches(action) ? 'newFunction'
: Extract_Type.matches(action) ? 'NewType'
: Extract_Interface.matches(action) ? 'NewInterface'
: action.name.startsWith('Infer function return') ? 'newReturnType'
: '';
: Extract_Type.matches(action) ? 'NewType'
: Extract_Interface.matches(action) ? 'NewInterface'
: action.name.startsWith('Infer function return') ? 'newReturnType'
: '';
copilotRename = info => ({
title: '',
command: EditorChatFollowUp.ID,
arguments: [<EditorChatFollowUp.Args>{
message: `Rename ${newName} to a better name based on usage.`,
expand: Extract_Constant.matches(action) ? {
expand: Extract_Constant.matches(action) ? {
kind: 'navtree-function',
pos: typeConverters.Position.fromLocation(info.renameLocation!),
} : {
} : {
kind: 'refactor-info',
refactor: info,
},
},
document,
}]
});

View File

@@ -1,3 +1,8 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { Command } from '../../commands/commandManager';
import { nulToken } from '../../utils/cancellation';
@@ -8,16 +13,33 @@ import { ITypeScriptServiceClient } from '../../typescriptService';
export class EditorChatFollowUp implements Command {
public static readonly ID = '_typescript.quickFix.editorChatReplacement2';
public readonly id = EditorChatFollowUp.ID;
constructor(
private readonly client: ITypeScriptServiceClient,
) {
}
constructor(private readonly client: ITypeScriptServiceClient) { }
async execute({ message, document, expand }: EditorChatFollowUp.Args) {
const initialRange = expand.kind === 'navtree-function' ? await findScopeEndLineFromNavTree(this.client, document, expand.pos.line)
: expand.kind === 'refactor-info' ? await findEditScope(this.client, document, expand.refactor.edits.flatMap(e => e.textChanges))
: expand.kind === 'code-action' ? await findEditScope(this.client, document, expand.action.changes.flatMap(c => c.textChanges))
: expand.range;
await vscode.commands.executeCommand('vscode.editorChat.start', { initialRange, message, autoSend: true });
const initialRange =
expand.kind === 'navtree-function'
? await findScopeEndLineFromNavTree(
this.client,
document,
expand.pos.line
)
: expand.kind === 'refactor-info'
? await findEditScope(
this.client,
document,
expand.refactor.edits.flatMap((e) => e.textChanges)
)
: expand.kind === 'code-action'
? await findEditScope(
this.client,
document,
expand.action.changes.flatMap((c) => c.textChanges)
)
: expand.range;
await vscode.commands.executeCommand('vscode.editorChat.start', {
initialRange,
message,
autoSend: true,
});
}
}
export namespace EditorChatFollowUp {
@@ -34,62 +56,92 @@ export class CompositeCommand implements Command {
public async execute(...commands: vscode.Command[]): Promise<void> {
for (const command of commands) {
await vscode.commands.executeCommand(command.command, ...(command.arguments ?? []));
await vscode.commands.executeCommand(
command.command,
...(command.arguments ?? [])
);
}
}
}
export type Expand = { kind: 'none', readonly range: vscode.Range }
| { kind: 'navtree-function', readonly pos: vscode.Position }
| { kind: 'refactor-info', readonly refactor: Proto.RefactorEditInfo }
| { kind: 'code-action', readonly action: Proto.CodeAction }
export type Expand =
| { kind: 'none'; readonly range: vscode.Range }
| { kind: 'navtree-function'; readonly pos: vscode.Position }
| { kind: 'refactor-info'; readonly refactor: Proto.RefactorEditInfo }
| { kind: 'code-action'; readonly action: Proto.CodeAction };
function findScopeEndLineFromNavTreeWorker(startLine: number, navigationTree: Proto.NavigationTree[]): vscode.Range | undefined {
function findScopeEndLineFromNavTreeWorker(
startLine: number,
navigationTree: Proto.NavigationTree[]
): vscode.Range | undefined {
for (const node of navigationTree) {
const range = typeConverters.Range.fromTextSpan(node.spans[0]);
if (startLine === range.start.line) {
return range;
} else if (startLine > range.start.line && startLine <= range.end.line && node.childItems) {
} else if (
startLine > range.start.line &&
startLine <= range.end.line &&
node.childItems
) {
return findScopeEndLineFromNavTreeWorker(startLine, node.childItems);
}
}
return undefined;
}
async function findScopeEndLineFromNavTree(client: ITypeScriptServiceClient, document: vscode.TextDocument, startLine: number) {
const filepath = client.toOpenTsFilePath(document);
if (!filepath) {
return;
}
const response = await client.execute('navtree', { file: filepath }, nulToken);
if (response.type !== 'response' || !response.body?.childItems) {
return;
}
return findScopeEndLineFromNavTreeWorker(startLine, response.body.childItems);
async function findScopeEndLineFromNavTree(
client: ITypeScriptServiceClient,
document: vscode.TextDocument,
startLine: number
) {
const filepath = client.toOpenTsFilePath(document);
if (!filepath) {
return;
}
const response = await client.execute(
'navtree',
{ file: filepath },
nulToken
);
if (response.type !== 'response' || !response.body?.childItems) {
return;
}
return findScopeEndLineFromNavTreeWorker(startLine, response.body.childItems);
}
async function findEditScope(client: ITypeScriptServiceClient, document: vscode.TextDocument, edits: Proto.CodeEdit[]): Promise<vscode.Range> {
let first = typeConverters.Position.fromLocation(edits[0].start)
let firstEdit = edits[0]
let lastEdit = edits[0]
let last = typeConverters.Position.fromLocation(edits[0].start)
async function findEditScope(
client: ITypeScriptServiceClient,
document: vscode.TextDocument,
edits: Proto.CodeEdit[]
): Promise<vscode.Range> {
let first = typeConverters.Position.fromLocation(edits[0].start);
let firstEdit = edits[0];
let lastEdit = edits[0];
let last = typeConverters.Position.fromLocation(edits[0].start);
for (const edit of edits) {
const start = typeConverters.Position.fromLocation(edit.start)
const end = typeConverters.Position.fromLocation(edit.end)
const start = typeConverters.Position.fromLocation(edit.start);
const end = typeConverters.Position.fromLocation(edit.end);
if (start.compareTo(first) < 0) {
first = start
firstEdit = edit
first = start;
firstEdit = edit;
}
if (end.compareTo(last) > 0) {
last = end
lastEdit = edit
last = end;
lastEdit = edit;
}
}
const text = document.getText()
let startIndex = text.indexOf(firstEdit.newText)
let start = startIndex > -1 ? document.positionAt(startIndex) : first
let endIndex = text.lastIndexOf(lastEdit.newText)
let end = endIndex > -1 ? document.positionAt(endIndex + lastEdit.newText.length) : last
const expandEnd = await findScopeEndLineFromNavTree(client, document, end.line)
return new vscode.Range(start, expandEnd?.end ?? end)
const text = document.getText();
const startIndex = text.indexOf(firstEdit.newText);
const start = startIndex > -1 ? document.positionAt(startIndex) : first;
const endIndex = text.lastIndexOf(lastEdit.newText);
const end =
endIndex > -1
? document.positionAt(endIndex + lastEdit.newText.length)
: last;
const expandEnd = await findScopeEndLineFromNavTree(
client,
document,
end.line
);
return new vscode.Range(start, expandEnd?.end ?? end);
}