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