|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { DocumentContentProvider } from "./DocumentContentProvider"; |
| 3 | + |
| 4 | +interface StudioLink { |
| 5 | + uri: vscode.Uri; |
| 6 | + range: vscode.Range; |
| 7 | + filename: string; |
| 8 | + methodname?: string; |
| 9 | + offset: number; |
| 10 | +} |
| 11 | + |
| 12 | +export class DocumentLinkProvider implements vscode.DocumentLinkProvider { |
| 13 | + public provideDocumentLinks(document: vscode.TextDocument): vscode.ProviderResult<StudioLink[]> { |
| 14 | + // Possible link formats: |
| 15 | + // SomePackage.SomeClass.cls(SomeMethod+offset) |
| 16 | + // SomePackage.SomeClass.cls(offset) |
| 17 | + // SomeRoutine.int(offset) |
| 18 | + const regexs = [/((?:\w|\.)+)\((\w+)\+(\d+)\)/, /((?:\w|\.)+)\((\d+)\)/]; |
| 19 | + const documentLinks: StudioLink[] = []; |
| 20 | + |
| 21 | + for (let i = 0; i < document.lineCount; i++) { |
| 22 | + const text = document.lineAt(i).text; |
| 23 | + |
| 24 | + regexs.forEach((regex) => { |
| 25 | + const match = regex.exec(text); |
| 26 | + if (match != null) { |
| 27 | + const filename = match[1]; |
| 28 | + let methodname; |
| 29 | + let offset; |
| 30 | + |
| 31 | + if (match.length >= 4) { |
| 32 | + methodname = match[2]; |
| 33 | + offset = parseInt(match[3]); |
| 34 | + } else { |
| 35 | + offset = parseInt(match[2]); |
| 36 | + } |
| 37 | + |
| 38 | + documentLinks.push({ |
| 39 | + range: new vscode.Range( |
| 40 | + new vscode.Position(i, match.index), |
| 41 | + new vscode.Position(i, match.index + match[0].length) |
| 42 | + ), |
| 43 | + uri: DocumentContentProvider.getUri(filename), |
| 44 | + filename, |
| 45 | + methodname, |
| 46 | + offset, |
| 47 | + }); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + return documentLinks; |
| 52 | + } |
| 53 | + |
| 54 | + public async resolveDocumentLink(link: StudioLink, token: vscode.CancellationToken): Promise<vscode.DocumentLink> { |
| 55 | + const editor = await vscode.window.showTextDocument(link.uri); |
| 56 | + let offset = link.offset; |
| 57 | + |
| 58 | + // add the offset of the method if it is a class |
| 59 | + if (link.methodname) { |
| 60 | + const symbols = await vscode.commands.executeCommand("vscode.executeDocumentSymbolProvider", link.uri); |
| 61 | + const method = symbols[0].children.find( |
| 62 | + (info) => (info.detail === "ClassMethod" || info.detail === "Method") && info.name === link.methodname |
| 63 | + ); |
| 64 | + offset += method.location.range.start.line + 1; |
| 65 | + } |
| 66 | + |
| 67 | + // move the cursor |
| 68 | + const cursor = editor.selection.active; |
| 69 | + const newPosition = cursor.with(offset, 0); |
| 70 | + editor.selection = new vscode.Selection(newPosition, newPosition); |
| 71 | + |
| 72 | + return new vscode.DocumentLink(link.range, link.uri); |
| 73 | + } |
| 74 | +} |
0 commit comments