|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as vscode from 'vscode'; |
| 4 | + |
| 5 | +const WORD_RE = /[A-Za-z_][A-Za-z0-9_.]*/; |
| 6 | + |
| 7 | +/** import "path"; 或 import public "path"; */ |
| 8 | +const IMPORT_RE = /\bimport\s+(?:public\s+|weak\s+)?"([^"]+)"\s*;/g; |
| 9 | + |
| 10 | +const DEF_PATTERNS: { re: RegExp }[] = [ |
| 11 | + { re: /\bmessage\s+(\w+)\s*\{/g }, |
| 12 | + { re: /\benum\s+(\w+)\s*\{/g }, |
| 13 | + { re: /\bservice\s+(\w+)\s*\{/g }, |
| 14 | + { re: /\brpc\s+(\w+)\s*\(/g }, |
| 15 | +]; |
| 16 | + |
| 17 | +function offsetToPosition(fullText: string, offset: number): vscode.Position { |
| 18 | + const upTo = fullText.slice(0, offset); |
| 19 | + const lines = upTo.split('\n'); |
| 20 | + const line = lines.length - 1; |
| 21 | + const character = lines[line].length; |
| 22 | + return new vscode.Position(line, character); |
| 23 | +} |
| 24 | + |
| 25 | +function stripLineCommentsForScan(line: string): string { |
| 26 | + return line.split('//')[0]; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * 从文本中收集符号名 → 定义位置(同一文件内 uri)。 |
| 31 | + * 行尾 // 注释不参与匹配,减少误匹配注释里的 message 等字样。 |
| 32 | + */ |
| 33 | +function collectDefinitionsInText(text: string, uri: vscode.Uri): Map<string, vscode.Location> { |
| 34 | + const map = new Map<string, vscode.Location>(); |
| 35 | + let idx = 0; |
| 36 | + while (idx < text.length) { |
| 37 | + const nl = text.indexOf('\n', idx); |
| 38 | + const lineEnd = nl === -1 ? text.length : nl; |
| 39 | + let raw = text.slice(idx, lineEnd); |
| 40 | + if (raw.endsWith('\r')) { |
| 41 | + raw = raw.slice(0, -1); |
| 42 | + } |
| 43 | + const line = stripLineCommentsForScan(raw); |
| 44 | + const lineStart = idx; |
| 45 | + |
| 46 | + for (const { re } of DEF_PATTERNS) { |
| 47 | + re.lastIndex = 0; |
| 48 | + let m: RegExpExecArray | null; |
| 49 | + while ((m = re.exec(line)) !== null) { |
| 50 | + const name = m[1]; |
| 51 | + const nameInLine = m.index + m[0].indexOf(name); |
| 52 | + const start = lineStart + nameInLine; |
| 53 | + const endOff = start + name.length; |
| 54 | + const loc = new vscode.Location( |
| 55 | + uri, |
| 56 | + new vscode.Range(offsetToPosition(text, start), offsetToPosition(text, endOff)) |
| 57 | + ); |
| 58 | + map.set(name, loc); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (nl === -1) { |
| 63 | + break; |
| 64 | + } |
| 65 | + idx = nl + 1; |
| 66 | + } |
| 67 | + return map; |
| 68 | +} |
| 69 | + |
| 70 | +function extractImportPaths(text: string): string[] { |
| 71 | + const paths: string[] = []; |
| 72 | + let m: RegExpExecArray | null; |
| 73 | + IMPORT_RE.lastIndex = 0; |
| 74 | + while ((m = IMPORT_RE.exec(text)) !== null) { |
| 75 | + paths.push(m[1]); |
| 76 | + } |
| 77 | + return paths; |
| 78 | +} |
| 79 | + |
| 80 | +function resolveImportUri(fromDoc: vscode.TextDocument, importPath: string): vscode.Uri | undefined { |
| 81 | + if (fromDoc.uri.scheme !== 'file') { |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + const dir = path.dirname(fromDoc.uri.fsPath); |
| 85 | + const full = path.normalize(path.join(dir, importPath)); |
| 86 | + if (fs.existsSync(full) && fs.statSync(full).isFile()) { |
| 87 | + return vscode.Uri.file(full); |
| 88 | + } |
| 89 | + return undefined; |
| 90 | +} |
| 91 | + |
| 92 | +function lookupSymbol( |
| 93 | + map: Map<string, vscode.Location>, |
| 94 | + word: string |
| 95 | +): vscode.Location | undefined { |
| 96 | + if (map.has(word)) { |
| 97 | + return map.get(word); |
| 98 | + } |
| 99 | + const dot = word.lastIndexOf('.'); |
| 100 | + if (dot !== -1) { |
| 101 | + const tail = word.slice(dot + 1); |
| 102 | + return map.get(tail); |
| 103 | + } |
| 104 | + return undefined; |
| 105 | +} |
| 106 | + |
| 107 | +async function buildDefinitionMap(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<Map<string, vscode.Location>> { |
| 108 | + const merged = new Map<string, vscode.Location>(); |
| 109 | + const body = document.getText(); |
| 110 | + const imports = extractImportPaths(body); |
| 111 | + |
| 112 | + for (const imp of imports) { |
| 113 | + if (token.isCancellationRequested) { |
| 114 | + return merged; |
| 115 | + } |
| 116 | + const uri = resolveImportUri(document, imp); |
| 117 | + if (!uri) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + try { |
| 121 | + const imported = await vscode.workspace.openTextDocument(uri); |
| 122 | + const part = collectDefinitionsInText(imported.getText(), imported.uri); |
| 123 | + for (const [k, v] of part) { |
| 124 | + if (!merged.has(k)) { |
| 125 | + merged.set(k, v); |
| 126 | + } |
| 127 | + } |
| 128 | + } catch { |
| 129 | + // 忽略无法打开的 import |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (token.isCancellationRequested) { |
| 134 | + return merged; |
| 135 | + } |
| 136 | + const local = collectDefinitionsInText(body, document.uri); |
| 137 | + for (const [k, v] of local) { |
| 138 | + merged.set(k, v); |
| 139 | + } |
| 140 | + return merged; |
| 141 | +} |
| 142 | + |
| 143 | +export function createProto3DefinitionProvider(): vscode.DefinitionProvider { |
| 144 | + return { |
| 145 | + async provideDefinition( |
| 146 | + document: vscode.TextDocument, |
| 147 | + position: vscode.Position, |
| 148 | + token: vscode.CancellationToken |
| 149 | + ): Promise<vscode.Location | vscode.Location[] | undefined> { |
| 150 | + const range = document.getWordRangeAtPosition(position, WORD_RE); |
| 151 | + if (!range) { |
| 152 | + return undefined; |
| 153 | + } |
| 154 | + const word = document.getText(range); |
| 155 | + if (!word) { |
| 156 | + return undefined; |
| 157 | + } |
| 158 | + |
| 159 | + const map = await buildDefinitionMap(document, token); |
| 160 | + const loc = lookupSymbol(map, word); |
| 161 | + return loc; |
| 162 | + }, |
| 163 | + }; |
| 164 | +} |
0 commit comments