Skip to content

Commit a0dd822

Browse files
committed
Auto merge of rust-lang#16473 - SomeoneToIgnore:kb/remove-resolve-stub, r=Veykril
Stop eagerly resolving inlay hint text edits for VSCode Send less json over the wire. After microsoft/vscode#193124 was fixed, this change is not needed anymore. VSCode 1.86.0 now supports double click for unresolved hint data too.
2 parents a58f574 + 0dbaccd commit a0dd822

File tree

8 files changed

+50
-29
lines changed

8 files changed

+50
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ triomphe.workspace = true
4242
nohash-hasher.workspace = true
4343
always-assert = "0.2.0"
4444
walkdir = "2.3.2"
45+
semver.workspace = true
4546

4647
cfg.workspace = true
4748
flycheck.workspace = true

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::{env, fs, path::PathBuf, process::ExitCode, sync::Arc};
1616
use anyhow::Context;
1717
use lsp_server::Connection;
1818
use rust_analyzer::{cli::flags, config::Config, from_json};
19+
use semver::Version;
1920
use tracing_subscriber::fmt::writer::BoxMakeWriter;
2021
use vfs::AbsPathBuf;
2122

@@ -193,10 +194,18 @@ fn run_server() -> anyhow::Result<()> {
193194
}
194195
};
195196

196-
let mut is_visual_studio_code = false;
197+
let mut visual_studio_code_version = None;
197198
if let Some(client_info) = client_info {
198-
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
199-
is_visual_studio_code = client_info.name.starts_with("Visual Studio Code");
199+
tracing::info!(
200+
"Client '{}' {}",
201+
client_info.name,
202+
client_info.version.as_deref().unwrap_or_default()
203+
);
204+
visual_studio_code_version = client_info
205+
.name
206+
.starts_with("Visual Studio Code")
207+
.then(|| client_info.version.as_deref().map(Version::parse).and_then(Result::ok))
208+
.flatten();
200209
}
201210

