From 41510f437e87e013f2015bed1a964163c6d3f1ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 May 2021 18:37:06 +0300 Subject: minor: adjust config name --- crates/rust-analyzer/src/config.rs | 20 +++++++------- crates/rust-analyzer/src/handlers.rs | 12 ++++----- crates/rust-analyzer/src/semantic_tokens.rs | 4 +-- crates/rust-analyzer/src/to_proto.rs | 8 +++--- crates/rust-analyzer/tests/rust-analyzer/main.rs | 34 ------------------------ docs/dev/style.md | 9 ++++--- docs/user/generated_config.adoc | 18 ++++++------- editors/code/package.json | 10 +++---- 8 files changed, 41 insertions(+), 74 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 905a6ee55..a3866c1ba 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -124,6 +124,13 @@ config_data! { /// These directories will be ignored by rust-analyzer. files_excludeDirs: Vec = "[]", + /// Use semantic tokens for strings. + /// + /// In some editors (e.g. vscode) semantic tokens override other highlighting grammars. + /// By disabling semantic tokens for strings, other grammars can be used to highlight + /// their contents. + highlighting_strings: bool = "true", + /// Whether to show `Debug` action. Only applies when /// `#rust-analyzer.hoverActions.enable#` is set. hoverActions_debug: bool = "true", @@ -208,13 +215,6 @@ config_data! { /// Advanced option, fully override the command rust-analyzer uses for /// formatting. rustfmt_overrideCommand: Option> = "null", - - /// Use semantic tokens for strings. - /// - /// In some editors (e.g. vscode) semantic tokens override other highlighting grammars. - /// By disabling semantic tokens for strings, other grammars can be used to highlight - /// their contents. - semanticStringTokens: bool = "true", } } @@ -388,9 +388,6 @@ impl Config { pub fn line_folding_only(&self) -> bool { try_or!(self.caps.text_document.as_ref()?.folding_range.as_ref()?.line_folding_only?, false) } - pub fn semantic_strings(&self) -> bool { - self.data.semanticStringTokens - } pub fn hierarchical_symbols(&self) -> bool { try_or!( self.caps @@ -665,6 +662,9 @@ impl Config { refs: self.data.lens_enable && self.data.lens_references, } } + pub fn highlighting_strings(&self) -> bool { + self.data.highlighting_strings + } pub fn hover(&self) -> HoverConfig { HoverConfig { implementations: self.data.hoverActions_enable diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 85e45337c..8fe97fd7c 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1394,9 +1394,9 @@ pub(crate) fn handle_semantic_tokens_full( let line_index = snap.file_line_index(file_id)?; let highlights = snap.analysis.highlight(file_id)?; - let semantic_strings = snap.config.semantic_strings(); + let highlight_strings = snap.config.highlighting_strings(); let semantic_tokens = - to_proto::semantic_tokens(&text, &line_index, highlights, semantic_strings); + to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings); // Unconditionally cache the tokens snap.semantic_tokens_cache.lock().insert(params.text_document.uri, semantic_tokens.clone()); @@ -1415,9 +1415,9 @@ pub(crate) fn handle_semantic_tokens_full_delta( let line_index = snap.file_line_index(file_id)?; let highlights = snap.analysis.highlight(file_id)?; - let semantic_strings = snap.config.semantic_strings(); + let highlight_strings = snap.config.highlighting_strings(); let semantic_tokens = - to_proto::semantic_tokens(&text, &line_index, highlights, semantic_strings); + to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings); let mut cache = snap.semantic_tokens_cache.lock(); let cached_tokens = cache.entry(params.text_document.uri).or_default(); @@ -1446,9 +1446,9 @@ pub(crate) fn handle_semantic_tokens_range( let line_index = snap.file_line_index(frange.file_id)?; let highlights = snap.analysis.highlight_range(frange)?; - let semantic_strings = snap.config.semantic_strings(); + let highlight_strings = snap.config.highlighting_strings(); let semantic_tokens = - to_proto::semantic_tokens(&text, &line_index, highlights, semantic_strings); + to_proto::semantic_tokens(&text, &line_index, highlights, highlight_strings); Ok(Some(semantic_tokens.into())) } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index c9d38693e..4fd576adb 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -184,8 +184,8 @@ pub(crate) fn diff_tokens(old: &[SemanticToken], new: &[SemanticToken]) -> Vec u32 { - SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32 +pub(crate) fn type_index(ty: SemanticTokenType) -> u32 { + SUPPORTED_TYPES.iter().position(|it| *it == ty).unwrap() as u32 } #[cfg(test)] diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 6dc9f82ab..9dec46c78 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -381,7 +381,7 @@ pub(crate) fn semantic_tokens( text: &str, line_index: &LineIndex, highlights: Vec, - include_strings: bool, + highlight_strings: bool, ) -> lsp_types::SemanticTokens { let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string(); let mut builder = semantic_tokens::SemanticTokensBuilder::new(id); @@ -390,11 +390,11 @@ pub(crate) fn semantic_tokens( if highlight_range.highlight.is_empty() { continue; } - let (typ, mods) = semantic_token_type_and_modifiers(highlight_range.highlight); - if !include_strings && typ == lsp_types::SemanticTokenType::STRING { + let (ty, mods) = semantic_token_type_and_modifiers(highlight_range.highlight); + if !highlight_strings && ty == lsp_types::SemanticTokenType::STRING { continue; } - let token_index = semantic_tokens::type_index(typ); + let token_index = semantic_tokens::type_index(ty); let modifier_bitset = mods.0; for mut text_range in line_index.index.lines(highlight_range.range) { diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs index 920c43f25..c940ef214 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/main.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs @@ -38,40 +38,6 @@ use crate::{ const PROFILE: &str = ""; // const PROFILE: &'static str = "*@3>100"; -#[test] -fn can_disable_semantic_strings() { - if skip_slow_tests() { - return; - } - - [true, false].iter().for_each(|semantic_strings| { - let server = Project::with_fixture( - r#" -//- /Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- /src/lib.rs -const foo: &'static str = "hi"; -"#, - ) - .with_config(serde_json::json!({ "semanticStringTokens": semantic_strings })) - .server() - .wait_until_workspace_is_loaded(); - - let res = server.send_request::(SemanticTokensRangeParams { - text_document: server.doc_id("src/lib.rs"), - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - range: Range::new(Position::new(0, 26), Position::new(0, 30)), - }); - - let tok_res: SemanticTokens = from_value(res).expect("invalid server response"); - assert!(tok_res.data.len() == *semantic_strings as usize); - }); -} - #[test] fn completes_items_from_standard_library() { if skip_slow_tests() { diff --git a/docs/dev/style.md b/docs/dev/style.md index f22b69768..96dd684b3 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -791,13 +791,14 @@ Many names in rust-analyzer conflict with keywords. We use mangled names instead of `r#ident` syntax: ``` -struct -> strukt crate -> krate -impl -> imp -trait -> trait_ -fn -> func enum -> enum_ +fn -> func +impl -> imp mod -> module +struct -> strukt +trait -> trait_ +type -> ty ``` **Rationale:** consistency. diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index e2d74e164..feba43ff1 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -179,6 +179,15 @@ Controls file watching implementation. -- These directories will be ignored by rust-analyzer. -- +[[rust-analyzer.highlighting.strings]]rust-analyzer.highlighting.strings (default: `true`):: ++ +-- +Use semantic tokens for strings. + +In some editors (e.g. vscode) semantic tokens override other highlighting grammars. +By disabling semantic tokens for strings, other grammars can be used to highlight +their contents. +-- [[rust-analyzer.hoverActions.debug]]rust-analyzer.hoverActions.debug (default: `true`):: + -- @@ -332,12 +341,3 @@ Additional arguments to `rustfmt`. Advanced option, fully override the command rust-analyzer uses for formatting. -- -[[rust-analyzer.semanticStringTokens]]rust-analyzer.semanticStringTokens (default: `true`):: -+ --- -Use semantic tokens for strings. - -In some editors (e.g. vscode) semantic tokens override other highlighting grammars. -By disabling semantic tokens for strings, other grammars can be used to highlight -their contents. --- diff --git a/editors/code/package.json b/editors/code/package.json index 14cffac06..2e67b6775 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -613,6 +613,11 @@ "type": "string" } }, + "rust-analyzer.highlighting.strings": { + "markdownDescription": "Use semantic tokens for strings.\n\nIn some editors (e.g. vscode) semantic tokens override other highlighting grammars.\nBy disabling semantic tokens for strings, other grammars can be used to highlight\ntheir contents.", + "default": true, + "type": "boolean" + }, "rust-analyzer.hoverActions.debug": { "markdownDescription": "Whether to show `Debug` action. Only applies when\n`#rust-analyzer.hoverActions.enable#` is set.", "default": true, @@ -778,11 +783,6 @@ "type": "string" } }, - "rust-analyzer.semanticStringTokens": { - "markdownDescription": "Use semantic tokens for strings.\n\nIn some editors (e.g. vscode) semantic tokens override other highlighting grammars.\nBy disabling semantic tokens for strings, other grammars can be used to highlight\ntheir contents.", - "default": true, - "type": "boolean" - }, "$generated-end": false } }, -- cgit v1.2.3