Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 8x 8x 8x 8x 8x 7x 7x 8x 7x 7x 8x 15x 15x 15x 15x 15x 15x 15x 7x 7x 7x 8x 7x 7x 7x 7x 8x 15x 8x 15x 1x 1x 8x 15x 1x 1x 15x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 15x 15x 15x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 2x 11x 11x 11x 11x 11x 9x 9x 11x 1x 1x 11x 11x 11x 11x 19x 19x 19x 20x 20x 20x 20x 20x 20x 20x 20x 19x 11x 11x 2x 15x 15x 15x 15x 15x 15x 15x 15x 684x 684x 2x 2x 684x 1x 682x 1x 1x 1x 1x 681x 19x 680x 19x 19x 15x 15x 19x 668x 668x | import * as vscode from 'vscode';
export interface MethodScope {
name: string;
range: vscode.Range;
code: string;
signature: string;
callers: string[];
callees: string[];
role: string;
}
export interface MethodSymbol {
name: string;
range: vscode.Range;
containerName?: string;
}
interface RawDocSymbol {
name: string;
kind: vscode.SymbolKind;
range?: vscode.Range;
children?: RawDocSymbol[];
location?: vscode.Location;
}
const CONTAINER_KINDS = new Set([
vscode.SymbolKind.Class,
vscode.SymbolKind.Interface,
vscode.SymbolKind.Namespace,
vscode.SymbolKind.Module,
vscode.SymbolKind.Object,
vscode.SymbolKind.Struct,
vscode.SymbolKind.Enum,
vscode.SymbolKind.Package,
]);
function isMethodKind(kind: vscode.SymbolKind): boolean {
return (
kind === vscode.SymbolKind.Function ||
kind === vscode.SymbolKind.Method ||
kind === vscode.SymbolKind.Constructor
);
}
function collectSymbols(symbol: RawDocSymbol, containerName: string | undefined, out: MethodSymbol[]): void {
const kind = symbol.kind;
if (isMethodKind(kind)) {
const range = symbol.range ?? symbol.location?.range;
if (range) {
out.push({ name: symbol.name, range, containerName });
}
}
const nextContainer = CONTAINER_KINDS.has(kind)
? containerName
? `${containerName}.${symbol.name}`
: symbol.name
: containerName;
for (const child of symbol.children ?? []) {
collectSymbols(child, nextContainer, out);
}
}
export async function getMethodSymbols(document: vscode.TextDocument): Promise<MethodSymbol[]> {
let raw: RawDocSymbol[] | undefined;
try {
raw = await vscode.commands.executeCommand<RawDocSymbol[]>(
'vscode.executeDocumentSymbolProvider',
document.uri
);
} catch {
raw = undefined;
}
if (raw && raw.length > 0) {
const symbols: MethodSymbol[] = [];
for (const symbol of raw) {
collectSymbols(symbol, undefined, symbols);
}
if (symbols.length > 0) {
return symbols;
}
}
return fallbackRegexSymbols(document);
}
export async function extractMethodScope(
document: vscode.TextDocument,
range: vscode.Range
): Promise<MethodScope | null> {
const symbols = await getMethodSymbols(document);
if (symbols.length === 0) { return null; }
const target = findTargetSymbol(symbols, range);
if (!target) { return null; }
const ranges = new Map<MethodSymbol, vscode.Range>();
for (const symbol of symbols) {
ranges.set(
symbol,
symbol.range.isEmpty
? expandToMethodBody(document, symbol.range.start.line)
: symbol.range
);
}
const targetRange = ranges.get(target)!;
const code = document.getText(targetRange);
if (!code.trim()) { return null; }
const callers: string[] = [];
const callees: string[] = [];
const targetPattern = new RegExp(`\\b${escapeRegExp(target.name)}\\s*\\(`);
for (const symbol of symbols) {
if (symbol === target) { continue; }
const otherCode = document.getText(ranges.get(symbol)!);
if (otherCode && targetPattern.test(otherCode)) {
callers.push(symbol.name);
}
const otherPattern = new RegExp(`\\b${escapeRegExp(symbol.name)}\\s*\\(`);
if (code && otherPattern.test(code)) {
callees.push(symbol.name);
}
}
return {
name: target.name,
range: targetRange,
code,
signature: extractSignature(code, target.name),
callers,
callees,
role: inferRole(target),
};
}
function findTargetSymbol(symbols: MethodSymbol[], range: vscode.Range): MethodSymbol | null {
const containing = symbols.filter(s => s.range.contains(range));
if (containing.length === 0) { return null; }
let best = containing[0];
for (const symbol of containing) {
if (symbol.range.start.isAfter(best.range.start)) {
best = symbol;
}
}
return best;
}
function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function extractSignature(code: string, fallbackName: string): string {
const lines = code.split('\n');
const sigLines: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) { continue; }
if (trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.startsWith('*') || trimmed.startsWith('*/')) {
continue;
}
sigLines.push(trimmed);
if (trimmed.includes('{') || trimmed.endsWith(')')) { break; }
}
let sig = sigLines.join(' ').replace(/\{.*$/, '').trim();
sig = sig.replace(/\s{2,}/g, ' ');
return sig || fallbackName;
}
function inferRole(symbol: MethodSymbol): string {
const container = symbol.containerName ?? '';
const name = symbol.name.toLowerCase();
if (container.includes('Controller')) { return 'HTTP 请求处理入口'; }
if (container.includes('Service')) { return '业务逻辑处理'; }
if (container.includes('Repository') || container.includes('Dao')) { return '数据访问'; }
if (name.startsWith('get') || name.startsWith('set') || name.startsWith('is')) { return '属性访问器'; }
if (name.startsWith('init') || name.startsWith('on')) { return '生命周期回调'; }
if (name.startsWith('handle') || name.startsWith('process')) { return '流程处理'; }
if (name.startsWith('build') || name.startsWith('create')) { return '工厂/构建'; }
if (name.startsWith('parse') || name.startsWith('convert') || name.startsWith('transform')) { return '数据转换'; }
return '通用方法';
}
function fallbackRegexSymbols(document: vscode.TextDocument): MethodSymbol[] {
const text = document.getText();
const lang = document.languageId;
const patterns: RegExp[] = [];
if (['typescript', 'javascript', 'typescriptreact', 'javascriptreact'].includes(lang)) {
patterns.push(/(?:async\s+)?function\s+(\w+)/g);
patterns.push(/(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?(?:function\s*)?\(/g);
} else if (['java', 'kotlin', 'go'].includes(lang)) {
patterns.push(/((?:public|private|protected|static)\s+)*\w+(?:<[^>]+>)?\s+(\w+)\s*\(/g);
}
const symbols: MethodSymbol[] = [];
const seen = new Set<string>();
for (const pattern of patterns) {
pattern.lastIndex = 0;
let match: RegExpExecArray | null;
while ((match = pattern.exec(text)) !== null) {
const name = match[2] ?? match[1];
if (!name) { continue; }
const start = document.positionAt(match.index);
const key = `${name}@${start.line}`;
if (seen.has(key)) { continue; }
seen.add(key);
symbols.push({ name, range: new vscode.Range(start, start) });
}
}
return symbols;
}
function expandToMethodBody(document: vscode.TextDocument, startLine: number): vscode.Range {
const start = new vscode.Position(startLine, 0);
const text = document.getText();
const startOffset = document.offsetAt(start);
let depth = 0;
let inString: string | null = null;
let i = startOffset;
while (i < text.length) {
const ch = text[i];
if (inString) {
if (ch === '\\') { i += 2; continue; }
if (ch === inString) { inString = null; }
} else if (ch === '"' || ch === "'" || ch === '`') {
inString = ch;
} else if (ch === '/') {
if (text[i + 1] === '/') {
while (i < text.length && text[i] !== '\n') { i++; }
continue;
}
if (text[i + 1] === '*') {
i += 2;
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) { i++; }
i += 2;
continue;
}
} else if (ch === '{') {
depth++;
} else if (ch === '}') {
depth--;
if (depth === 0) {
return new vscode.Range(start, document.positionAt(i + 1));
}
}
i++;
}
const lastLine = document.lineCount - 1;
return new vscode.Range(start, document.lineAt(lastLine).range.end);
}
|