|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { commands, ExtensionContext, HoverProvider, languages, CancellationToken, Hover, Position, TextDocument, MarkdownString, window, Uri, MarkedString, Command } from "vscode"; |
| 4 | +import { LanguageClient, TextDocumentPositionParams, HoverRequest } from "vscode-languageclient"; |
| 5 | +import { Commands as javaCommands } from "./commands"; |
| 6 | +import { FindLinks } from "./protocol"; |
| 7 | +import { registerHoverCommand, provideHoverCommandFn } from "./extension.api"; |
| 8 | +import { logger } from "./log"; |
| 9 | + |
| 10 | +export function registerClientHoverProvider(languageClient: LanguageClient, context: ExtensionContext): registerHoverCommand { |
| 11 | + const hoverProvider: JavaHoverProvider = new JavaHoverProvider(languageClient); |
| 12 | + hoverProvider.registerHoverCommand(async (params: TextDocumentPositionParams, token: CancellationToken) => { |
| 13 | + return await provideHoverCommand(languageClient, params, token); |
| 14 | + }); |
| 15 | + context.subscriptions.push(languages.registerHoverProvider('java', hoverProvider)); |
| 16 | + context.subscriptions.push(commands.registerCommand(javaCommands.NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND, (location: any) => { |
| 17 | + navigateToSuperImplementation(languageClient, location); |
| 18 | + })); |
| 19 | + |
| 20 | + return hoverProvider.registerHoverCommand; |
| 21 | +} |
| 22 | + |
| 23 | +async function provideHoverCommand(languageClient: LanguageClient, params: TextDocumentPositionParams, token: CancellationToken): Promise<Command[] | undefined> { |
| 24 | + const response = await languageClient.sendRequest(FindLinks.type, { |
| 25 | + type: 'superImplementation', |
| 26 | + position: params, |
| 27 | + }, token); |
| 28 | + if (response && response.length) { |
| 29 | + const location = response[0]; |
| 30 | + let tooltip; |
| 31 | + if (location.kind === 'method') { |
| 32 | + tooltip = `Go to super method '${location.displayName}'`; |
| 33 | + } else { |
| 34 | + tooltip = `Go to super implementation '${location.displayName}'`; |
| 35 | + } |
| 36 | + |
| 37 | + return [{ |
| 38 | + title: 'Go to Super Implementation', |
| 39 | + command: javaCommands.NAVIGATE_TO_SUPER_IMPLEMENTATION_COMMAND, |
| 40 | + tooltip, |
| 41 | + arguments: [{ |
| 42 | + uri: encodeBase64(location.uri), |
| 43 | + range: location.range, |
| 44 | + }], |
| 45 | + }]; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function navigateToSuperImplementation(languageClient: LanguageClient, location: any) { |
| 50 | + const range = languageClient.protocol2CodeConverter.asRange(location.range); |
| 51 | + window.showTextDocument(Uri.parse(decodeBase64(location.uri)), { |
| 52 | + preserveFocus: true, |
| 53 | + selection: range, |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +function encodeBase64(text: string): string { |
| 58 | + return Buffer.from(text).toString('base64'); |
| 59 | +} |
| 60 | + |
| 61 | +function decodeBase64(text: string): string { |
| 62 | + return Buffer.from(text, 'base64').toString('ascii'); |
| 63 | +} |
| 64 | + |
| 65 | +class JavaHoverProvider implements HoverProvider { |
| 66 | + private _hoverRegistry: provideHoverCommandFn[] = []; |
| 67 | + |
| 68 | + constructor(readonly languageClient: LanguageClient) { |
| 69 | + } |
| 70 | + |
| 71 | + async provideHover(document: TextDocument, position: Position, token: CancellationToken): Promise<Hover> { |
| 72 | + const params = { |
| 73 | + textDocument: this.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document), |
| 74 | + position: this.languageClient.code2ProtocolConverter.asPosition(position), |
| 75 | + }; |
| 76 | + |
| 77 | + // Fetch the javadoc from Java language server. |
| 78 | + const hoverResponse = await this.languageClient.sendRequest(HoverRequest.type, params, token); |
| 79 | + const serverHover = this.languageClient.protocol2CodeConverter.asHover(hoverResponse); |
| 80 | + |
| 81 | + // Fetch the contributed hover commands from third party extensions. |
| 82 | + const contributedCommands: Command[] = await this.getContributedHoverCommands(params, token); |
| 83 | + if (!contributedCommands.length) { |
| 84 | + return serverHover; |
| 85 | + } |
| 86 | + |
| 87 | + const contributed = new MarkdownString(contributedCommands.map((command) => this.convertCommandToMarkdown(command)).join(' | ')); |
| 88 | + contributed.isTrusted = true; |
| 89 | + let contents: MarkedString[] = [ contributed ]; |
| 90 | + let range; |
| 91 | + if (serverHover && serverHover.contents) { |
| 92 | + contents = contents.concat(serverHover.contents); |
| 93 | + range = serverHover.range; |
| 94 | + } |
| 95 | + return new Hover(contents, range); |
| 96 | + } |
| 97 | + |
| 98 | + registerHoverCommand(callback: provideHoverCommandFn) { |
| 99 | + this._hoverRegistry.push(callback); |
| 100 | + } |
| 101 | + |
| 102 | + private async getContributedHoverCommands(params: TextDocumentPositionParams, token: CancellationToken): Promise<Command[]> { |
| 103 | + const contributedCommands: Command[] = []; |
| 104 | + for (const provideFn of this._hoverRegistry) { |
| 105 | + try { |
| 106 | + if (token.isCancellationRequested) { |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + const commands = (await provideFn(params, token)) || []; |
| 111 | + commands.forEach((command) => { |
| 112 | + contributedCommands.push(command); |
| 113 | + }); |
| 114 | + } catch (error) { |
| 115 | + logger.error(`Failed to provide hover command ${String(error)}`); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return contributedCommands; |
| 120 | + } |
| 121 | + |
| 122 | + private convertCommandToMarkdown(command: Command): string { |
| 123 | + return `[${command.title}](command:${command.command}?${encodeURIComponent(JSON.stringify(command.arguments || []))} "${command.tooltip || command.command}")`; |
| 124 | + } |
| 125 | +} |
0 commit comments