aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting/html.rs
diff options
context:
space:
mode:
authorLeander Tentrup <[email protected]>2020-04-06 22:00:09 +0100
committerLeander Tentrup <[email protected]>2020-04-06 22:00:09 +0100
commitbf96d46fee1242ad701cb037a03c9575e84221b1 (patch)
treebd01242f4e504b25d07a3d193b6715443f7b2b86 /crates/ra_ide/src/syntax_highlighting/html.rs
parentbb45aca9093ffe61cf444d538b3de737117c9a64 (diff)
Simplify HTML highlighter and add test case for highlight_injection logic
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/html.rs')
-rw-r--r--crates/ra_ide/src/syntax_highlighting/html.rs66
1 files changed, 25 insertions, 41 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs
index e13766c9d..4496529a1 100644
--- a/crates/ra_ide/src/syntax_highlighting/html.rs
+++ b/crates/ra_ide/src/syntax_highlighting/html.rs
@@ -1,9 +1,9 @@
1//! Renders a bit of code as HTML. 1//! Renders a bit of code as HTML.
2 2
3use ra_db::SourceDatabase; 3use ra_db::SourceDatabase;
4use ra_syntax::AstNode; 4use ra_syntax::{AstNode, TextUnit};
5 5
6use crate::{FileId, HighlightedRange, RootDatabase}; 6use crate::{FileId, RootDatabase};
7 7
8use super::highlight; 8use super::highlight;
9 9
@@ -21,51 +21,35 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
21 ) 21 )
22 } 22 }
23 23
24 let mut ranges = highlight(db, file_id, None); 24 let ranges = highlight(db, file_id, None);
25 ranges.sort_by_key(|it| it.range.start()); 25 let text = parse.tree().syntax().to_string();
26 // quick non-optimal heuristic to intersect token ranges and highlighted ranges 26 let mut prev_pos = TextUnit::from(0);
27 let mut frontier = 0;
28 let mut could_intersect: Vec<&HighlightedRange> = Vec::new();
29
30 let mut buf = String::new(); 27 let mut buf = String::new();
31 buf.push_str(&STYLE); 28 buf.push_str(&STYLE);
32 buf.push_str("<pre><code>"); 29 buf.push_str("<pre><code>");
33 let tokens = parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.into_token()); 30 for range in &ranges {
34 for token in tokens { 31 if range.range.start() > prev_pos {
35 could_intersect.retain(|it| token.text_range().start() <= it.range.end()); 32 let curr = &text[prev_pos.to_usize()..range.range.start().to_usize()];
36 while let Some(r) = ranges.get(frontier) { 33 let text = html_escape(curr);
37 if r.range.start() <= token.text_range().end() {
38 could_intersect.push(r);
39 frontier += 1;
40 } else {
41 break;
42 }
43 }
44 let text = html_escape(&token.text());
45 let ranges = could_intersect
46 .iter()
47 .filter(|it| token.text_range().is_subrange(&it.range))
48 .collect::<Vec<_>>();
49 if ranges.is_empty() {
50 buf.push_str(&text); 34 buf.push_str(&text);
51 } else {
52 let classes = ranges
53 .iter()
54 .map(|it| it.highlight.to_string().replace('.', " "))
55 .collect::<Vec<_>>()
56 .join(" ");
57 let binding_hash = ranges.first().and_then(|x| x.binding_hash);
58 let color = match (rainbow, binding_hash) {
59 (true, Some(hash)) => format!(
60 " data-binding-hash=\"{}\" style=\"color: {};\"",
61 hash,
62 rainbowify(hash)
63 ),
64 _ => "".into(),
65 };
66 buf.push_str(&format!("<span class=\"{}\"{}>{}</span>", classes, color, text));
67 } 35 }
36 let curr = &text[range.range.start().to_usize()..range.range.end().to_usize()];
37
38 let class = range.highlight.to_string().replace('.', " ");
39 let color = match (rainbow, range.binding_hash) {
40 (true, Some(hash)) => {
41 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
42 }
43 _ => "".into(),
44 };
45 buf.push_str(&format!("<span class=\"{}\"{}>{}</span>", class, color, html_escape(curr)));
46
47 prev_pos = range.range.end();
68 } 48 }
49 // Add the remaining (non-highlighted) text
50 let curr = &text[prev_pos.to_usize()..];
51 let text = html_escape(curr);
52 buf.push_str(&text);
69 buf.push_str("</code></pre>"); 53 buf.push_str("</code></pre>");
70 buf 54 buf
71} 55}