Truncate hints longer than 20 characters

This commit is contained in:
Wilco Kusee 2019-10-10 13:26:17 +02:00
parent 523d7d2c82
commit ce4fb06dec
No known key found for this signature in database
GPG key ID: D5B2BB5CDC3334BC

View file

@ -13,6 +13,8 @@ interface InlayHint {
label: string;
}
const maxHintLength = 20;
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: new vscode.ThemeColor('ralsp.inlayHint')
@ -83,10 +85,20 @@ export class HintsUpdater {
): Promise<void> {
const newHints = await this.queryHints(editor.document.uri.toString());
if (newHints !== null) {
const newDecorations = newHints.map(hint => ({
range: hint.range,
renderOptions: { after: { contentText: `: ${hint.label}` } }
}));
const newDecorations = newHints.map(hint => {
let label = hint.label.substring(0, maxHintLength);
if (hint.label.length > maxHintLength) {
label += '…';
}
return {
range: this.truncateHint(hint.range),
renderOptions: {
after: {
contentText: `: ${label}`
}
}
};
});
return editor.setDecorations(
typeHintDecorationType,
newDecorations
@ -94,6 +106,18 @@ export class HintsUpdater {
}
}
private truncateHint(range: Range): Range {
if (!range.isSingleLine) {
return range;
}
const maxEnd = new vscode.Position(
range.start.line,
range.start.character + maxHintLength
);
const end = range.end.isAfter(maxEnd) ? maxEnd : range.end;
return new Range(range.start, end);
}
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
const request: InlayHintsParams = {
textDocument: { uri: documentUri }