-
Notifications
You must be signed in to change notification settings - Fork 167
feat: support textDocument/inlayHint
request from 3.17.0 spec
#566
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,100 @@ | ||
/* | ||
* Copyright (C) 2022 TypeFox and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import tsp from 'typescript/lib/protocol.d.js'; | ||
import * as lsp from 'vscode-languageserver'; | ||
import API from '../utils/api.js'; | ||
import type { ConfigurationManager } from '../configuration-manager.js'; | ||
import type { LspDocuments } from '../document.js'; | ||
import type { TspClient } from '../tsp-client.js'; | ||
import type { LspClient } from '../lsp-client.js'; | ||
import { CommandTypes } from '../tsp-command-types.js'; | ||
import { Position } from '../utils/typeConverters.js'; | ||
import { uriToPath } from '../protocol-translation.js'; | ||
|
||
export class TypeScriptInlayHintsProvider { | ||
public static readonly minVersion = API.v440; | ||
|
||
public static async provideInlayHints( | ||
uri: lsp.DocumentUri, | ||
range: lsp.Range, | ||
documents: LspDocuments, | ||
tspClient: TspClient, | ||
lspClient: LspClient, | ||
configurationManager: ConfigurationManager | ||
): Promise<lsp.InlayHint[]> { | ||
if (tspClient.apiVersion.lt(TypeScriptInlayHintsProvider.minVersion)) { | ||
lspClient.showErrorMessage('Inlay Hints request failed. Requires TypeScript 4.4+.'); | ||
return []; | ||
} | ||
|
||
const file = uriToPath(uri); | ||
|
||
if (!file) { | ||
lspClient.showErrorMessage('Inlay Hints request failed. No resource provided.'); | ||
return []; | ||
} | ||
|
||
const document = documents.get(file); | ||
|
||
if (!document) { | ||
lspClient.showErrorMessage('Inlay Hints request failed. File not opened in the editor.'); | ||
return []; | ||
} | ||
|
||
if (!areInlayHintsEnabledForFile(configurationManager, file)) { | ||
return []; | ||
} | ||
|
||
await configurationManager.configureGloballyFromDocument(file); | ||
|
||
const start = document.offsetAt(range.start); | ||
const length = document.offsetAt(range.end) - start; | ||
|
||
const response = await tspClient.request(CommandTypes.ProvideInlayHints, { file, start, length }); | ||
if (response.type !== 'response' || !response.success || !response.body) { | ||
return []; | ||
} | ||
|
||
return response.body.map<lsp.InlayHint>(hint => { | ||
const inlayHint = lsp.InlayHint.create( | ||
Position.fromLocation(hint.position), | ||
hint.text, | ||
fromProtocolInlayHintKind(hint.kind)); | ||
hint.whitespaceBefore && (inlayHint.paddingLeft = true); | ||
hint.whitespaceAfter && (inlayHint.paddingRight = true); | ||
return inlayHint; | ||
}); | ||
} | ||
} | ||
|
||
function areInlayHintsEnabledForFile(configurationManager: ConfigurationManager, filename: string) { | ||
const preferences = configurationManager.getPreferences(filename); | ||
|
||
// Doesn't need to include `includeInlayVariableTypeHintsWhenTypeMatchesName` as it depends | ||
// on `includeInlayVariableTypeHints` being enabled. | ||
return preferences.includeInlayParameterNameHints === 'literals' || | ||
preferences.includeInlayParameterNameHints === 'all' || | ||
preferences.includeInlayEnumMemberValueHints || | ||
preferences.includeInlayFunctionLikeReturnTypeHints || | ||
preferences.includeInlayFunctionParameterTypeHints || | ||
preferences.includeInlayPropertyDeclarationTypeHints || | ||
preferences.includeInlayVariableTypeHints; | ||
} | ||
|
||
function fromProtocolInlayHintKind(kind: tsp.InlayHintKind): lsp.InlayHintKind | undefined { | ||
switch (kind) { | ||
case 'Parameter': return lsp.InlayHintKind.Parameter; | ||
case 'Type': return lsp.InlayHintKind.Type; | ||
case 'Enum': return undefined; | ||
default: return undefined; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this check necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Otherwise, even with all inlay hint options disabled, the server would still do expensive processing that takes a significant amount of time. The old implementation suffered from that which I've realized when working on this.