|
15 | 15 | */ |
16 | 16 | import { getRedHatService, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib'; |
17 | 17 | import { RedHatService } from '@redhat-developer/vscode-redhat-telemetry'; |
18 | | -import { CodeAction as VSCodeAction, CodeActionKind, Command as VSCommand, commands, Diagnostic as VSDiagnostic, ExtensionContext, extensions, window, workspace, TextDocument, FileCreateEvent } from 'vscode'; |
| 18 | +import { CodeAction as VSCodeAction, Hover as VSHover, CodeActionKind, Command as VSCommand, CompletionItem as VSCompletionItem, commands, Diagnostic as VSDiagnostic, ExtensionContext, extensions, window, workspace, TextDocument, FileCreateEvent, MarkedString, MarkdownString } from 'vscode'; |
19 | 19 | import { CancellationToken, CodeAction, CodeActionResolveRequest, Command, DidChangeConfigurationNotification, DocumentSelector, LanguageClientOptions, RequestType } from 'vscode-languageclient'; |
20 | 20 | import { LanguageClient } from 'vscode-languageclient/node'; |
21 | 21 | import { APPLY_CODE_ACTION_WITH_TELEMETRY } from './definitions/commands'; |
@@ -195,6 +195,27 @@ function registerVSCodeCommands(context: ExtensionContext) { |
195 | 195 | context.subscriptions.push(registerOpenURICommand()); |
196 | 196 | } |
197 | 197 |
|
| 198 | +const REPLACE_JDT_LINKS_PATTERN = /(\[(?:[^\]])+\]\()(jdt:\/\/(?:(?:(?:\\\))|([^)]))+))\)/g; |
| 199 | +const VSCODE_JAVA_OPEN_FILE_COMMAND_ID = "java.open.file"; |
| 200 | + |
| 201 | +/** |
| 202 | + * Replace `jdt://` links in the documentation with links that execute the VS Code command required to open the referenced file. |
| 203 | + * |
| 204 | + * Adapted from vscode-java. |
| 205 | + * |
| 206 | + * @param oldDocumentation the documentation to fix the links in |
| 207 | + * @returns the documentation with fixed links |
| 208 | + */ |
| 209 | +function fixJdtLinksInDocumentation(oldDocumentation: MarkdownString): MarkdownString { |
| 210 | + const newContent: string = oldDocumentation.value.replace(REPLACE_JDT_LINKS_PATTERN, (_substring, group1, group2) => { |
| 211 | + const uri = `command:${VSCODE_JAVA_OPEN_FILE_COMMAND_ID}?${encodeURI(JSON.stringify([encodeURIComponent(group2)]))}`; |
| 212 | + return `${group1}${uri})`; |
| 213 | + }); |
| 214 | + const mdString = new MarkdownString(newContent); |
| 215 | + mdString.isTrusted = true; |
| 216 | + return mdString; |
| 217 | +} |
| 218 | + |
198 | 219 | async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, documentSelector: DocumentSelector, microprofileContributions: MicroProfileContribution[]) { |
199 | 220 | const requirements = await resolveRequirements(api); |
200 | 221 | const clientOptions: LanguageClientOptions = { |
@@ -227,6 +248,29 @@ async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, doc |
227 | 248 | languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getVSCodeMicroProfileSettings() }); |
228 | 249 | } |
229 | 250 | }, |
| 251 | + provideHover: async (document, position, token, next): Promise<VSHover> => { |
| 252 | + const hover = await next(document, position, token); |
| 253 | + if (hover === null || hover === undefined) { |
| 254 | + return hover; |
| 255 | + } |
| 256 | + const newContents: (MarkedString | MarkdownString)[] = []; |
| 257 | + for (const content of hover.contents) { |
| 258 | + if (content instanceof MarkdownString) { |
| 259 | + newContents.push(fixJdtLinksInDocumentation(content)); |
| 260 | + } else { |
| 261 | + newContents.push(content); |
| 262 | + } |
| 263 | + } |
| 264 | + hover.contents = newContents; |
| 265 | + return hover; |
| 266 | + }, |
| 267 | + resolveCompletionItem: async (item, token, next): Promise<VSCompletionItem> => { |
| 268 | + const completionItem = await next(item, token); |
| 269 | + if (completionItem !== undefined && completionItem !== null && completionItem.documentation instanceof MarkdownString) { |
| 270 | + completionItem.documentation = fixJdtLinksInDocumentation(completionItem.documentation); |
| 271 | + } |
| 272 | + return completionItem; |
| 273 | + }, |
230 | 274 | provideCodeActions: async (document, range, context, token, next): Promise<VSCodeAction[]> => { |
231 | 275 | // Collect the code actions from the language server, |
232 | 276 | // then rewrite them to execute the command "microprofile.applyCodeAction" |
|
0 commit comments