From da53b26c33128aaebfea1274f4f8da7032227c76 Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Sun, 19 Sep 2021 00:14:21 +0200 Subject: [PATCH 01/16] feat: implement inlayHints --- package.json | 2 +- src/lsp-connection.ts | 3 +++ src/lsp-extensions.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/lsp-server.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/tsp-client.ts | 1 + src/tsp-command-types.ts | 3 ++- yarn.lock | 8 ++++---- 7 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/lsp-extensions.ts diff --git a/package.json b/package.json index 8de18715..1b1f6a22 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,6 @@ "rimraf": "^3.0.2", "source-map-support": "^0.5.19", "ts-node": "7.0.1", - "typescript": "^4.3.4" + "typescript": "^4.4.3" } } diff --git a/src/lsp-connection.ts b/src/lsp-connection.ts index d49ee3a6..c2a0b77e 100644 --- a/src/lsp-connection.ts +++ b/src/lsp-connection.ts @@ -11,6 +11,7 @@ import * as lspcalls from './lsp-protocol.calls.proposed'; import { LspClientLogger } from './logger'; import { LspServer } from './lsp-server'; import { LspClientImpl } from './lsp-client'; +import { extensions } from './lsp-extensions'; export interface IServerOptions { tsserverPath: string; @@ -59,5 +60,7 @@ export function createLspConnection(options: IServerOptions): lsp.Connection { // proposed `textDocument/calls` request connection.onRequest(lspcalls.CallsRequest.type, server.calls.bind(server)); + connection.onRequest(extensions.inlayHints.type, server.inlayHints.bind(server)); + return connection; } diff --git a/src/lsp-extensions.ts b/src/lsp-extensions.ts new file mode 100644 index 00000000..2f5bbc81 --- /dev/null +++ b/src/lsp-extensions.ts @@ -0,0 +1,39 @@ +import * as lsp from 'vscode-languageserver/node'; +import { RequestHandler } from 'vscode-jsonrpc'; + +export namespace extensions.inlayHints { + export type InlayHintsParams = { + /** + * The document to format. + */ + textDocument: lsp.TextDocumentIdentifier; + /** + * The range to format + */ + range: lsp.Range; + }; + + type InlayHint = { + text: string; + position: lsp.Position; + kind?: 'Type' | 'Parameter' | 'Enum'; + whitespaceBefore?: boolean; + whitespaceAfter?: boolean; + }; + + export type InlayHintsResult = { + inlayHints: InlayHint[]; + }; + + export const type = new lsp.RequestType< + InlayHintsParams, + InlayHintsResult, + lsp.TextDocumentRegistrationOptions + >('typescript/inlayHints'); + + export type HandlerSignature = RequestHandler< + InlayHintsParams, + InlayHintsResult | null, + void + >; +} diff --git a/src/lsp-server.ts b/src/lsp-server.ts index 33117233..f147bdd3 100644 --- a/src/lsp-server.ts +++ b/src/lsp-server.ts @@ -37,6 +37,7 @@ import { provideOrganizeImports } from './organize-imports'; import { TypeScriptInitializeParams, TypeScriptInitializationOptions, TypeScriptInitializeResult } from './ts-protocol'; import { collectDocumentSymbols, collectSymbolInformation } from './document-symbol'; import { computeCallers, computeCallees } from './calls'; +import { extensions } from './lsp-extensions'; export interface IServerOptions { logger: Logger; @@ -983,4 +984,41 @@ export class LspServer { } return callsResult; } + + async inlayHints( + params: extensions.inlayHints.InlayHintsParams + ): Promise { + const file = uriToPath(params.textDocument.uri); + this.logger.log('inlayHints', params, file); + if (!file) { + return { inlayHints: [] }; + } + + const doc = this.documents.get(file); + if (!doc) { + return { inlayHints: [] }; + } + + const start = doc.offsetAt(params.range.start); + const end = doc.offsetAt(params.range.end); + const result = await this.tspClient.request( + CommandTypes.ProvideInlayHints, + { + file, + start: start, + length: end - start + } + ); + + return { + inlayHints: + result.body?.map((item) => ({ + text: item.text, + position: toPosition(item.position), + whitespaceAfter: item.whitespaceAfter, + whitespaceBefore: item.whitespaceBefore, + kind: item.kind + })) ?? [] + }; + } } diff --git a/src/tsp-client.ts b/src/tsp-client.ts index 9a316295..9a453ef8 100644 --- a/src/tsp-client.ts +++ b/src/tsp-client.ts @@ -61,6 +61,7 @@ interface TypeScriptRequestTypes { 'rename': [protocol.RenameRequestArgs, protocol.RenameResponse]; 'signatureHelp': [protocol.SignatureHelpRequestArgs, protocol.SignatureHelpResponse]; 'typeDefinition': [protocol.FileLocationRequestArgs, protocol.TypeDefinitionResponse]; + 'provideInlayHints': [protocol.InlayHintsRequestArgs, protocol.InlayHintsResponse]; } export class TspClient { diff --git a/src/tsp-command-types.ts b/src/tsp-command-types.ts index 1187518a..f2f8d607 100644 --- a/src/tsp-command-types.ts +++ b/src/tsp-command-types.ts @@ -85,7 +85,8 @@ export const enum CommandTypes { UncommentSelection = 'uncommentSelection', PrepareCallHierarchy = 'prepareCallHierarchy', ProvideCallHierarchyIncomingCalls = 'provideCallHierarchyIncomingCalls', - ProvideCallHierarchyOutgoingCalls = 'provideCallHierarchyOutgoingCalls' + ProvideCallHierarchyOutgoingCalls = 'provideCallHierarchyOutgoingCalls', + ProvideInlayHints = 'provideInlayHints', } export const enum EventTypes { diff --git a/yarn.lock b/yarn.lock index 0567a1fb..33e48e0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1624,10 +1624,10 @@ type-fest@^0.6.0: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b" integrity sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg== -typescript@^4.3.4: - version "4.3.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc" - integrity sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew== +typescript@^4.4.3: + version "4.4.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324" + integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA== unique-string@^2.0.0: version "2.0.0" From 413527d172c03d496ca243c72907c9d0064d25cb Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Sun, 19 Sep 2021 00:43:35 +0200 Subject: [PATCH 02/16] feat: make range optional --- src/lsp-extensions.ts | 2 +- src/lsp-server.ts | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lsp-extensions.ts b/src/lsp-extensions.ts index 2f5bbc81..a871e90e 100644 --- a/src/lsp-extensions.ts +++ b/src/lsp-extensions.ts @@ -10,7 +10,7 @@ export namespace extensions.inlayHints { /** * The range to format */ - range: lsp.Range; + range?: lsp.Range; }; type InlayHint = { diff --git a/src/lsp-server.ts b/src/lsp-server.ts index f147bdd3..110f2981 100644 --- a/src/lsp-server.ts +++ b/src/lsp-server.ts @@ -999,8 +999,14 @@ export class LspServer { return { inlayHints: [] }; } - const start = doc.offsetAt(params.range.start); - const end = doc.offsetAt(params.range.end); + const start = doc.offsetAt(params.range?.start ?? { + line: 0, + character: 0 + }); + const end = doc.offsetAt(params.range?.end ?? { + line: doc.lineCount + 1, + character: 0 + }); const result = await this.tspClient.request( CommandTypes.ProvideInlayHints, { From f5cbc8c4f1f3f593148be22bbbe047e867cb6e29 Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Thu, 23 Sep 2021 22:50:23 +0200 Subject: [PATCH 03/16] add test --- src/tsp-client.spec.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/tsp-client.spec.ts b/src/tsp-client.spec.ts index 6e0b8e54..4cdec883 100644 --- a/src/tsp-client.spec.ts +++ b/src/tsp-client.spec.ts @@ -65,6 +65,30 @@ for (const [serverName, server] of Object.entries({ executableServer, moduleServ assert.equal(references.body!.symbolName, 'doStuff'); }).timeout(10000); + it('inlayHints', async () => { + const f = filePath('module2.ts'); + server.notify(CommandTypes.Open, { + file: f, + fileContent: readContents(f) + }); + await server.request(CommandTypes.Configure, { + preferences: { + // @ts-expect-error preference exist + includeInlayFunctionLikeReturnTypeHints: true + } + }); + const inlayHints = await server.request( + CommandTypes.ProvideInlayHints, + { + file: f, + start: 0, + length: 1000 + } + ); + assert.isDefined(inlayHints.body); + assert.equal(inlayHints.body![0].text, ': boolean'); + }).timeout(10000); + it('documentHighlight', async () => { const f = filePath('module2.ts'); server.notify(CommandTypes.Open, { From 3ac38c3dac8a84ee27401dc567539368bc2b156e Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Fri, 24 Sep 2021 12:29:48 +0200 Subject: [PATCH 04/16] add test to lsp-server --- src/lsp-server.spec.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/lsp-server.spec.ts b/src/lsp-server.spec.ts index b760d72c..5059fe5c 100644 --- a/src/lsp-server.spec.ts +++ b/src/lsp-server.spec.ts @@ -934,3 +934,41 @@ describe('diagnostics (no client support)', () => { assert.isUndefined(resultsForFile, 'Unexpected diagnostics received'); }).timeout(10000); }); + +describe('inlayHints', () => { + it('inlayHints', async () => { + const doc = { + uri: uri('module.ts'), + languageId: 'typescript', + version: 1, + text: ` + export function foo() { + return 3 + } + ` + }; + server.initialize({ + initializationOptions: { + preferences: { + includeInlayFunctionLikeReturnTypeHints: true + } + }, + processId: null, + capabilities: {}, + workspaceFolders: [], + rootUri: '' + }); + server.didOpenTextDocument({ + textDocument: doc + }); + + const { inlayHints } = await server.inlayHints({ + textDocument: doc + }); + + assert.isDefined(inlayHints); + assert.equal(inlayHints[0].text, ': number'); + assert.isDefined(inlayHints[0].kind); + assert.isDefined(inlayHints[0].position); + }).timeout(10000); +}); From ca191d2c4091a662dfffbf120e380eec61badcc7 Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Fri, 24 Sep 2021 12:34:00 +0200 Subject: [PATCH 05/16] move extensions to .proposed --- src/lsp-connection.ts | 4 +-- src/lsp-extensions.ts | 39 ------------------------- src/lsp-protocol.inlayHints.proposed.ts | 37 +++++++++++++++++++++++ src/lsp-server.ts | 6 ++-- 4 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 src/lsp-extensions.ts create mode 100644 src/lsp-protocol.inlayHints.proposed.ts diff --git a/src/lsp-connection.ts b/src/lsp-connection.ts index c2a0b77e..8952b173 100644 --- a/src/lsp-connection.ts +++ b/src/lsp-connection.ts @@ -7,11 +7,11 @@ import * as lsp from 'vscode-languageserver/node'; import * as lspcalls from './lsp-protocol.calls.proposed'; +import * as lspinlayHints from './lsp-protocol.inlayHints.proposed'; import { LspClientLogger } from './logger'; import { LspServer } from './lsp-server'; import { LspClientImpl } from './lsp-client'; -import { extensions } from './lsp-extensions'; export interface IServerOptions { tsserverPath: string; @@ -60,7 +60,7 @@ export function createLspConnection(options: IServerOptions): lsp.Connection { // proposed `textDocument/calls` request connection.onRequest(lspcalls.CallsRequest.type, server.calls.bind(server)); - connection.onRequest(extensions.inlayHints.type, server.inlayHints.bind(server)); + connection.onRequest(lspinlayHints.type, server.inlayHints.bind(server)); return connection; } diff --git a/src/lsp-extensions.ts b/src/lsp-extensions.ts deleted file mode 100644 index a871e90e..00000000 --- a/src/lsp-extensions.ts +++ /dev/null @@ -1,39 +0,0 @@ -import * as lsp from 'vscode-languageserver/node'; -import { RequestHandler } from 'vscode-jsonrpc'; - -export namespace extensions.inlayHints { - export type InlayHintsParams = { - /** - * The document to format. - */ - textDocument: lsp.TextDocumentIdentifier; - /** - * The range to format - */ - range?: lsp.Range; - }; - - type InlayHint = { - text: string; - position: lsp.Position; - kind?: 'Type' | 'Parameter' | 'Enum'; - whitespaceBefore?: boolean; - whitespaceAfter?: boolean; - }; - - export type InlayHintsResult = { - inlayHints: InlayHint[]; - }; - - export const type = new lsp.RequestType< - InlayHintsParams, - InlayHintsResult, - lsp.TextDocumentRegistrationOptions - >('typescript/inlayHints'); - - export type HandlerSignature = RequestHandler< - InlayHintsParams, - InlayHintsResult | null, - void - >; -} diff --git a/src/lsp-protocol.inlayHints.proposed.ts b/src/lsp-protocol.inlayHints.proposed.ts new file mode 100644 index 00000000..73d51611 --- /dev/null +++ b/src/lsp-protocol.inlayHints.proposed.ts @@ -0,0 +1,37 @@ +import * as lsp from 'vscode-languageserver/node'; +import { RequestHandler } from 'vscode-jsonrpc'; + +export type InlayHintsParams = { + /** + * The document to format. + */ + textDocument: lsp.TextDocumentIdentifier; + /** + * The range to format + */ + range?: lsp.Range; +}; + +type InlayHint = { + text: string; + position: lsp.Position; + kind?: 'Type' | 'Parameter' | 'Enum'; + whitespaceBefore?: boolean; + whitespaceAfter?: boolean; +}; + +export type InlayHintsResult = { + inlayHints: InlayHint[]; +}; + +export const type = new lsp.RequestType< +InlayHintsParams, +InlayHintsResult, +lsp.TextDocumentRegistrationOptions +>('typescript/inlayHints'); + +export type HandlerSignature = RequestHandler< +InlayHintsParams, +InlayHintsResult | null, +void +>; diff --git a/src/lsp-server.ts b/src/lsp-server.ts index 110f2981..7ff498f4 100644 --- a/src/lsp-server.ts +++ b/src/lsp-server.ts @@ -9,6 +9,7 @@ import * as path from 'path'; import tempy from 'tempy'; import * as lsp from 'vscode-languageserver/node'; import * as lspcalls from './lsp-protocol.calls.proposed'; +import * as lspinlayHints from './lsp-protocol.inlayHints.proposed'; import tsp from 'typescript/lib/protocol'; import * as fs from 'fs-extra'; import * as commandExists from 'command-exists'; @@ -37,7 +38,6 @@ import { provideOrganizeImports } from './organize-imports'; import { TypeScriptInitializeParams, TypeScriptInitializationOptions, TypeScriptInitializeResult } from './ts-protocol'; import { collectDocumentSymbols, collectSymbolInformation } from './document-symbol'; import { computeCallers, computeCallees } from './calls'; -import { extensions } from './lsp-extensions'; export interface IServerOptions { logger: Logger; @@ -986,8 +986,8 @@ export class LspServer { } async inlayHints( - params: extensions.inlayHints.InlayHintsParams - ): Promise { + params: lspinlayHints.InlayHintsParams + ): Promise { const file = uriToPath(params.textDocument.uri); this.logger.log('inlayHints', params, file); if (!file) { From 4a3d557fd9bc0e07167892274e77b1907dcf3e9a Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Sat, 25 Sep 2021 08:59:18 +0200 Subject: [PATCH 06/16] add new UserPreferences options to the README.md --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad2a1551..1f39d648 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ The language server accepts various settings through the `initializationOptions` | plugins | object[] | An array of `{ name: string, location: string }` objects for registering a Typescript plugins. **Default**: [] | | preferences | object | Preferences passed to the Typescript (`tsserver`) process. See below for more info. | -The `preferences` object is an object specifying preferences for the internal `tsserver` process. Those options depend on the version of Typescript used but at the time of writing Typescript v4.3.4 contains these options: +The `preferences` object is an object specifying preferences for the internal `tsserver` process. Those options depend on the version of Typescript used but at the time of writing Typescript v4.4.3 contains these options: ```ts interface UserPreferences { @@ -85,6 +85,7 @@ interface UserPreferences { * values, with insertion text to replace preceding `.` tokens with `?.`. */ includeAutomaticOptionalChainCompletions: boolean; + allowIncompleteCompletions: boolean; importModuleSpecifierPreference: "shortest" | "project-relative" | "relative" | "non-relative"; /** Determines whether we import `foo/index.ts` as "foo", "foo/index", or "foo/index.js" */ importModuleSpecifierEnding: "auto" | "minimal" | "index" | "js"; @@ -97,6 +98,16 @@ interface UserPreferences { displayPartsForJSDoc: boolean; generateReturnInDocTemplate: boolean; } +// Not officially part of UserPreferences yet but you can send them along with the UserPreferences just fine: +export interface InlayHintsOptions extends UserPreferences { + includeInlayParameterNameHints: "none" | "literals" | "all"; + includeInlayParameterNameHintsWhenArgumentMatchesName: boolean; + includeInlayFunctionParameterTypeHints: boolean, + includeInlayVariableTypeHints: boolean; + includeInlayPropertyDeclarationTypeHints: boolean; + includeInlayFunctionLikeReturnTypeHints: boolean; + includeInlayEnumMemberValueHints: boolean; +} ``` From the `preferences` options listed above, this server explicilty sets the following options (all other options use their default values): From 56c52a963365b8fb866c50ee8f280ef619838e4a Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Sat, 25 Sep 2021 08:59:40 +0200 Subject: [PATCH 07/16] use getDefaultClientCapabilities in the test --- src/lsp-server.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lsp-server.spec.ts b/src/lsp-server.spec.ts index 5059fe5c..5b0d7976 100644 --- a/src/lsp-server.spec.ts +++ b/src/lsp-server.spec.ts @@ -954,7 +954,7 @@ describe('inlayHints', () => { } }, processId: null, - capabilities: {}, + capabilities: getDefaultClientCapabilities(), workspaceFolders: [], rootUri: '' }); From ecbd3b5adc8993a1d1fbb3b219d95934c1a93397 Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Sat, 25 Sep 2021 08:59:58 +0200 Subject: [PATCH 08/16] return empty list if the request fails --- src/lsp-server.ts | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/lsp-server.ts b/src/lsp-server.ts index 7ff498f4..3f2953c0 100644 --- a/src/lsp-server.ts +++ b/src/lsp-server.ts @@ -1007,24 +1007,31 @@ export class LspServer { line: doc.lineCount + 1, character: 0 }); - const result = await this.tspClient.request( - CommandTypes.ProvideInlayHints, - { - file, - start: start, - length: end - start - } - ); - return { - inlayHints: - result.body?.map((item) => ({ - text: item.text, - position: toPosition(item.position), - whitespaceAfter: item.whitespaceAfter, - whitespaceBefore: item.whitespaceBefore, - kind: item.kind - })) ?? [] - }; + try { + const result = await this.tspClient.request( + CommandTypes.ProvideInlayHints, + { + file, + start: start, + length: end - start + } + ); + + return { + inlayHints: + result.body?.map((item) => ({ + text: item.text, + position: toPosition(item.position), + whitespaceAfter: item.whitespaceAfter, + whitespaceBefore: item.whitespaceBefore, + kind: item.kind + })) ?? [] + }; + } catch { + return { + inlayHints: [] + }; + } } } From 990949d2df8ca6ed2a307c260601ba8c2dc63cb9 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 26 Sep 2021 12:07:16 +0200 Subject: [PATCH 09/16] minor style fixes --- README.md | 52 ++++++++++++++++++++----- src/lsp-protocol.inlayHints.proposed.ts | 15 ++----- src/lsp-server.spec.ts | 7 ++-- src/lsp-server.ts | 4 +- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 1f39d648..a5a211af 100644 --- a/README.md +++ b/README.md @@ -98,16 +98,6 @@ interface UserPreferences { displayPartsForJSDoc: boolean; generateReturnInDocTemplate: boolean; } -// Not officially part of UserPreferences yet but you can send them along with the UserPreferences just fine: -export interface InlayHintsOptions extends UserPreferences { - includeInlayParameterNameHints: "none" | "literals" | "all"; - includeInlayParameterNameHintsWhenArgumentMatchesName: boolean; - includeInlayFunctionParameterTypeHints: boolean, - includeInlayVariableTypeHints: boolean; - includeInlayPropertyDeclarationTypeHints: boolean; - includeInlayFunctionLikeReturnTypeHints: boolean; - includeInlayEnumMemberValueHints: boolean; -} ``` From the `preferences` options listed above, this server explicilty sets the following options (all other options use their default values): @@ -140,6 +130,48 @@ From the `preferences` options listed above, this server explicilty sets the fol - [x] textDocument/signatureHelp - [x] workspace/symbol +## `typescript/inlayHints` (experimental) + +Request: + +```ts +type { + textDocument: TextDocumentIdentifier, + range?: Range, +} +``` + +Response: + +```ts +type { + inlayHints: InlayHint[]; +} + +type InlayHint = { + text: string; + position: lsp.Position; + kind: 'Type' | 'Parameter' | 'Enum'; + whitespaceBefore?: boolean; + whitespaceAfter?: boolean; +}; +``` + +Requires the some or all of the following options to be passed through `preferences`: + +```ts +// Not officially part of UserPreferences yet but you can send them along with the UserPreferences just fine: +export interface InlayHintsOptions extends UserPreferences { + includeInlayParameterNameHints: "none" | "literals" | "all"; + includeInlayParameterNameHintsWhenArgumentMatchesName: boolean; + includeInlayFunctionParameterTypeHints: boolean, + includeInlayVariableTypeHints: boolean; + includeInlayPropertyDeclarationTypeHints: boolean; + includeInlayFunctionLikeReturnTypeHints: boolean; + includeInlayEnumMemberValueHints: boolean; +} +``` + # Development ### Build diff --git a/src/lsp-protocol.inlayHints.proposed.ts b/src/lsp-protocol.inlayHints.proposed.ts index 73d51611..4737b171 100644 --- a/src/lsp-protocol.inlayHints.proposed.ts +++ b/src/lsp-protocol.inlayHints.proposed.ts @@ -1,4 +1,5 @@ import * as lsp from 'vscode-languageserver/node'; +import tsp from 'typescript/lib/protocol'; import { RequestHandler } from 'vscode-jsonrpc'; export type InlayHintsParams = { @@ -15,7 +16,7 @@ export type InlayHintsParams = { type InlayHint = { text: string; position: lsp.Position; - kind?: 'Type' | 'Parameter' | 'Enum'; + kind: tsp.InlayHintKind; whitespaceBefore?: boolean; whitespaceAfter?: boolean; }; @@ -24,14 +25,6 @@ export type InlayHintsResult = { inlayHints: InlayHint[]; }; -export const type = new lsp.RequestType< -InlayHintsParams, -InlayHintsResult, -lsp.TextDocumentRegistrationOptions ->('typescript/inlayHints'); +export const type = new lsp.RequestType('typescript/inlayHints'); -export type HandlerSignature = RequestHandler< -InlayHintsParams, -InlayHintsResult | null, -void ->; +export type HandlerSignature = RequestHandler; diff --git a/src/lsp-server.spec.ts b/src/lsp-server.spec.ts index 5b0d7976..254f3f2d 100644 --- a/src/lsp-server.spec.ts +++ b/src/lsp-server.spec.ts @@ -967,8 +967,9 @@ describe('inlayHints', () => { }); assert.isDefined(inlayHints); - assert.equal(inlayHints[0].text, ': number'); - assert.isDefined(inlayHints[0].kind); - assert.isDefined(inlayHints[0].position); + assert.strictEqual(inlayHints.length, 1); + assert.strictEqual(inlayHints[0].text, ': number'); + assert.strictEqual(inlayHints[0].kind, 'Type'); + assert.deepStrictEqual(inlayHints[0].position, { line: 1, character: 33 }); }).timeout(10000); }); diff --git a/src/lsp-server.ts b/src/lsp-server.ts index 3f2953c0..50974f6d 100644 --- a/src/lsp-server.ts +++ b/src/lsp-server.ts @@ -985,9 +985,7 @@ export class LspServer { return callsResult; } - async inlayHints( - params: lspinlayHints.InlayHintsParams - ): Promise { + async inlayHints(params: lspinlayHints.InlayHintsParams): Promise { const file = uriToPath(params.textDocument.uri); this.logger.log('inlayHints', params, file); if (!file) { From 75af710ba1b90d1b48a564d54c398c2c70dc3c71 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 26 Sep 2021 12:08:16 +0200 Subject: [PATCH 10/16] formatting --- README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a5a211af..428cbcc5 100644 --- a/README.md +++ b/README.md @@ -132,19 +132,13 @@ From the `preferences` options listed above, this server explicilty sets the fol ## `typescript/inlayHints` (experimental) -Request: - ```ts -type { +type Request { textDocument: TextDocumentIdentifier, range?: Range, } -``` -Response: - -```ts -type { +type Response { inlayHints: InlayHint[]; } From 45163ae636c10589757b31d3729609723daa3e79 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 26 Sep 2021 12:09:16 +0200 Subject: [PATCH 11/16] reforamt --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 428cbcc5..c28da3e7 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ type InlayHint = { }; ``` -Requires the some or all of the following options to be passed through `preferences`: +For the request to return any reuslts some or all of the following options need to be passed through `preferences`: ```ts // Not officially part of UserPreferences yet but you can send them along with the UserPreferences just fine: From d00fad78dcfb6e0b583cbd89fee39e4efed70975 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 26 Sep 2021 12:16:34 +0200 Subject: [PATCH 12/16] correct character --- src/lsp-server.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lsp-server.spec.ts b/src/lsp-server.spec.ts index 254f3f2d..af143129 100644 --- a/src/lsp-server.spec.ts +++ b/src/lsp-server.spec.ts @@ -970,6 +970,6 @@ describe('inlayHints', () => { assert.strictEqual(inlayHints.length, 1); assert.strictEqual(inlayHints[0].text, ': number'); assert.strictEqual(inlayHints[0].kind, 'Type'); - assert.deepStrictEqual(inlayHints[0].position, { line: 1, character: 33 }); + assert.deepStrictEqual(inlayHints[0].position, { line: 1, character: 29 }); }).timeout(10000); }); From 55403ca1f88f9f5690ba5543d2a6e37084732825 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Mon, 27 Sep 2021 11:57:43 +0200 Subject: [PATCH 13/16] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c28da3e7..686123eb 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ type InlayHint = { }; ``` -For the request to return any reuslts some or all of the following options need to be passed through `preferences`: +For the request to return any results, some or all of the following options need to be enabled through `preferences`: ```ts // Not officially part of UserPreferences yet but you can send them along with the UserPreferences just fine: From 833a0d75ac14cd972bc0168df80a3953de45bcb0 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Mon, 27 Sep 2021 11:59:31 +0200 Subject: [PATCH 14/16] add version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 686123eb..3c7e3786 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ From the `preferences` options listed above, this server explicilty sets the fol - [x] textDocument/signatureHelp - [x] workspace/symbol -## `typescript/inlayHints` (experimental) +## `typescript/inlayHints` (experimental, supported from Typescript v4.4.2) ```ts type Request { From c0860da25de8ed2c15b197626c270af17ddad6b7 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Mon, 27 Sep 2021 12:04:35 +0200 Subject: [PATCH 15/16] fix syntax --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c7e3786..d917bcc5 100644 --- a/README.md +++ b/README.md @@ -130,15 +130,15 @@ From the `preferences` options listed above, this server explicilty sets the fol - [x] textDocument/signatureHelp - [x] workspace/symbol -## `typescript/inlayHints` (experimental, supported from Typescript v4.4.2) +## [x] `typescript/inlayHints` (experimental, supported from Typescript v4.4.2) ```ts -type Request { +type Request = { textDocument: TextDocumentIdentifier, range?: Range, } -type Response { +type Response = { inlayHints: InlayHint[]; } From 5bea09a280bbdd4a21980c24e2bd2b473ce30db1 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Mon, 27 Sep 2021 13:06:26 +0200 Subject: [PATCH 16/16] well --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d917bcc5..fd4650a8 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ From the `preferences` options listed above, this server explicilty sets the fol - [x] textDocument/signatureHelp - [x] workspace/symbol -## [x] `typescript/inlayHints` (experimental, supported from Typescript v4.4.2) +## `typescript/inlayHints` (experimental, supported from Typescript v4.4.2) ```ts type Request = {