|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { CodeLensProvider, TextDocument, CancellationToken, CodeLens, Command, Range, Location } from 'vscode'; |
| 4 | +import { Selector } from './selector'; |
| 5 | +import * as Constants from './constants'; |
| 6 | + |
| 7 | +export class CustomVariableReferencesCodeLensProvider implements CodeLensProvider { |
| 8 | + public provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 9 | + let blocks: CodeLens[] = []; |
| 10 | + let lines: string[] = document.getText().split(/\r?\n/g); |
| 11 | + let delimitedLines: number[] = Selector.getDelimiterRows(lines); |
| 12 | + delimitedLines.push(lines.length); |
| 13 | + |
| 14 | + let requestRange: [number, number][] = []; |
| 15 | + let start: number = 0; |
| 16 | + for (let index = 0; index < delimitedLines.length; index++) { |
| 17 | + let end = delimitedLines[index] - 1; |
| 18 | + if (start <= end) { |
| 19 | + requestRange.push([start, end]); |
| 20 | + start = delimitedLines[index] + 1; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + for (let index = 0; index < requestRange.length; index++) { |
| 25 | + let [blockStart, blockEnd] = requestRange[index]; |
| 26 | + |
| 27 | + while (blockStart <= blockEnd) { |
| 28 | + if (Selector.isVariableDefinitionLine(lines[blockStart])) { |
| 29 | + const range = new Range(blockStart, 0, blockEnd, 0); |
| 30 | + const line = lines[blockStart]; |
| 31 | + let match: RegExpExecArray; |
| 32 | + if (match = Constants.VariableDefinitionRegex.exec(line)) { |
| 33 | + const variableName = match[1]; |
| 34 | + const locations = this.getReferenceRanges(lines, variableName); |
| 35 | + const cmd: Command = { |
| 36 | + arguments: [document.uri, range.start, locations.map(loc => new Location(document.uri, loc))], |
| 37 | + title: locations.length === 1 ? '1 reference' : `${locations.length} references`, |
| 38 | + command: locations.length ? 'editor.action.showReferences' : '', |
| 39 | + }; |
| 40 | + blocks.push(new CodeLens(range, cmd)); |
| 41 | + } |
| 42 | + blockStart++; |
| 43 | + } else { |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (Selector.isResponseStatusLine(lines[blockStart])) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return Promise.resolve(blocks); |
| 54 | + } |
| 55 | + |
| 56 | + private getReferenceRanges(lines: string[], variable: string): Range[] { |
| 57 | + let locations: Range[] = []; |
| 58 | + for (let index = 0; index < lines.length; index++) { |
| 59 | + let line = lines[index]; |
| 60 | + if (Constants.CommentIdentifiersRegex.test(line)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + let regex = new RegExp(`\{\{${variable}\}\}`, 'g'); |
| 65 | + let match: RegExpExecArray; |
| 66 | + while (match = regex.exec(line)) { |
| 67 | + let startPos = match.index + 2; |
| 68 | + let endPos = startPos + variable.length; |
| 69 | + locations.push(new Range(index, startPos, index, endPos)); |
| 70 | + regex.lastIndex = match.index + 1; |
| 71 | + } |
| 72 | + }; |
| 73 | + return locations; |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments