1 //! Renders a bit of code as HTML.
3 use ide_db::base_db::SourceDatabase;
9 syntax_highlighting::{highlight, HighlightConfig},
13 pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
14 let parse = db.parse(file_id);
16 fn rainbowify(seed: u64) -> String {
17 let mut rng = Rand32::new(seed);
20 h = rng.rand_range(0..361),
21 s = rng.rand_range(42..99),
22 l = rng.rand_range(40..91),
26 let hl_ranges = highlight(
31 specialize_punctuation: true,
32 specialize_operator: true,
34 inject_doc_comment: true,
36 syntactic_name_ref_highlighting: false,
41 let text = parse.tree().syntax().to_string();
42 let mut buf = String::new();
44 buf.push_str("<pre><code>");
46 let chunk = html_escape(&text[r.range]);
47 if r.highlight.is_empty() {
48 format_to!(buf, "{}", chunk);
52 let class = r.highlight.to_string().replace('.', " ");
53 let color = match (rainbow, r.binding_hash) {
54 (true, Some(hash)) => {
55 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
59 format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, chunk);
61 buf.push_str("</code></pre>");
65 //FIXME: like, real html escaping
66 fn html_escape(text: &str) -> String {
67 text.replace('<', "<").replace('>', ">")
73 pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
75 .lifetime { color: #DFAF8F; font-style: italic; }
76 .label { color: #DFAF8F; font-style: italic; }
77 .comment { color: #7F9F7F; }
78 .documentation { color: #629755; }
79 .intra_doc_link { font-style: italic; }
80 .injected { opacity: 0.65 ; }
81 .struct, .enum { color: #7CB8BB; }
82 .enum_variant { color: #BDE0F3; }
83 .string_literal { color: #CC9393; }
84 .field { color: #94BFF3; }
85 .function { color: #93E0E3; }
86 .function.unsafe { color: #BC8383; }
87 .trait.unsafe { color: #BC8383; }
88 .operator.unsafe { color: #BC8383; }
89 .mutable.unsafe { color: #BC8383; text-decoration: underline; }
90 .keyword.unsafe { color: #BC8383; font-weight: bold; }
91 .macro.unsafe { color: #BC8383; }
92 .parameter { color: #94BFF3; }
93 .text { color: #DCDCCC; }
94 .type { color: #7CB8BB; }
95 .builtin_type { color: #8CD0D3; }
96 .type_param { color: #DFAF8F; }
97 .attribute { color: #94BFF3; }
98 .numeric_literal { color: #BFEBBF; }
99 .bool_literal { color: #BFE6EB; }
100 .macro { color: #94BFF3; }
101 .derive { color: #94BFF3; font-style: italic; }
102 .module { color: #AFD8AF; }
103 .value_param { color: #DCDCCC; }
104 .variable { color: #DCDCCC; }
105 .format_specifier { color: #CC696B; }
106 .mutable { text-decoration: underline; }
107 .escape_sequence { color: #94BFF3; }
108 .keyword { color: #F0DFAF; font-weight: bold; }
109 .control { font-style: italic; }
110 .reference { font-style: italic; font-weight: bold; }
112 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }