Commit
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as shared from '@volar/shared'; | ||
import type * as ts from 'typescript/lib/tsserverlibrary'; | ||
import * as vscode from 'vscode-languageserver-protocol'; | ||
import type { TextDocument } from 'vscode-languageserver-textdocument'; | ||
import type { Settings } from '..'; | ||
|
||
export function register( | ||
languageService: ts.LanguageService, | ||
getTextDocument: (uri: string) => TextDocument | undefined, | ||
settings: Settings, | ||
ts: typeof import('typescript/lib/tsserverlibrary'), | ||
) { | ||
return async (uri: string, range: vscode.Range) => { | ||
|
||
const document = getTextDocument(uri); | ||
if (!document) return; | ||
|
||
const preferences = await settings.getPreferences?.(document) ?? {}; | ||
const fileName = shared.uriToFsPath(document.uri); | ||
const start = document.offsetAt(range.start); | ||
const end = document.offsetAt(range.end); | ||
const inlayHints = languageService.provideInlayHints(fileName, { start, length: end - start }, preferences); | ||
|
||
return inlayHints.map(inlayHint => { | ||
const result = vscode.InlayHint.create( | ||
document.positionAt(inlayHint.position), | ||
inlayHint.text, | ||
inlayHint.kind === ts.InlayHintKind.Type ? vscode.InlayHintKind.Type | ||
: inlayHint.kind === ts.InlayHintKind.Parameter ? vscode.InlayHintKind.Parameter | ||
: undefined, | ||
); | ||
result.paddingLeft = inlayHint.whitespaceBefore; | ||
result.paddingRight = inlayHint.whitespaceAfter; | ||
return result; | ||
}); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as shared from '@volar/shared'; | ||
import { transformTextEdit } from '@volar/transforms'; | ||
import * as vscode from 'vscode-languageserver-protocol'; | ||
import type { LanguageServiceRuntimeContext } from '../types'; | ||
import { languageFeatureWorker } from '../utils/featureWorkers'; | ||
|
||
export function register(context: LanguageServiceRuntimeContext) { | ||
|
||
return async (uri: string, range: vscode.Range) => { | ||
|
||
const document = context.getTextDocument(uri); | ||
|
||
if (!document) | ||
return; | ||
|
||
const offsetRange = { | ||
start: document.offsetAt(range.start), | ||
end: document.offsetAt(range.end), | ||
}; | ||
|
||
return languageFeatureWorker( | ||
context, | ||
uri, | ||
range, | ||
(arg, sourceMap) => { | ||
|
||
/** | ||
* copy from ./codeActions.ts | ||
*/ | ||
|
||
if (!sourceMap.embeddedFile.capabilities.codeActions) | ||
return []; | ||
|
||
let minStart: number | undefined; | ||
let maxEnd: number | undefined; | ||
|
||
for (const mapping of sourceMap.mappings) { | ||
const overlapRange = shared.getOverlapRange2(offsetRange, mapping.sourceRange); | ||
if (overlapRange) { | ||
const embeddedRange = sourceMap.getMappedRange(overlapRange.start, overlapRange.end)?.[0]; | ||
if (embeddedRange) { | ||
minStart = minStart === undefined ? embeddedRange.start : Math.min(embeddedRange.start, minStart); | ||
maxEnd = maxEnd === undefined ? embeddedRange.end : Math.max(embeddedRange.end, maxEnd); | ||
} | ||
} | ||
} | ||
|
||
if (minStart !== undefined && maxEnd !== undefined) { | ||
return [vscode.Range.create( | ||
sourceMap.mappedDocument.positionAt(minStart), | ||
sourceMap.mappedDocument.positionAt(maxEnd), | ||
)]; | ||
} | ||
|
||
return []; | ||
}, | ||
(plugin, document, arg, sourceMap) => { | ||
return plugin.getInlayHints?.(document, arg); | ||
}, | ||
(inlayHints, sourceMap) => inlayHints.map(_inlayHint => { | ||
|
||
if (!sourceMap) | ||
return _inlayHint; | ||
|
||
const position = sourceMap.getSourceRange(_inlayHint.position, _inlayHint.position, data => !!data.capabilities.completion)?.[0].start; | ||
const edits = _inlayHint.textEdits?.map(textEdit => transformTextEdit(textEdit, range => sourceMap.getSourceRange(range.start, range.end)?.[0])).filter(shared.notEmpty); | ||
|
||
if (position) { | ||
return { | ||
..._inlayHint, | ||
position, | ||
edits, | ||
}; | ||
} | ||
}).filter(shared.notEmpty), | ||
arr => arr.flat(), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,15 @@ export async function getPreferences( | |
allowIncompleteCompletions: true, | ||
displayPartsForJSDoc: true, | ||
|
||
// inlay hints | ||
includeInlayParameterNameHints: getInlayParameterNameHintsPreference(config), | ||
includeInlayParameterNameHintsWhenArgumentMatchesName: !(config.inlayHints?.parameterNames?.suppressWhenArgumentMatchesName ?? true), | ||
includeInlayFunctionParameterTypeHints: config.inlayHints?.parameterTypes?.enabled ?? false, | ||
includeInlayVariableTypeHints: config.inlayHints?.variableTypes?.enabled ?? false, | ||
includeInlayPropertyDeclarationTypeHints: config.inlayHints?.propertyDeclarationTypes?.enabled ?? false, | ||
includeInlayFunctionLikeReturnTypeHints: config.inlayHints?.functionLikeReturnTypes?.enabled ?? false, | ||
includeInlayEnumMemberValueHints: config.inlayHints?.enumMemberValues?.enabled ?? false, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
johnsoncodehk
Author
Member
|
||
|
||
// custom | ||
includeCompletionsForModuleExports: config.suggest?.autoImports ?? true, | ||
}; | ||
|
@@ -111,3 +120,12 @@ function getImportModuleSpecifierEndingPreference(config: any) { | |
function isTypeScriptDocument(doc: TextDocument) { | ||
return ['typescript', 'typescriptreact'].includes(doc.languageId); | ||
} | ||
|
||
function getInlayParameterNameHintsPreference(config: any) { | ||
switch (config.inlayHints?.parameterNames?.enabled) { | ||
case 'none': return 'none'; | ||
case 'literals': return 'literals'; | ||
case 'all': return 'all'; | ||
default: return undefined; | ||
} | ||
} |
5 comments
on commit ae8821f
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.
😲🥰 Happy to test this out on tuesday
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.
How Can I set this up in vscode?
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.
@dajpes Try setting typescript.inlayHints.*
or javascript.inlayHints.*
to true
Example settings.json:
{
// ...snip
"typescript.inlayHints.enumMemberValues.enabled": true,
"typescript.inlayHints.functionLikeReturnTypes.enabled": true,
"typescript.inlayHints.propertyDeclarationTypes.enabled": true,
"typescript.inlayHints.parameterTypes.enabled": true,
"typescript.inlayHints.variableTypes.enabled": true,
"typescript.inlayHints.parameterNames.suppressWhenArgumentMatchesName": true,
"javascript.inlayHints.variableTypes.enabled": true,
"javascript.inlayHints.parameterTypes.enabled": true,
"javascript.inlayHints.enumMemberValues.enabled": true,
"javascript.inlayHints.functionLikeReturnTypes.enabled": true,
"javascript.inlayHints.propertyDeclarationTypes.enabled": true,
"javascript.inlayHints.parameterNames.suppressWhenArgumentMatchesName": true,
// ...snip
}
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.
Hey @yaegassy , you method actually works, but only if I have enabled vscode built in typescript extension.
Since I have that disabled (because volar required it so), then I'm not able to turn the inlay hints on.
So, in other works is there a way to enable vscode inlay hints with volar extension, but having vscode built in typescript extension off?
cc. @johnsoncodehk
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.
Hey @yaegassy , you method actually works, but only if I have enabled vscode built in typescript extension.
Since I have that disabled (because volar required it so), then I'm not able to turn the inlay hints on.
So, in other works is there a way to enable vscode inlay hints with volar extension, but having vscode built in typescript extension off?
cc. @johnsoncodehk
You need to add the settings to your project's VSCode settings.json file. The typescript settings are only recognized in the workspace settings.
How can a client provide these config options to the server?
I thought it can provide it through
initializationOptions
, but they do not accept inlayHint options.Than I looked at workspace settings, but I haven't found any workspace setting for inlay hints here.