1 //! Renders a bit of code as HTML.
3 use ide_db::base_db::SourceDatabase;
8 use crate::{syntax_highlighting::highlight, FileId, RootDatabase};
10 pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
11 let parse = db.parse(file_id);
13 fn rainbowify(seed: u64) -> String {
14 let mut rng = Rand32::new(seed);
17 h = rng.rand_range(0..361),
18 s = rng.rand_range(42..99),
19 l = rng.rand_range(40..91),
23 let hl_ranges = highlight(db, file_id, None, false);
24 let text = parse.tree().syntax().to_string();
25 let mut buf = String::new();
27 buf.push_str("<pre><code>");
29 let chunk = html_escape(&text[r.range]);
30 if r.highlight.is_empty() {
31 format_to!(buf, "{}", chunk);
35 let class = r.highlight.to_string().replace('.', " ");
36 let color = match (rainbow, r.binding_hash) {
37 (true, Some(hash)) => {
38 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
42 format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, chunk);
44 buf.push_str("</code></pre>");
48 //FIXME: like, real html escaping
49 fn html_escape(text: &str) -> String {
50 text.replace("<", "<").replace(">", ">")
56 pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
58 .lifetime { color: #DFAF8F; font-style: italic; }
59 .label { color: #DFAF8F; font-style: italic; }
60 .comment { color: #7F9F7F; }
61 .documentation { color: #629755; }
62 .injected { opacity: 0.65 ; }
63 .struct, .enum { color: #7CB8BB; }
64 .enum_variant { color: #BDE0F3; }
65 .string_literal { color: #CC9393; }
66 .field { color: #94BFF3; }
67 .function { color: #93E0E3; }
68 .function.unsafe { color: #BC8383; }
69 .operator.unsafe { color: #BC8383; }
70 .parameter { color: #94BFF3; }
71 .text { color: #DCDCCC; }
72 .type { color: #7CB8BB; }
73 .builtin_type { color: #8CD0D3; }
74 .type_param { color: #DFAF8F; }
75 .attribute { color: #94BFF3; }
76 .numeric_literal { color: #BFEBBF; }
77 .bool_literal { color: #BFE6EB; }
78 .macro { color: #94BFF3; }
79 .module { color: #AFD8AF; }
80 .value_param { color: #DCDCCC; }
81 .variable { color: #DCDCCC; }
82 .format_specifier { color: #CC696B; }
83 .mutable { text-decoration: underline; }
84 .escape_sequence { color: #94BFF3; }
85 .keyword { color: #F0DFAF; font-weight: bold; }
86 .keyword.unsafe { color: #BC8383; font-weight: bold; }
87 .control { font-style: italic; }
89 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }