Skip to content

Commit

Permalink
Support for textDocument/InlayHint
Browse files Browse the repository at this point in the history
See redhat-developer/quarkus-ls#595

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Mar 18, 2022
1 parent 4235b37 commit 4e91fa9
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 12 deletions.
5 changes: 4 additions & 1 deletion docs/qute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

## Settings

* `qute.codeLens.enabled`: Enable/disable Qute CodeLens. Default is `true`.
* `qute.trace.server`: Trace the communication between VS Code and the Qute language server in the Output view. Default is `off`.
* `qute.codeLens.enabled`: Enable/disable Qute CodeLens. Default is `true`.
* `qute.inlayHint.enabled`: Enable/disable Inlay Hint. Default is `true`.
* `qute.inlayHint.showSectionParameterType`: Show section parameter type. Default is `true`.
* `qute.inlayHint.showMethodParameterType`: Show method parameter type. Default is `true`.
* `qute.validation.enabled`: Enable/disable all Qute validation. Default is `false`.
* `qute.validation.excluded`: Disable Qute validation for the given file name patterns.\n\nExample:\n```\n[\n \"**/*items.qute.*\"\n]```.
* `qute.validation.undefinedObject.severity`: Validation severity for undefined object in Qute template files. Default is `warning`.
Expand Down
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.

29 changes: 22 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "Apache-2.0",
"bugs": "https://github.com/redhat-developer/vscode-quarkus/issues",
"engines": {
"vscode": "^1.37.0"
"vscode": "^1.65.0"
},
"galleryBanner": {
"color": "#d8ebff",
Expand Down Expand Up @@ -218,11 +218,6 @@
"description": "Action performed when detected Quarkus properties have an incorrect language.",
"scope": "window"
},
"qute.codeLens.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable/disable Qute CodeLens. Default is `true`."
},
"qute.templates.languageMismatch": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -250,6 +245,26 @@
"markdownDescription": "Traces the communication between VS Code and the Qute language server in the Output view. Default is `off`.",
"scope": "window"
},
"qute.codeLens.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable/disable Qute CodeLens. Default is `true`."
},
"qute.inlayHint.enabled": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable/disable Qute Inlay Hint. Default is `true`."
},
"qute.inlayHint.showSectionParameterType": {
"type": "boolean",
"default": true,
"markdownDescription": "Show section parameter type. Default is `true`."
},
"qute.inlayHint.showMethodParameterType": {
"type": "boolean",
"default": true,
"markdownDescription": "Show method parameter type. Default is `true`."
},
"qute.validation.enabled": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -409,7 +424,7 @@
"@types/request": "^2.48.3",
"@types/request-promise": "^4.1.44",
"@types/semver": "^6.2.0",
"@types/vscode": "^1.37.0",
"@types/vscode": "^1.65.0",
"@types/which": "^2.0.1",
"@types/yauzl": "^2.9.1",
"chai": "^4.2.0",
Expand Down
7 changes: 6 additions & 1 deletion src/qute/languageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import * as requirements from './requirements';

import { DidChangeConfigurationNotification, LanguageClientOptions } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { ExtensionContext, commands, workspace, window, ConfigurationTarget, WorkspaceConfiguration } from 'vscode';
import { ExtensionContext, commands, workspace, window, ConfigurationTarget, WorkspaceConfiguration, InlayHintsProvider, CancellationToken, Event, InlayHint, ProviderResult, Range, TextDocument, languages } from 'vscode';
import { prepareExecutable } from './javaServerStarter';
import { registerQuteExecuteWorkspaceCommand, registerVSCodeQuteCommands, synchronizeQuteValidationButton } from '../commands/registerCommands';
import { QuteClientCommandConstants } from '../commands/commandConstants';
import { QuteSettings } from './settings';
import { JavaExtensionAPI } from '../../extension';
import { QuteInlayHintsProvider } from './inlayHintsProvider';

export function connectToQuteLS(context: ExtensionContext, api: JavaExtensionAPI) {
registerVSCodeQuteCommands(context);
Expand Down Expand Up @@ -107,6 +108,10 @@ export function connectToQuteLS(context: ExtensionContext, api: JavaExtensionAPI
if (!hasShownQuteValidationPopUp(context)) {
await showQuteValidationPopUp(context);
}
const supportRegisterInlayHintsProvider = (languages as any).registerInlayHintsProvider;
if (supportRegisterInlayHintsProvider) {
context.subscriptions.push(languages.registerInlayHintsProvider(clientOptions.documentSelector, new QuteInlayHintsProvider(quteLanguageClient)));
}
});
});
}
Expand Down
96 changes: 96 additions & 0 deletions src/qute/languageServer/inlayHintsProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { LanguageClient, RequestType, TextDocumentPositionParams } from "vscode-languageclient/node";

import * as code from 'vscode';
import * as ls from 'vscode-languageserver-protocol';

/**
* 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');
}

export class QuteInlayHintsProvider implements code.InlayHintsProvider {
private readonly _onDidChangeInlayHints = new code.EventEmitter<void>();
public readonly onDidChangeInlayHints = this._onDidChangeInlayHints.event;

constructor(private client: LanguageClient) {
this.client.onRequest(InlayHintRefreshRequest.type, async () => {
this._onDidChangeInlayHints.fire();
});
}
async provideInlayHints(document: code.TextDocument, range: code.Range, token: code.CancellationToken): Promise<code.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 null;
}
return asInlayHints(values, this.client, token);
} catch (error) {
return this.client.handleFailedRequest(InlayHintRequest.type, token, error);
}
}
async resolveInlayHint?(hint: code.InlayHint, token: code.CancellationToken): Promise<code.InlayHint> {
throw new Error("Method not implemented.");
}
}

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

function asInlayHint(value: LSInlayHint, client: LanguageClient, token?: code.CancellationToken): code.InlayHint {
const label = value.label;
const result = new code.InlayHint(client.protocol2CodeConverter.asPosition(value.position), label);
return result;
}

0 comments on commit 4e91fa9

Please sign in to comment.