From 7ab83934ff58664ed9815403fb9775ded13bf655 Mon Sep 17 00:00:00 2001 From: Dima Date: Tue, 12 Dec 2023 01:27:02 +0200 Subject: [PATCH] fixes --- .../{codeLens => }/ChatCodeLensProvider.ts | 62 +++++++++++------- .../extensionCommands/codeLens/index.ts | 65 ------------------- .../{slashCommands.ts => commands.ts} | 32 ++++++++- .../registerChatCommnmads.ts | 54 ++++++++++++++- .../tabnineChatWidgetWebview.ts | 4 +- 5 files changed, 119 insertions(+), 98 deletions(-) rename src/tabnineChatWidget/extensionCommands/{codeLens => }/ChatCodeLensProvider.ts (63%) delete mode 100644 src/tabnineChatWidget/extensionCommands/codeLens/index.ts rename src/tabnineChatWidget/extensionCommands/{slashCommands.ts => commands.ts} (62%) diff --git a/src/tabnineChatWidget/extensionCommands/codeLens/ChatCodeLensProvider.ts b/src/tabnineChatWidget/extensionCommands/ChatCodeLensProvider.ts similarity index 63% rename from src/tabnineChatWidget/extensionCommands/codeLens/ChatCodeLensProvider.ts rename to src/tabnineChatWidget/extensionCommands/ChatCodeLensProvider.ts index a05c9eddf3..a3aa8a6e58 100644 --- a/src/tabnineChatWidget/extensionCommands/codeLens/ChatCodeLensProvider.ts +++ b/src/tabnineChatWidget/extensionCommands/ChatCodeLensProvider.ts @@ -3,31 +3,37 @@ import { CancellationToken, CodeLens, CodeLensProvider, - commands, Diagnostic, DiagnosticSeverity, - DocumentSymbol, Event, EventEmitter, + ExtensionContext, languages, Location, - SymbolInformation, - SymbolKind, TextDocument, Uri, } from "vscode"; -import { fireEvent } from "../../../binary/requests/requests"; - -const CODE_LENS_ACTIONS = [ - ["test", "generate-test-for-code"], - ["fix", "fix-code"], - ["explain", "explain-code"], - ["document", "document-code"], -]; +import { fireEvent } from "../../binary/requests/requests"; +import { getFuctionsSymbols } from "./getFuctionsSymbols"; +import { Action, COMANDS } from "./commands"; +import tabnineExtensionProperties from "../../globals/tabnineExtensionProperties"; +import { languagesFilter } from "./const"; const MAX_LINES = 2500; -export class ChatCodeLensProvider implements CodeLensProvider { +export default function registerChatCodeLens(context: ExtensionContext) { + if (!tabnineExtensionProperties.codeLensEnabled) { + return; + } + context.subscriptions.push( + languages.registerCodeLensProvider( + languagesFilter, + new ChatCodeLensProvider() + ) + ); +} + +class ChatCodeLensProvider implements CodeLensProvider { private visitedFiles: Set = new Set(); private didChangeCodeLenses = new EventEmitter(); @@ -63,7 +69,7 @@ export class ChatCodeLensProvider implements CodeLensProvider { const lenses: CodeLens[] = []; documnetSymbols.forEach(({ location }) => { - lenses.push(...toIntentLens(location), toAskLens(location)); + lenses.push(...toIntentLens(location, diagnostic), toAskLens(location)); }); if (!this.visitedFiles.has(document.uri)) { @@ -102,20 +108,26 @@ function toIntentLens( location: Location, diagnostics: Diagnostic[] ): CodeLens[] { - return CODE_LENS_ACTIONS.filter(([text]) => - filterRelevantActions(text, location, diagnostics) - ).map( - ([text, action], index) => - new CodeLens(location.range, { - title: `${index === 0 ? "tabnine: " : ""}${text}`, - tooltip: `tabnine ${text}`, - command: "tabnine.chat.commands.any", - arguments: [location.range, action], - }) + return ( + COMANDS.filter( + ({ text, lensOrder }) => + lensOrder && filterRelevantActions(text, location, diagnostics) + ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + .sort((a, b) => a.lensOrder! - b.lensOrder!) + .map( + ({ text, intent }, index) => + new CodeLens(location.range, { + title: `${index === 0 ? "tabnine: " : ""}${text}`, + tooltip: `tabnine ${text}`, + command: "tabnine.chat.commands.any", + arguments: [location.range, intent], + }) + ) ); } function filterRelevantActions( - text: Intents, + text: Action, location: Location, diagnostics: Diagnostic[] ): boolean { diff --git a/src/tabnineChatWidget/extensionCommands/codeLens/index.ts b/src/tabnineChatWidget/extensionCommands/codeLens/index.ts deleted file mode 100644 index a08aa5648b..0000000000 --- a/src/tabnineChatWidget/extensionCommands/codeLens/index.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { - commands, - ExtensionContext, - languages, - Range, - Selection, - window, -} from "vscode"; - -import ChatViewProvider from "../../ChatViewProvider"; -import tabnineExtensionProperties from "../../../globals/tabnineExtensionProperties"; -import { fireEvent } from "../../../binary/requests/requests"; -import { showInput } from "../showInput"; -import { SLASH_COMANDS } from "../slashCommands"; -import { languagesFilter } from "../const"; - -export default function registerChatCodeLens( - context: ExtensionContext, - chatProvider: ChatViewProvider -) { - if (!tabnineExtensionProperties.codeLensEnabled) { - return; - } - context.subscriptions.push( - languages.registerCodeLensProvider( - languagesFilter, - new ChatCodeLensProvider() - ), - commands.registerCommand( - "tabnine.chat.commands.any", - (range: Range, intent: Actions) => { - void fireEvent({ - name: "chat-lens-action", - intent, - language: window.activeTextEditor?.document.languageId, - }); - - const editor = window.activeTextEditor; - if (editor) { - const newSelection = new Selection(range.start, range.end); - editor.selection = newSelection; - } - void chatProvider.handleMessageSubmitted(`/${intent}`); - } - ), - commands.registerCommand("tabnine.chat.commands.ask", (range: Range) => { - void fireEvent({ - name: "chat-lens-action", - intent: "ask-question", - language: window.activeTextEditor?.document.languageId, - }); - - const editor = window.activeTextEditor; - if (editor) { - const newSelection = new Selection(range.start, range.end); - editor.selection = newSelection; - } - void showInput(SLASH_COMANDS).then((question) => { - if (question) { - void chatProvider.handleMessageSubmitted(question); - } - }); - }) - ); -} diff --git a/src/tabnineChatWidget/extensionCommands/slashCommands.ts b/src/tabnineChatWidget/extensionCommands/commands.ts similarity index 62% rename from src/tabnineChatWidget/extensionCommands/slashCommands.ts rename to src/tabnineChatWidget/extensionCommands/commands.ts index 1e23c8ed51..d79d00451b 100644 --- a/src/tabnineChatWidget/extensionCommands/slashCommands.ts +++ b/src/tabnineChatWidget/extensionCommands/commands.ts @@ -1,43 +1,69 @@ type SCOPE = "block" | "selection" | "none"; -export type SlashCommand = { +export type Action = "test" | "fix" | "explain" | "document" | "workspace"; +export type Intent = + | "/generate-test-for-code" + | "/fix-code" + | "/explain-code" + | "/document-code" + | "/workspace"; + +enum LensOrder { + test = 1, + fix = 2, + explain = 3, + document = 4, +} + +export type Command = { label: string; - intent: string; + text: Action; + intent: Intent; description: string; scope: SCOPE[]; multistep: boolean; + lensOrder?: LensOrder | undefined; }; -export const SLASH_COMANDS: SlashCommand[] = [ +export const COMANDS: Command[] = [ { label: "$(feedback) explain", + text: "explain", intent: "/explain-code", description: "Explain the selected code", scope: ["selection", "block"], multistep: false, + lensOrder: LensOrder.explain, }, { label: "$(beaker) test", + text: "test", intent: "/generate-test-for-code", description: "Write tests for the selected code", scope: ["block"], multistep: false, + lensOrder: LensOrder.test, }, { label: "$(checklist) document", + text: "document", intent: "/document-code", description: "Add documentation for the selected code", scope: ["block"], multistep: false, + lensOrder: LensOrder.document, }, { label: "$(symbol-property) fix", + text: "fix", intent: "/fix-code", description: "Find errors in the selected code and fix them", scope: ["block"], multistep: false, + lensOrder: LensOrder.fix, }, { label: "$(search) workspace", + text: "workspace", intent: "/workspace", description: "Ask a question related to any code within your current workspace", diff --git a/src/tabnineChatWidget/extensionCommands/registerChatCommnmads.ts b/src/tabnineChatWidget/extensionCommands/registerChatCommnmads.ts index 10abffa976..e192acc6d7 100644 --- a/src/tabnineChatWidget/extensionCommands/registerChatCommnmads.ts +++ b/src/tabnineChatWidget/extensionCommands/registerChatCommnmads.ts @@ -1,15 +1,18 @@ /* eslint-disable no-param-reassign */ import { Disposable, + Range, Selection, SymbolInformation, TextEditor, commands, + window, } from "vscode"; import ChatViewProvider from "../ChatViewProvider"; import { getFuctionsSymbols } from "./getFuctionsSymbols"; -import { SLASH_COMANDS } from "./slashCommands"; +import { COMANDS, Intent } from "./commands"; import { showInput } from "./showInput"; +import { fireEvent } from "../../binary/requests/requests"; export function registerChatCommnmads( chatProvider: ChatViewProvider @@ -42,7 +45,7 @@ export function registerChatCommnmads( commands.registerTextEditorCommand( "tabnine.chat.commands.inline.action", (textEditor: TextEditor) => { - void showInput(SLASH_COMANDS).then((result) => { + void showInput(COMANDS).then((result) => { if (textEditor.selection.isEmpty) { void getFuctionsSymbols(textEditor.document).then( (relevantSymbols: SymbolInformation[]) => { @@ -60,12 +63,52 @@ export function registerChatCommnmads( ); if (result) { + void fireEvent({ + name: "chat-ide-action", + intent: result, + language: window.activeTextEditor?.document.languageId, + }); void chatProvider.handleMessageSubmitted(result); } } }); } - ) + ), + commands.registerCommand( + "tabnine.chat.commands.any", + (range: Range, intent: Intent) => { + void fireEvent({ + name: "chat-lens-action", + intent, + language: window.activeTextEditor?.document.languageId, + }); + + const editor = window.activeTextEditor; + if (editor) { + const newSelection = new Selection(range.start, range.end); + editor.selection = newSelection; + } + void chatProvider.handleMessageSubmitted(intent); + } + ), + commands.registerCommand("tabnine.chat.commands.ask", (range: Range) => { + void fireEvent({ + name: "chat-lens-action", + intent: "ask-question", + language: window.activeTextEditor?.document.languageId, + }); + + const editor = window.activeTextEditor; + if (editor) { + const newSelection = new Selection(range.start, range.end); + editor.selection = newSelection; + } + void showInput(COMANDS).then((question) => { + if (question) { + void chatProvider.handleMessageSubmitted(question); + } + }); + }) ); } @@ -74,6 +117,11 @@ function contextActionHandler( textEditor: TextEditor, intent: string ): void { + void fireEvent({ + name: "chat-ide-action", + intent, + language: window.activeTextEditor?.document.languageId, + }); if (textEditor.selection.isEmpty) { void getFuctionsSymbols(textEditor.document).then( (relevantSymbols: SymbolInformation[]) => { diff --git a/src/tabnineChatWidget/tabnineChatWidgetWebview.ts b/src/tabnineChatWidget/tabnineChatWidgetWebview.ts index fedf4f8ae6..27d148f56d 100644 --- a/src/tabnineChatWidget/tabnineChatWidgetWebview.ts +++ b/src/tabnineChatWidget/tabnineChatWidgetWebview.ts @@ -4,9 +4,9 @@ import ChatViewProvider from "./ChatViewProvider"; import { getState } from "../binary/requests/requests"; import { Logger } from "../utils/logger"; import { registerChatActionProvider } from "./extensionCommands/ChatActionProvider"; -import registerChatCodeLens from "./extensionCommands/codeLens"; import { registerChatCommnmads } from "./extensionCommands/registerChatCommnmads"; import ChatEnabledState, { ChatNotEnabledReason } from "./ChatEnabledState"; +import registerChatCodeLens from "./extensionCommands/ChatCodeLensProvider"; const VIEW_ID = "tabnine.chat"; @@ -103,7 +103,7 @@ function registerWebview( ); context.subscriptions.push(registerChatCommnmads(chatProvider)); registerChatActionProvider(context); - registerChatCodeLens(context, chatProvider); + registerChatCodeLens(context); hasRegisteredChatWebview = true; }