mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
Make inlay hint length configurable
This commit is contained in:
parent
ce4fb06dec
commit
3b61acb4ae
3 changed files with 37 additions and 14 deletions
|
@ -260,6 +260,11 @@
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": true,
|
"default": true,
|
||||||
"description": "Display additional type information in the editor"
|
"description": "Display additional type information in the editor"
|
||||||
|
},
|
||||||
|
"rust-analyzer.maxInlayHintLength": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 20,
|
||||||
|
"description": "Maximum length for inlay hints"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -13,8 +13,6 @@ interface InlayHint {
|
||||||
label: string;
|
label: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxHintLength = 20;
|
|
||||||
|
|
||||||
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
|
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
|
||||||
after: {
|
after: {
|
||||||
color: new vscode.ThemeColor('ralsp.inlayHint')
|
color: new vscode.ThemeColor('ralsp.inlayHint')
|
||||||
|
@ -86,12 +84,12 @@ export class HintsUpdater {
|
||||||
const newHints = await this.queryHints(editor.document.uri.toString());
|
const newHints = await this.queryHints(editor.document.uri.toString());
|
||||||
if (newHints !== null) {
|
if (newHints !== null) {
|
||||||
const newDecorations = newHints.map(hint => {
|
const newDecorations = newHints.map(hint => {
|
||||||
let label = hint.label.substring(0, maxHintLength);
|
const [label, range] = this.truncateHint(
|
||||||
if (hint.label.length > maxHintLength) {
|
hint.label,
|
||||||
label += '…';
|
hint.range
|
||||||
}
|
);
|
||||||
return {
|
return {
|
||||||
range: this.truncateHint(hint.range),
|
range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
after: {
|
after: {
|
||||||
contentText: `: ${label}`
|
contentText: `: ${label}`
|
||||||
|
@ -106,16 +104,30 @@ export class HintsUpdater {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private truncateHint(range: Range): Range {
|
private truncateHint(
|
||||||
if (!range.isSingleLine) {
|
label: string,
|
||||||
return range;
|
range: vscode.Range
|
||||||
|
): [string, vscode.Range] {
|
||||||
|
if (!Server.config.maxInlayHintLength) {
|
||||||
|
return [label, range];
|
||||||
}
|
}
|
||||||
const maxEnd = new vscode.Position(
|
|
||||||
|
let newLabel = label.substring(0, Server.config.maxInlayHintLength);
|
||||||
|
if (label.length > Server.config.maxInlayHintLength) {
|
||||||
|
newLabel += '…';
|
||||||
|
}
|
||||||
|
|
||||||
|
range = new vscode.Range(
|
||||||
range.start.line,
|
range.start.line,
|
||||||
range.start.character + maxHintLength
|
range.start.character,
|
||||||
|
range.end.line,
|
||||||
|
Math.min(
|
||||||
|
range.start.character + Server.config.maxInlayHintLength,
|
||||||
|
range.end.character
|
||||||
|
)
|
||||||
);
|
);
|
||||||
const end = range.end.isAfter(maxEnd) ? maxEnd : range.end;
|
|
||||||
return new Range(range.start, end);
|
return [newLabel, range];
|
||||||
}
|
}
|
||||||
|
|
||||||
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
||||||
|
|
|
@ -22,6 +22,7 @@ export class Config {
|
||||||
public showWorkspaceLoadedNotification = true;
|
public showWorkspaceLoadedNotification = true;
|
||||||
public lruCapacity: null | number = null;
|
public lruCapacity: null | number = null;
|
||||||
public displayInlayHints = true;
|
public displayInlayHints = true;
|
||||||
|
public maxInlayHintLength: null | number = null;
|
||||||
public excludeGlobs = [];
|
public excludeGlobs = [];
|
||||||
public useClientWatching = false;
|
public useClientWatching = false;
|
||||||
public featureFlags = {};
|
public featureFlags = {};
|
||||||
|
@ -131,6 +132,11 @@ export class Config {
|
||||||
if (config.has('displayInlayHints')) {
|
if (config.has('displayInlayHints')) {
|
||||||
this.displayInlayHints = config.get('displayInlayHints') as boolean;
|
this.displayInlayHints = config.get('displayInlayHints') as boolean;
|
||||||
}
|
}
|
||||||
|
if (config.has('maxInlayHintLength')) {
|
||||||
|
this.maxInlayHintLength = config.get(
|
||||||
|
'maxInlayHintLength'
|
||||||
|
) as number;
|
||||||
|
}
|
||||||
if (config.has('excludeGlobs')) {
|
if (config.has('excludeGlobs')) {
|
||||||
this.excludeGlobs = config.get('excludeGlobs') || [];
|
this.excludeGlobs = config.get('excludeGlobs') || [];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue