ide/syntax_highlighting/
html.rs

1//! Renders a bit of code as HTML.
2
3use hir::Semantics;
4use ide_db::MiniCore;
5use oorandom::Rand32;
6use stdx::format_to;
7use syntax::AstNode;
8
9use crate::{
10    FileId, RootDatabase,
11    syntax_highlighting::{HighlightConfig, highlight},
12};
13
14pub(crate) fn highlight_as_html_with_config(
15    db: &RootDatabase,
16    config: &HighlightConfig<'_>,
17    file_id: FileId,
18    rainbow: bool,
19) -> String {
20    let sema = Semantics::new(db);
21    let file_id = sema.attach_first_edition(file_id);
22    let file = sema.parse(file_id);
23    let file = file.syntax();
24    fn rainbowify(seed: u64) -> String {
25        let mut rng = Rand32::new(seed);
26        format!(
27            "hsl({h},{s}%,{l}%)",
28            h = rng.rand_range(0..361),
29            s = rng.rand_range(42..99),
30            l = rng.rand_range(40..91),
31        )
32    }
33
34    let hl_ranges = highlight(db, config, file_id.file_id(db), None);
35    let text = file.to_string();
36    let mut buf = String::new();
37    buf.push_str(STYLE);
38    buf.push_str("<pre><code>");
39    for r in &hl_ranges {
40        let chunk = html_escape(&text[r.range]);
41        if r.highlight.is_empty() {
42            format_to!(buf, "{}", chunk);
43            continue;
44        }
45
46        let class = r.highlight.to_string().replace('.', " ");
47        let color = match (rainbow, r.binding_hash) {
48            (true, Some(hash)) => {
49                format!(" data-binding-hash=\"{hash}\" style=\"color: {};\"", rainbowify(hash))
50            }
51            _ => "".into(),
52        };
53        format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, chunk);
54    }
55    buf.push_str("</code></pre>");
56    buf
57}
58
59pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
60    highlight_as_html_with_config(
61        db,
62        &HighlightConfig {
63            strings: true,
64            comments: true,
65            punctuation: true,
66            specialize_punctuation: true,
67            specialize_operator: true,
68            operator: true,
69            inject_doc_comment: true,
70            macro_bang: true,
71            syntactic_name_ref_highlighting: false,
72            minicore: MiniCore::default(),
73        },
74        file_id,
75        rainbow,
76    )
77}
78
79//FIXME: like, real html escaping
80fn html_escape(text: &str) -> String {
81    text.replace('<', "&lt;").replace('>', "&gt;")
82}
83
84const STYLE: &str = "
85<style>
86body                { margin: 0; }
87pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
88
89.lifetime           { color: #DFAF8F; font-style: italic; }
90.label              { color: #DFAF8F; font-style: italic; }
91.comment            { color: #7F9F7F; }
92.documentation      { color: #629755; }
93.intra_doc_link     { font-style: italic; }
94.injected           { opacity: 0.65 ; }
95.struct, .enum      { color: #7CB8BB; }
96.enum_variant       { color: #BDE0F3; }
97.string_literal     { color: #CC9393; }
98.field              { color: #94BFF3; }
99.function           { color: #93E0E3; }
100.parameter          { color: #94BFF3; }
101.text               { color: #DCDCCC; }
102.type               { color: #7CB8BB; }
103.builtin_type       { color: #8CD0D3; }
104.type_param         { color: #DFAF8F; }
105.attribute          { color: #94BFF3; }
106.numeric_literal    { color: #BFEBBF; }
107.bool_literal       { color: #BFE6EB; }
108.macro              { color: #94BFF3; }
109.proc_macro         { color: #94BFF3; text-decoration: underline; }
110.derive             { color: #94BFF3; font-style: italic; }
111.module             { color: #AFD8AF; }
112.value_param        { color: #DCDCCC; }
113.variable           { color: #DCDCCC; }
114.format_specifier   { color: #CC696B; }
115.mutable            { text-decoration: underline; }
116.escape_sequence    { color: #94BFF3; }
117.keyword            { color: #F0DFAF; font-weight: bold; }
118.control            { font-style: italic; }
119.reference          { font-style: italic; font-weight: bold; }
120.const              { font-weight: bolder; }
121.unsafe             { color: #BC8383; }
122.deprecated         { text-decoration: line-through; }
123
124.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
125.unresolved_reference    { color: #FC5555; text-decoration: wavy underline; }
126</style>
127";