From 6fb52af521ab8ca2fdd3ea7cfa95eaebd0cba1fc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Jan 2021 14:41:31 +0300 Subject: Rename dummy -> none --- crates/ide/src/syntax_highlighting/highlights.rs | 2 +- crates/ide/src/syntax_highlighting/tags.rs | 8 ++++---- .../test_data/highlight_doctest.html | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) (limited to 'crates/ide/src/syntax_highlighting') diff --git a/crates/ide/src/syntax_highlighting/highlights.rs b/crates/ide/src/syntax_highlighting/highlights.rs index 3e733c87c..7ff1593ae 100644 --- a/crates/ide/src/syntax_highlighting/highlights.rs +++ b/crates/ide/src/syntax_highlighting/highlights.rs @@ -20,7 +20,7 @@ impl Highlights { Highlights { root: Node::new(HighlightedRange { range, - highlight: HighlightTag::Dummy.into(), + highlight: HighlightTag::None.into(), binding_hash: None, }), } diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index a0286b72d..07c788b50 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs @@ -33,8 +33,8 @@ pub enum HighlightTag { Operator, UnresolvedReference, - // For things which don't have proper Tag, but want to use modifiers. - Dummy, + // For things which don't have a specific highlight. + None, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -100,7 +100,7 @@ impl HighlightTag { HighlightTag::Operator => "operator", HighlightTag::StringLiteral => "string_literal", HighlightTag::UnresolvedReference => "unresolved_reference", - HighlightTag::Dummy => "dummy", + HighlightTag::None => "none", } } } @@ -174,7 +174,7 @@ impl Highlight { Highlight { tag, modifiers: HighlightModifiers::default() } } pub fn is_empty(&self) -> bool { - self.tag == HighlightTag::Dummy && self.modifiers == HighlightModifiers::default() + self.tag == HighlightTag::None && self.modifiers == HighlightModifiers::default() } } 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 9d42b11c1..7d1d2a839 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -37,7 +37,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
/// ```
-/// let _ = "early doctests should not go boom";
+/// let _ = "early doctests should not go boom";
 /// ```
 struct Foo {
     bar: bool,
@@ -45,7 +45,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 
 impl Foo {
     /// ```
-    /// let _ = "Call me
+    /// let _ = "Call me
     //    KILLER WHALE
     ///     Ishmael.";
     /// ```
@@ -56,8 +56,8 @@ 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 }
@@ -68,26 +68,26 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     /// # Examples
     ///
     /// ```
-    /// use x::y;
+    /// use x::y;
     ///
-    /// let foo = Foo::new();
+    /// let foo = Foo::new();
     ///
     /// // calls bar on foo
-    /// assert!(foo.bar());
+    /// assert!(foo.bar());
     ///
-    /// let bar = foo.bar || Foo::bar;
+    /// let bar = foo.bar || Foo::bar;
     ///
     /// /* multi-line
     ///        comment */
     ///
-    /// let multi_line_string = "Foo
+    /// let multi_line_string = "Foo
     ///   bar
     ///          ";
     ///
     /// ```
     ///
     /// ```rust,no_run
-    /// let foobar = Foo::new().bar();
+    /// let foobar = Foo::new().bar();
     /// ```
     ///
     /// ```sh
-- 
cgit v1.2.3


From 64a6ee45351a71cdeb1d1dddc8fc746abdcff07b Mon Sep 17 00:00:00 2001
From: Aleksey Kladov 
Date: Sat, 9 Jan 2021 14:44:01 +0300
Subject: Shorten frequent names

---
 crates/ide/src/syntax_highlighting/format.rs      |  10 +-
 crates/ide/src/syntax_highlighting/highlights.rs  |   4 +-
 crates/ide/src/syntax_highlighting/injection.rs   |  10 +-
 crates/ide/src/syntax_highlighting/macro_rules.rs |   4 +-
 crates/ide/src/syntax_highlighting/tags.rs        | 134 +++++++++++-----------
 5 files changed, 81 insertions(+), 81 deletions(-)

(limited to 'crates/ide/src/syntax_highlighting')

diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs
index ab66b406c..94cecd97f 100644
--- a/crates/ide/src/syntax_highlighting/format.rs
+++ b/crates/ide/src/syntax_highlighting/format.rs
@@ -4,7 +4,7 @@ use syntax::{
     AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
 };
 
-use crate::{HighlightTag, HighlightedRange, SymbolKind};
+use crate::{HighlightedRange, HlTag, SymbolKind};
 
 use super::highlights::Highlights;
 
@@ -57,7 +57,7 @@ impl FormatStringHighlighter {
     }
 }
 
-fn highlight_format_specifier(kind: FormatSpecifier) -> Option {
+fn highlight_format_specifier(kind: FormatSpecifier) -> Option {
     Some(match kind {
         FormatSpecifier::Open
         | FormatSpecifier::Close
@@ -69,8 +69,8 @@ fn highlight_format_specifier(kind: FormatSpecifier) -> Option {
         | FormatSpecifier::DollarSign
         | FormatSpecifier::Dot
         | FormatSpecifier::Asterisk
-        | FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier,
-        FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
-        FormatSpecifier::Identifier => HighlightTag::Symbol(SymbolKind::Local),
+        | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
+        FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
+        FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
     })
 }
diff --git a/crates/ide/src/syntax_highlighting/highlights.rs b/crates/ide/src/syntax_highlighting/highlights.rs
index 7ff1593ae..f52076509 100644
--- a/crates/ide/src/syntax_highlighting/highlights.rs
+++ b/crates/ide/src/syntax_highlighting/highlights.rs
@@ -4,7 +4,7 @@ use std::{cmp::Ordering, iter};
 use stdx::equal_range_by;
 use syntax::TextRange;
 
-use crate::{HighlightTag, HighlightedRange};
+use crate::{HighlightedRange, HlTag};
 
 pub(super) struct Highlights {
     root: Node,
@@ -20,7 +20,7 @@ impl Highlights {
         Highlights {
             root: Node::new(HighlightedRange {
                 range,
-                highlight: HighlightTag::None.into(),
+                highlight: HlTag::None.into(),
                 binding_hash: None,
             }),
         }
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
index 98ee03e0d..a6941234e 100644
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ b/crates/ide/src/syntax_highlighting/injection.rs
@@ -7,7 +7,7 @@ use ide_db::call_info::ActiveParameter;
 use itertools::Itertools;
 use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};
 
-use crate::{Analysis, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase};
+use crate::{Analysis, HighlightedRange, HlMod, HlTag, RootDatabase};
 
 use super::{highlights::Highlights, injector::Injector};
 
@@ -28,7 +28,7 @@ pub(super) fn highlight_injection(
     if let Some(range) = literal.open_quote_text_range() {
         acc.add(HighlightedRange {
             range,
-            highlight: HighlightTag::StringLiteral.into(),
+            highlight: HlTag::StringLiteral.into(),
             binding_hash: None,
         })
     }
@@ -44,7 +44,7 @@ pub(super) fn highlight_injection(
     if let Some(range) = literal.close_quote_text_range() {
         acc.add(HighlightedRange {
             range,
-            highlight: HighlightTag::StringLiteral.into(),
+            highlight: HlTag::StringLiteral.into(),
             binding_hash: None,
         })
     }
@@ -171,7 +171,7 @@ pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec &'static str {
         match self {
-            HighlightTag::Symbol(symbol) => match symbol {
+            HlTag::Symbol(symbol) => match symbol {
                 SymbolKind::Const => "constant",
                 SymbolKind::Static => "static",
                 SymbolKind::Enum => "enum",
@@ -86,59 +86,59 @@ impl HighlightTag {
                 SymbolKind::SelfParam => "self_keyword",
                 SymbolKind::Impl => "self_type",
             },
-            HighlightTag::Attribute => "attribute",
-            HighlightTag::BoolLiteral => "bool_literal",
-            HighlightTag::BuiltinType => "builtin_type",
-            HighlightTag::ByteLiteral => "byte_literal",
-            HighlightTag::CharLiteral => "char_literal",
-            HighlightTag::Comment => "comment",
-            HighlightTag::EscapeSequence => "escape_sequence",
-            HighlightTag::FormatSpecifier => "format_specifier",
-            HighlightTag::Keyword => "keyword",
-            HighlightTag::Punctuation => "punctuation",
-            HighlightTag::NumericLiteral => "numeric_literal",
-            HighlightTag::Operator => "operator",
-            HighlightTag::StringLiteral => "string_literal",
-            HighlightTag::UnresolvedReference => "unresolved_reference",
-            HighlightTag::None => "none",
+            HlTag::Attribute => "attribute",
+            HlTag::BoolLiteral => "bool_literal",
+            HlTag::BuiltinType => "builtin_type",
+            HlTag::ByteLiteral => "byte_literal",
+            HlTag::CharLiteral => "char_literal",
+            HlTag::Comment => "comment",
+            HlTag::EscapeSequence => "escape_sequence",
+            HlTag::FormatSpecifier => "format_specifier",
+            HlTag::Keyword => "keyword",
+            HlTag::Punctuation => "punctuation",
+            HlTag::NumericLiteral => "numeric_literal",
+            HlTag::Operator => "operator",
+            HlTag::StringLiteral => "string_literal",
+            HlTag::UnresolvedReference => "unresolved_reference",
+            HlTag::None => "none",
         }
     }
 }
 
-impl fmt::Display for HighlightTag {
+impl fmt::Display for HlTag {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Display::fmt(self.as_str(), f)
     }
 }
 
-impl HighlightModifier {
-    const ALL: &'static [HighlightModifier; HighlightModifier::Unsafe as u8 as usize + 1] = &[
-        HighlightModifier::Attribute,
-        HighlightModifier::ControlFlow,
-        HighlightModifier::Definition,
-        HighlightModifier::Documentation,
-        HighlightModifier::Injected,
-        HighlightModifier::Mutable,
-        HighlightModifier::Consuming,
-        HighlightModifier::Callable,
-        HighlightModifier::Static,
-        HighlightModifier::Associated,
-        HighlightModifier::Unsafe,
+impl HlMod {
+    const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[
+        HlMod::Attribute,
+        HlMod::ControlFlow,
+        HlMod::Definition,
+        HlMod::Documentation,
+        HlMod::Injected,
+        HlMod::Mutable,
+        HlMod::Consuming,
+        HlMod::Callable,
+        HlMod::Static,
+        HlMod::Associated,
+        HlMod::Unsafe,
     ];
 
     fn as_str(self) -> &'static str {
         match self {
-            HighlightModifier::Attribute => "attribute",
-            HighlightModifier::ControlFlow => "control",
-            HighlightModifier::Definition => "declaration",
-            HighlightModifier::Documentation => "documentation",
-            HighlightModifier::Injected => "injected",
-            HighlightModifier::Mutable => "mutable",
-            HighlightModifier::Consuming => "consuming",
-            HighlightModifier::Unsafe => "unsafe",
-            HighlightModifier::Callable => "callable",
-            HighlightModifier::Static => "static",
-            HighlightModifier::Associated => "associated",
+            HlMod::Attribute => "attribute",
+            HlMod::ControlFlow => "control",
+            HlMod::Definition => "declaration",
+            HlMod::Documentation => "documentation",
+            HlMod::Injected => "injected",
+            HlMod::Mutable => "mutable",
+            HlMod::Consuming => "consuming",
+            HlMod::Unsafe => "unsafe",
+            HlMod::Callable => "callable",
+            HlMod::Static => "static",
+            HlMod::Associated => "associated",
         }
     }
 
@@ -147,7 +147,7 @@ impl HighlightModifier {
     }
 }
 
-impl fmt::Display for HighlightModifier {
+impl fmt::Display for HlMod {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Display::fmt(self.as_str(), f)
     }
@@ -156,63 +156,63 @@ impl fmt::Display for HighlightModifier {
 impl fmt::Display for Highlight {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{}", self.tag)?;
-        for modifier in self.modifiers.iter() {
+        for modifier in self.mods.iter() {
             write!(f, ".{}", modifier)?
         }
         Ok(())
     }
 }
 
-impl From for Highlight {
-    fn from(tag: HighlightTag) -> Highlight {
+impl From for Highlight {
+    fn from(tag: HlTag) -> Highlight {
         Highlight::new(tag)
     }
 }
 
 impl Highlight {
-    pub(crate) fn new(tag: HighlightTag) -> Highlight {
-        Highlight { tag, modifiers: HighlightModifiers::default() }
+    pub(crate) fn new(tag: HlTag) -> Highlight {
+        Highlight { tag, mods: HlMods::default() }
     }
     pub fn is_empty(&self) -> bool {
-        self.tag == HighlightTag::None && self.modifiers == HighlightModifiers::default()
+        self.tag == HlTag::None && self.mods == HlMods::default()
     }
 }
 
-impl ops::BitOr for HighlightTag {
+impl ops::BitOr for HlTag {
     type Output = Highlight;
 
-    fn bitor(self, rhs: HighlightModifier) -> Highlight {
+    fn bitor(self, rhs: HlMod) -> Highlight {
         Highlight::new(self) | rhs
     }
 }
 
-impl ops::BitOrAssign for HighlightModifiers {
-    fn bitor_assign(&mut self, rhs: HighlightModifier) {
+impl ops::BitOrAssign for HlMods {
+    fn bitor_assign(&mut self, rhs: HlMod) {
         self.0 |= rhs.mask();
     }
 }
 
-impl ops::BitOrAssign for Highlight {
-    fn bitor_assign(&mut self, rhs: HighlightModifier) {
-        self.modifiers |= rhs;
+impl ops::BitOrAssign for Highlight {
+    fn bitor_assign(&mut self, rhs: HlMod) {
+        self.mods |= rhs;
     }
 }
 
-impl ops::BitOr for Highlight {
+impl ops::BitOr for Highlight {
     type Output = Highlight;
 
-    fn bitor(mut self, rhs: HighlightModifier) -> Highlight {
+    fn bitor(mut self, rhs: HlMod) -> Highlight {
         self |= rhs;
         self
     }
 }
 
-impl HighlightModifiers {
-    pub fn contains(self, m: HighlightModifier) -> bool {
+impl HlMods {
+    pub fn contains(self, m: HlMod) -> bool {
         self.0 & m.mask() == m.mask()
     }
 
-    pub fn iter(self) -> impl Iterator {
-        HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
+    pub fn iter(self) -> impl Iterator {
+        HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
     }
 }
-- 
cgit v1.2.3


From 3dfa2768acdd6bbf0e6999cdc4eb5cf0718b95a4 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov 
Date: Sat, 9 Jan 2021 14:48:15 +0300
Subject: Shorten names

---
 crates/ide/src/syntax_highlighting/format.rs      |  4 +-
 crates/ide/src/syntax_highlighting/highlights.rs  | 61 ++++++++++-------------
 crates/ide/src/syntax_highlighting/injection.rs   | 22 +++-----
 crates/ide/src/syntax_highlighting/macro_rules.rs |  6 +--
 4 files changed, 38 insertions(+), 55 deletions(-)

(limited to 'crates/ide/src/syntax_highlighting')

diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs
index 94cecd97f..d807ad0ad 100644
--- a/crates/ide/src/syntax_highlighting/format.rs
+++ b/crates/ide/src/syntax_highlighting/format.rs
@@ -4,7 +4,7 @@ use syntax::{
     AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
 };
 
-use crate::{HighlightedRange, HlTag, SymbolKind};
+use crate::{HlRange, HlTag, SymbolKind};
 
 use super::highlights::Highlights;
 
@@ -46,7 +46,7 @@ impl FormatStringHighlighter {
         if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) {
             string.lex_format_specifier(|piece_range, kind| {
                 if let Some(highlight) = highlight_format_specifier(kind) {
-                    stack.add(HighlightedRange {
+                    stack.add(HlRange {
                         range: piece_range + range.start(),
                         highlight: highlight.into(),
                         binding_hash: None,
diff --git a/crates/ide/src/syntax_highlighting/highlights.rs b/crates/ide/src/syntax_highlighting/highlights.rs
index f52076509..11c11ed28 100644
--- a/crates/ide/src/syntax_highlighting/highlights.rs
+++ b/crates/ide/src/syntax_highlighting/highlights.rs
@@ -4,33 +4,29 @@ use std::{cmp::Ordering, iter};
 use stdx::equal_range_by;
 use syntax::TextRange;
 
-use crate::{HighlightedRange, HlTag};
+use crate::{HlRange, HlTag};
 
 pub(super) struct Highlights {
     root: Node,
 }
 
 struct Node {
-    highlighted_range: HighlightedRange,
+    hl_range: HlRange,
     nested: Vec,
 }
 
 impl Highlights {
     pub(super) fn new(range: TextRange) -> Highlights {
         Highlights {
-            root: Node::new(HighlightedRange {
-                range,
-                highlight: HlTag::None.into(),
-                binding_hash: None,
-            }),
+            root: Node::new(HlRange { range, highlight: HlTag::None.into(), binding_hash: None }),
         }
     }
 
-    pub(super) fn add(&mut self, highlighted_range: HighlightedRange) {
-        self.root.add(highlighted_range);
+    pub(super) fn add(&mut self, hl_range: HlRange) {
+        self.root.add(hl_range);
     }
 
-    pub(super) fn to_vec(self) -> Vec {
+    pub(super) fn to_vec(self) -> Vec {
         let mut res = Vec::new();
         self.root.flatten(&mut res);
         res
@@ -38,59 +34,54 @@ impl Highlights {
 }
 
 impl Node {
-    fn new(highlighted_range: HighlightedRange) -> Node {
-        Node { highlighted_range, nested: Vec::new() }
+    fn new(hl_range: HlRange) -> Node {
+        Node { hl_range, nested: Vec::new() }
     }
 
-    fn add(&mut self, highlighted_range: HighlightedRange) {
-        assert!(self.highlighted_range.range.contains_range(highlighted_range.range));
+    fn add(&mut self, hl_range: HlRange) {
+        assert!(self.hl_range.range.contains_range(hl_range.range));
 
         // Fast path
         if let Some(last) = self.nested.last_mut() {
-            if last.highlighted_range.range.contains_range(highlighted_range.range) {
-                return last.add(highlighted_range);
+            if last.hl_range.range.contains_range(hl_range.range) {
+                return last.add(hl_range);
             }
-            if last.highlighted_range.range.end() <= highlighted_range.range.start() {
-                return self.nested.push(Node::new(highlighted_range));
+            if last.hl_range.range.end() <= hl_range.range.start() {
+                return self.nested.push(Node::new(hl_range));
             }
         }
 
-        let (start, len) = equal_range_by(&self.nested, |n| {
-            ordering(n.highlighted_range.range, highlighted_range.range)
-        });
+        let (start, len) =
+            equal_range_by(&self.nested, |n| ordering(n.hl_range.range, hl_range.range));
 
-        if len == 1
-            && self.nested[start].highlighted_range.range.contains_range(highlighted_range.range)
-        {
-            return self.nested[start].add(highlighted_range);
+        if len == 1 && self.nested[start].hl_range.range.contains_range(hl_range.range) {
+            return self.nested[start].add(hl_range);
         }
 
         let nested = self
             .nested
-            .splice(start..start + len, iter::once(Node::new(highlighted_range)))
+            .splice(start..start + len, iter::once(Node::new(hl_range)))
             .collect::>();
         self.nested[start].nested = nested;
     }
 
-    fn flatten(&self, acc: &mut Vec) {
-        let mut start = self.highlighted_range.range.start();
+    fn flatten(&self, acc: &mut Vec) {
+        let mut start = self.hl_range.range.start();
         let mut nested = self.nested.iter();
         loop {
             let next = nested.next();
-            let end = next.map_or(self.highlighted_range.range.end(), |it| {
-                it.highlighted_range.range.start()
-            });
+            let end = next.map_or(self.hl_range.range.end(), |it| it.hl_range.range.start());
             if start < end {
-                acc.push(HighlightedRange {
+                acc.push(HlRange {
                     range: TextRange::new(start, end),
-                    highlight: self.highlighted_range.highlight,
-                    binding_hash: self.highlighted_range.binding_hash,
+                    highlight: self.hl_range.highlight,
+                    binding_hash: self.hl_range.binding_hash,
                 });
             }
             start = match next {
                 Some(child) => {
                     child.flatten(acc);
-                    child.highlighted_range.range.end()
+                    child.hl_range.range.end()
                 }
                 None => break,
             }
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
index a6941234e..13dde1dc4 100644
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ b/crates/ide/src/syntax_highlighting/injection.rs
@@ -7,7 +7,7 @@ use ide_db::call_info::ActiveParameter;
 use itertools::Itertools;
 use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};
 
-use crate::{Analysis, HighlightedRange, HlMod, HlTag, RootDatabase};
+use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase};
 
 use super::{highlights::Highlights, injector::Injector};
 
@@ -26,11 +26,7 @@ pub(super) fn highlight_injection(
     let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());
 
     if let Some(range) = literal.open_quote_text_range() {
-        acc.add(HighlightedRange {
-            range,
-            highlight: HlTag::StringLiteral.into(),
-            binding_hash: None,
-        })
+        acc.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
     }
 
     for mut h in analysis.highlight(tmp_file_id).unwrap() {
@@ -42,11 +38,7 @@ pub(super) fn highlight_injection(
     }
 
     if let Some(range) = literal.close_quote_text_range() {
-        acc.add(HighlightedRange {
-            range,
-            highlight: HlTag::StringLiteral.into(),
-            binding_hash: None,
-        })
+        acc.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
     }
 
     Some(())
@@ -116,7 +108,7 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
 /// Lastly, a vector of new comment highlight ranges (spanning only the
 /// comment prefix) is returned which is used in the syntax highlighting
 /// injection to replace the previous (line-spanning) comment ranges.
-pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec, Injector)> {
+pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec, Injector)> {
     let mut inj = Injector::default();
     // wrap the doctest into function body to get correct syntax highlighting
     let prefix = "fn doctest() {\n";
@@ -166,7 +158,7 @@ pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec Option<(Vec,
+    new_comments: Vec,
     inj: Injector,
     stack: &mut Highlights,
 ) {
@@ -207,7 +199,7 @@ pub(super) fn highlight_doc_comment(
 
     for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
         for r in inj.map_range_up(h.range) {
-            stack.add(HighlightedRange {
+            stack.add(HlRange {
                 range: r,
                 highlight: h.highlight | HlMod::Injected,
                 binding_hash: h.binding_hash,
diff --git a/crates/ide/src/syntax_highlighting/macro_rules.rs b/crates/ide/src/syntax_highlighting/macro_rules.rs
index 71dd1ccc5..21d8a9835 100644
--- a/crates/ide/src/syntax_highlighting/macro_rules.rs
+++ b/crates/ide/src/syntax_highlighting/macro_rules.rs
@@ -1,7 +1,7 @@
 //! Syntax highlighting for macro_rules!.
 use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
 
-use crate::{HighlightedRange, HlTag};
+use crate::{HlRange, HlTag};
 
 #[derive(Default)]
 pub(super) struct MacroRulesHighlighter {
@@ -19,11 +19,11 @@ impl MacroRulesHighlighter {
         }
     }
 
-    pub(super) fn highlight(&self, element: SyntaxElement) -> Option {
+    pub(super) fn highlight(&self, element: SyntaxElement) -> Option {
         if let Some(state) = self.state.as_ref() {
             if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) {
                 if let Some(range) = is_metavariable(element) {
-                    return Some(HighlightedRange {
+                    return Some(HlRange {
                         range,
                         highlight: HlTag::UnresolvedReference.into(),
                         binding_hash: None,
-- 
cgit v1.2.3


From 8a0bd500363fd2953c2a469083b00be54c602ebb Mon Sep 17 00:00:00 2001
From: Aleksey Kladov 
Date: Sat, 9 Jan 2021 15:12:21 +0300
Subject: Reduce duplication

---
 crates/ide/src/syntax_highlighting/injector.rs | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

(limited to 'crates/ide/src/syntax_highlighting')

diff --git a/crates/ide/src/syntax_highlighting/injector.rs b/crates/ide/src/syntax_highlighting/injector.rs
index 0513a9fd6..e8f17eb69 100644
--- a/crates/ide/src/syntax_highlighting/injector.rs
+++ b/crates/ide/src/syntax_highlighting/injector.rs
@@ -17,17 +17,15 @@ impl Injector {
     pub(super) fn add(&mut self, text: &str, source_range: TextRange) {
         let len = TextSize::of(text);
         assert_eq!(len, source_range.len());
-
-        let target_range = TextRange::at(TextSize::of(&self.buf), len);
-        self.ranges
-            .push((target_range, Some(Delta::new(target_range.start(), source_range.start()))));
-        self.buf.push_str(text);
+        self.add_impl(text, Some(source_range.start()));
     }
     pub(super) fn add_unmapped(&mut self, text: &str) {
+        self.add_impl(text, None);
+    }
+    fn add_impl(&mut self, text: &str, source: Option) {
         let len = TextSize::of(text);
-
         let target_range = TextRange::at(TextSize::of(&self.buf), len);
-        self.ranges.push((target_range, None));
+        self.ranges.push((target_range, source.map(|it| Delta::new(target_range.start(), it))));
         self.buf.push_str(text);
     }
 
-- 
cgit v1.2.3


From d4fb7476efc8bf956c56ba2b0e946f48f38a6efc Mon Sep 17 00:00:00 2001
From: Aleksey Kladov 
Date: Sat, 9 Jan 2021 15:18:49 +0300
Subject: Better names

---
 crates/ide/src/syntax_highlighting/html.rs      | 16 ++++++++--------
 crates/ide/src/syntax_highlighting/injection.rs | 15 ++++++++-------
 2 files changed, 16 insertions(+), 15 deletions(-)

(limited to 'crates/ide/src/syntax_highlighting')

diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs
index 44f611b25..0ee7bc96e 100644
--- a/crates/ide/src/syntax_highlighting/html.rs
+++ b/crates/ide/src/syntax_highlighting/html.rs
@@ -20,26 +20,26 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
         )
     }
 
-    let ranges = highlight(db, file_id, None, false);
+    let hl_ranges = highlight(db, file_id, None, false);
     let text = parse.tree().syntax().to_string();
     let mut buf = String::new();
     buf.push_str(&STYLE);
     buf.push_str("
");
-    for range in &ranges {
-        let curr = &text[range.range];
-        if range.highlight.is_empty() {
-            format_to!(buf, "{}", html_escape(curr));
+    for r in &hl_ranges {
+        let chunk = html_escape(&text[r.range]);
+        if r.highlight.is_empty() {
+            format_to!(buf, "{}", chunk);
             continue;
         }
 
-        let class = range.highlight.to_string().replace('.', " ");
-        let color = match (rainbow, range.binding_hash) {
+        let class = r.highlight.to_string().replace('.', " ");
+        let color = match (rainbow, r.binding_hash) {
             (true, Some(hash)) => {
                 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
             }
             _ => "".into(),
         };
-        format_to!(buf, "{}", class, color, html_escape(curr));
+        format_to!(buf, "{}", class, color, chunk);
     }
     buf.push_str("
"); buf diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index 13dde1dc4..22d7f601a 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -12,7 +12,7 @@ use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase}; use super::{highlights::Highlights, injector::Injector}; pub(super) fn highlight_injection( - acc: &mut Highlights, + hl: &mut Highlights, sema: &Semantics, literal: ast::String, expanded: SyntaxToken, @@ -21,24 +21,25 @@ pub(super) fn highlight_injection( if !active_parameter.name.starts_with("ra_fixture") { return None; } + let value = literal.value()?; let marker_info = MarkerInfo::new(&*value); let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone()); if let Some(range) = literal.open_quote_text_range() { - acc.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) + hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) } - for mut h in analysis.highlight(tmp_file_id).unwrap() { - let range = marker_info.map_range_up(h.range); + for mut hl_range in analysis.highlight(tmp_file_id).unwrap() { + let range = marker_info.map_range_up(hl_range.range); if let Some(range) = literal.map_range_up(range) { - h.range = range; - acc.add(h); + hl_range.range = range; + hl.add(hl_range); } } if let Some(range) = literal.close_quote_text_range() { - acc.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) + hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) } Some(()) -- cgit v1.2.3 From 62b17bac92595d384aaeb0a0fdc5c03269a0dbac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Jan 2021 15:54:38 +0300 Subject: Simplify --- crates/ide/src/syntax_highlighting/injection.rs | 78 +++++++++---------------- 1 file changed, 27 insertions(+), 51 deletions(-) (limited to 'crates/ide/src/syntax_highlighting') diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index 22d7f601a..de2180b04 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -21,74 +21,50 @@ pub(super) fn highlight_injection( if !active_parameter.name.starts_with("ra_fixture") { return None; } - let value = literal.value()?; - let marker_info = MarkerInfo::new(&*value); - let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone()); if let Some(range) = literal.open_quote_text_range() { hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) } - for mut hl_range in analysis.highlight(tmp_file_id).unwrap() { - let range = marker_info.map_range_up(hl_range.range); - if let Some(range) = literal.map_range_up(range) { - hl_range.range = range; - hl.add(hl_range); - } - } - - if let Some(range) = literal.close_quote_text_range() { - hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) - } - - Some(()) -} + let mut inj = Injector::default(); -/// Data to remove `$0` from string and map ranges -#[derive(Default, Debug)] -struct MarkerInfo { - cleaned_text: String, - markers: Vec, -} + let mut text = &*value; + let mut offset: TextSize = 0.into(); -impl MarkerInfo { - fn new(mut text: &str) -> Self { + while !text.is_empty() { let marker = "$0"; + let idx = text.find(marker).unwrap_or(text.len()); + let (chunk, next) = text.split_at(idx); + inj.add(chunk, TextRange::at(offset, TextSize::of(chunk))); - let mut res = MarkerInfo::default(); - let mut offset: TextSize = 0.into(); - while !text.is_empty() { - let idx = text.find(marker).unwrap_or(text.len()); - let (chunk, next) = text.split_at(idx); - text = next; - res.cleaned_text.push_str(chunk); - offset += TextSize::of(chunk); + text = next; + offset += TextSize::of(chunk); - if let Some(next) = text.strip_prefix(marker) { - text = next; + if let Some(next) = text.strip_prefix(marker) { + text = next; - let marker_len = TextSize::of(marker); - res.markers.push(TextRange::at(offset, marker_len)); - offset += marker_len; - } + let marker_len = TextSize::of(marker); + offset += marker_len; } - res } - fn map_range_up(&self, range: TextRange) -> TextRange { - TextRange::new( - self.map_offset_up(range.start(), true), - self.map_offset_up(range.end(), false), - ) - } - fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize { - for r in &self.markers { - if r.start() < offset || (start && r.start() == offset) { - offset += r.len() + + let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string()); + + for mut hl_range in analysis.highlight(tmp_file_id).unwrap() { + for range in inj.map_range_up(hl_range.range) { + if let Some(range) = literal.map_range_up(range) { + hl_range.range = range; + hl.add(hl_range.clone()); } } - offset } + + if let Some(range) = literal.close_quote_text_range() { + hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) + } + + Some(()) } const RUSTDOC_FENCE: &'static str = "```"; -- cgit v1.2.3 From 4a8d2c098d88da6f36f3cf95f424b471987c5daa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Jan 2021 16:07:41 +0300 Subject: Better highlight for fixtures --- crates/ide/src/syntax_highlighting/injection.rs | 4 ++++ crates/ide/src/syntax_highlighting/test_data/injection.html | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ide/src/syntax_highlighting') diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index de2180b04..008d5ce24 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -42,6 +42,10 @@ pub(super) fn highlight_injection( offset += TextSize::of(chunk); if let Some(next) = text.strip_prefix(marker) { + if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) { + hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None }); + } + text = next; let marker_len = TextSize::of(marker); diff --git a/crates/ide/src/syntax_highlighting/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html index a54d303b4..6703a84e5 100644 --- a/crates/ide/src/syntax_highlighting/test_data/injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/injection.html @@ -40,9 +40,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd fn main() { f(r" fn foo() { - foo($0{ + foo($0{ 92 - }$0) + }$0) }"); }
\ No newline at end of file -- cgit v1.2.3