202211
let workspace_roots = workspace_folders
@@ -210,7 +219,8 @@ fn run_server() -> anyhow::Result<()> {
210219
})
211220
.filter(|workspaces| !workspaces.is_empty())
212221
.unwrap_or_else(|| vec![root_path.clone()]);
213-
let mut config = Config::new(root_path, capabilities, workspace_roots, is_visual_studio_code);
222+
let mut config =
223+
Config::new(root_path, capabilities, workspace_roots, visual_studio_code_version);
214224
if let Some(json) = initialization_options {
215225
if let Err(e) = config.update(json) {
216226
use lsp_types::{

crates/rust-analyzer/src/cli/scip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl flags::Scip {
3232
let mut config = crate::config::Config::new(
3333
root.clone(),
3434
lsp_types::ClientCapabilities::default(),
35-
/* workspace_roots = */ vec![],
36-
/* is_visual_studio_code = */ false,
35+
vec![],
36+
None,
3737
);
3838

3939
if let Some(p) = self.config_path {

crates/rust-analyzer/src/config.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use project_model::{
3131
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustLibSource,
3232
};
3333
use rustc_hash::{FxHashMap, FxHashSet};
34+
use semver::Version;
3435
use serde::{de::DeserializeOwned, Deserialize};
3536
use stdx::format_to_acc;
3637
use vfs::{AbsPath, AbsPathBuf};
@@ -624,7 +625,7 @@ pub struct Config {
624625
data: ConfigData,
625626
detached_files: Vec<AbsPathBuf>,
626627
snippets: Vec<Snippet>,
627-
is_visual_studio_code: bool,
628+
visual_studio_code_version: Option<Version>,
628629
}
629630

630631
type ParallelCachePrimingNumThreads = u8;
@@ -823,7 +824,7 @@ impl Config {
823824
root_path: AbsPathBuf,
824825
caps: ClientCapabilities,
825826
workspace_roots: Vec<AbsPathBuf>,
826-
is_visual_studio_code: bool,
827+
visual_studio_code_version: Option<Version>,
827828
) -> Self {
828829
Config {
829830
caps,
@@ -833,7 +834,7 @@ impl Config {
833834
root_path,
834835
snippets: Default::default(),
835836
workspace_roots,
836-
is_visual_studio_code,
837+
visual_studio_code_version,
837838
}
838839
}
839840

@@ -1778,10 +1779,10 @@ impl Config {
17781779
self.data.typing_autoClosingAngleBrackets_enable
17791780
}
17801781

1781-
// FIXME: VSCode seems to work wrong sometimes, see https://github.com/microsoft/vscode/issues/193124
1782-
// hence, distinguish it for now.
1783-
pub fn is_visual_studio_code(&self) -> bool {
1784-
self.is_visual_studio_code
1782+
// VSCode is our reference implementation, so we allow ourselves to work around issues by
1783+
// special casing certain versions
1784+
pub fn visual_studio_code_version(&self) -> Option<&Version> {
1785+
self.visual_studio_code_version.as_ref()
17851786
}
17861787
}
17871788
// Deserialization definitions
@@ -2694,7 +2695,7 @@ mod tests {
26942695
AbsPathBuf::try_from(project_root()).unwrap(),
26952696
Default::default(),
26962697
vec![],
2697-
false,
2698+
None,
26982699
);
26992700
config
27002701
.update(serde_json::json!({
@@ -2710,7 +2711,7 @@ mod tests {
27102711
AbsPathBuf::try_from(project_root()).unwrap(),
27112712
Default::default(),
27122713
vec![],
2713-
false,
2714+
None,
27142715
);
27152716
config
27162717
.update(serde_json::json!({
@@ -2726,7 +2727,7 @@ mod tests {
27262727
AbsPathBuf::try_from(project_root()).unwrap(),
27272728
Default::default(),
27282729
vec![],
2729-
false,
2730+
None,
27302731
);
27312732
config
27322733
.update(serde_json::json!({
@@ -2745,7 +2746,7 @@ mod tests {
27452746
AbsPathBuf::try_from(project_root()).unwrap(),
27462747
Default::default(),
27472748
vec![],
2748-
false,
2749+
None,
27492750
);
27502751
config
27512752
.update(serde_json::json!({
@@ -2764,7 +2765,7 @@ mod tests {
27642765
AbsPathBuf::try_from(project_root()).unwrap(),
27652766
Default::default(),
27662767
vec![],
2767-
false,
2768+
None,
27682769
);
27692770
config
27702771
.update(serde_json::json!({
@@ -2783,7 +2784,7 @@ mod tests {
27832784
AbsPathBuf::try_from(project_root()).unwrap(),
27842785
Default::default(),
27852786
vec![],
2786-
false,
2787+
None,
27872788
);
27882789
config
27892790
.update(serde_json::json!({

crates/rust-analyzer/src/diagnostics/to_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ mod tests {
542542
workspace_root.to_path_buf(),
543543
ClientCapabilities::default(),
544544
Vec::new(),
545-
false,
545+
None,
546546
),
547547
);
548548
let snap = state.snapshot();

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ide::{
1515
};
1616
use ide_db::rust_doc::format_docs;
1717
use itertools::Itertools;
18+
use semver::VersionReq;
1819
use serde_json::to_value;
1920
use vfs::AbsPath;
2021

@@ -443,17 +444,24 @@ pub(crate) fn inlay_hint(
443444
file_id: FileId,
444445
inlay_hint: InlayHint,
445446
) -> Cancellable<lsp_types::InlayHint> {
446-
let is_visual_studio_code = snap.config.is_visual_studio_code();
447447
let needs_resolve = inlay_hint.needs_resolve;
448448
let (label, tooltip, mut something_to_resolve) =
449449
inlay_hint_label(snap, fields_to_resolve, needs_resolve, inlay_hint.label)?;
450-
let text_edits =
451-
if !is_visual_studio_code && needs_resolve && fields_to_resolve.resolve_text_edits {
452-
something_to_resolve |= inlay_hint.text_edit.is_some();
453-
None
454-
} else {
455-
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
456-
};
450+
451+
let text_edits = if snap
452+
.config
453+
.visual_studio_code_version()
454+
// https://github.com/microsoft/vscode/issues/193124
455+
.map_or(true, |version| VersionReq::parse(">=1.86.0").unwrap().matches(version))
456+
&& needs_resolve
457+
&& fields_to_resolve.resolve_text_edits
458+
{
459+
something_to_resolve |= inlay_hint.text_edit.is_some();
460+
None
461+
} else {
462+
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
463+
};
464+
457465
let data = if needs_resolve && something_to_resolve {
458466
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.index() }).unwrap())
459467
} else {

crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Project<'_> {
171171
..Default::default()
172172
},
173173
roots,
174-
false,
174+
None,
175175
);
176176
config.update(self.config).expect("invalid config");
177177
config.rediscover_workspaces();

0 commit comments

Comments
 (0)