Skip to content

internal: Stop eagerly resolving inlay hint text edits for VSCode #16473

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

Merged
merged 2 commits into from
Mar 11, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ triomphe.workspace = true
nohash-hasher.workspace = true
always-assert = "0.2.0"
walkdir = "2.3.2"
semver.workspace = true

cfg.workspace = true
flycheck.workspace = true
Expand Down
18 changes: 14 additions & 4 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{env, fs, path::PathBuf, process::ExitCode, sync::Arc};
use anyhow::Context;
use lsp_server::Connection;
use rust_analyzer::{cli::flags, config::Config, from_json};
use semver::Version;
use tracing_subscriber::fmt::writer::BoxMakeWriter;
use vfs::AbsPathBuf;

Expand Down Expand Up @@ -193,10 +194,18 @@ fn run_server() -> anyhow::Result<()> {
}
};

let mut is_visual_studio_code = false;
let mut visual_studio_code_version = None;
if let Some(client_info) = client_info {
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
is_visual_studio_code = client_info.name.starts_with("Visual Studio Code");
tracing::info!(
"Client '{}' {}",
client_info.name,
client_info.version.as_deref().unwrap_or_default()
);
visual_studio_code_version = client_info
.name
.starts_with("Visual Studio Code")
.then(|| client_info.version.as_deref().map(Version::parse).and_then(Result::ok))
.flatten();
}

let workspace_roots = workspace_folders
Expand All @@ -210,7 +219,8 @@ fn run_server() -> anyhow::Result<()> {
})
.filter(|workspaces| !workspaces.is_empty())
.unwrap_or_else(|| vec![root_path.clone()]);
let mut config = Config::new(root_path, capabilities, workspace_roots, is_visual_studio_code);
let mut config =
Config::new(root_path, capabilities, workspace_roots, visual_studio_code_version);
if let Some(json) = initialization_options {
if let Err(e) = config.update(json) {
use lsp_types::{
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/cli/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl flags::Scip {
let mut config = crate::config::Config::new(
root.clone(),
lsp_types::ClientCapabilities::default(),
/* workspace_roots = */ vec![],
/* is_visual_studio_code = */ false,
vec![],
None,
);

if let Some(p) = self.config_path {
Expand Down
27 changes: 14 additions & 13 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use project_model::{
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustLibSource,
};
use rustc_hash::{FxHashMap, FxHashSet};
use semver::Version;
use serde::{de::DeserializeOwned, Deserialize};
use stdx::format_to_acc;
use vfs::{AbsPath, AbsPathBuf};
Expand Down Expand Up @@ -624,7 +625,7 @@ pub struct Config {
data: ConfigData,
detached_files: Vec<AbsPathBuf>,
snippets: Vec<Snippet>,
is_visual_studio_code: bool,
visual_studio_code_version: Option<Version>,
}

type ParallelCachePrimingNumThreads = u8;
Expand Down Expand Up @@ -823,7 +824,7 @@ impl Config {
root_path: AbsPathBuf,
caps: ClientCapabilities,
workspace_roots: Vec<AbsPathBuf>,
is_visual_studio_code: bool,
visual_studio_code_version: Option<Version>,
) -> Self {
Config {
caps,
Expand All @@ -833,7 +834,7 @@ impl Config {
root_path,
snippets: Default::default(),
workspace_roots,
is_visual_studio_code,
visual_studio_code_version,
}
}

Expand Down Expand Up @@ -1778,10 +1779,10 @@ impl Config {
self.data.typing_autoClosingAngleBrackets_enable
}

// FIXME: VSCode seems to work wrong sometimes, see https://github.com/microsoft/vscode/issues/193124
// hence, distinguish it for now.
pub fn is_visual_studio_code(&self) -> bool {
self.is_visual_studio_code
// VSCode is our reference implementation, so we allow ourselves to work around issues by
// special casing certain versions
pub fn visual_studio_code_version(&self) -> Option<&Version> {
self.visual_studio_code_version.as_ref()
}
}
// Deserialization definitions
Expand Down Expand Up @@ -2694,7 +2695,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand All @@ -2710,7 +2711,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand All @@ -2726,7 +2727,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand All @@ -2745,7 +2746,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand All @@ -2764,7 +2765,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand All @@ -2783,7 +2784,7 @@ mod tests {
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
None,
);
config
.update(serde_json::json!({
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/diagnostics/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ mod tests {
workspace_root.to_path_buf(),
ClientCapabilities::default(),
Vec::new(),
false,
None,
),
);
let snap = state.snapshot();
Expand Down
24 changes: 16 additions & 8 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use ide::{
};
use ide_db::rust_doc::format_docs;
use itertools::Itertools;
use semver::VersionReq;
use serde_json::to_value;
use vfs::AbsPath;

Expand Down Expand Up @@ -443,17 +444,24 @@ pub(crate) fn inlay_hint(
file_id: FileId,
inlay_hint: InlayHint,
) -> Cancellable<lsp_types::InlayHint> {
let is_visual_studio_code = snap.config.is_visual_studio_code();
let needs_resolve = inlay_hint.needs_resolve;
let (label, tooltip, mut something_to_resolve) =
inlay_hint_label(snap, fields_to_resolve, needs_resolve, inlay_hint.label)?;
let text_edits =
if !is_visual_studio_code && needs_resolve && fields_to_resolve.resolve_text_edits {
something_to_resolve |= inlay_hint.text_edit.is_some();
None
} else {
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
};

let text_edits = if snap
.config
.visual_studio_code_version()
// https://github.com/microsoft/vscode/issues/193124
.map_or(true, |version| VersionReq::parse(">=1.86.0").unwrap().matches(version))
&& needs_resolve
&& fields_to_resolve.resolve_text_edits
{
something_to_resolve |= inlay_hint.text_edit.is_some();
None
} else {
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
};

let data = if needs_resolve && something_to_resolve {
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.index() }).unwrap())
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/tests/slow-tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Project<'_> {
..Default::default()
},
roots,
false,
None,
);
config.update(self.config).expect("invalid config");
config.rediscover_workspaces();
Expand Down