|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as osPath from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import { openTextDocumentAtRange } from "./util"; |
| 5 | + |
| 6 | +export type TerminalFileLink = vscode.TerminalLink & { file: { path: vscode.Uri, line?: number, column?: number } }; |
| 7 | + |
| 8 | +export class DTerminalLinkProvider implements vscode.TerminalLinkProvider { |
| 9 | + provideTerminalLinks(context: { line: string, cwd?: string }, token?: vscode.CancellationToken): Thenable<TerminalFileLink[]> { |
| 10 | + let cwd = context.cwd || (vscode.workspace.workspaceFolders |
| 11 | + ? vscode.workspace.workspaceFolders[0].uri.fsPath |
| 12 | + : process.cwd()); |
| 13 | + // context.terminal.creationOptions.cwd is useless here, possibly |
| 14 | + // pointing to entirely different paths because vscode reuses terminals |
| 15 | + // (or sessions) across different workspaces, keeping old defaults. |
| 16 | + return Promise.all(findDErrorLines(cwd, context.line)) |
| 17 | + .then(v => <TerminalFileLink[]>v.filter(l => l !== null)); |
| 18 | + } |
| 19 | + |
| 20 | + handleTerminalLink(link: TerminalFileLink): vscode.ProviderResult<void> { |
| 21 | + var range: null | number | vscode.Position = null; |
| 22 | + if (link.file.line && link.file.column) |
| 23 | + range = new vscode.Position(link.file.line - 1, link.file.column - 1); |
| 24 | + else if (link.file.line) |
| 25 | + range = link.file.line - 1; |
| 26 | + |
| 27 | + openTextDocumentAtRange(link.file.path, range); |
| 28 | + } |
| 29 | + |
| 30 | + static register(): { dispose(): any; } { |
| 31 | + const provider = new DTerminalLinkProvider(); |
| 32 | + return vscode.window.registerTerminalLinkProvider(provider); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const dubFileSearch = process.platform == "win32" ? "dub\\packages\\" : "dub/packages/"; |
| 37 | +function findDErrorLines(cwd: string, line: string): Promise<TerminalFileLink | null>[] { |
| 38 | + const ret: Promise<TerminalFileLink | null>[] = []; |
| 39 | + let i = 0; |
| 40 | + while (true) |
| 41 | + { |
| 42 | + i = line.indexOf("(", i); |
| 43 | + if (i == -1) |
| 44 | + break; |
| 45 | + |
| 46 | + let firstLineDigit = line[i + 1]; |
| 47 | + if (isDigit(firstLineDigit) && ( |
| 48 | + line.endsWith(".d", i) |
| 49 | + || line.endsWith(".di", i) |
| 50 | + || line.endsWith(".dt", i) // diet templates |
| 51 | + || endsWithMixin(line, i) |
| 52 | + )) |
| 53 | + ret.push(extractFileLinkAt(cwd, line, i)); |
| 54 | + |
| 55 | + i++; |
| 56 | + } |
| 57 | + return ret; |
| 58 | +} |
| 59 | + |
| 60 | +function endsWithMixin(line: string, endIndex: number): boolean { |
| 61 | + // format = "file.d-mixin-5(5, 8)" |
| 62 | + if (endIndex == 0 || !isDigit(line[endIndex - 1])) |
| 63 | + return false; |
| 64 | + |
| 65 | + endIndex--; |
| 66 | + while (endIndex > 0 && isDigit(line[endIndex - 1])) |
| 67 | + endIndex--; |
| 68 | + |
| 69 | + return line.endsWith("-mixin-", endIndex); |
| 70 | +} |
| 71 | + |
| 72 | +function isDigit(c: string): boolean { |
| 73 | + return c >= '0' && c <= '9'; |
| 74 | +} |
| 75 | + |
| 76 | +async function extractFileLinkAt( |
| 77 | + cwd: string, |
| 78 | + line: string, |
| 79 | + i: number, |
| 80 | +): Promise<TerminalFileLink | null> { |
| 81 | + function isValidFilePathPart(c: string) { |
| 82 | + return c != ' ' |
| 83 | + && c != '(' && c != ')' |
| 84 | + && c != '[' && c != ']' |
| 85 | + && c != ':' && c != '@' |
| 86 | + && c != '`' && c != '"' && c != '\'' |
| 87 | + && c != ',' && c != '!' && c != '?'; |
| 88 | + } |
| 89 | + |
| 90 | + let endOffset = 0; |
| 91 | + |
| 92 | + let gotDriveLetter = false; |
| 93 | + let prefixDone = false; |
| 94 | + function isValidPrefix(c: string) { |
| 95 | + if (prefixDone) |
| 96 | + return false; |
| 97 | + |
| 98 | + if (process.platform == "win32" && c == ':' && !gotDriveLetter) { |
| 99 | + gotDriveLetter = true; |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + if (gotDriveLetter) { |
| 104 | + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { |
| 105 | + prefixDone = true; |
| 106 | + return true; |
| 107 | + } else { |
| 108 | + i++; |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return isValidFilePathPart(c) || c == ':'; |
| 114 | + } |
| 115 | + |
| 116 | + let lineNo: number | undefined = undefined; |
| 117 | + let column: number | undefined = undefined; |
| 118 | + while (i > 0 && isValidPrefix(line[i - 1])) |
| 119 | + i--; |
| 120 | + let file = line.substring(i); |
| 121 | + |
| 122 | + let end = 0; |
| 123 | + while (isValidFilePathPart(file[end])) |
| 124 | + end++; |
| 125 | + |
| 126 | + if (end == 0 || file[end - 1] == '.') |
| 127 | + return null; |
| 128 | + |
| 129 | + const lineNoMatcher = /^[(:](\d+)(?:[,:](\d+))?/; |
| 130 | + const lineNoMatch = file.substring(end).match(lineNoMatcher); |
| 131 | + |
| 132 | + if (lineNoMatch) |
| 133 | + { |
| 134 | + lineNo = parseInt(lineNoMatch[1]); |
| 135 | + if (lineNoMatch[2]) |
| 136 | + column = parseInt(lineNoMatch[2]); |
| 137 | + endOffset += lineNoMatch[0].length; |
| 138 | + if (lineNoMatch[0][0] == '(') |
| 139 | + endOffset++; |
| 140 | + } |
| 141 | + |
| 142 | + if (endsWithMixin(file, end)) |
| 143 | + { |
| 144 | + let newEnd = file.lastIndexOf("-mixin-", end); |
| 145 | + if (newEnd == -1) |
| 146 | + throw new Error("this should not happen"); |
| 147 | + lineNo = parseInt(file.substring(newEnd + 7, end)); |
| 148 | + column = undefined; |
| 149 | + endOffset += (end - newEnd); |
| 150 | + end = newEnd; |
| 151 | + } |
| 152 | + |
| 153 | + let filePath = await resolveFilePath(cwd, file.substring(0, end)); |
| 154 | + if (!filePath) |
| 155 | + return null; |
| 156 | + |
| 157 | + return { |
| 158 | + startIndex: i, |
| 159 | + length: end + endOffset, |
| 160 | + file: { |
| 161 | + path: filePath, |
| 162 | + line: lineNo, |
| 163 | + column: column |
| 164 | + } |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +export const dubPackagesHome = determineDubPackageHome(); |
| 169 | + |
| 170 | +let resolveAllFilePathsForTest: boolean = false; |
| 171 | +export function enableResolveAllFilePathsForTest() { |
| 172 | + return resolveAllFilePathsForTest = true; |
| 173 | +} |
| 174 | + |
| 175 | +function resolveFilePath(cwd: string, path: string): Promise<vscode.Uri | null> { |
| 176 | + return new Promise(function(resolve) { |
| 177 | + if (!osPath.isAbsolute(path)) |
| 178 | + path = osPath.join(cwd, path); |
| 179 | + fs.stat(path, function(err, stats) { |
| 180 | + if (!err && stats.isFile()) |
| 181 | + return resolve(vscode.Uri.file(path)); |
| 182 | + |
| 183 | + var dubPathStart = path.indexOf(dubFileSearch); |
| 184 | + if (dubPathStart != -1) { |
| 185 | + path = osPath.join(dubPackagesHome, path.substring(dubPathStart + dubFileSearch.length)); |
| 186 | + fs.stat(path, function(err, stats) { |
| 187 | + if ((!err && stats.isFile()) || resolveAllFilePathsForTest) { |
| 188 | + resolve(vscode.Uri.file(path)); |
| 189 | + } else { |
| 190 | + resolve(null); |
| 191 | + } |
| 192 | + }); |
| 193 | + } else { |
| 194 | + if (resolveAllFilePathsForTest) |
| 195 | + return resolve(vscode.Uri.file(path)); |
| 196 | + else |
| 197 | + resolve(null); |
| 198 | + } |
| 199 | + }); |
| 200 | + }); |
| 201 | +} |
| 202 | + |
| 203 | +function determineDubPackageHome(): string { |
| 204 | + let dubHome = process.env["DUB_HOME"]; |
| 205 | + if (!dubHome) { |
| 206 | + let dpath = process.env["DPATH"]; |
| 207 | + if (dpath) { |
| 208 | + dubHome = osPath.join(dpath, "dub"); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + if (dubHome) { |
| 213 | + return osPath.join(dubHome, "packages"); |
| 214 | + } |
| 215 | + |
| 216 | + if (process.platform == "win32") { |
| 217 | + return osPath.join(process.env["LOCALAPPDATA"] || process.env["APPDATA"] || process.cwd(), "dub", "packages"); |
| 218 | + } else { |
| 219 | + return osPath.join(process.env["HOME"] || process.cwd(), ".dub", "packages"); |
| 220 | + } |
| 221 | +} |
0 commit comments