From 351bba9bee136d856f987037b6ecffd0642d606f Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 18 Jun 2020 09:37:22 -0400 Subject: Add support for marking doctest items as distinct from normal code, add default tag to all doctest elements --- crates/ra_ide/src/lib.rs | 4 +- crates/ra_ide/src/prime_caches.rs | 2 +- crates/ra_ide/src/snapshots/highlight_doctest.html | 44 +++++++++++----------- crates/ra_ide/src/syntax_highlighting.rs | 18 +++++---- crates/ra_ide/src/syntax_highlighting/html.rs | 2 +- crates/ra_ide/src/syntax_highlighting/injection.rs | 6 ++- crates/ra_ide/src/syntax_highlighting/tags.rs | 3 ++ crates/rust-analyzer/src/semantic_tokens.rs | 1 + crates/rust-analyzer/src/to_proto.rs | 1 + 9 files changed, 47 insertions(+), 34 deletions(-) diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index ecac5134e..22203b4a3 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -443,13 +443,13 @@ impl Analysis { /// Computes syntax highlighting for the given file pub fn highlight(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) + self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, None)) } /// Computes syntax highlighting for the given file range. pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { self.with_db(|db| { - syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) + syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false, None) }) } diff --git a/crates/ra_ide/src/prime_caches.rs b/crates/ra_ide/src/prime_caches.rs index c5ab5a1d8..dbed8a506 100644 --- a/crates/ra_ide/src/prime_caches.rs +++ b/crates/ra_ide/src/prime_caches.rs @@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase}; pub(crate) fn prime_caches(db: &RootDatabase, files: Vec) { for file in files { - let _ = crate::syntax_highlighting::highlight(db, file, None, false); + let _ = crate::syntax_highlighting::highlight(db, file, None, false, None); } } diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index ac546806e..6b3932aff 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html @@ -47,9 +47,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # Examples /// /// ``` - /// # #![allow(unused_mut)] - /// let mut foo: Foo = Foo::new(); - /// ``` + /// # #![allow(unused_mut)] + /// let mut foo: Foo = Foo::new(); + /// ``` pub const fn new() -> Foo { Foo { bar: true } } @@ -59,27 +59,27 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// # Examples /// /// ``` - /// use x::y; - /// - /// let foo = Foo::new(); - /// - /// // calls bar on foo - /// assert!(foo.bar()); - /// - /// let bar = foo.bar || Foo::bar; - /// - /// /* multi-line - /// comment */ - /// - /// let multi_line_string = "Foo - /// bar - /// "; - /// - /// ``` + /// use x::y; + /// + /// let foo = Foo::new(); + /// + /// // calls bar on foo + /// assert!(foo.bar()); + /// + /// let bar = foo.bar || Foo::bar; + /// + /// /* multi-line + /// comment */ + /// + /// let multi_line_string = "Foo + /// bar + /// "; + /// + /// ``` /// /// ```rust,no_run - /// let foobar = Foo::new().bar(); - /// ``` + /// let foobar = Foo::new().bar(); + /// ``` /// /// ```sh /// echo 1 diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 448645bdc..b4dcdba39 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -45,6 +45,7 @@ pub(crate) fn highlight( file_id: FileId, range_to_highlight: Option, syntactic_name_ref_highlighting: bool, + default_tag: Option, ) -> Vec { let _p = profile("highlight"); let sema = Semantics::new(db); @@ -107,6 +108,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, name.syntax().clone().into(), + default_tag, ) { stack.add(HighlightedRange { range: name.syntax().text_range(), @@ -206,6 +208,7 @@ pub(crate) fn highlight( &mut bindings_shadow_count, syntactic_name_ref_highlighting, element_to_highlight.clone(), + default_tag, ) { stack.add(HighlightedRange { range, highlight, binding_hash }); if let Some(string) = @@ -430,13 +433,14 @@ fn highlight_element( bindings_shadow_count: &mut FxHashMap, syntactic_name_ref_highlighting: bool, element: SyntaxElement, + default_tag: Option, ) -> Option<(Highlight, Option)> { let db = sema.db; let mut binding_hash = None; let highlight: Highlight = match element.kind() { FN_DEF => { bindings_shadow_count.clear(); - return None; + default_tag?.into() } // Highlight definitions depending on the "type" of the definition. @@ -515,12 +519,12 @@ fn highlight_element( let expr = prefix_expr.expr()?; let ty = sema.type_of_expr(&expr)?; if !ty.is_raw_ptr() { - return None; + default_tag?.into() + } else { + let mut h = Highlight::new(HighlightTag::Operator); + h |= HighlightModifier::Unsafe; + h } - - let mut h = Highlight::new(HighlightTag::Operator); - h |= HighlightModifier::Unsafe; - h } T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { Highlight::new(HighlightTag::Macro) @@ -546,7 +550,7 @@ fn highlight_element( } } - _ => return None, + _ => default_tag?.into(), }; return Some((highlight, binding_hash)); diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 99b6b25ab..9043024df 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs @@ -19,7 +19,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo ) } - let ranges = highlight(db, file_id, None, false); + let ranges = highlight(db, file_id, None, false, None); let text = parse.tree().syntax().to_string(); let mut prev_pos = TextSize::from(0); let mut buf = String::new(); diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 9d82b4009..bd38cdb6f 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -150,7 +150,10 @@ pub(super) fn highlight_doc_comment( let (analysis, tmp_file_id) = Analysis::from_single_file(text); stack.push(); - for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { + for mut h in analysis + .with_db(|db| super::highlight(db, tmp_file_id, None, true, Some(HighlightTag::Operator))) + .unwrap() + { // Determine start offset and end offset in case of multi-line ranges let mut start_offset = None; let mut end_offset = None; @@ -172,6 +175,7 @@ pub(super) fn highlight_doc_comment( h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), ); + h.highlight |= HighlightModifier::Injected; stack.add(h); } } diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 93bbb4b4d..f5ab73865 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs @@ -57,6 +57,7 @@ pub enum HighlightModifier { /// not. Definition, Documentation, + Injected, Mutable, Unsafe, } @@ -110,6 +111,7 @@ impl HighlightModifier { HighlightModifier::ControlFlow, HighlightModifier::Definition, HighlightModifier::Documentation, + HighlightModifier::Injected, HighlightModifier::Mutable, HighlightModifier::Unsafe, ]; @@ -120,6 +122,7 @@ impl HighlightModifier { HighlightModifier::ControlFlow => "control", HighlightModifier::Definition => "declaration", HighlightModifier::Documentation => "documentation", + HighlightModifier::Injected => "injected", HighlightModifier::Mutable => "mutable", HighlightModifier::Unsafe => "unsafe", } diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs index 2ea63d33b..6f43667a3 100644 --- a/crates/rust-analyzer/src/semantic_tokens.rs +++ b/crates/rust-analyzer/src/semantic_tokens.rs @@ -68,6 +68,7 @@ macro_rules! define_semantic_token_modifiers { define_semantic_token_modifiers![ (CONSTANT, "constant"), (CONTROL_FLOW, "controlFlow"), + (INJECTED, "injected"), (MUTABLE, "mutable"), (UNSAFE, "unsafe"), (ATTRIBUTE_MODIFIER, "attribute"), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 7b45b169d..dee5d7859 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -331,6 +331,7 @@ fn semantic_token_type_and_modifiers( HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER, HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION, HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION, + HighlightModifier::Injected => semantic_tokens::INJECTED, HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW, HighlightModifier::Mutable => semantic_tokens::MUTABLE, HighlightModifier::Unsafe => semantic_tokens::UNSAFE, -- cgit v1.2.3