diff options
author | Lukas Wirth <[email protected]> | 2021-04-19 12:20:37 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-04-19 12:32:52 +0100 |
commit | 2f62c0117a1d59a531f9c84fbdb2f70ff87d22e0 (patch) | |
tree | b5d6941cad6a68b786dd8993bfb8a9bb8997aa80 /crates/ide/src/syntax_highlighting | |
parent | 8a959497b1fab35294d8ccfa5e51c80a3551a224 (diff) |
Check for rust doc code attributes like rustdoc does
Diffstat (limited to 'crates/ide/src/syntax_highlighting')
-rw-r--r-- | crates/ide/src/syntax_highlighting/inject.rs | 24 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 2 |
2 files changed, 3 insertions, 23 deletions
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 6b1037870..bc221d599 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs | |||
@@ -4,7 +4,7 @@ use std::mem; | |||
4 | 4 | ||
5 | use either::Either; | 5 | use either::Either; |
6 | use hir::{InFile, Semantics}; | 6 | use hir::{InFile, Semantics}; |
7 | use ide_db::{call_info::ActiveParameter, SymbolKind}; | 7 | use ide_db::{call_info::ActiveParameter, helpers::rust_doc::is_rust_fence, SymbolKind}; |
8 | use syntax::{ | 8 | use syntax::{ |
9 | ast::{self, AstNode}, | 9 | ast::{self, AstNode}, |
10 | AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize, | 10 | AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize, |
@@ -78,26 +78,6 @@ pub(super) fn ra_fixture( | |||
78 | } | 78 | } |
79 | 79 | ||
80 | const RUSTDOC_FENCE: &'static str = "```"; | 80 | const RUSTDOC_FENCE: &'static str = "```"; |
81 | const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ | ||
82 | "", | ||
83 | "rust", | ||
84 | "should_panic", | ||
85 | "ignore", | ||
86 | "no_run", | ||
87 | "compile_fail", | ||
88 | "allow_fail", | ||
89 | "test_harness", | ||
90 | "edition2015", | ||
91 | "edition2018", | ||
92 | "edition2021", | ||
93 | ]; | ||
94 | |||
95 | fn is_rustdoc_fence_token(token: &str) -> bool { | ||
96 | if RUSTDOC_FENCE_TOKENS.contains(&token) { | ||
97 | return true; | ||
98 | } | ||
99 | token.starts_with('E') && token.len() == 5 && token[1..].parse::<u32>().is_ok() | ||
100 | } | ||
101 | 81 | ||
102 | /// Injection of syntax highlighting of doctests. | 82 | /// Injection of syntax highlighting of doctests. |
103 | pub(super) fn doc_comment( | 83 | pub(super) fn doc_comment( |
@@ -183,7 +163,7 @@ pub(super) fn doc_comment( | |||
183 | is_codeblock = !is_codeblock; | 163 | is_codeblock = !is_codeblock; |
184 | // Check whether code is rust by inspecting fence guards | 164 | // Check whether code is rust by inspecting fence guards |
185 | let guards = &line[idx + RUSTDOC_FENCE.len()..]; | 165 | let guards = &line[idx + RUSTDOC_FENCE.len()..]; |
186 | let is_rust = guards.split(',').any(|sub| is_rustdoc_fence_token(sub.trim())); | 166 | let is_rust = is_rust_fence(guards); |
187 | is_doctest = is_codeblock && is_rust; | 167 | is_doctest = is_codeblock && is_rust; |
188 | continue; | 168 | continue; |
189 | } | 169 | } |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 933cfa6f3..17cc6334b 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -307,7 +307,7 @@ fn benchmark_syntax_highlighting_parser() { | |||
307 | .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function)) | 307 | .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function)) |
308 | .count() | 308 | .count() |
309 | }; | 309 | }; |
310 | assert_eq!(hash, 1629); | 310 | assert_eq!(hash, 1632); |
311 | } | 311 | } |
312 | 312 | ||
313 | #[test] | 313 | #[test] |