Clean up for review

Remove unused, abandoned ideas.
This commit is contained in:
Nathan Shively-Sanders
2023-09-08 09:28:53 -07:00
parent 7906b2bb61
commit 3328d54c89
7 changed files with 27 additions and 103 deletions

View File

@@ -18,7 +18,7 @@ import { DiagnosticsManager } from './diagnostics';
import FileConfigurationManager from './fileConfigurationManager';
import { applyCodeActionCommands, getEditForCodeAction } from './util/codeAction';
import { conditionalRegistration, requireSomeCapability } from './util/dependentRegistration';
import { ChatPanelFollowup, Expand, EditorChatReplacementCommand2, CompositeCommand } from './util/copilot';
import { Expand, EditorChatFollowUp, CompositeCommand } from './util/copilot';
type ApplyCodeActionCommand_args = {
readonly document: vscode.TextDocument;
@@ -223,8 +223,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
commandManager.register(new CompositeCommand());
commandManager.register(new ApplyCodeActionCommand(client, diagnosticsManager, telemetryReporter));
commandManager.register(new ApplyFixAllCodeAction(client, telemetryReporter));
commandManager.register(new EditorChatReplacementCommand2(client));
commandManager.register(new ChatPanelFollowup(client));
commandManager.register(new EditorChatFollowUp(client));
this.supportedCodeActionProvider = new SupportedCodeActionProvider(client);
}
@@ -345,7 +344,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
expand = { kind: 'code-action', action };
}
else if (action.fixName === fixNames.fixMissingFunctionDeclaration) {
message = `Provide a reasonable implementation of the function ${document.getText(diagnostic.range)}} given its type and the context it's called in.`;
message = `Provide a reasonable implementation of the function ${document.getText(diagnostic.range)} given its type and the context it's called in.`;
expand = { kind: 'code-action', action };
}
else if (action.fixName === fixNames.inferFromUsage) {
@@ -353,8 +352,8 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
inferFromBody.edit = new vscode.WorkspaceEdit();
inferFromBody.diagnostics = [diagnostic];
inferFromBody.command = {
command: EditorChatReplacementCommand2.ID,
arguments: [<EditorChatReplacementCommand2.Args>{
command: EditorChatFollowUp.ID,
arguments: [<EditorChatFollowUp.Args>{
message: 'Add types to this code. Add separate interfaces when possible. Do not change the code except for adding types.',
expand: { kind: 'navtree-function', pos: diagnostic.range.start },
document
@@ -376,9 +375,9 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
command: CompositeCommand.ID,
title: '',
arguments: [codeAction.command, {
command: EditorChatReplacementCommand2.ID,
command: EditorChatFollowUp.ID,
title: '',
arguments: [<EditorChatReplacementCommand2.Args>{
arguments: [<EditorChatFollowUp.Args>{
message,
expand,
document

View File

@@ -20,7 +20,7 @@ import { coalesce } from '../utils/arrays';
import { nulToken } from '../utils/cancellation';
import FormattingOptionsManager from './fileConfigurationManager';
import { conditionalRegistration, requireSomeCapability } from './util/dependentRegistration';
import { ChatPanelFollowup, EditorChatReplacementCommand2, CompositeCommand } from './util/copilot';
import { EditorChatFollowUp, CompositeCommand } from './util/copilot';
function toWorkspaceEdit(client: ITypeScriptServiceClient, edits: readonly Proto.FileCodeEdits[]): vscode.WorkspaceEdit {
const workspaceEdit = new vscode.WorkspaceEdit();
@@ -345,7 +345,7 @@ class InlinedCodeAction extends vscode.CodeAction {
public readonly refactor: Proto.ApplicableRefactorInfo,
public readonly action: Proto.RefactorActionInfo,
public readonly range: vscode.Range,
public readonly airename?: (x: Proto.RefactorEditInfo) => vscode.Command,
public readonly copilotRename?: (info: Proto.RefactorEditInfo) => vscode.Command,
) {
super(action.description, InlinedCodeAction.getKind(action));
@@ -390,9 +390,8 @@ class InlinedCodeAction extends vscode.CodeAction {
command: CompositeCommand.ID,
title: '',
arguments: coalesce([
// TODO: this.bonus should really get renameLocation as well (at least most of the time)
this.command,
this.airename ? this.airename(response.body) : {
this.copilotRename ? this.copilotRename(response.body) : {
command: 'editor.action.rename',
arguments: [[
this.document.uri,
@@ -448,29 +447,7 @@ class SelectCodeAction extends vscode.CodeAction {
};
}
}
class InferTypesAction extends vscode.CodeAction {
constructor(
document: vscode.TextDocument,
rangeOrSelection: vscode.Range | vscode.Selection
) {
const title = "Copilot REFACTOR: Infer and add types"
super(title, vscode.CodeActionKind.Refactor);
this.command = {
title,
command: EditorChatReplacementCommand2.ID,
arguments: [<EditorChatReplacementCommand2.Args>{
message: 'Add types to this code. Add separate interfaces when possible. Do not change the code except for adding types.',
document,
expand: {
kind: 'none',
range: rangeOrSelection
}
}]
};
}
}
type TsCodeAction = InlinedCodeAction | MoveToFileCodeAction | SelectCodeAction | InferTypesAction;
type TsCodeAction = InlinedCodeAction | MoveToFileCodeAction | SelectCodeAction;
class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeAction> {
@@ -484,8 +461,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
commandManager.register(new CompositeCommand());
commandManager.register(new SelectRefactorCommand(this.client));
commandManager.register(new MoveToFileRefactorCommand(this.client, didApplyRefactoringCommand));
commandManager.register(new EditorChatReplacementCommand2(this.client));
commandManager.register(new ChatPanelFollowup(this.client));
commandManager.register(new EditorChatFollowUp(this.client));
}
public static readonly metadata: vscode.CodeActionProviderMetadata = {
@@ -584,9 +560,6 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
}
}
}
if (true) {
yield new InferTypesAction(document, rangeOrSelection)
}
}
private refactorActionToCodeAction(
@@ -600,7 +573,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
if (action.name === 'Move to file') {
codeAction = new MoveToFileCodeAction(document, action, rangeOrSelection);
} else {
let airename: ((rename: 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)
@@ -613,17 +586,17 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
: Extract_Interface.matches(action) ? 'NewInterface'
: action.name.startsWith('Infer function return') ? 'newReturnType'
: '';
airename = refactorInfo => ({
copilotRename = info => ({
title: '',
command: EditorChatReplacementCommand2.ID,
arguments: [<EditorChatReplacementCommand2.Args>{
command: EditorChatFollowUp.ID,
arguments: [<EditorChatFollowUp.Args>{
message: `Rename ${newName} to a better name based on usage.`,
expand: Extract_Constant.matches(action) ? {
kind: 'navtree-function',
pos: typeConverters.Position.fromLocation(refactorInfo.renameLocation!),
pos: typeConverters.Position.fromLocation(info.renameLocation!),
} : {
kind: 'refactor-info',
refactor: refactorInfo,
refactor: info,
},
document,
}]
@@ -631,7 +604,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
}
}
codeAction = new InlinedCodeAction(this.client, document, refactor, action, rangeOrSelection, airename);
codeAction = new InlinedCodeAction(this.client, document, refactor, action, rangeOrSelection, copilotRename);
}
codeAction.isPreferred = TypeScriptRefactorProvider.isPreferred(action, allActions);

View File

@@ -5,33 +5,22 @@ import type * as Proto from '../../tsServer/protocol/protocol';
import * as typeConverters from '../../typeConverters';
import { ITypeScriptServiceClient } from '../../typescriptService';
export class EditorChatReplacementCommand2 implements Command {
export class EditorChatFollowUp implements Command {
public static readonly ID = '_typescript.quickFix.editorChatReplacement2';
public readonly id = EditorChatReplacementCommand2.ID;
public readonly id = EditorChatFollowUp.ID;
constructor(
private readonly client: ITypeScriptServiceClient,
) {
}
async execute({ message, document, expand }: EditorChatReplacementCommand2.Args) {
// TODO: "this code" is not specific; might get better results with a more specific referent
// TODO: Doesn't work in JS files? Is this the span-finder's fault? Try falling back to startLine plus something.
// TODO: Need to emit jsdoc in JS files once it's working at all
// TODO: When there are "enough" types around, leave off the "Add separate interfaces when possible" because it's not helpful.
// (brainstorming: enough non-primitives, or evidence of type aliases in the same file, or imported)
async execute({ message, document, expand }: EditorChatFollowUp.Args) {
const initialRange = expand.kind === 'navtree-function' ? await findScopeEndLineFromNavTree(this.client, document, expand.pos.line)
: expand.kind === 'identifier' ? findScopeEndMarker(document, expand.range.start, expand.marker)
: 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;
if (initialRange) {
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + message
+ `\nWith context(${expand}): ` + document.getText().slice(document.offsetAt(initialRange.start), document.offsetAt(initialRange.end))
+ "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
}
await vscode.commands.executeCommand('vscode.editorChat.start', { initialRange, message, autoSend: true });
}
}
export namespace EditorChatReplacementCommand2 {
export namespace EditorChatFollowUp {
export interface Args {
readonly message: string;
readonly document: vscode.TextDocument;
@@ -39,33 +28,6 @@ export namespace EditorChatReplacementCommand2 {
}
}
export namespace ChatPanelFollowup {
export interface Args {
readonly prompt: string;
readonly document: vscode.TextDocument;
readonly range: vscode.Range;
readonly expand: Expand;
readonly marker?: string;
readonly refactor?: Proto.RefactorEditInfo;
}
// assuming there is an ast to walk, I'm convinced I can do a more consistent job than the navtree code.
}
export class ChatPanelFollowup implements Command {
public readonly id = ChatPanelFollowup.ID;
public static readonly ID: string = '_typescript.refactor.chatPanelFollowUp';
constructor(private readonly client: ITypeScriptServiceClient) {
}
async execute({ prompt, document, range, expand, marker }: ChatPanelFollowup.Args) {
console.log("-------------------------------" + prompt + "------------------------------")
const enclosingRange = expand.kind === 'navtree-function' ? await findScopeEndLineFromNavTree(this.client, document, range.start.line)
: expand.kind === 'identifier' ? findScopeEndMarker(document, range.start, marker!)
: range;
vscode.interactive.sendInteractiveRequestToProvider('copilot', { message: prompt, autoSend: true, initialRange: enclosingRange } as any)
}
}
export class CompositeCommand implements Command {
public static readonly ID = '_typescript.compositeCommand';
public readonly id = CompositeCommand.ID;
@@ -78,10 +40,9 @@ export class CompositeCommand implements Command {
}
export type Expand = { kind: 'none', readonly range: vscode.Range }
| { kind: "navtree-function", readonly pos: vscode.Position }
| { kind: 'navtree-function', readonly pos: vscode.Position }
| { kind: 'refactor-info', readonly refactor: Proto.RefactorEditInfo }
| { kind: 'code-action', readonly action: Proto.CodeAction }
| { kind: "identifier", readonly range: vscode.Range, readonly marker: string };
function findScopeEndLineFromNavTreeWorker(startLine: number, navigationTree: Proto.NavigationTree[]): vscode.Range | undefined {
for (const node of navigationTree) {
@@ -107,13 +68,6 @@ async function findScopeEndLineFromNavTree(client: ITypeScriptServiceClient, doc
return findScopeEndLineFromNavTreeWorker(startLine, response.body.childItems);
}
function findScopeEndMarker(document: vscode.TextDocument, start: vscode.Position, marker: string): vscode.Range {
const text = document.getText();
const offset = text.indexOf(marker, text.indexOf(marker)) + marker.length
// TODO: Expand to the end of whatever marker is. (OR, chain to findScopeEndLineFromnavTree)
return new vscode.Range(start, document.positionAt(offset))
}
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]