Merge pull request #365 from devchat-ai/codelens_add_class_scope

Add containerRange property to FunctionDefinition
This commit is contained in:
boob.yang 2023-12-19 12:40:10 +08:00 committed by GitHub
commit c924834400
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ import { has } from 'mobx';
interface FunctionDefinition { interface FunctionDefinition {
name: string; name: string;
containerName: string | null; containerName: string | null;
containerRange: vscode.Range | null;
range: vscode.Range; range: vscode.Range;
} }
@ -86,24 +87,25 @@ async function getFunctionDefinitions(document: vscode.TextDocument, inner_funct
return []; return [];
} }
function extractFunctions(symbol: vscode.DocumentSymbol, containerName: string | null, hasInFunction: boolean = false): FunctionDefinition[] { function extractFunctions(symbol: vscode.DocumentSymbol, containerSymbol: vscode.DocumentSymbol | null, hasInFunction: boolean = false): FunctionDefinition[] {
console.log('==> symbol range:', symbol.range, symbol.name, symbol.kind);
let functions: FunctionDefinition[] = []; let functions: FunctionDefinition[] = [];
if (symbol.kind === vscode.SymbolKind.Function || symbol.kind === vscode.SymbolKind.Method) { const isFunction = symbol.kind === vscode.SymbolKind.Function || symbol.kind === vscode.SymbolKind.Method;
if (isFunction) {
if (!inner_function || (inner_function && hasInFunction)) { if (!inner_function || (inner_function && hasInFunction)) {
functions.push({ functions.push({
name: symbol.name, name: symbol.name,
containerName: containerName, containerName: containerSymbol? containerSymbol.name : null,
containerRange: containerSymbol? containerSymbol.range : null,
range: symbol.range range: symbol.range
}); });
} }
hasInFunction = true; hasInFunction = true;
} }
if (inner_function) { if (inner_function || !isFunction) {
if (symbol.children && symbol.children.length > 0) { if (symbol.children && symbol.children.length > 0) {
symbol.children.forEach(child => { symbol.children.forEach(child => {
functions = functions.concat(extractFunctions(child, symbol.name, hasInFunction)); functions = functions.concat(extractFunctions(child, symbol, hasInFunction));
}); });
} }
} }
@ -149,12 +151,16 @@ class FunctionTestCodeLensProvider implements vscode.CodeLensProvider {
// Read range content in document // Read range content in document
const functionCode = document.getText(range); const functionCode = document.getText(range);
const parentRange = funcDef.containerRange;
const parentRangeStr = parentRange? `[${parentRange.start.line}, ${parentRange.end.line}]` : "";
// Fix the string replacement syntax and closing parentheses // Fix the string replacement syntax and closing parentheses
const prompt = codelenRegister.promptGenerator const prompt = codelenRegister.promptGenerator
.replace('{__filename__}', document.uri.fsPath) .replace('{__filename__}', document.uri.fsPath)
.replace('{__functionName__}', funcDef.name) .replace('{__functionName__}', funcDef.name)
.replace('{__functionRange__}', `[${range.start.line}, ${range.end.line}]`) .replace('{__functionRange__}', `[${range.start.line}, ${range.end.line}]`)
.replace('{__containerName__}', funcDef.containerName || '')
.replace('{__containerRange__}', parentRangeStr)
.replace('{__functionCode__}', functionCode); // Fixed syntax .replace('{__functionCode__}', functionCode); // Fixed syntax
const lens = new vscode.CodeLens(range, { const lens = new vscode.CodeLens(range, {