reovim_driver_syntax/
highlight.rs1use std::{fmt, ops::Range, sync::Arc};
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash)]
42pub struct HighlightCategory(Arc<str>);
43
44impl HighlightCategory {
45 #[must_use]
47 pub fn new(s: impl Into<Arc<str>>) -> Self {
48 Self(s.into())
49 }
50
51 #[must_use]
53 pub fn as_str(&self) -> &str {
54 &self.0
55 }
56}
57
58impl std::fmt::Display for HighlightCategory {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.write_str(&self.0)
61 }
62}
63
64impl HighlightCategory {
68 pub const KEYWORD: &str = "keyword";
70 pub const KEYWORD_CONTROL: &str = "keyword.control";
71 pub const KEYWORD_OPERATOR: &str = "keyword.operator";
72 pub const KEYWORD_FUNCTION: &str = "keyword.function";
73 pub const KEYWORD_TYPE: &str = "keyword.type";
74
75 pub const TYPE: &str = "type";
77 pub const TYPE_BUILTIN: &str = "type.builtin";
78
79 pub const FUNCTION: &str = "function";
81 pub const FUNCTION_BUILTIN: &str = "function.builtin";
82 pub const FUNCTION_MACRO: &str = "function.macro";
83 pub const FUNCTION_METHOD: &str = "function.method";
84
85 pub const VARIABLE: &str = "variable";
87 pub const VARIABLE_BUILTIN: &str = "variable.builtin";
88 pub const VARIABLE_PARAMETER: &str = "variable.parameter";
89 pub const VARIABLE_FIELD: &str = "variable.field";
90 pub const CONSTANT: &str = "constant";
91
92 pub const STRING: &str = "string";
94 pub const STRING_ESCAPE: &str = "string.escape";
95 pub const CHARACTER: &str = "character";
96 pub const NUMBER: &str = "number";
97 pub const BOOLEAN: &str = "boolean";
98
99 pub const COMMENT: &str = "comment";
101 pub const COMMENT_DOC: &str = "comment.doc";
102
103 pub const PUNCTUATION: &str = "punctuation";
105 pub const PUNCTUATION_BRACKET: &str = "punctuation.bracket";
106 pub const PUNCTUATION_DELIMITER: &str = "punctuation.delimiter";
107
108 pub const OPERATOR: &str = "operator";
110
111 pub const DIAGNOSTIC_ERROR: &str = "diagnostic.error";
113 pub const DIAGNOSTIC_WARNING: &str = "diagnostic.warning";
114 pub const DIAGNOSTIC_INFO: &str = "diagnostic.info";
115 pub const DIAGNOSTIC_HINT: &str = "diagnostic.hint";
116
117 pub const NAMESPACE: &str = "namespace";
119 pub const CONSTRUCTOR: &str = "constructor";
120 pub const LABEL: &str = "label";
121 pub const ATTRIBUTE: &str = "attribute";
122 pub const TAG: &str = "tag";
123
124 pub const MARKUP_HEADING: &str = "markup.heading";
126 pub const MARKUP_BOLD: &str = "markup.bold";
127 pub const MARKUP_ITALIC: &str = "markup.italic";
128 pub const MARKUP_STRIKETHROUGH: &str = "markup.strikethrough";
129 pub const MARKUP_LINK: &str = "markup.link";
130 pub const MARKUP_LINK_URL: &str = "markup.link.url";
131 pub const MARKUP_LIST: &str = "markup.list";
132 pub const MARKUP_RAW: &str = "markup.raw";
133 pub const MARKUP_RAW_INLINE: &str = "markup.raw.inline";
134
135 pub const EMBEDDED: &str = "embedded";
137 pub const SPECIAL: &str = "special";
138}
139
140#[derive(Debug, Clone, PartialEq, Eq)]
158pub struct Annotation {
159 pub start_byte: usize,
161 pub end_byte: usize,
163 pub category: HighlightCategory,
165}
166
167impl Annotation {
168 #[must_use]
170 pub const fn new(start_byte: usize, end_byte: usize, category: HighlightCategory) -> Self {
171 Self {
172 start_byte,
173 end_byte,
174 category,
175 }
176 }
177
178 #[must_use]
180 pub const fn len(&self) -> usize {
181 self.end_byte - self.start_byte
182 }
183
184 #[must_use]
186 pub const fn is_empty(&self) -> bool {
187 self.start_byte == self.end_byte
188 }
189
190 #[must_use]
192 pub const fn overlaps(&self, range: &Range<usize>) -> bool {
193 self.start_byte < range.end && self.end_byte > range.start
194 }
195
196 #[must_use]
198 pub const fn contains(&self, byte: usize) -> bool {
199 self.start_byte <= byte && byte < self.end_byte
200 }
201
202 #[must_use]
204 pub const fn byte_range(&self) -> Range<usize> {
205 self.start_byte..self.end_byte
206 }
207}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq)]
218pub enum SyntaxContext {
219 Code,
221 String,
223 Comment,
225}
226
227#[cfg_attr(coverage_nightly, coverage(off))]
228impl fmt::Display for SyntaxContext {
229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230 match self {
231 Self::Code => f.write_str("code"),
232 Self::String => f.write_str("string"),
233 Self::Comment => f.write_str("comment"),
234 }
235 }
236}
237
238#[cfg(test)]
239#[path = "highlight_tests.rs"]
240mod tests;