From de77d7f6615773d40da2a1b71d85bb2c5c0a53ac Mon Sep 17 00:00:00 2001 From: asuto15 Date: Tue, 11 Feb 2025 15:06:56 +0900 Subject: [PATCH 1/5] Fix highlighting for extern crate in doc comments --- crates/ide/src/syntax_highlighting/highlight.rs | 1 + .../src/syntax_highlighting/test_data/highlight_doctest.html | 3 +++ crates/ide/src/syntax_highlighting/tests.rs | 3 +++ 3 files changed, 7 insertions(+) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 479c0b381a4b..c6e11b9cbdf7 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -703,6 +703,7 @@ fn highlight_name_ref_by_syntax( }; match parent.kind() { + EXTERN_CRATE => HlTag::Symbol(SymbolKind::Module).into(), METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, krate, &it, edition)) .unwrap_or_else(|| SymbolKind::Method.into()), diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 5ff96ae2a74f..2f7bc65d14bb 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -50,6 +50,9 @@ //! fn test() {} //! ``` +//! ```rust +//! extern crate Krate; +//! ``` mod outline_module; /// ``` diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index b9520ae2bba2..ad3d47639111 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -722,6 +722,9 @@ fn test_highlight_doc_comment() { //! fn test() {} //! ``` +//! ```rust +//! extern crate Krate; +//! ``` mod outline_module; /// ``` From 535338d8bb269f10903db0848c405b414a1777ec Mon Sep 17 00:00:00 2001 From: asuto15 Date: Thu, 13 Feb 2025 03:58:20 +0900 Subject: [PATCH 2/5] Add modifiers to highlighting for extern crate --- .../ide/src/syntax_highlighting/highlight.rs | 19 ++++++++++++++++++- .../test_data/highlight_doctest.html | 8 +++++++- crates/ide/src/syntax_highlighting/tests.rs | 6 ++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index c6e11b9cbdf7..d580b3001c3e 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -703,7 +703,24 @@ fn highlight_name_ref_by_syntax( }; match parent.kind() { - EXTERN_CRATE => HlTag::Symbol(SymbolKind::Module).into(), + EXTERN_CRATE => { + let mut h: Highlight = HlTag::Symbol(SymbolKind::Module).into(); + let is_crate_root = if let Some(extern_crate) = ast::ExternCrate::cast(parent.clone()) { + if let Some(first_segment) = extern_crate.name_ref() { + first_segment.syntax().text() == name.syntax().text() + } else { + false + } + } else { + false + }; + + if is_crate_root { + h |= HlMod::CrateRoot; + } + + h | HlMod::Library + }, METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, krate, &it, edition)) .unwrap_or_else(|| SymbolKind::Method.into()), diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 2f7bc65d14bb..9a55f7d01b90 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -51,7 +51,13 @@ //! ``` //! ```rust -//! extern crate Krate; +//! extern crate self; +//! extern crate std; +//! extern crate core; +//! extern crate alloc; +//! extern crate proc_macro; +//! extern crate test; +//! extern crate Krate; //! ``` mod outline_module; diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index ad3d47639111..364d0909243f 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -723,6 +723,12 @@ fn test_highlight_doc_comment() { //! ``` //! ```rust +//! extern crate self; +//! extern crate std; +//! extern crate core; +//! extern crate alloc; +//! extern crate proc_macro; +//! extern crate test; //! extern crate Krate; //! ``` mod outline_module; From 22b1977840c912059c66999fcdcf7ab67e093b3d Mon Sep 17 00:00:00 2001 From: asuto15 Date: Thu, 13 Feb 2025 04:33:08 +0900 Subject: [PATCH 3/5] Delete useless comma --- crates/ide/src/syntax_highlighting/highlight.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index d580b3001c3e..8abaccd25368 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -720,7 +720,7 @@ fn highlight_name_ref_by_syntax( } h | HlMod::Library - }, + } METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, krate, &it, edition)) .unwrap_or_else(|| SymbolKind::Method.into()), From 24a778f6c19c67296e5519abe34e0aa90e362ee7 Mon Sep 17 00:00:00 2001 From: asuto15 Date: Sat, 15 Feb 2025 12:06:21 +0900 Subject: [PATCH 4/5] Delete library modifier to highlighting for extern crate --- .../ide/src/syntax_highlighting/highlight.rs | 19 +------------------ .../test_data/highlight_doctest.html | 12 ++++++------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 8abaccd25368..92211e9e3a9b 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -703,24 +703,7 @@ fn highlight_name_ref_by_syntax( }; match parent.kind() { - EXTERN_CRATE => { - let mut h: Highlight = HlTag::Symbol(SymbolKind::Module).into(); - let is_crate_root = if let Some(extern_crate) = ast::ExternCrate::cast(parent.clone()) { - if let Some(first_segment) = extern_crate.name_ref() { - first_segment.syntax().text() == name.syntax().text() - } else { - false - } - } else { - false - }; - - if is_crate_root { - h |= HlMod::CrateRoot; - } - - h | HlMod::Library - } + EXTERN_CRATE => (HlTag::Symbol(SymbolKind::Module) | HlMod::CrateRoot).into(), METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, krate, &it, edition)) .unwrap_or_else(|| SymbolKind::Method.into()), diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 9a55f7d01b90..eb77c14c2a57 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -52,12 +52,12 @@ //! ```rust //! extern crate self; -//! extern crate std; -//! extern crate core; -//! extern crate alloc; -//! extern crate proc_macro; -//! extern crate test; -//! extern crate Krate; +//! extern crate std; +//! extern crate core; +//! extern crate alloc; +//! extern crate proc_macro; +//! extern crate test; +//! extern crate Krate; //! ``` mod outline_module; From c14140ab8b21e126c5ec12f3240b447af6c6bf91 Mon Sep 17 00:00:00 2001 From: asuto15 Date: Sat, 15 Feb 2025 13:18:18 +0900 Subject: [PATCH 5/5] fix: remove unnecessary conversion --- crates/ide/src/syntax_highlighting/highlight.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 92211e9e3a9b..194fde116011 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -703,7 +703,7 @@ fn highlight_name_ref_by_syntax( }; match parent.kind() { - EXTERN_CRATE => (HlTag::Symbol(SymbolKind::Module) | HlMod::CrateRoot).into(), + EXTERN_CRATE => HlTag::Symbol(SymbolKind::Module) | HlMod::CrateRoot, METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent) .and_then(|it| highlight_method_call(sema, krate, &it, edition)) .unwrap_or_else(|| SymbolKind::Method.into()),