Skip to content

Support inlay hints for parameter names #2354

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

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"virtualWorkspaces": false
},
"engines": {
"vscode": "^1.61.0"
"vscode": "^1.65.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -857,6 +857,22 @@
"default": "line",
"description": "Show quickfixes at the problem or line level.",
"scope": "window"
},
"java.inlayHints.parameterNames.enabled": {
"type": "string",
"enum": [
"none",
"literals",
"all"
],
"enumDescriptions": [
"Disable parameter name hints",
"Enable parameter name hints only for literal arguments",
"Enable parameter name hints for literal and non-literal arguments"
],
"default": "literals",
"markdownDescription": "Enable/disable inlay hints for parameter names:\n```java\n\nInteger.valueOf(/* s: */ '123', /* radix: */ 10)\n \n```\n",
"scope": "window"
}
}
},
Expand Down Expand Up @@ -1141,7 +1157,7 @@
"@types/mocha": "^5.2.5",
"@types/node": "^8.10.51",
"@types/semver": "^7.3.8",
"@types/vscode": "^1.53.0",
"@types/vscode": "^1.65.0",
"@types/winreg": "^1.2.30",
"@types/winston": "^2.4.4",
"gulp": "^4.0.2",
Expand Down
8 changes: 7 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import { workspace, extensions, ExtensionContext, window, commands, ViewColumn, Uri, languages, IndentAction, InputBoxOptions, EventEmitter, OutputChannel, TextDocument, RelativePattern, ConfigurationTarget, WorkspaceConfiguration, env, UIKind, CodeActionContext, Diagnostic } from 'vscode';
import { workspace, extensions, ExtensionContext, window, commands, ViewColumn, Uri, languages, IndentAction, InputBoxOptions, EventEmitter, OutputChannel, TextDocument, RelativePattern, ConfigurationTarget, WorkspaceConfiguration, env, UIKind, CodeActionContext, Diagnostic, CodeActionTriggerKind } from 'vscode';
import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn, ErrorHandler, Message, ErrorAction, CloseAction, DidChangeConfigurationNotification, CancellationToken, CodeActionRequest, CodeActionParams, Command } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { collectJavaExtensions, isContributedPartUpdated } from './plugin';
Expand Down Expand Up @@ -134,6 +134,11 @@ export class OutputInfoCollector implements OutputChannel {
this.channel.appendLine(value);
}

replace(value: string): void {
this.clear();
this.append(value);
}

clear(): void {
this.channel.clear();
}
Expand Down Expand Up @@ -279,6 +284,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
const codeActionContext: CodeActionContext = {
diagnostics: allDiagnostics,
only: context.only,
triggerKind: CodeActionTriggerKind.Invoke,
};
params.context = client.code2ProtocolConverter.asCodeActionContext(codeActionContext);
}
Expand Down
4 changes: 2 additions & 2 deletions src/hoverAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class JavaHoverProvider implements HoverProvider {

const contributed = new MarkdownString(contributedCommands.map((command) => this.convertCommandToMarkdown(command)).join(' | '));
contributed.isTrusted = true;
let contents: MarkedString[] = [ contributed ];
let contents: MarkdownString[] = [ contributed ];
let range;
if (serverHover && serverHover.contents) {
contents = contents.concat(serverHover.contents);
contents = contents.concat(serverHover.contents as MarkdownString[]);
range = serverHover.range;
}
return new Hover(contents, range);
Expand Down
95 changes: 95 additions & 0 deletions src/inlayHintsProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { CancellationToken, EventEmitter, InlayHint, InlayHintKind, InlayHintsProvider, Range, TextDocument } from "vscode";
import * as ls from 'vscode-languageserver-protocol';
import { LanguageClient, RequestType } from "vscode-languageclient/node";

export class JavaInlayHintsProvider implements InlayHintsProvider {

private onDidChange = new EventEmitter<void>();
public onDidChangeInlayHints = this.onDidChange.event;

constructor(private client: LanguageClient) {
this.client.onRequest(InlayHintRefreshRequest.type, async () => {
this.onDidChange.fire();
});
}

public async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
const requestParams: InlayHintParams = {
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document),
range: this.client.code2ProtocolConverter.asRange(range)
};
try {
const values = await this.client.sendRequest(InlayHintRequest.type, requestParams, token);
if (token.isCancellationRequested) {
return [];
}
return asInlayHints(values, this.client);
} catch (error) {
return this.client.handleFailedRequest(InlayHintRequest.type, token, error, []);
}
}
}

/**
* A parameter literal used in inlay hints requests.
*
* @since 3.17.0 - proposed state
*/
export type InlayHintParams = /*WorkDoneProgressParams &*/ {
/**
* The text document.
*/
textDocument: ls.TextDocumentIdentifier;

/**
* The document range for which inlay hints should be computed.
*/
range: ls.Range;
};

/**
* Inlay hint information.
*
* @since 3.17.0 - proposed state
*/
export type LSInlayHint = {

/**
* The position of this hint.
*/
position: ls.Position;

/**
* The label of this hint. A human readable string or an array of
* InlayHintLabelPart label parts.
*
* *Note* that neither the string nor the label part can be empty.
*/
label: string; // label: string | InlayHintLabelPart[];
};

namespace InlayHintRequest {
export const type: RequestType<InlayHintParams, LSInlayHint[], any> = new RequestType('textDocument/inlayHint');
}

/**
* @since 3.17.0 - proposed state
*/
namespace InlayHintRefreshRequest {
export const type: RequestType<void, void, void> = new RequestType('workspace/inlayHint/refresh');
}

async function asInlayHints(values: LSInlayHint[] | undefined | null, client: LanguageClient, ): Promise<InlayHint[] | undefined> {
if (!Array.isArray(values)) {
return undefined;
}
return values.map(lsHint => asInlayHint(lsHint, client));
}

function asInlayHint(value: LSInlayHint, client: LanguageClient): InlayHint {
const label = value.label;
const result = new InlayHint(client.protocol2CodeConverter.asPosition(value.position), label);
result.paddingRight = true;
result.kind = InlayHintKind.Parameter;
return result;
}
9 changes: 9 additions & 0 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { buildFilePatterns } from './plugin';
import { pomCodeActionMetadata, PomCodeActionProvider } from "./pom/pomCodeActionProvider";
import { findRuntimes, IJavaRuntime } from "jdk-utils";
import { snippetCompletionProvider } from "./snippetCompletionProvider";
import { JavaInlayHintsProvider } from "./inlayHintsProvider";

const extensionName = 'Language Support for Java';
const GRADLE_CHECKSUM = "gradle/checksum/prompt";
Expand Down Expand Up @@ -501,6 +502,14 @@ export class StandardLanguageClient {
scheme: "file",
pattern: "**/pom.xml"
}, new PomCodeActionProvider(context), pomCodeActionMetadata);

if (languages.registerInlayHintsProvider) {
context.subscriptions.push(languages.registerInlayHintsProvider([
{ scheme: "file", language: "java", pattern: "**/*.java" },
{ scheme: "jdt", language: "java", pattern: "**/*.class" },
{ scheme: "untitled", language: "java", pattern: "**/*.java" }
], new JavaInlayHintsProvider(this.languageClient)));
}
});
}

Expand Down