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 13, 2022
1 parent 7d70d6b commit 6231a1d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
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.

4 changes: 2 additions & 2 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 @@ -393,7 +393,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
6 changes: 5 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,9 @@ export function connectToQuteLS(context: ExtensionContext, api: JavaExtensionAPI
if (!hasShownQuteValidationPopUp(context)) {
await showQuteValidationPopUp(context);
}
if (languages.registerInlayHintsProvider) {
context.subscriptions.push(languages.registerInlayHintsProvider(clientOptions.documentSelector, new QuteInlayHintsProvider(quteLanguageClient)));
}
});
});
}
Expand Down
98 changes: 98 additions & 0 deletions src/qute/languageServer/inlayHintsProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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');
}

export class QuteInlayHintsProvider implements code.InlayHintsProvider {
onDidChangeInlayHints?: code.Event<void>;
converter: any;

constructor(private client: LanguageClient) {

}
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));
//return async.mapAsync(values, asInlayHint, token);
}

function asInlayHint(value: LSInlayHint, client: LanguageClient, token?: code.CancellationToken): code.InlayHint {
//const label = typeof value.label === 'string'
// ? value.label
// : await async.map(value.label, asInlayHintLabelPart, token);
const label = value.label;
const result = new code.InlayHint(client.protocol2CodeConverter.asPosition(value.position), label);
/*if (value.kind !== undefined) { result.kind = value.kind; }
if (value.textEdits !== undefined) { result.textEdits = await asTextEdits(value.textEdits, token); }
if (value.tooltip !== undefined) { result.tooltip = asTooltip(value.tooltip); }
if (value.paddingLeft !== undefined) { result.paddingLeft = value.paddingLeft; }
if (value.paddingRight !== undefined) { result.paddingRight = value.paddingRight; }
if (value.data !== undefined) { result.data = value.data; }*/
return result;
}

0 comments on commit 6231a1d

Please sign in to comment.