From 7fe536f9d8b82956bee84ebc777bf85f47cbbaa1 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 24 Jul 2023 08:16:47 +0800 Subject: [PATCH] Include start line in code selection context - Added start line parameter to handleCodeSelected function. - Updated all calls to handleCodeSelected to include the start line. - The start line is now included in the context data for symbol references and definitions. --- src/context/contextCodeSelected.ts | 3 ++- src/context/contextDefRefs.ts | 7 ++++--- src/context/contextRefDefs.ts | 8 ++++---- src/contributes/commands.ts | 2 +- src/contributes/util.ts | 4 ++-- test/context/contextCodeSelected.test.ts | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/context/contextCodeSelected.ts b/src/context/contextCodeSelected.ts index 569aa40..0f5d435 100644 --- a/src/context/contextCodeSelected.ts +++ b/src/context/contextCodeSelected.ts @@ -3,7 +3,7 @@ import * as path from 'path'; import { createTempSubdirectory, getLanguageIdByFileName } from '../util/commonUtil'; import { UiUtilWrapper } from '../util/uiUtil'; -export async function handleCodeSelected(fileSelected: string, codeSelected: string) { +export async function handleCodeSelected(fileSelected: string, codeSelected: string, startLine: number) { // get file name from fileSelected const fileName = path.basename(fileSelected); @@ -22,6 +22,7 @@ export async function handleCodeSelected(fileSelected: string, codeSelected: str const data = { languageId: languageId, path: relativePath, + startLine: startLine, content: codeSelected }; const jsonData = JSON.stringify(data); diff --git a/src/context/contextDefRefs.ts b/src/context/contextDefRefs.ts index 3d20a30..d98463d 100644 --- a/src/context/contextDefRefs.ts +++ b/src/context/contextDefRefs.ts @@ -53,7 +53,7 @@ async function getSelectedSymbol(): Promise { export const defRefsContext: ChatContext = { name: 'symbol references', - description: 'The context where the selected symbol is referenced', + description: 'References of symble', handler: async () => { const activeEditor = vscode.window.activeTextEditor; if (!activeEditor) { @@ -91,8 +91,9 @@ export const defRefsContext: ChatContext = { const refLocationFile = refLocation.uri.fsPath; const documentNew = await vscode.workspace.openTextDocument(refLocationFile); - const renageNew = new vscode.Range(refLocation.range.start.line - 2, 0, refLocation.range.end.line + 2, 10000); - contextList.push(await handleCodeSelected(refLocationFile, documentNew.getText(renageNew))); + const startLine = refLocation.range.start.line - 2 > 0 ? refLocation.range.start.line - 2 : 0; + const renageNew = new vscode.Range(startLine, 0, refLocation.range.end.line + 2, 10000); + contextList.push(await handleCodeSelected(refLocationFile, documentNew.getText(renageNew), startLine)); } } return contextList; diff --git a/src/context/contextRefDefs.ts b/src/context/contextRefDefs.ts index 9940bbb..1c4eb94 100644 --- a/src/context/contextRefDefs.ts +++ b/src/context/contextRefDefs.ts @@ -63,7 +63,7 @@ async function getSymbolDefine(symbolList: string[]): Promise { const selection = activeEditor!.selection; const selectedText = document.getText(selection); - let contextList: string[] = [await handleCodeSelected(activeEditor!.document.uri.fsPath, selectedText)]; + let contextList: string[] = [await handleCodeSelected(activeEditor!.document.uri.fsPath, selectedText, selection.start.line)]; let hasVisitedSymbols: Set = new Set(); // visit each symbol in symbolList, and get it's define @@ -138,9 +138,9 @@ async function getSymbolDefine(symbolList: string[]): Promise { if (targetSymbol.kind === vscode.SymbolKind.Variable) { const renageNew = new vscode.Range(targetSymbol.range.start.line, 0, targetSymbol.range.end.line, 10000); - contextList.push(await handleCodeSelected(refLocation.uri.fsPath, documentNew.getText(renageNew))); + contextList.push(await handleCodeSelected(refLocation.uri.fsPath, documentNew.getText(renageNew), targetSymbol.range.start.line)); } else { - contextList.push(await handleCodeSelected(refLocation.uri.fsPath, documentNew.getText(targetSymbol.range))); + contextList.push(await handleCodeSelected(refLocation.uri.fsPath, documentNew.getText(targetSymbol.range), targetSymbol.range.start.line)); } } } @@ -153,7 +153,7 @@ async function getSymbolDefine(symbolList: string[]): Promise { export const refDefsContext: ChatContext = { name: 'symbol definitions', - description: 'Context of symbol definition in selected text', + description: 'Definitions of symbol', handler: async () => { const selectedText = await getCurrentSelectText(); const symbolList = await getUndefinedSymbols(selectedText); diff --git a/src/contributes/commands.ts b/src/contributes/commands.ts index bdd0cd5..ebc350a 100644 --- a/src/contributes/commands.ts +++ b/src/contributes/commands.ts @@ -42,7 +42,7 @@ function registerAskForCodeCommand(context: vscode.ExtensionContext) { } const selectedText = editor.document.getText(editor.selection); - await sendCodeSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName, selectedText); + await sendCodeSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName, selectedText, editor.selection.start.line); } }; context.subscriptions.push(vscode.commands.registerCommand('devchat.askForCode', callback)); diff --git a/src/contributes/util.ts b/src/contributes/util.ts index 20923b2..230546c 100644 --- a/src/contributes/util.ts +++ b/src/contributes/util.ts @@ -14,8 +14,8 @@ export async function sendFileSelectMessage(panel: vscode.WebviewPanel|vscode.We } regOutMessage({command: 'appendContext', context: ''}); -export async function sendCodeSelectMessage(panel: vscode.WebviewPanel|vscode.WebviewView, filePath: string, codeBlock: string): Promise { +export async function sendCodeSelectMessage(panel: vscode.WebviewPanel|vscode.WebviewView, filePath: string, codeBlock: string, startLine: number): Promise { logger.channel()?.info(`File selected: ${filePath}`); - const codeContext = await handleCodeSelected(filePath, codeBlock); + const codeContext = await handleCodeSelected(filePath, codeBlock, startLine); MessageHandler.sendMessage(panel, { command: 'appendContext', context: codeContext }); } \ No newline at end of file diff --git a/test/context/contextCodeSelected.test.ts b/test/context/contextCodeSelected.test.ts index 751785a..fea999a 100644 --- a/test/context/contextCodeSelected.test.ts +++ b/test/context/contextCodeSelected.test.ts @@ -28,7 +28,7 @@ describe('handleCodeSelected', () => { const fileSelected = path.join(__dirname, 'testFile.ts'); const codeSelected = 'console.log("Hello, world!");'; - const contextFile = await handleCodeSelected(fileSelected, codeSelected); + const contextFile = await handleCodeSelected(fileSelected, codeSelected, 0); // Check if the mocked functions were called with the correct arguments expect(languageIdStub.calledWith(fileSelected)).to.be.true;