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.
This commit is contained in:
bobo.yang 2023-07-24 08:16:47 +08:00
parent f389a7bbb5
commit 7fe536f9d8
6 changed files with 14 additions and 12 deletions

View File

@ -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);

View File

@ -53,7 +53,7 @@ async function getSelectedSymbol(): Promise<vscode.DocumentSymbol | undefined> {
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;

View File

@ -63,7 +63,7 @@ async function getSymbolDefine(symbolList: string[]): Promise<string[]> {
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<string> = new Set();
// visit each symbol in symbolList, and get it's define
@ -138,9 +138,9 @@ async function getSymbolDefine(symbolList: string[]): Promise<string[]> {
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<string[]> {
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);

View File

@ -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));

View File

@ -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<void> {
export async function sendCodeSelectMessage(panel: vscode.WebviewPanel|vscode.WebviewView, filePath: string, codeBlock: string, startLine: number): Promise<void> {
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 });
}

View File

@ -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;