Skip to content

Commit

Permalink
Do not resolve inlayHint.textEdit for VSCode client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SomeoneToIgnore committed Sep 19, 2023
1 parent 22b18b9 commit 238de1d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
12 changes: 7 additions & 5 deletions crates/rust-analyzer/src/bin/main.rs
Expand Up @@ -190,6 +190,12 @@ fn run_server() -> anyhow::Result<()> {
}
};

let mut is_visual_studio = false;
if let Some(client_info) = client_info {
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
is_visual_studio = client_info.name == "Visual Studio Code";
}

let workspace_roots = workspace_folders
.map(|workspaces| {
workspaces
Expand All @@ -201,7 +207,7 @@ 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);
let mut config = Config::new(root_path, capabilities, workspace_roots, is_visual_studio);
if let Some(json) = initialization_options {
if let Err(e) = config.update(json) {
use lsp_types::{
Expand Down Expand Up @@ -231,10 +237,6 @@ fn run_server() -> anyhow::Result<()> {

connection.initialize_finish(initialize_id, initialize_result)?;

if let Some(client_info) = client_info {
tracing::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default());
}

if !config.has_linked_projects() && config.detached_files().is_empty() {
config.rediscover_workspaces();
}
Expand Down
34 changes: 28 additions & 6 deletions crates/rust-analyzer/src/config.rs
Expand Up @@ -565,6 +565,7 @@ pub struct Config {
data: ConfigData,
detached_files: Vec<AbsPathBuf>,
snippets: Vec<Snippet>,
is_visual_studio: bool,
}

type ParallelCachePrimingNumThreads = u8;
Expand Down Expand Up @@ -760,6 +761,7 @@ impl Config {
root_path: AbsPathBuf,
caps: ClientCapabilities,
workspace_roots: Vec<AbsPathBuf>,
is_visual_studio: bool,
) -> Self {
Config {
caps,
Expand All @@ -769,6 +771,7 @@ impl Config {
root_path,
snippets: Default::default(),
workspace_roots,
is_visual_studio,
}
}

Expand Down Expand Up @@ -1336,6 +1339,7 @@ impl Config {
}

pub fn inlay_hints(&self) -> InlayHintsConfig {
dbg!("TODO kb");
let client_capability_fields = self
.caps
.text_document
Expand Down Expand Up @@ -1667,6 +1671,12 @@ impl Config {
pub fn typing_autoclose_angle(&self) -> bool {
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(&self) -> bool {
self.is_visual_studio
}
}
// Deserialization definitions

Expand Down Expand Up @@ -2555,8 +2565,12 @@ mod tests {

#[test]
fn proc_macro_srv_null() {
let mut config =
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
let mut config = Config::new(
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
);
config
.update(serde_json::json!({
"procMacro_server": null,
Expand All @@ -2567,8 +2581,12 @@ mod tests {

#[test]
fn proc_macro_srv_abs() {
let mut config =
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
let mut config = Config::new(
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
);
config
.update(serde_json::json!({
"procMacro": {"server": project_root().display().to_string()}
Expand All @@ -2579,8 +2597,12 @@ mod tests {

#[test]
fn proc_macro_srv_rel() {
let mut config =
Config::new(AbsPathBuf::try_from(project_root()).unwrap(), Default::default(), vec![]);
let mut config = Config::new(
AbsPathBuf::try_from(project_root()).unwrap(),
Default::default(),
vec![],
false,
);
config
.update(serde_json::json!({
"procMacro": {"server": "./server"}
Expand Down
7 changes: 6 additions & 1 deletion crates/rust-analyzer/src/diagnostics/to_proto.rs
Expand Up @@ -538,7 +538,12 @@ mod tests {
let (sender, _) = crossbeam_channel::unbounded();
let state = GlobalState::new(
sender,
Config::new(workspace_root.to_path_buf(), ClientCapabilities::default(), Vec::new()),
Config::new(
workspace_root.to_path_buf(),
ClientCapabilities::default(),
Vec::new(),
false,
),
);
let snap = state.snapshot();
let mut actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root, &snap);
Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/lsp/to_proto.rs
Expand Up @@ -443,10 +443,11 @@ pub(crate) fn inlay_hint(
file_id: FileId,
inlay_hint: InlayHint,
) -> Cancellable<lsp_types::InlayHint> {
let is_visual_studio = snap.config.is_visual_studio();
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 needs_resolve && fields_to_resolve.resolve_text_edits {
let text_edits = if !is_visual_studio && needs_resolve && fields_to_resolve.resolve_text_edits {
something_to_resolve |= inlay_hint.text_edit.is_some();
None
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/tests/slow-tests/support.rs
Expand Up @@ -150,6 +150,7 @@ impl Project<'_> {
..Default::default()
},
roots,
false,
);
config.update(self.config).expect("invalid config");
config.rediscover_workspaces();
Expand Down

0 comments on commit 238de1d

Please sign in to comment.