Skip to content
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

feat(code/inlay): hide hints when selected by cursor #11491

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct InlayHintsConfig {
pub type_hints: bool,
pub parameter_hints: bool,
pub chaining_hints: bool,
pub hide_when_selected: bool,
pub hide_named_constructor_hints: bool,
pub max_length: Option<usize>,
}
Expand Down Expand Up @@ -554,6 +555,7 @@ mod tests {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
};
Expand All @@ -570,6 +572,7 @@ mod tests {
parameter_hints: true,
type_hints: false,
chaining_hints: false,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand All @@ -584,6 +587,7 @@ mod tests {
parameter_hints: false,
type_hints: true,
chaining_hints: false,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand All @@ -598,6 +602,7 @@ mod tests {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down Expand Up @@ -629,6 +634,7 @@ mod tests {
type_hints: false,
parameter_hints: false,
chaining_hints: false,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down Expand Up @@ -1344,6 +1350,7 @@ fn main() {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: true,
max_length: None,
},
Expand Down Expand Up @@ -1521,6 +1528,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down Expand Up @@ -1578,6 +1586,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down Expand Up @@ -1623,6 +1632,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down Expand Up @@ -1669,6 +1679,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: None,
},
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/static_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl StaticIndex<'_> {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_when_selected: false,
hide_named_constructor_hints: false,
max_length: Some(25),
},
Expand Down
3 changes: 3 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ config_data! {
inlayHints_parameterHints: bool = "true",
/// Whether to show inlay type hints for variables.
inlayHints_typeHints: bool = "true",
/// Whether to hide inlay hints when selected.
inlayHints_hideWhenSelected: bool = "false",
/// Whether to hide inlay hints for constructors.
inlayHints_hideNamedConstructorHints: bool = "false",

Expand Down Expand Up @@ -847,6 +849,7 @@ impl Config {
type_hints: self.data.inlayHints_typeHints,
parameter_hints: self.data.inlayHints_parameterHints,
chaining_hints: self.data.inlayHints_chainingHints,
hide_when_selected: self.data.inlayHints_hideWhenSelected,
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
max_length: self.data.inlayHints_maxLength,
}
Expand Down
5 changes: 5 additions & 0 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ site.
--
Whether to show inlay type hints for variables.
--
[[rust-analyzer.inlayHints.hideWhenSelected]]rust-analyzer.inlayHints.hideWhenSelected (default: `false`)::
+
--
Whether to hide inlay hints when selected.
--
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
+
--
Expand Down
7 changes: 6 additions & 1 deletion editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,11 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.inlayHints.hideWhenSelected": {
"markdownDescription": "Whether to hide inlay hints when selected.",
"default": false,
"type": "boolean"
},
"rust-analyzer.inlayHints.hideNamedConstructorHints": {
"markdownDescription": "Whether to hide inlay hints for constructors.",
"default": false,
Expand Down Expand Up @@ -1527,4 +1532,4 @@
]
}
}
}
}
1 change: 1 addition & 0 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class Config {
typeHints: this.get<boolean>("inlayHints.typeHints"),
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
hideWhenSelected: this.get<boolean>("inlayHints.hideWhenSelected"),
hideNamedConstructorHints: this.get<boolean>("inlayHints.hideNamedConstructorHints"),
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
maxLength: this.get<null | number>("inlayHints.maxLength"),
Expand Down
39 changes: 35 additions & 4 deletions editors/code/src/inlay_hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from 'vscode';
import * as ra from './lsp_ext';

import { Ctx, Disposable } from './ctx';
import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep, isRustEditor } from './util';

interface InlayHintStyle {
decorationType: vscode.TextEditorDecorationType;
Expand Down Expand Up @@ -112,6 +112,12 @@ class HintsUpdater implements Disposable {
this.disposables
);

vscode.window.onDidChangeTextEditorSelection(
this.onDidChangeTextEditorSelection,
this,
this.disposables
);

// Set up initial cache shape
ctx.visibleRustEditors.forEach(editor => this.sourceFiles.set(
editor.document.uri.toString(),
Expand All @@ -132,6 +138,16 @@ class HintsUpdater implements Disposable {
this.disposables.forEach(d => d.dispose());
}

onDidChangeTextEditorSelection({ textEditor, selections }: vscode.TextEditorSelectionChangeEvent) {
if (selections.length === 0 || !isRustEditor(textEditor)) return;

const uri = textEditor.document.uri.toString();
const file = this.sourceFiles.get(uri);
if (!file || !file.cachedDecorations) return;

this.renderDecorations(textEditor, file.cachedDecorations);
}

onDidChangeTextDocument({ contentChanges, document }: vscode.TextDocumentChangeEvent) {
if (contentChanges.length === 0 || !isRustDocument(document)) return;
this.syncCacheAndRenderHints();
Expand Down Expand Up @@ -200,9 +216,24 @@ class HintsUpdater implements Disposable {
editor.setDecorations(paramHints.decorationType, []);
editor.setDecorations(chainingHints.decorationType, []);
}
editor.setDecorations(typeHints.decorationType, decorations.type);
editor.setDecorations(paramHints.decorationType, decorations.param);
editor.setDecorations(chainingHints.decorationType, decorations.chaining);

let type = decorations.type;
let param = decorations.param;
let chaining = decorations.chaining;

if (this.ctx.config.inlayHints.hideWhenSelected) {
const predicate = (dec: vscode.DecorationOptions): boolean => {
return !editor.selections.some(sel => sel.contains(dec.range));
};

type = type.filter(predicate);
param = param.filter(predicate);
chaining = chaining.filter(predicate);
}

editor.setDecorations(typeHints.decorationType, type);
editor.setDecorations(paramHints.decorationType, param);
editor.setDecorations(chainingHints.decorationType, chaining);
}

private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
Expand Down