]> git.lizzy.rs Git - rust.git/commitdiff
Make inlay hint length configurable
authorWilco Kusee <wilcokusee@gmail.com>
Fri, 18 Oct 2019 11:40:03 +0000 (13:40 +0200)
committerWilco Kusee <wilcokusee@gmail.com>
Fri, 18 Oct 2019 11:45:04 +0000 (13:45 +0200)
editors/code/package.json
editors/code/src/commands/inlay_hints.ts
editors/code/src/config.ts

index b9982c624a697f9046afc8822327f7ca52806c44..35211bcc23a2eb24df60ccfbe4d7117ed263bea6 100644 (file)
                     "type": "boolean",
                     "default": true,
                     "description": "Display additional type information in the editor"
+                },
+                "rust-analyzer.maxInlayHintLength": {
+                    "type": "number",
+                    "default": 20,
+                    "description": "Maximum length for inlay hints"
                 }
             }
         },
index 3157c04c8b27bca1770af163bed4de4bc7da3dc5..454a464d4a8a93f476e88e661654cf4aa1d32995 100644 (file)
@@ -13,8 +13,6 @@ interface InlayHint {
     label: string;
 }
 
-const maxHintLength = 20;
-
 const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
     after: {
         color: new vscode.ThemeColor('ralsp.inlayHint')
@@ -86,12 +84,12 @@ export class HintsUpdater {
         const newHints = await this.queryHints(editor.document.uri.toString());
         if (newHints !== null) {
             const newDecorations = newHints.map(hint => {
-                let label = hint.label.substring(0, maxHintLength);
-                if (hint.label.length > maxHintLength) {
-                    label += '…';
-                }
+                const [label, range] = this.truncateHint(
+                    hint.label,
+                    hint.range
+                );
                 return {
-                    range: this.truncateHint(hint.range),
+                    range,
                     renderOptions: {
                         after: {
                             contentText: `: ${label}`
@@ -106,16 +104,30 @@ export class HintsUpdater {
         }
     }
 
-    private truncateHint(range: Range): Range {
-        if (!range.isSingleLine) {
-            return range;
+    private truncateHint(
+        label: string,
+        range: vscode.Range
+    ): [string, vscode.Range] {
+        if (!Server.config.maxInlayHintLength) {
+            return [label, range];
+        }
+
+        let newLabel = label.substring(0, Server.config.maxInlayHintLength);
+        if (label.length > Server.config.maxInlayHintLength) {
+            newLabel += '…';
         }
-        const maxEnd = new vscode.Position(
+
+        range = new vscode.Range(
             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> {
index a4581485cccb83b764d0c7bd1f22e017acb575f6..2578bc6d10450dbabe30b26561ba10dd6b994a2e 100644 (file)
@@ -22,6 +22,7 @@ export class Config {
     public showWorkspaceLoadedNotification = true;
     public lruCapacity: null | number = null;
     public displayInlayHints = true;
+    public maxInlayHintLength: null | number = null;
     public excludeGlobs = [];
     public useClientWatching = false;
     public featureFlags = {};
@@ -131,6 +132,11 @@ export class Config {
         if (config.has('displayInlayHints')) {
             this.displayInlayHints = config.get('displayInlayHints') as boolean;
         }
+        if (config.has('maxInlayHintLength')) {
+            this.maxInlayHintLength = config.get(
+                'maxInlayHintLength'
+            ) as number;
+        }
         if (config.has('excludeGlobs')) {
             this.excludeGlobs = config.get('excludeGlobs') || [];
         }