From 1c359ab634edb81b51e3c7eadfb83d46c926e890 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 15:04:06 +0200 Subject: Replace SepBy with Itertools --- crates/ra_ide/src/syntax_highlighting/injection.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting/injection.rs') diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 8665b480f..6046643ef 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -4,8 +4,8 @@ use std::{collections::BTreeMap, convert::TryFrom}; use ast::{HasQuotes, HasStringValue}; use hir::Semantics; +use itertools::Itertools; use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; -use stdx::SepBy; use crate::{ call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, @@ -129,8 +129,7 @@ pub(super) fn extract_doc_comments( line[pos..].to_owned() }) - .sep_by("\n") - .to_string(); + .join("\n"); if doctest.is_empty() { return None; -- cgit v1.2.3 From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_ide/src/syntax_highlighting/injection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src/syntax_highlighting/injection.rs') diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 6046643ef..43f4e6fea 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -5,7 +5,7 @@ use std::{collections::BTreeMap, convert::TryFrom}; use ast::{HasQuotes, HasStringValue}; use hir::Semantics; use itertools::Itertools; -use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; +use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; use crate::{ call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, -- cgit v1.2.3 From 1b0c7701cc97cd7bef8bb9729011d4cf291a60c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 17:42:52 +0200 Subject: Rename ra_ide -> ide --- crates/ra_ide/src/syntax_highlighting/injection.rs | 187 --------------------- 1 file changed, 187 deletions(-) delete mode 100644 crates/ra_ide/src/syntax_highlighting/injection.rs (limited to 'crates/ra_ide/src/syntax_highlighting/injection.rs') diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs deleted file mode 100644 index 43f4e6fea..000000000 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ /dev/null @@ -1,187 +0,0 @@ -//! Syntax highlighting injections such as highlighting of documentation tests. - -use std::{collections::BTreeMap, convert::TryFrom}; - -use ast::{HasQuotes, HasStringValue}; -use hir::Semantics; -use itertools::Itertools; -use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; - -use crate::{ - call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, - HighlightedRange, RootDatabase, -}; - -use super::HighlightedRangeStack; - -pub(super) fn highlight_injection( - acc: &mut HighlightedRangeStack, - sema: &Semantics, - literal: ast::RawString, - expanded: SyntaxToken, -) -> Option<()> { - let active_parameter = ActiveParameter::at_token(&sema, expanded)?; - if !active_parameter.name.starts_with("ra_fixture") { - return None; - } - let value = literal.value()?; - let (analysis, tmp_file_id) = Analysis::from_single_file(value.into_owned()); - - if let Some(range) = literal.open_quote_text_range() { - acc.add(HighlightedRange { - range, - highlight: HighlightTag::StringLiteral.into(), - binding_hash: None, - }) - } - - for mut h in analysis.highlight(tmp_file_id).unwrap() { - if let Some(r) = literal.map_range_up(h.range) { - h.range = r; - acc.add(h) - } - } - - if let Some(range) = literal.close_quote_text_range() { - acc.add(HighlightedRange { - range, - highlight: HighlightTag::StringLiteral.into(), - binding_hash: None, - }) - } - - Some(()) -} - -/// Mapping from extracted documentation code to original code -type RangesMap = BTreeMap; - -const RUSTDOC_FENCE: &'static str = "```"; -const RUSTDOC_FENCE_TOKENS: &[&'static str] = - &["", "rust", "should_panic", "ignore", "no_run", "compile_fail", "edition2015", "edition2018"]; - -/// Extracts Rust code from documentation comments as well as a mapping from -/// the extracted source code back to the original source ranges. -/// 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<(String, RangesMap, Vec)> { - // wrap the doctest into function body to get correct syntax highlighting - let prefix = "fn doctest() {\n"; - let suffix = "}\n"; - // Mapping from extracted documentation code to original code - let mut range_mapping: RangesMap = BTreeMap::new(); - let mut line_start = TextSize::try_from(prefix.len()).unwrap(); - let mut is_codeblock = false; - let mut is_doctest = false; - // Replace the original, line-spanning comment ranges by new, only comment-prefix - // spanning comment ranges. - let mut new_comments = Vec::new(); - let doctest = node - .children_with_tokens() - .filter_map(|el| el.into_token().and_then(ast::Comment::cast)) - .filter(|comment| comment.kind().doc.is_some()) - .filter(|comment| { - if let Some(idx) = comment.text().find(RUSTDOC_FENCE) { - is_codeblock = !is_codeblock; - // Check whether code is rust by inspecting fence guards - let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..]; - let is_rust = - guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim())); - is_doctest = is_codeblock && is_rust; - false - } else { - is_doctest - } - }) - .map(|comment| { - let prefix_len = comment.prefix().len(); - let line: &str = comment.text().as_str(); - let range = comment.syntax().text_range(); - - // whitespace after comment is ignored - let pos = if let Some(ws) = line.chars().nth(prefix_len).filter(|c| c.is_whitespace()) { - prefix_len + ws.len_utf8() - } else { - prefix_len - }; - - // lines marked with `#` should be ignored in output, we skip the `#` char - let pos = if let Some(ws) = line.chars().nth(pos).filter(|&c| c == '#') { - pos + ws.len_utf8() - } else { - pos - }; - - range_mapping.insert(line_start, range.start() + TextSize::try_from(pos).unwrap()); - new_comments.push(HighlightedRange { - range: TextRange::new( - range.start(), - range.start() + TextSize::try_from(pos).unwrap(), - ), - highlight: HighlightTag::Comment | HighlightModifier::Documentation, - binding_hash: None, - }); - line_start += range.len() - TextSize::try_from(pos).unwrap(); - line_start += TextSize::try_from('\n'.len_utf8()).unwrap(); - - line[pos..].to_owned() - }) - .join("\n"); - - if doctest.is_empty() { - return None; - } - - let doctest = format!("{}{}{}", prefix, doctest, suffix); - Some((doctest, range_mapping, new_comments)) -} - -/// Injection of syntax highlighting of doctests. -pub(super) fn highlight_doc_comment( - text: String, - range_mapping: RangesMap, - new_comments: Vec, - stack: &mut HighlightedRangeStack, -) { - 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() { - // Determine start offset and end offset in case of multi-line ranges - let mut start_offset = None; - let mut end_offset = None; - for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { - // It's possible for orig_line_start - line_start to be negative. Add h.range.start() - // here and remove it from the end range after the loop below so that the values are - // always non-negative. - let offset = h.range.start() + orig_line_start - line_start; - if line_start <= &h.range.start() { - start_offset.get_or_insert(offset); - break; - } else { - end_offset.get_or_insert(offset); - } - } - if let Some(start_offset) = start_offset { - h.range = TextRange::new( - start_offset, - h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), - ); - - h.highlight |= HighlightModifier::Injected; - stack.add(h); - } - } - - // Inject the comment prefix highlight ranges - stack.push(); - for comment in new_comments { - stack.add(comment); - } - stack.pop_and_inject(None); - stack - .pop_and_inject(Some(Highlight::from(HighlightTag::Generic) | HighlightModifier::Injected)); -} -- cgit v1.2.3