| 1 | import * as path from "path"; |
| 2 | |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | export class ConfigYamlDocumentLinkProvider |
| 6 | implements vscode.DocumentLinkProvider |
| 7 | { |
| 8 | private usesPattern = /^\s*#?\s*-\s*uses:\s*(.+)$/; |
| 9 | provideDocumentLinks( |
| 10 | document: vscode.TextDocument, |
| 11 | token: vscode.CancellationToken, |
| 12 | ): vscode.ProviderResult<vscode.DocumentLink[]> { |
| 13 | const links: vscode.DocumentLink[] = []; |
| 14 | |
| 15 | for (let lineIndex = 0; lineIndex < document.lineCount; lineIndex++) { |
| 16 | if (token.isCancellationRequested) { |
| 17 | return []; |
| 18 | } |
| 19 | const line = document.lineAt(lineIndex); |
| 20 | const match = this.usesPattern.exec(line.text); |
| 21 | |
| 22 | if (match) { |
| 23 | let slug = match[1].trim(); |
| 24 | // Remove any leading comment symbols (#) |
| 25 | slug = slug.replace(/^\s*(#\s*)+/, ""); |
| 26 | |
| 27 | // Check for surrounding quotes |
| 28 | const quoteMatch = slug.match(/^(['"])(.*)\1/); |
| 29 | if (quoteMatch) { |
| 30 | // If quoted, remove the quotes but keep everything inside (including #) |
| 31 | slug = quoteMatch[2].trim(); |
| 32 | } else { |
| 33 | // If not quoted, remove any trailing comment |
| 34 | slug = slug.replace(/\s*#.*$/, "").trim(); |
| 35 | } |
| 36 | |
| 37 | if (slug === "") { |
| 38 | continue; // Skip empty slugs |
| 39 | } |
| 40 | |
| 41 | if (/^(https?:\/\/|file:\/\/)/.test(slug)) { |
| 42 | // VS Code already handles external links, so skip them |
| 43 | continue; |
| 44 | } |
| 45 | const startPos = line.text.indexOf(slug); |
| 46 | const range = new vscode.Range( |
| 47 | lineIndex, |
| 48 | startPos, |
| 49 | lineIndex, |
| 50 | startPos + slug.length, |
| 51 | ); |
| 52 | |
| 53 | let linkUri: vscode.Uri; |
| 54 | if (slug.startsWith("./") || slug.startsWith("../")) { |
| 55 | const currentFilePath = document.uri.fsPath; |
| 56 | const parentPath = path.dirname(currentFilePath); |
| 57 | const resolvedPath = path.resolve(parentPath, slug); |
| 58 | linkUri = vscode.Uri.file(resolvedPath); |
| 59 | } else { |
| 60 | linkUri = vscode.Uri.parse(`https://hub.continue.dev/${slug}`); |
| 61 | } |
| 62 | |
| 63 | const link = new vscode.DocumentLink(range, linkUri); |
| 64 | links.push(link); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return links; |
| 69 | } |
| 70 | resolveDocumentLink( |
| 71 | link: vscode.DocumentLink, |
| 72 | token: vscode.CancellationToken, |
| 73 | ): vscode.ProviderResult<vscode.DocumentLink> { |
| 74 | return link; |
| 75 | } |
| 76 | } |