Skip to content

Commit e1aa73e

Browse files
committed
Disable inlay hint location links on vscode < 1.76
1 parent 801a223 commit e1aa73e

File tree

9 files changed

+121
-10
lines changed

9 files changed

+121
-10
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod bind_pat;
2727

2828
#[derive(Clone, Debug, PartialEq, Eq)]
2929
pub struct InlayHintsConfig {
30+
pub location_links: bool,
3031
pub render_colons: bool,
3132
pub type_hints: bool,
3233
pub parameter_hints: bool,
@@ -182,6 +183,7 @@ struct InlayHintLabelBuilder<'a> {
182183
db: &'a RootDatabase,
183184
result: InlayHintLabel,
184185
last_part: String,
186+
location_link_enabled: bool,
185187
location: Option<FileRange>,
186188
}
187189

@@ -193,6 +195,9 @@ impl fmt::Write for InlayHintLabelBuilder<'_> {
193195

194196
impl HirWrite for InlayHintLabelBuilder<'_> {
195197
fn start_location_link(&mut self, def: ModuleDefId) {
198+
if !self.location_link_enabled {
199+
return;
200+
}
196201
if self.location.is_some() {
197202
never!("location link is already started");
198203
}
@@ -204,6 +209,9 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
204209
}
205210

206211
fn end_location_link(&mut self) {
212+
if !self.location_link_enabled {
213+
return;
214+
}
207215
self.make_new_part();
208216
}
209217
}
@@ -260,6 +268,7 @@ fn label_of_ty(
260268
db: sema.db,
261269
last_part: String::new(),
262270
location: None,
271+
location_link_enabled: config.location_links,
263272
result: InlayHintLabel::default(),
264273
};
265274
rec(sema, &famous_defs, config.max_length, ty, &mut label_builder);
@@ -416,6 +425,7 @@ mod tests {
416425
use super::ClosureReturnTypeHints;
417426

418427
pub(super) const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
428+
location_links: false,
419429
render_colons: false,
420430
type_hints: false,
421431
parameter_hints: false,
@@ -430,14 +440,16 @@ mod tests {
430440
max_length: None,
431441
closing_brace_hints_min_lines: None,
432442
};
443+
pub(super) const DISABLED_CONFIG_WITH_LINKS: InlayHintsConfig =
444+
InlayHintsConfig { location_links: true, ..DISABLED_CONFIG };
433445
pub(super) const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig {
434446
type_hints: true,
435447
parameter_hints: true,
436448
chaining_hints: true,
437449
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
438450
binding_mode_hints: true,
439451
lifetime_elision_hints: LifetimeElisionHints::Always,
440-
..DISABLED_CONFIG
452+
..DISABLED_CONFIG_WITH_LINKS
441453
};
442454

443455
#[track_caller]

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ mod tests {
194194
use crate::{fixture, inlay_hints::InlayHintsConfig};
195195

196196
use crate::inlay_hints::tests::{
197-
check, check_expect, check_with_config, DISABLED_CONFIG, TEST_CONFIG,
197+
check, check_expect, check_with_config, DISABLED_CONFIG, DISABLED_CONFIG_WITH_LINKS,
198+
TEST_CONFIG,
198199
};
199200
use crate::ClosureReturnTypeHints;
200201

@@ -290,7 +291,7 @@ fn main() {
290291
fn iterator_hint_regression_issue_12674() {
291292
// Ensure we don't crash while solving the projection type of iterators.
292293
check_expect(
293-
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
294+
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG_WITH_LINKS },
294295
r#"
295296
//- minicore: iterators
296297
struct S<T>(T);

crates/ide/src/inlay_hints/chaining.rs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ mod tests {
7474
use expect_test::expect;
7575

7676
use crate::{
77-
inlay_hints::tests::{check_expect, check_with_config, DISABLED_CONFIG, TEST_CONFIG},
77+
inlay_hints::tests::{
78+
check_expect, check_with_config, DISABLED_CONFIG, DISABLED_CONFIG_WITH_LINKS,
79+
TEST_CONFIG,
80+
},
7881
InlayHintsConfig,
7982
};
8083

@@ -86,7 +89,11 @@ mod tests {
8689
#[test]
8790
fn chaining_hints_ignore_comments() {
8891
check_expect(
89-
InlayHintsConfig { type_hints: false, chaining_hints: true, ..DISABLED_CONFIG },
92+
InlayHintsConfig {
93+
type_hints: false,
94+
chaining_hints: true,
95+
..DISABLED_CONFIG_WITH_LINKS
96+
},
9097
r#"
9198
struct A(B);
9299
impl A { fn into_b(self) -> B { self.0 } }
@@ -179,10 +186,69 @@ fn main() {
179186
}
180187

181188
#[test]
182-
fn struct_access_chaining_hints() {
189+
fn disabled_location_links() {
183190
check_expect(
184191
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
185192
r#"
193+
struct A { pub b: B }
194+
struct B { pub c: C }
195+
struct C(pub bool);
196+
struct D;
197+
198+
impl D {
199+
fn foo(&self) -> i32 { 42 }
200+
}
201+
202+
fn main() {
203+
let x = A { b: B { c: C(true) } }
204+
.b
205+
.c
206+
.0;
207+
let x = D
208+
.foo();
209+
}"#,
210+
expect![[r#"
211+
[
212+
InlayHint {
213+
range: 143..190,
214+
kind: ChainingHint,
215+
label: [
216+
"C",
217+
],
218+
tooltip: Some(
219+
HoverRanged(
220+
FileId(
221+
0,
222+
),
223+
143..190,
224+
),
225+
),
226+
},
227+
InlayHint {
228+
range: 143..179,
229+
kind: ChainingHint,
230+
label: [
231+
"B",
232+
],
233+
tooltip: Some(
234+
HoverRanged(
235+
FileId(
236+
0,
237+
),
238+
143..179,
239+
),
240+
),
241+
},
242+
]
243+
"#]],
244+
);
245+
}
246+
247+
#[test]
248+
fn struct_access_chaining_hints() {
249+
check_expect(
250+
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG_WITH_LINKS },
251+
r#"
186252
struct A { pub b: B }
187253
struct B { pub c: C }
188254
struct C(pub bool);
@@ -264,7 +330,7 @@ fn main() {
264330
#[test]
265331
fn generic_chaining_hints() {
266332
check_expect(
267-
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
333+
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG_WITH_LINKS },
268334
r#"
269335
struct A<T>(T);
270336
struct B<T>(T);
@@ -372,7 +438,7 @@ fn main() {
372438
#[test]
373439
fn shorten_iterator_chaining_hints() {
374440
check_expect(
375-
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
441+
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG_WITH_LINKS },
376442
r#"
377443
//- minicore: iterators
378444
use core::iter;

crates/ide/src/inlay_hints/closing_brace.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ pub(super) fn hints(
109109
return None;
110110
}
111111

112-
let linked_location = name_range.map(|range| FileRange { file_id, range });
112+
let linked_location = config
113+
.location_links
114+
.then(|| name_range.map(|range| FileRange { file_id, range }))
115+
.flatten();
113116
acc.push(InlayHint {
114117
range: closing_token.text_range(),
115118
kind: InlayKind::ClosingBraceHint,

crates/ide/src/static_index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl StaticIndex<'_> {
106106
.analysis
107107
.inlay_hints(
108108
&InlayHintsConfig {
109+
location_links: true,
109110
render_colons: true,
110111
type_hints: true,
111112
parameter_hints: true,

crates/rust-analyzer/src/bin/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ fn run_server() -> Result<()> {
183183
}
184184
}
185185

186+
config.client_specific_adjustments(&initialize_params.client_info);
187+
186188
let server_capabilities = rust_analyzer::server_capabilities(&config);
187189

188190
let initialize_result = lsp_types::InitializeResult {

crates/rust-analyzer/src/config.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ide_db::{
2020
SnippetCap,
2121
};
2222
use itertools::Itertools;
23-
use lsp_types::{ClientCapabilities, MarkupKind};
23+
use lsp_types::{ClientCapabilities, ClientInfo, MarkupKind};
2424
use project_model::{
2525
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustcSource,
2626
UnsetTestCrates,
@@ -333,6 +333,8 @@ config_data! {
333333
inlayHints_lifetimeElisionHints_enable: LifetimeElisionDef = "\"never\"",
334334
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
335335
inlayHints_lifetimeElisionHints_useParameterNames: bool = "false",
336+
/// Whether to use location links for parts of type mentioned in inlay hints.
337+
inlayHints_locationLinks: bool = "true",
336338
/// Maximum length for inlay hints. Set to null to have an unlimited length.
337339
inlayHints_maxLength: Option<usize> = "25",
338340
/// Whether to show function parameter name inlay hints at the call
@@ -714,6 +716,19 @@ impl Config {
714716
}
715717
}
716718

719+
pub fn client_specific_adjustments(&mut self, client_info: &Option<ClientInfo>) {
720+
// FIXME: remove this when we drop support for vscode 1.65 and below
721+
if let Some(client) = client_info {
722+
if client.name.contains("Code") || client.name.contains("Codium") {
723+
if let Some(version) = &client.version {
724+
if version.as_str() < "1.76" {
725+
self.data.inlayHints_locationLinks = false;
726+
}
727+
}
728+
}
729+
}
730+
}
731+
717732
pub fn update(&mut self, mut json: serde_json::Value) -> Result<(), ConfigUpdateError> {
718733
tracing::info!("updating config from JSON: {:#}", json);
719734
if json.is_null() || json.as_object().map_or(false, |it| it.is_empty()) {
@@ -1196,6 +1211,7 @@ impl Config {
11961211

11971212
pub fn inlay_hints(&self) -> InlayHintsConfig {
11981213
InlayHintsConfig {
1214+
location_links: self.data.inlayHints_locationLinks,
11991215
render_colons: self.data.inlayHints_renderColons,
12001216
type_hints: self.data.inlayHints_typeHints_enable,
12011217
parameter_hints: self.data.inlayHints_parameterHints_enable,

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ Whether to show inlay type hints for elided lifetimes in function signatures.
469469
--
470470
Whether to prefer using parameter names as the name for elided lifetime hints if possible.
471471
--
472+
[[rust-analyzer.inlayHints.locationLinks]]rust-analyzer.inlayHints.locationLinks (default: `true`)::
473+
+
474+
--
475+
Whether to use location links for parts of type mentioned in inlay hints.
476+
--
472477
[[rust-analyzer.inlayHints.maxLength]]rust-analyzer.inlayHints.maxLength (default: `25`)::
473478
+
474479
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@
995995
"default": false,
996996
"type": "boolean"
997997
},
998+
"rust-analyzer.inlayHints.locationLinks": {
999+
"markdownDescription": "Whether to use location links for parts of type mentioned in inlay hints.",
1000+
"default": true,
1001+
"type": "boolean"
1002+
},
9981003
"rust-analyzer.inlayHints.maxLength": {
9991004
"markdownDescription": "Maximum length for inlay hints. Set to null to have an unlimited length.",
10001005
"default": 25,

0 commit comments

Comments
 (0)