Skip to content

Commit d6801a2

Browse files
committed
Fix links in hover and completion documentation
Fix links of the form `jdt://` in documentation to instead invoke the vscode-java command to open the corresponding file. Adapted from vscode-java Fixes #245 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 03afae8 commit d6801a2

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

src/extension.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import { getRedHatService, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib';
1717
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';
1919
import { CancellationToken, CodeAction, CodeActionResolveRequest, Command, DidChangeConfigurationNotification, DocumentSelector, LanguageClientOptions, RequestType } from 'vscode-languageclient';
2020
import { LanguageClient } from 'vscode-languageclient/node';
2121
import { APPLY_CODE_ACTION_WITH_TELEMETRY } from './definitions/commands';
@@ -195,6 +195,27 @@ function registerVSCodeCommands(context: ExtensionContext) {
195195
context.subscriptions.push(registerOpenURICommand());
196196
}
197197

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+
198219
async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, documentSelector: DocumentSelector, microprofileContributions: MicroProfileContribution[]) {
199220
const requirements = await resolveRequirements(api);
200221
const clientOptions: LanguageClientOptions = {
@@ -227,6 +248,29 @@ async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, doc
227248
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getVSCodeMicroProfileSettings() });
228249
}
229250
},
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+
},
230274
provideCodeActions: async (document, range, context, token, next): Promise<VSCodeAction[]> => {
231275
// Collect the code actions from the language server,
232276
// then rewrite them to execute the command "microprofile.applyCodeAction"

0 commit comments

Comments
 (0)