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.rs | 2 +- crates/ide/src/syntax_highlighting/injection.rs | 78 +++++++++---------------- 2 files changed, 28 insertions(+), 52 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 3ba91b3f5..ad456bc00 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -33,7 +33,7 @@ use crate::{ pub(crate) use html::highlight_as_html; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct HlRange { pub range: TextRange, pub highlight: Highlight, 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