forked from microsoft/vscode-languageserver-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proposed textDocument/inlayHints to protocol & client.
This addresses the FR microsoft/language-server-protocol#956 And corresponds to the spec in microsoft/language-server-protocol#1249
- Loading branch information
1 parent
2645fb5
commit 2d8e6da
Showing
5 changed files
with
308 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import { languages as Languages, Disposable, TextDocument, Range, ProviderResult, InlayHintKind, InlayHint as VInlayHint, InlayHintsProvider } from 'vscode'; | ||
|
||
import { | ||
ClientCapabilities, CancellationToken, ServerCapabilities, DocumentSelector, Proposed | ||
} from 'vscode-languageserver-protocol'; | ||
|
||
import { TextDocumentFeature, BaseLanguageClient, Middleware } from './client'; | ||
import * as p2c from './protocolConverter'; | ||
|
||
function ensure<T, K extends keyof T>(target: T, key: K): T[K] { | ||
if (target[key] === void 0) { | ||
target[key] = {} as any; | ||
} | ||
return target[key]; | ||
} | ||
|
||
export interface ProvideInlayHintsSignature { | ||
(this: void, document: TextDocument, range: Range, token: CancellationToken): ProviderResult<VInlayHint[]>; | ||
} | ||
|
||
export interface InlayHintsMiddleware { | ||
provideInlayHints?: (this: void, document: TextDocument, range: Range, token: CancellationToken, next: ProvideInlayHintsSignature) => ProviderResult<VInlayHint[]>; | ||
} | ||
|
||
namespace protocol2code { | ||
function asInlayHintKind(_: p2c.Converter, value: string | null | undefined) : InlayHintKind | undefined { | ||
switch (value) { | ||
case Proposed.InlayHintCategory.Parameter: | ||
return InlayHintKind.Parameter; | ||
case Proposed.InlayHintCategory.Type: | ||
return InlayHintKind.Type; | ||
default: | ||
return InlayHintKind.Other; | ||
} | ||
} | ||
export function asInlayHint(converter: p2c.Converter, item: Proposed.InlayHint) : VInlayHint { | ||
const result = new VInlayHint(item.label.trim(), converter.asPosition(item.position), asInlayHintKind(converter, item.category)); | ||
result.whitespaceBefore = item.label.startsWith(' '); | ||
result.whitespaceAfter = item.label.endsWith(' '); | ||
return result; | ||
} | ||
} | ||
|
||
export class InlayHintsFeature extends TextDocumentFeature<boolean | Proposed.InlayHintsOptions, Proposed.InlayHintsRegistrationOptions, InlayHintsProvider> { | ||
|
||
constructor(client: BaseLanguageClient) { | ||
super(client, Proposed.InlayHintsRequest.type); | ||
} | ||
|
||
public fillClientCapabilities(capabilities: ClientCapabilities & Proposed.$InlayHintsClientCapabilities): void { | ||
let capability = ensure(ensure(capabilities, 'textDocument')!, 'inlayHints')!; | ||
capability.dynamicRegistration = true; | ||
} | ||
|
||
public initialize(capabilities: ServerCapabilities & Proposed.$InlayHintsServerCapabilities, documentSelector: DocumentSelector): void { | ||
let [id, options] = this.getRegistration(documentSelector, capabilities.inlayHintsProvider); | ||
if (!id || !options) { | ||
return; | ||
} | ||
this.register({ id: id, registerOptions: options }); | ||
} | ||
|
||
protected registerLanguageProvider(options: Proposed.InlayHintsRegistrationOptions): [Disposable, InlayHintsProvider] { | ||
const provider: InlayHintsProvider = { | ||
provideInlayHints: (document, range, token) => { | ||
const client = this._client; | ||
const provideInlayHints: ProvideInlayHintsSignature = (document, range, token) => { | ||
const requestParams: Proposed.InlayHintsParams = { | ||
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), | ||
range: client.code2ProtocolConverter.asRange(range) | ||
}; | ||
return client.sendRequest(Proposed.InlayHintsRequest.type, requestParams, token).then( | ||
(m: Proposed.InlayHint[]) => m.map(h => protocol2code.asInlayHint(client.protocol2CodeConverter, h)), | ||
(error: any) => { | ||
return client.handleFailedRequest(Proposed.InlayHintsRequest.type, token, error, null); | ||
} | ||
); | ||
}; | ||
const middleware = client.clientOptions.middleware as (Middleware & InlayHintsMiddleware) | undefined; | ||
return middleware?.provideInlayHints | ||
? middleware.provideInlayHints(document, range, token, provideInlayHints) | ||
: provideInlayHints(document, range, token); | ||
} | ||
}; | ||
return [Languages.registerInlayHintsProvider(options.documentSelector!, provider), provider]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import { ProtocolRequestType } from './messages'; | ||
import { Position, Range, TextDocumentIdentifier } from 'vscode-languageserver-types'; | ||
import { | ||
WorkDoneProgressOptions, WorkDoneProgressParams, PartialResultParams, TextDocumentRegistrationOptions, TextDocumentClientCapabilities | ||
} from './protocol'; | ||
|
||
/** | ||
* Well-known kinds of information conveyed by InlayHints. | ||
* Clients may choose which categories to display according to user preferences. | ||
* | ||
* @since 3.17.0 | ||
*/ | ||
export enum InlayHintCategory { | ||
/** | ||
* The range is an expression passed as an argument to a function. | ||
* The label is the name of the parameter. | ||
*/ | ||
Parameter = 'parameter', | ||
/** | ||
* The range is an entity whose type is unknown. | ||
* The label is its inferred type. | ||
*/ | ||
Type = 'type' | ||
} | ||
|
||
/** | ||
* An inlay hint is a short textual annotation for a range of source code. | ||
* | ||
* @since 3.17.0 | ||
*/ | ||
export interface InlayHint { | ||
/** | ||
* The text to be shown. | ||
*/ | ||
label: string; | ||
|
||
/** | ||
* The position within the code this hint is attached to. | ||
*/ | ||
position: Position; | ||
|
||
/** | ||
* The kind of information this hint conveys. | ||
* May be an InlayHintCategory or any other value, clients should treat | ||
* unrecognized values as if missing. | ||
*/ | ||
category?: string; | ||
} | ||
|
||
|
||
/** | ||
* Client capabilities specific to the inlayHints request. | ||
* | ||
* @since 3.17.0 | ||
*/ | ||
export interface InlayHintsClientCapabilities { | ||
/** | ||
* Whether implementation supports dynamic registration. If this is set to | ||
* `true` the client supports the new `(TextDocumentRegistrationOptions & | ||
* StaticRegistrationOptions)` return value for the corresponding server | ||
* capability as well. | ||
*/ | ||
dynamicRegistration?: boolean; | ||
} | ||
|
||
export interface $InlayHintsClientCapabilities { | ||
textDocument?: TextDocumentClientCapabilities & { | ||
inlayHints?: InlayHintsClientCapabilities; | ||
} | ||
} | ||
|
||
export interface InlayHintsServerCapabilities { | ||
} | ||
|
||
export interface InlayHintsOptions extends WorkDoneProgressOptions { | ||
} | ||
|
||
export interface InlayHintsRegistrationOptions extends TextDocumentRegistrationOptions, InlayHintsOptions { | ||
} | ||
|
||
export interface $InlayHintsServerCapabilities { | ||
inlayHintsProvider?: InlayHintsOptions; | ||
} | ||
|
||
export interface InlayHintsParams extends WorkDoneProgressParams, PartialResultParams { | ||
/** | ||
* The text document. | ||
*/ | ||
textDocument: TextDocumentIdentifier; | ||
|
||
/** | ||
* The range the inlay hints are requested for. | ||
* If unset, returns all hints for the document. | ||
*/ | ||
range?: Range; | ||
|
||
/** | ||
* The categories of inlay hints that are interesting to the client. | ||
* The client should filter out hints of other categories, so the server may | ||
* skip computing them. | ||
*/ | ||
only?: string[]; | ||
} | ||
|
||
/** | ||
* The `textDocument/inlayHints` request is sent from the client to the server to retrieve inlay hints for a document. | ||
*/ | ||
export namespace InlayHintsRequest { | ||
export const method: 'textDocument/inlayHints' = 'textDocument/inlayHints'; | ||
export const type = new ProtocolRequestType<InlayHintsParams, InlayHint[], InlayHint[], void, InlayHintsRegistrationOptions>(method); | ||
} |