]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/html.rs
Auto merge of #93873 - Stovent:big-ints, r=m-ou-se
[rust.git] / src / tools / rust-analyzer / crates / ide / src / syntax_highlighting / html.rs
1 //! Renders a bit of code as HTML.
2
3 use ide_db::base_db::SourceDatabase;
4 use oorandom::Rand32;
5 use stdx::format_to;
6 use syntax::AstNode;
7
8 use crate::{
9     syntax_highlighting::{highlight, HighlightConfig},
10     FileId, RootDatabase,
11 };
12
13 pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
14     let parse = db.parse(file_id);
15
16     fn rainbowify(seed: u64) -> String {
17         let mut rng = Rand32::new(seed);
18         format!(
19             "hsl({h},{s}%,{l}%)",
20             h = rng.rand_range(0..361),
21             s = rng.rand_range(42..99),
22             l = rng.rand_range(40..91),
23         )
24     }
25
26     let hl_ranges = highlight(
27         db,
28         HighlightConfig {
29             strings: true,
30             punctuation: true,
31             specialize_punctuation: true,
32             specialize_operator: true,
33             operator: true,
34             inject_doc_comment: true,
35             macro_bang: true,
36             syntactic_name_ref_highlighting: false,
37         },
38         file_id,
39         None,
40     );
41     let text = parse.tree().syntax().to_string();
42     let mut buf = String::new();
43     buf.push_str(STYLE);
44     buf.push_str("<pre><code>");
45     for r in &hl_ranges {
46         let chunk = html_escape(&text[r.range]);
47         if r.highlight.is_empty() {
48             format_to!(buf, "{}", chunk);
49             continue;
50         }
51
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))
56             }
57             _ => "".into(),
58         };
59         format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, chunk);
60     }
61     buf.push_str("</code></pre>");
62     buf
63 }
64
65 //FIXME: like, real html escaping
66 fn html_escape(text: &str) -> String {
67     text.replace('<', "&lt;").replace('>', "&gt;")
68 }
69
70 const STYLE: &str = "
71 <style>
72 body                { margin: 0; }
73 pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
74
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; }
111
112 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
113 </style>
114 ";