Skip to content

Commit

Permalink
Fix class links in completion documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hopehadfield authored and rgrunber committed Jul 15, 2024
1 parent 38c8b58 commit 4d8492f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
30 changes: 28 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import { CodeActionContext, commands, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, QuickPickItemKind, RelativePattern, TextDocument, TextEditorRevealType, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from 'vscode';
import { CodeActionContext, commands, CompletionItem, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, MarkdownString, QuickPickItemKind, RelativePattern, TextDocument, TextEditorRevealType, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from 'vscode';
import { CancellationToken, CodeActionParams, CodeActionRequest, Command, CompletionRequest, DidChangeConfigurationNotification, ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { apiManager } from './apiManager';
Expand All @@ -20,7 +20,7 @@ import { cleanupLombokCache } from "./lombokSupport";
import { markdownPreviewProvider } from "./markdownPreviewProvider";
import { OutputInfoCollector } from './outputInfoCollector';
import { collectJavaExtensions, getBundlesToReload, getShortcuts, IJavaShortcut, isContributedPartUpdated } from './plugin';
import { registerClientProviders } from './providerDispatcher';
import { fixJdtSchemeHoverLinks, registerClientProviders } from './providerDispatcher';
import { initialize as initializeRecommendation } from './recommendation';
import * as requirements from './requirements';
import { languageStatusBarProvider } from './runtimeStatusBarProvider';
Expand Down Expand Up @@ -93,6 +93,25 @@ function getHeapDumpFolderFromSettings(): string {
return results[1] || results[2] || results[3];
}

const REPLACE_JDT_LINKS_PATTERN: RegExp = /(\[(?:[^\]])+\]\()(jdt:\/\/(?:(?:(?:\\\))|([^)]))+))\)/g;

/**
* Replace `jdt://` links in the documentation with links that execute the VS Code command required to open the referenced file.
*
* Extracted from {@link fixJdtSchemeHoverLinks} for use in completion item documentation.
*
* @param oldDocumentation the documentation to fix the links in
* @returns the documentation with fixed links
*/
export function fixJdtLinksInDocumentation(oldDocumentation: MarkdownString): MarkdownString {
const newContent: string = oldDocumentation.value.replace(REPLACE_JDT_LINKS_PATTERN, (_substring, group1, group2) => {
const uri = `command:${Commands.OPEN_FILE}?${encodeURI(JSON.stringify([encodeURIComponent(group2)]))}`;
return `${group1}${uri})`;
});
const mdString = new MarkdownString(newContent);
mdString.isTrusted = true;
return mdString;
}

export async function activate(context: ExtensionContext): Promise<ExtensionAPI> {
await loadSupportedJreNames(context);
Expand Down Expand Up @@ -225,6 +244,13 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
});
}
},
resolveCompletionItem: async (item, token, next): Promise<CompletionItem> => {
const completionItem = await next(item, token);
if (completionItem.documentation instanceof MarkdownString) {
completionItem.documentation = fixJdtLinksInDocumentation(completionItem.documentation);
}
return completionItem;
},
// https://github.com/redhat-developer/vscode-java/issues/2130
// include all diagnostics for the current line in the CodeActionContext params for the performance reason
provideCodeActions: async (document, range, context, token, next) => {
Expand Down
14 changes: 3 additions & 11 deletions src/providerDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DocumentSymbol as clientDocumentSymbol, DocumentSymbolRequest, HoverReq
import { LanguageClient } from "vscode-languageclient/node";
import { apiManager } from "./apiManager";
import { Commands } from "./commands";
import { getActiveLanguageClient } from "./extension";
import { fixJdtLinksInDocumentation, getActiveLanguageClient } from "./extension";
import { createClientHoverProvider } from "./hoverAction";
import { ClassFileContentsRequest } from "./protocol";
import { ServerMode } from "./settings";
Expand Down Expand Up @@ -197,8 +197,6 @@ function overwriteWorkspaceSymbolProvider(context: ExtensionContext): void {
});
}

const REPLACE_JDT_LINKS_PATTERN: RegExp = /(\[(?:[^\]])+\]\()(jdt:\/\/(?:(?:(?:\\\))|([^)]))+))\)/g;

/**
* Returns the hover with all jdt:// links replaced with a command:// link that opens the jdt URI.
*
Expand All @@ -209,17 +207,11 @@ const REPLACE_JDT_LINKS_PATTERN: RegExp = /(\[(?:[^\]])+\]\()(jdt:\/\/(?:(?:(?:\
* @param hover The hover to fix the jdt:// links for
* @returns the hover with all jdt:// links replaced with a command:// link that opens the jdt URI
*/
function fixJdtSchemeHoverLinks(hover: Hover): Hover {
export function fixJdtSchemeHoverLinks(hover: Hover): Hover {
const newContents: (MarkedString | MarkdownString)[] = [];
for (const content of hover.contents) {
if (content instanceof MarkdownString) {
const newContent: string = (<MarkdownString>content).value.replace(REPLACE_JDT_LINKS_PATTERN, (_substring, group1, group2) => {
const uri = `command:${Commands.OPEN_FILE}?${encodeURI(JSON.stringify([encodeURIComponent(group2)]))}`;
return `${group1}${uri})`;
});
const mdString = new MarkdownString(newContent);
mdString.isTrusted = true;
newContents.push(mdString);
newContents.push(fixJdtLinksInDocumentation(content));
} else {
newContents.push(content);
}
Expand Down

0 comments on commit 4d8492f

Please sign in to comment.