.cargo/
crates/
brain/
diff/
files/
highlight/
benches/
src/
grammar/
blob.rs
14.3 KiB
error.rs
1.3 KiB
grammar.rs
10.3 KiB
json.rs
5.4 KiB
lib.rs
1.4 KiB
raw.rs
7.0 KiB
registry.rs
3.0 KiB
syntaxes.rs
2.2 KiB
tests.rs
34.0 KiB
theme.rs
18.7 KiB
util.rs
1.8 KiB
syntaxes/
tests/
Cargo.toml
492 B
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/highlight/src/raw.rs
raw
| 1 | //! Raw TextMate JSON data structures. |
| 2 | //! |
| 3 | //! These types mirror the loose JSON shapes accepted by TextMate and VS Code |
| 4 | //! grammar/theme files before the crate compiles them into stricter runtime |
| 5 | //! data. Keeping this layer separate lets parsing tolerate source-file quirks |
| 6 | //! without leaking those shapes into tokenization and styling code. |
| 7 | |
| 8 | use std::collections::BTreeMap; |
| 9 | use std::str::FromStr; |
| 10 | |
| 11 | use serde::{Deserialize, json as serde_json}; |
| 12 | |
| 13 | use crate::Error; |
| 14 | |
| 15 | /// A parsed, uncompiled TextMate grammar. |
| 16 | #[derive(Clone, Debug, Deserialize)] |
| 17 | pub struct RawGrammar { |
| 18 | /// Human-readable display name. |
| 19 | #[serde(rename = "displayName")] |
| 20 | pub display_name: Option<String>, |
| 21 | /// Human-readable grammar name. |
| 22 | pub name: String, |
| 23 | /// Root scope name. |
| 24 | #[serde(rename = "scopeName")] |
| 25 | pub scope_name: String, |
| 26 | /// File extensions and special file names. |
| 27 | #[serde(default)] |
| 28 | #[serde(rename = "fileTypes")] |
| 29 | pub file_types: Vec<String>, |
| 30 | /// First-line match expressions, as either a string or string array. |
| 31 | #[serde(rename = "firstLineMatch")] |
| 32 | pub first_line_match: Option<serde_json::Value>, |
| 33 | /// Root patterns. |
| 34 | pub patterns: Vec<RawPattern>, |
| 35 | /// Named pattern repository. |
| 36 | #[serde(default)] |
| 37 | pub repository: BTreeMap<String, RawPattern>, |
| 38 | /// Injection selectors and the pattern injected at each selector. |
| 39 | #[serde(default)] |
| 40 | pub injections: BTreeMap<String, RawPattern>, |
| 41 | } |
| 42 | |
| 43 | /// A parsed, uncompiled TextMate pattern. |
| 44 | #[derive(Clone, Debug, Default, Deserialize)] |
| 45 | pub struct RawPattern { |
| 46 | /// Scope assigned to the match. |
| 47 | pub name: Option<String>, |
| 48 | /// Scope assigned to a begin rule's inner content. |
| 49 | #[serde(rename = "contentName")] |
| 50 | pub content_name: Option<String>, |
| 51 | /// Single-line match expression. |
| 52 | #[serde(rename = "match")] |
| 53 | pub match_rule: Option<String>, |
| 54 | /// Begin expression for a stateful rule. |
| 55 | pub begin: Option<String>, |
| 56 | /// End expression for a stateful rule. |
| 57 | pub end: Option<String>, |
| 58 | /// Whether nested patterns should be searched before this rule's end pattern. |
| 59 | #[serde(rename = "applyEndPatternLast")] |
| 60 | pub apply_end_pattern_last: Option<serde_json::Value>, |
| 61 | /// While expression for a stateful rule. |
| 62 | #[serde(rename = "while")] |
| 63 | pub while_rule: Option<String>, |
| 64 | /// Nested patterns. |
| 65 | #[serde(default)] |
| 66 | pub patterns: Vec<RawPattern>, |
| 67 | /// Include target such as `$self`, `$base`, or `#name`. |
| 68 | pub include: Option<String>, |
| 69 | /// Captures shared by match rules and by begin/end/while rules that do not |
| 70 | /// define slot-specific captures. |
| 71 | pub captures: Option<BTreeMap<String, RawCapture>>, |
| 72 | /// Begin captures. |
| 73 | #[serde(rename = "beginCaptures")] |
| 74 | pub begin_captures: Option<BTreeMap<String, RawCapture>>, |
| 75 | /// End captures. |
| 76 | #[serde(rename = "endCaptures")] |
| 77 | pub end_captures: Option<BTreeMap<String, RawCapture>>, |
| 78 | /// While captures. |
| 79 | #[serde(rename = "whileCaptures")] |
| 80 | pub while_captures: Option<BTreeMap<String, RawCapture>>, |
| 81 | } |
| 82 | |
| 83 | /// A parsed, uncompiled TextMate capture. |
| 84 | #[derive(Clone, Debug, Deserialize)] |
| 85 | pub struct RawCapture { |
| 86 | /// Scope assigned to the captured group. |
| 87 | pub name: Option<String>, |
| 88 | /// Scope assigned to retokenized captured content. |
| 89 | #[serde(rename = "contentName")] |
| 90 | pub content_name: Option<String>, |
| 91 | /// Nested patterns used to retokenize the captured group. |
| 92 | #[serde(default)] |
| 93 | pub patterns: Vec<RawPattern>, |
| 94 | } |
| 95 | |
| 96 | /// A parsed, uncompiled TextMate theme. |
| 97 | #[derive(Clone, Debug, Deserialize)] |
| 98 | pub struct RawTheme { |
| 99 | /// Theme name. |
| 100 | pub name: String, |
| 101 | /// Editor UI color keys. |
| 102 | #[serde(default)] |
| 103 | pub colors: BTreeMap<String, String>, |
| 104 | /// TextMate theme settings. |
| 105 | #[serde(default)] |
| 106 | pub settings: Vec<RawThemeRule>, |
| 107 | /// TextMate token color rules. |
| 108 | #[serde(default)] |
| 109 | #[serde(rename = "tokenColors")] |
| 110 | pub token_colors: Vec<RawThemeRule>, |
| 111 | } |
| 112 | |
| 113 | /// One parsed TextMate theme rule. |
| 114 | #[derive(Clone, Debug, Deserialize)] |
| 115 | pub struct RawThemeRule { |
| 116 | /// Optional selector string or selector array. |
| 117 | pub scope: Option<serde_json::Value>, |
| 118 | /// Style settings. |
| 119 | pub settings: RawStyle, |
| 120 | } |
| 121 | |
| 122 | /// Parsed TextMate style settings. |
| 123 | #[derive(Clone, Debug, Default, Deserialize)] |
| 124 | pub struct RawStyle { |
| 125 | /// Foreground color as `#rrggbb`. |
| 126 | pub foreground: Option<String>, |
| 127 | /// Space-separated text style flags. |
| 128 | #[serde(rename = "fontStyle")] |
| 129 | pub font_style: Option<String>, |
| 130 | } |
| 131 | |
| 132 | impl RawGrammar { |
| 133 | /// Parses a raw grammar from JSON. |
| 134 | pub fn parse(input: &str) -> Result<Self, Error> { |
| 135 | input.parse() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl FromStr for RawGrammar { |
| 140 | type Err = Error; |
| 141 | |
| 142 | fn from_str(input: &str) -> Result<Self, Self::Err> { |
| 143 | serde_json::from_str(input).map_err(|_| Error::InvalidGrammar) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl RawTheme { |
| 148 | /// Parses a raw theme from JSON. |
| 149 | pub fn parse(input: &str) -> Result<Self, Error> { |
| 150 | input.parse() |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl FromStr for RawTheme { |
| 155 | type Err = Error; |
| 156 | |
| 157 | fn from_str(input: &str) -> Result<Self, Self::Err> { |
| 158 | serde_json::from_str(input).map_err(|_| Error::InvalidTheme) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | impl RawThemeRule { |
| 163 | /// Returns normalized selector strings from a TextMate `scope` value. |
| 164 | pub(crate) fn scope_selectors(&self) -> Vec<String> { |
| 165 | let Some(scope) = self.scope.as_ref() else { |
| 166 | return Vec::new(); |
| 167 | }; |
| 168 | let mut selectors = Vec::new(); |
| 169 | match scope { |
| 170 | serde_json::Value::String(scope) => push_selectors(scope, &mut selectors), |
| 171 | serde_json::Value::Array(scopes) => { |
| 172 | for scope in scopes { |
| 173 | if let serde_json::Value::String(scope) = scope { |
| 174 | push_selectors(scope, &mut selectors); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | _ => {} |
| 179 | } |
| 180 | selectors |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// Normalizes `firstLineMatch` from TextMate's string-or-array shape. |
| 185 | pub(crate) fn head_patterns(value: Option<&serde_json::Value>) -> Vec<String> { |
| 186 | match value { |
| 187 | Some(serde_json::Value::String(pattern)) => vec![pattern.clone()], |
| 188 | Some(serde_json::Value::Array(values)) => values |
| 189 | .iter() |
| 190 | .filter_map(|value| { |
| 191 | if let serde_json::Value::String(pattern) = value { |
| 192 | Some(pattern.clone()) |
| 193 | } else { |
| 194 | None |
| 195 | } |
| 196 | }) |
| 197 | .collect(), |
| 198 | _ => Vec::new(), |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Returns true for TextMate's loose truthy JSON flag values. |
| 203 | pub(crate) fn truthy_flag(value: Option<&serde_json::Value>) -> bool { |
| 204 | matches!( |
| 205 | value, |
| 206 | Some(serde_json::Value::Bool(true)) |
| 207 | | Some(serde_json::Value::Number(serde_json::Number::U64(1))) |
| 208 | | Some(serde_json::Value::Number(serde_json::Number::I64(1))) |
| 209 | | Some(serde_json::Value::Number(serde_json::Number::F64(1.0))) |
| 210 | ) |
| 211 | } |
| 212 | |
| 213 | /// Splits a comma-separated TextMate selector list. |
| 214 | fn push_selectors(input: &str, selectors: &mut Vec<String>) { |
| 215 | selectors.extend( |
| 216 | input |
| 217 | .split(',') |
| 218 | .map(str::trim) |
| 219 | .filter(|selector| !selector.is_empty()) |
| 220 | .map(str::to_owned), |
| 221 | ); |
| 222 | } |