Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix links in hover and completion documentation #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { getRedHatService, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib';
import { RedHatService } from '@redhat-developer/vscode-redhat-telemetry';
import { CodeAction as VSCodeAction, CodeActionKind, Command as VSCommand, commands, Diagnostic as VSDiagnostic, ExtensionContext, extensions, window, workspace, TextDocument, FileCreateEvent } from 'vscode';
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';
import { CancellationToken, CodeAction, CodeActionResolveRequest, Command, DidChangeConfigurationNotification, DocumentSelector, LanguageClientOptions, RequestType } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { APPLY_CODE_ACTION_WITH_TELEMETRY } from './definitions/commands';
Expand All @@ -33,7 +33,7 @@
let languageClient: LanguageClient;

// alias for vscode-java's ExtensionAPI
export type JavaExtensionAPI = any;

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type

Check warning on line 36 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type

export async function activate(context: ExtensionContext): Promise<void> {
if (await isJavaProject()) {
Expand Down Expand Up @@ -195,6 +195,27 @@
context.subscriptions.push(registerOpenURICommand());
}

const REPLACE_JDT_LINKS_PATTERN = /(\[(?:[^\]])+\]\()(jdt:\/\/(?:(?:(?:\\\))|([^)]))+))\)/g;
const VSCODE_JAVA_OPEN_FILE_COMMAND_ID = "java.open.file";

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

async function connectToLS(context: ExtensionContext, api: JavaExtensionAPI, documentSelector: DocumentSelector, microprofileContributions: MicroProfileContribution[]) {
const requirements = await resolveRequirements(api);
const clientOptions: LanguageClientOptions = {
Expand Down Expand Up @@ -227,6 +248,26 @@
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getVSCodeMicroProfileSettings() });
}
},
provideHover: async (document, position, token, next): Promise<VSHover> => {
const hover = await next(document, position, token);
const newContents: (MarkedString | MarkdownString)[] = [];
for (const content of hover.contents) {
if (content instanceof MarkdownString) {
newContents.push(fixJdtLinksInDocumentation(content));
} else {
newContents.push(content);
}
}
hover.contents = newContents;
return hover;
},
resolveCompletionItem: async (item, token, next): Promise<VSCompletionItem> => {
const completionItem = await next(item, token);
if (completionItem.documentation instanceof MarkdownString) {
completionItem.documentation = fixJdtLinksInDocumentation(completionItem.documentation);
}
return completionItem;
},
provideCodeActions: async (document, range, context, token, next): Promise<VSCodeAction[]> => {
// Collect the code actions from the language server,
// then rewrite them to execute the command "microprofile.applyCodeAction"
Expand Down Expand Up @@ -296,7 +337,7 @@
* Returns a json object with key 'microprofile' and a json object value that
* holds all microprofile settings.
*/
function getVSCodeMicroProfileSettings(): { microprofile: any } {

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type

Check warning on line 340 in src/extension.ts

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

Unexpected any. Specify a different type
const defaultMicroProfileSettings = {};
const configMicroProfile = workspace.getConfiguration().get('microprofile');
const microprofileSettings = configMicroProfile ? configMicroProfile : defaultMicroProfileSettings;
Expand Down
Loading