Skip to content

Commit 0427a23

Browse files
committed
Auto merge of #15635 - SomeoneToIgnore:fix-vscode-edits, r=Veykril
Do not resolve inlayHint.textEdit for VSCode client Closes rust-lang/rust-analyzer#15604 VSCode behaves strangely, allowing to navigate into label location, but not allowing to apply hint's text edit, after hint is resolved. See microsoft/vscode#193124 for details. For now, stub hint resolution for VSCode specifically.
2 parents 22b18b9 + f9fac02 commit 0427a23

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ fn run_server() -> anyhow::Result<()> {
190190
}
191191
};
192192

193+
let mut is_visual_studio_code = false;
194+
if let Some(client_info) = client_info {
195+
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
196+
is_visual_studio_code = client_info.name == "Visual Studio Code";
197+
}
198+
193199
let workspace_roots = workspace_folders
194200
.map(|workspaces| {
195201
workspaces
@@ -201,7 +207,7 @@ fn run_server() -> anyhow::Result<()> {
201207
})
202208
.filter(|workspaces| !workspaces.is_empty())
203209
.unwrap_or_else(|| vec![root_path.clone()]);
204-
let mut config = Config::new(root_path, capabilities, workspace_roots);
210+
let mut config = Config::new(root_path, capabilities, workspace_roots, is_visual_studio_code);
205211
if let Some(json) = initialization_options {
206212
if let Err(e) = config.update(json) {
207213
use lsp_types::{
@@ -231,10 +237,6 @@ fn run_server() -> anyhow::Result<()> {
231237

232238
connection.initialize_finish(initialize_id, initialize_result)?;
233239

234-
if let Some(client_info) = client_info {
235-
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
236-
}
237-
238240
if !config.has_linked_projects() && config.detached_files().is_empty() {
239241
config.rediscover_workspaces();
240242
}

crates/rust-analyzer/src/config.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ pub struct Config {
565565
data: ConfigData,
566566
detached_files: Vec<AbsPathBuf>,
567567
snippets: Vec<Snippet>,
568+
is_visual_studio_code: bool,
568569
}
569570

570571
type ParallelCachePrimingNumThreads = u8;
@@ -760,6 +761,7 @@ impl Config {
760761
root_path: AbsPathBuf,
761762
caps: ClientCapabilities,
762763
workspace_roots: Vec<AbsPathBuf>,
764+
is_visual_studio_code: bool,
763765
) -> Self {
764766
Config {
765767
caps,
@@ -769,6 +771,7 @@ impl Config {
769771
root_path,
770772
snippets: Default::default(),
771773
workspace_roots,
774+
is_visual_studio_code,
772775
}
773776
}
774777

@@ -1667,6 +1670,12 @@ impl Config {
16671670
pub fn typing_autoclose_angle(&self) -> bool {
16681671
self.data.typing_autoClosingAngleBrackets_enable
16691672
}
1673+
1674+
// FIXME: VSCode seems to work wrong sometimes, see https://github.com/microsoft/vscode/issues/193124
1675+
// hence, distinguish it for now.
1676+
pub fn is_visual_studio_code(&self) -> bool {
1677+
self.is_visual_studio_code
1678+
}
16701679
}
16711680
// Deserialization definitions
16721681

@@ -2555,8 +2564,12 @@ mod tests {
25552564

25562565
#[test]
25572566
fn proc_macro_srv_null() {
2558-
let mut config =
2559-
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
2567+
let mut config = Config::new(
2568+
AbsPathBuf::try_from(project_root()).unwrap(),
2569+
Default::default(),
2570+
vec![],
2571+
false,
2572+
);
25602573
config
25612574
.update(serde_json::json!({
25622575
"procMacro_server": null,
@@ -2567,8 +2580,12 @@ mod tests {
25672580

25682581
#[test]
25692582
fn proc_macro_srv_abs() {
2570-
let mut config =
2571-
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
2583+
let mut config = Config::new(
2584+
AbsPathBuf::try_from(project_root()).unwrap(),
2585+
Default::default(),
2586+
vec![],
2587+
false,
2588+
);
25722589
config
25732590
.update(serde_json::json!({
25742591
"procMacro": {"server": project_root().display().to_string()}
@@ -2579,8 +2596,12 @@ mod tests {
25792596

25802597
#[test]
25812598
fn proc_macro_srv_rel() {
2582-
let mut config =
2583-
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
2599+
let mut config = Config::new(
2600+
AbsPathBuf::try_from(project_root()).unwrap(),
2601+
Default::default(),
2602+
vec![],
2603+
false,
2604+
);
25842605
config
25852606
.update(serde_json::json!({
25862607
"procMacro": {"server": "./server"}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,12 @@ mod tests {
538538
let (sender, _) = crossbeam_channel::unbounded();
539539
let state = GlobalState::new(
540540
sender,
541-
Config::new(workspace_root.to_path_buf(), ClientCapabilities::default(), Vec::new()),
541+
Config::new(
542+
workspace_root.to_path_buf(),
543+
ClientCapabilities::default(),
544+
Vec::new(),
545+
false,
546+
),
542547
);
543548
let snap = state.snapshot();
544549
let mut actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root, &snap);

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,17 @@ pub(crate) fn inlay_hint(
443443
file_id: FileId,
444444
inlay_hint: InlayHint,
445445
) -> Cancellable<lsp_types::InlayHint> {
446+
let is_visual_studio_code = snap.config.is_visual_studio_code();
446447
let needs_resolve = inlay_hint.needs_resolve;
447448
let (label, tooltip, mut something_to_resolve) =
448449
inlay_hint_label(snap, fields_to_resolve, needs_resolve, inlay_hint.label)?;
449-
let text_edits = if needs_resolve && fields_to_resolve.resolve_text_edits {
450-
something_to_resolve |= inlay_hint.text_edit.is_some();
451-
None
452-
} else {
453-
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
454-
};
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+
};
455457
let data = if needs_resolve && something_to_resolve {
456458
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.0 }).unwrap())
457459
} else {

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

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl Project<'_> {
150150
..Default::default()
151151
},
152152
roots,
153+
false,
153154
);
154155
config.update(self.config).expect("invalid config");
155156
config.rediscover_workspaces();

0 commit comments

Comments
 (0)