|
| 1 | +import { type Disposable, Location, languages, type Position, type TextDocument, workspace } from 'vscode'; |
| 2 | + |
| 3 | +const NSIS_GLOB = '**/*.{nsi,nsh,bnsi,bnsh,nsdinc}'; |
| 4 | + |
| 5 | +const DEFINITION_PATTERNS = { |
| 6 | + function: /^\s*Function\s+(\.?\w+)/i, |
| 7 | + macro: /^\s*!macro\s+(\w+)/i, |
| 8 | + define: /^\s*!define\s+(?:\/\w+\s+)*(\w+)/i, |
| 9 | + variable: /^\s*Var\s+(?:\/GLOBAL\s+)?"?(\w+)"?/i, |
| 10 | +}; |
| 11 | + |
| 12 | +function getWordAtPosition(document: TextDocument, position: Position): string | undefined { |
| 13 | + const range = document.getWordRangeAtPosition(position, /[\w.]+/); |
| 14 | + return range ? document.getText(range) : undefined; |
| 15 | +} |
| 16 | + |
| 17 | +function getReferencePatterns(name: string): RegExp[] { |
| 18 | + const escaped = name.replace(/\./g, '\\.'); |
| 19 | + |
| 20 | + return [ |
| 21 | + // Function references: Call name, GetFunctionAddress ... name |
| 22 | + new RegExp(`\\b(?:Call|GetFunctionAddress\\s+\\S+)\\s+${escaped}\\b`, 'i'), |
| 23 | + // Macro references: !insertmacro name |
| 24 | + new RegExp(`^\\s*!insertmacro\\s+${escaped}\\b`, 'i'), |
| 25 | + // Define references: ${name} |
| 26 | + new RegExp(`\\$\\{${escaped}\\}`), |
| 27 | + // Variable references: $name |
| 28 | + new RegExp(`\\$${escaped}\\b`), |
| 29 | + ]; |
| 30 | +} |
| 31 | + |
| 32 | +function isDefinitionLine(line: string, name: string): boolean { |
| 33 | + for (const pattern of Object.values(DEFINITION_PATTERNS)) { |
| 34 | + const match = pattern.exec(line); |
| 35 | + |
| 36 | + if (match?.[1] === name) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +export function registerReferenceProvider(): Disposable { |
| 45 | + return languages.registerReferenceProvider('nsis', { |
| 46 | + async provideReferences(document, position, context) { |
| 47 | + const word = getWordAtPosition(document, position); |
| 48 | + |
| 49 | + if (!word) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + const locations: Location[] = []; |
| 54 | + const patterns = getReferencePatterns(word); |
| 55 | + const files = await workspace.findFiles(NSIS_GLOB); |
| 56 | + |
| 57 | + for (const file of files) { |
| 58 | + const doc = await workspace.openTextDocument(file); |
| 59 | + |
| 60 | + for (let i = 0; i < doc.lineCount; i++) { |
| 61 | + const line = doc.lineAt(i); |
| 62 | + |
| 63 | + if (!context.includeDeclaration && isDefinitionLine(line.text, word)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + for (const pattern of patterns) { |
| 68 | + const match = pattern.exec(line.text); |
| 69 | + |
| 70 | + if (match) { |
| 71 | + const nameStart = line.text.indexOf(word, match.index); |
| 72 | + |
| 73 | + if (nameStart !== -1) { |
| 74 | + locations.push(new Location(doc.uri, doc.positionAt(doc.offsetAt(line.range.start) + nameStart))); |
| 75 | + } |
| 76 | + |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return locations; |
| 84 | + }, |
| 85 | + }); |
| 86 | +} |
0 commit comments