|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { CancellationToken, commands, Position, ProviderResult, Range, TerminalLink, TerminalLinkContext, |
| 5 | + TerminalLinkProvider, Uri, window } from "vscode"; |
| 6 | +import { resolveSourceUri } from "./languageServerPlugin"; |
| 7 | + |
| 8 | +export class JavaTerminalLinkProvder implements TerminalLinkProvider<IJavaTerminalLink> { |
| 9 | + /** |
| 10 | + * Provide terminal links for the given context. Note that this can be called multiple times |
| 11 | + * even before previous calls resolve, make sure to not share global objects (eg. `RegExp`) |
| 12 | + * that could have problems when asynchronous usage may overlap. |
| 13 | + * @param context Information about what links are being provided for. |
| 14 | + * @param token A cancellation token. |
| 15 | + * @return A list of terminal links for the given line. |
| 16 | + */ |
| 17 | + public provideTerminalLinks(context: TerminalLinkContext, token: CancellationToken): ProviderResult<IJavaTerminalLink[]> { |
| 18 | + if (context.terminal.name !== "Java Debug Console" && context.terminal.name !== "Java Process Console") { |
| 19 | + return []; |
| 20 | + } |
| 21 | + |
| 22 | + const regex = new RegExp("(\\sat\\s+)([\\w$\\.]+\\/)?(([\\w$]+\\.)+[<\\w$>]+)\\(([\\w-$]+\\.java:\\d+)\\)"); |
| 23 | + const result: RegExpExecArray = regex.exec(context.line); |
| 24 | + if (result && result.length) { |
| 25 | + const stackTrace = `${result[2] || ""}${result[3]}(${result[5]})`; |
| 26 | + const sourceLineNumber = Number(result[5].split(":")[1]); |
| 27 | + return [{ |
| 28 | + startIndex: result.index + result[1].length, |
| 29 | + length: stackTrace.length, |
| 30 | + methodName: result[3], |
| 31 | + stackTrace, |
| 32 | + lineNumber: sourceLineNumber, |
| 33 | + }]; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Handle an activated terminal link. |
| 39 | + */ |
| 40 | + public async handleTerminalLink(link: IJavaTerminalLink): Promise<void> { |
| 41 | + const uri = await resolveSourceUri(link.stackTrace); |
| 42 | + if (uri) { |
| 43 | + const lineNumber = Math.max(link.lineNumber - 1, 0); |
| 44 | + window.showTextDocument(Uri.parse(uri), { |
| 45 | + preserveFocus: true, |
| 46 | + selection: new Range(new Position(lineNumber, 0), new Position(lineNumber, 0)), |
| 47 | + }); |
| 48 | + } else { |
| 49 | + // If no source is found, then open the searching symbols quickpick box. |
| 50 | + const fullyQualifiedName = link.methodName.substring(0, link.methodName.lastIndexOf(".")); |
| 51 | + const className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1); |
| 52 | + commands.executeCommand("workbench.action.quickOpen", "#" + className); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +interface IJavaTerminalLink extends TerminalLink { |
| 58 | + methodName: string; |
| 59 | + stackTrace: string; |
| 60 | + lineNumber: number; |
| 61 | +} |
0 commit comments