diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 674c72c23e4..409507bd0b7 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -77,7 +77,7 @@ macro_rules! eprintln { runnables::{Runnable, RunnableKind, TestId}, syntax_highlighting::{ tags::{Highlight, HlMod, HlMods, HlTag}, - HighlightedRange, + HlRange, }, }; pub use assists::{Assist, AssistConfig, AssistId, AssistKind, InsertUseConfig}; @@ -449,12 +449,12 @@ pub fn runnables(&self, file_id: FileId) -> Cancelable> { } /// Computes syntax highlighting for the given file - pub fn highlight(&self, file_id: FileId) -> Cancelable> { + pub fn highlight(&self, file_id: FileId) -> Cancelable> { self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) } /// Computes syntax highlighting for the given file range. - pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { + pub fn highlight_range(&self, frange: FileRange) -> Cancelable> { self.with_db(|db| { syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) }) diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 086c1245e8c..8bb7acb827b 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -34,7 +34,7 @@ pub(crate) use html::highlight_as_html; #[derive(Debug, Clone)] -pub struct HighlightedRange { +pub struct HlRange { pub range: TextRange, pub highlight: Highlight, pub binding_hash: Option, @@ -54,7 +54,7 @@ pub(crate) fn highlight( file_id: FileId, range_to_highlight: Option, syntactic_name_ref_highlighting: bool, -) -> Vec { +) -> Vec { let _p = profile::span("highlight"); let sema = Semantics::new(db); @@ -98,7 +98,7 @@ pub(crate) fn highlight( match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { WalkEvent::Enter(Some(mc)) => { if let Some(range) = macro_call_range(&mc) { - stack.add(HighlightedRange { + stack.add(HlRange { range, highlight: HlTag::Symbol(SymbolKind::Macro).into(), binding_hash: None, @@ -198,7 +198,7 @@ pub(crate) fn highlight( } if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() { - stack.add(HighlightedRange { range, highlight, binding_hash }); + stack.add(HlRange { range, highlight, binding_hash }); } if let Some(string) = @@ -209,7 +209,7 @@ pub(crate) fn highlight( if let Some(char_ranges) = string.char_ranges() { for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) { if string.text()[piece_range.start().into()..].starts_with('\\') { - stack.add(HighlightedRange { + stack.add(HlRange { range: piece_range + range.start(), highlight: HlTag::EscapeSequence.into(), binding_hash: None, diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index 94cecd97f5c..d807ad0ad5c 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -4,7 +4,7 @@ AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, }; -use crate::{HighlightedRange, HlTag, SymbolKind}; +use crate::{HlRange, HlTag, SymbolKind}; use super::highlights::Highlights; @@ -46,7 +46,7 @@ pub(super) fn highlight_format_string( 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 f52076509a9..11c11ed2887 100644 --- a/crates/ide/src/syntax_highlighting/highlights.rs +++ b/crates/ide/src/syntax_highlighting/highlights.rs @@ -4,33 +4,29 @@ 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 @@ pub(super) fn to_vec(self) -> Vec { } 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 a6941234e34..13dde1dc446 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -7,7 +7,7 @@ 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 @@ fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize { /// 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 71dd1ccc513..21d8a98350e 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 @@ pub(super) fn advance(&mut self, token: &SyntaxToken) { } } - 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, diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 0c31a9fedb0..d6bf60cde2a 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -6,9 +6,9 @@ use ide::{ Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, Documentation, FileId, - FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightedRange, HlMod, HlTag, Indel, - InlayHint, InlayKind, InsertTextFormat, LineIndex, Markup, NavigationTarget, ReferenceAccess, - Runnable, Severity, SourceChange, SourceFileEdit, SymbolKind, TextEdit, TextRange, TextSize, + FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlRange, HlTag, Indel, InlayHint, + InlayKind, InsertTextFormat, LineIndex, Markup, NavigationTarget, ReferenceAccess, Runnable, + Severity, SourceChange, SourceFileEdit, SymbolKind, TextEdit, TextRange, TextSize, }; use itertools::Itertools; @@ -336,7 +336,7 @@ pub(crate) fn inlay_hint(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_e pub(crate) fn semantic_tokens( text: &str, line_index: &LineIndex, - highlights: Vec, + highlights: Vec, ) -> lsp_types::SemanticTokens { let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string(); let mut builder = semantic_tokens::SemanticTokensBuilder::new(id);