ndg-commonmark 2.7.2

Flavored CommonMark processor for Nix-related projects, with support for CommonMark, GFM, and Nixpkgs extensions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Syntastica-based syntax highlighting backend.
//!
//! This module provides a modern tree-sitter based syntax highlighter using the
//! Syntastica library, which offers excellent language support including native
//! Nix highlighting.
//!
//! ## Theme Support
//!
//! We programmatically load all available themes from `syntastica-themes`
//! Some of the popular themes included are:
//!
//! - github (dark/light variants)
//! - gruvbox (dark/light)
//! - nord, dracula, catppuccin
//! - tokyo night, solarized, monokai
//! - And many more...

use std::{
  collections::HashMap,
  fs,
  path::{Path, PathBuf},
  sync::Mutex,
};

use syntastica::{
  Processor,
  language_set::{HighlightConfiguration, LanguageSet},
  render,
  renderer::HtmlRenderer,
};
use syntastica_core::theme::ResolvedTheme;
use syntastica_parsers::{LANGUAGES, Lang};
use syntastica_query_preprocessor::{
  process_highlights,
  process_injections,
  process_locals,
};

use super::{
  error::{SyntaxError, SyntaxResult},
  types::{SyntaxConfig, SyntaxHighlighter, SyntaxManager},
};

/// Syntastica-based syntax highlighter.
pub struct SyntasticaHighlighter {
  themes:        HashMap<String, ResolvedTheme>,
  default_theme: ResolvedTheme,
  processor:     Mutex<Processor<'static, UserQueryLanguageSet>>,
  renderer:      Mutex<HtmlRenderer>,
}

struct UserQueryLanguageSet {
  configs: HashMap<Lang, HighlightConfiguration>,
}

impl UserQueryLanguageSet {
  fn new(syntax_queries_dir: Option<&Path>) -> SyntaxResult<Self> {
    let mut configs = HashMap::new();

    for &lang in LANGUAGES {
      let mut highlights_query = lang.highlights_query().to_string();
      let mut injections_query = lang.injections_query().to_string();
      let mut locals_query = lang.locals_query().to_string();

      if let Some(base_dir) = syntax_queries_dir {
        if let Some(query) = read_user_query(base_dir, lang, "highlights.scm")?
        {
          let extends = is_extends_query(&query);
          let processed =
            process_highlights("", true, &rewrite_any_of_predicates(&query));
          if extends {
            highlights_query = format!("{highlights_query}\n{processed}");
          } else {
            highlights_query = processed;
          }
        }

        if let Some(query) = read_user_query(base_dir, lang, "injections.scm")?
        {
          let extends = is_extends_query(&query);
          let processed =
            process_injections("", true, &rewrite_any_of_predicates(&query));
          if extends {
            injections_query = format!("{injections_query}\n{processed}");
          } else {
            injections_query = processed;
          }
        }

        if let Some(query) = read_user_query(base_dir, lang, "locals.scm")? {
          let extends = is_extends_query(&query);
          let processed =
            process_locals("", true, &rewrite_any_of_predicates(&query));
          if extends {
            locals_query = format!("{locals_query}\n{processed}");
          } else {
            locals_query = processed;
          }
        }
      }

      let mut config = HighlightConfiguration::new(
        lang.get(),
        <&str>::from(lang),
        &highlights_query,
        &injections_query,
        &locals_query,
      )
      .map_err(|e| {
        SyntaxError::BackendError(format!(
          "failed to build highlight config for '{}': {e}",
          <&str>::from(lang)
        ))
      })?;
      config.configure(syntastica::theme::THEME_KEYS);
      configs.insert(lang, config);
    }

    Ok(Self { configs })
  }
}

impl<'s> LanguageSet<'s> for UserQueryLanguageSet {
  type Language = Lang;

  fn get_language(
    &self,
    language: Self::Language,
  ) -> syntastica::Result<&HighlightConfiguration> {
    self.configs.get(&language).ok_or_else(|| {
      syntastica::Error::UnsupportedLanguage(<&str>::from(language).to_string())
    })
  }
}

fn is_extends_query(content: &str) -> bool {
  content
    .lines()
    .next()
    .map(|l| matches!(l.trim(), ";; extends" | ";;extends"))
    .unwrap_or(false)
}

/// Rewrites `(#any-of? @cap "a" "b" ...)` into `(#match? @cap "^(a|b|...)$")`.
///
/// nvim-treesitter's `#any-of?` is a Lua-backed predicate with no standard
/// tree-sitter equivalent. The rewrite preserves the same semantics using the
/// `#match?` predicate that tree-sitter-highlight natively supports.
fn rewrite_any_of_predicates(query: &str) -> String {
  const NEEDLE: &str = "#any-of?";
  let mut result = String::with_capacity(query.len());
  let mut remaining = query;

  loop {
    match remaining.find(NEEDLE) {
      None => {
        result.push_str(remaining);
        break;
      },
      Some(pos) => {
        result.push_str(&remaining[..pos]);
        let from = &remaining[pos..];
        match parse_any_of_predicate(from) {
          Some((replacement, consumed)) => {
            result.push_str(&replacement);
            remaining = &from[consumed..];
          },
          None => {
            result.push_str(NEEDLE);
            remaining = &from[NEEDLE.len()..];
          },
        }
      },
    }
  }

  result
}

fn parse_any_of_predicate(s: &str) -> Option<(String, usize)> {
  const NEEDLE: &str = "#any-of?";
  let mut pos = NEEDLE.len();

  let skip_ws = |p: usize| p + s[p..].len() - s[p..].trim_start().len();

  pos = skip_ws(pos);

  if !s[pos..].starts_with('@') {
    return None;
  }

  let cap_start = pos;
  pos += 1;
  while pos < s.len() {
    let b = s.as_bytes()[pos];
    if b.is_ascii_whitespace() || b == b')' {
      break;
    }
    pos += 1;
  }
  let capture_name = &s[cap_start..pos];

  pos = skip_ws(pos);

  let mut values: Vec<&str> = Vec::new();
  while pos < s.len() && s.as_bytes()[pos] == b'"' {
    pos += 1;
    let val_start = pos;
    while pos < s.len() && s.as_bytes()[pos] != b'"' {
      if s.as_bytes()[pos] == b'\\' {
        pos += 1;
      }
      pos += 1;
    }
    if pos >= s.len() {
      return None;
    }
    values.push(&s[val_start..pos]);
    pos += 1;
    pos = skip_ws(pos);
  }

  if values.is_empty() {
    return None;
  }

  let pattern = format!(
    "^({})$",
    values
      .iter()
      .map(|v| ts_regex_escape(v))
      .collect::<Vec<_>>()
      .join("|")
  );
  Some((format!("#match? {capture_name} \"{pattern}\""), pos))
}

fn ts_regex_escape(s: &str) -> String {
  let mut out = String::with_capacity(s.len());
  for c in s.chars() {
    if matches!(
      c,
      '.'
        | '*'
        | '+'
        | '?'
        | '^'
        | '$'
        | '{'
        | '}'
        | '['
        | ']'
        | '|'
        | '('
        | ')'
        | '\\'
    ) {
      out.push('\\');
    }
    out.push(c);
  }
  out
}

fn read_user_query(
  base_dir: &Path,
  lang: Lang,
  file_name: &str,
) -> SyntaxResult<Option<String>> {
  let query_path = query_path_for_lang(base_dir, lang, file_name);
  if !query_path.exists() {
    return Ok(None);
  }

  fs::read_to_string(&query_path).map(Some).map_err(|e| {
    SyntaxError::BackendError(format!(
      "failed to read query override '{}': {e}",
      query_path.display()
    ))
  })
}

fn query_path_for_lang(
  base_dir: &Path,
  lang: Lang,
  file_name: &str,
) -> PathBuf {
  base_dir.join(<&str>::from(lang)).join(file_name)
}

impl SyntasticaHighlighter {
  /// Create a new Syntastica highlighter with all available themes.
  ///
  /// # Errors
  ///
  /// Currently never returns an error, but returns a Result for API
  /// consistency.
  pub fn new(syntax_queries_dir: Option<&Path>) -> SyntaxResult<Self> {
    let mut themes = HashMap::new();

    // Load all available themes
    for theme_name in syntastica_themes::THEMES {
      if let Some(theme) = syntastica_themes::from_str(theme_name) {
        themes.insert((*theme_name).to_string(), theme);
      }
    }

    let default_theme = syntastica_themes::one::dark();

    // Leak the language set into a `'static` reference so the `Processor` can
    // hold it for the remainder of the process lifetime. This is sound for a
    // CLI: the process exits when documentation generation completes and the OS
    // reclaims the memory. It avoids the unsound lifetime fabrication that a
    // raw-pointer cast would require.
    let language_set_static: &'static UserQueryLanguageSet =
      Box::leak(Box::new(UserQueryLanguageSet::new(syntax_queries_dir)?));
    let processor = Processor::new(language_set_static);

    Ok(Self {
      themes,
      default_theme,
      processor: Mutex::new(processor),
      renderer: Mutex::new(HtmlRenderer::new()),
    })
  }

  /// Add a custom theme
  pub fn add_theme(&mut self, name: String, theme: ResolvedTheme) {
    self.themes.insert(name, theme);
  }

  /// Set the default theme
  pub fn set_default_theme(&mut self, theme: ResolvedTheme) {
    self.default_theme = theme;
  }

  /// Convert a language string to a Lang enum
  fn parse_language(language: &str) -> Option<Lang> {
    match language.to_lowercase().as_str() {
      "rust" | "rs" => Some(Lang::Rust),
      "python" | "py" => Some(Lang::Python),
      "javascript" | "js" => Some(Lang::Javascript),
      "typescript" | "ts" => Some(Lang::Typescript),
      "tsx" => Some(Lang::Tsx),
      "nix" => Some(Lang::Nix),
      "bash" | "sh" | "shell" => Some(Lang::Bash),
      "c" => Some(Lang::C),
      "cpp" | "c++" | "cxx" => Some(Lang::Cpp),
      "c_sharp" | "csharp" | "cs" => Some(Lang::CSharp),
      "go" => Some(Lang::Go),
      "java" => Some(Lang::Java),
      "json" => Some(Lang::Json),
      "yaml" | "yml" => Some(Lang::Yaml),
      "html" => Some(Lang::Html),
      "css" => Some(Lang::Css),
      "markdown" | "md" => Some(Lang::Markdown),
      "markdown_inline" => Some(Lang::MarkdownInline),
      "sql" => Some(Lang::Sql),
      "lua" => Some(Lang::Lua),
      "ruby" | "rb" => Some(Lang::Ruby),
      "php" => Some(Lang::Php),
      "php_only" => Some(Lang::PhpOnly),
      "haskell" | "hs" => Some(Lang::Haskell),
      "scala" => Some(Lang::Scala),
      "swift" => Some(Lang::Swift),
      "makefile" | "make" => Some(Lang::Make),
      "cmake" => Some(Lang::Cmake),
      "asm" | "assembly" => Some(Lang::Asm),
      "diff" | "patch" => Some(Lang::Diff),
      "elixir" | "ex" | "exs" => Some(Lang::Elixir),
      "jsdoc" => Some(Lang::Jsdoc),
      "printf" => Some(Lang::Printf),
      "regex" | "regexp" => Some(Lang::Regex),
      "zig" => Some(Lang::Zig),
      #[allow(clippy::match_same_arms, reason = "Explicit for documentation")]
      "text" | "txt" | "plain" => None, // use fallback for plain text
      _ => None,
    }
  }

  /// Get the theme by name, falling back to default
  fn get_theme(&self, theme_name: Option<&str>) -> &ResolvedTheme {
    theme_name
      .and_then(|name| self.themes.get(name))
      .unwrap_or(&self.default_theme)
  }
}

impl SyntaxHighlighter for SyntasticaHighlighter {
  fn name(&self) -> &'static str {
    "Syntastica"
  }

  fn supported_languages(&self) -> Vec<String> {
    vec![
      "rust",
      "rs",
      "python",
      "py",
      "javascript",
      "js",
      "typescript",
      "ts",
      "tsx",
      "nix",
      "bash",
      "sh",
      "shell",
      "c",
      "cpp",
      "c++",
      "cxx",
      "c_sharp",
      "csharp",
      "cs",
      "go",
      "java",
      "json",
      "yaml",
      "yml",
      "html",
      "css",
      "markdown",
      "md",
      "markdown_inline",
      "sql",
      "lua",
      "ruby",
      "rb",
      "php",
      "php_only",
      "haskell",
      "hs",
      "scala",
      "swift",
      "makefile",
      "make",
      "cmake",
      "asm",
      "assembly",
      "diff",
      "patch",
      "elixir",
      "ex",
      "exs",
      "jsdoc",
      "printf",
      "regex",
      "regexp",
      "zig",
      "text",
      "txt",
      "plain",
    ]
    .into_iter()
    .map(String::from)
    .collect()
  }

  fn available_themes(&self) -> Vec<String> {
    let mut themes: Vec<String> = self.themes.keys().cloned().collect();
    themes.sort();
    themes
  }

  fn highlight(
    &self,
    code: &str,
    language: &str,
    theme: Option<&str>,
  ) -> SyntaxResult<String> {
    let lang = Self::parse_language(language)
      .ok_or_else(|| SyntaxError::UnsupportedLanguage(language.to_string()))?;

    let theme = self.get_theme(theme);

    // Use the reusable processor via Mutex for thread-safe interior mutability
    let highlights = self
      .processor
      .lock()
      .map_err(|e| {
        SyntaxError::HighlightingFailed(format!("Processor lock poisoned: {e}"))
      })?
      .process(code, lang)
      .map_err(|e| SyntaxError::HighlightingFailed(e.to_string()))?;

    // Use the reusable renderer via Mutex for thread-safe interior mutability
    let html = {
      let mut renderer = self.renderer.lock().map_err(|e| {
        SyntaxError::HighlightingFailed(format!("Renderer lock poisoned: {e}"))
      })?;
      render(&highlights, &mut *renderer, theme)
    };

    Ok(html)
  }

  fn language_from_extension(&self, extension: &str) -> Option<String> {
    match extension.to_lowercase().as_str() {
      "rs" => Some("rust".to_string()),
      "py" | "pyw" => Some("python".to_string()),
      "js" | "mjs" => Some("javascript".to_string()),
      "ts" => Some("typescript".to_string()),
      "tsx" => Some("tsx".to_string()),
      "nix" => Some("nix".to_string()),
      "sh" | "bash" | "zsh" | "fish" => Some("bash".to_string()),
      "c" | "h" => Some("c".to_string()),
      "cpp" | "cxx" | "cc" | "hpp" | "hxx" | "hh" => Some("cpp".to_string()),
      "cs" => Some("c_sharp".to_string()),
      "go" => Some("go".to_string()),
      "java" => Some("java".to_string()),
      "json" => Some("json".to_string()),
      "yaml" | "yml" => Some("yaml".to_string()),
      "html" | "htm" => Some("html".to_string()),
      "css" => Some("css".to_string()),
      "md" | "markdown" => Some("markdown".to_string()),
      "sql" => Some("sql".to_string()),
      "lua" => Some("lua".to_string()),
      "rb" => Some("ruby".to_string()),
      "php" => Some("php".to_string()),
      "hs" => Some("haskell".to_string()),
      "ml" | "mli" => Some("ocaml".to_string()),
      "scala" => Some("scala".to_string()),
      "swift" => Some("swift".to_string()),
      "s" | "asm" => Some("asm".to_string()),
      "diff" | "patch" => Some("diff".to_string()),
      "ex" | "exs" => Some("elixir".to_string()),
      "zig" => Some("zig".to_string()),
      "txt" => Some("text".to_string()),
      _ => None,
    }
  }
}

/// Create a Syntastica-based syntax manager with default configuration.
///
/// Syntastica provides modern tree-sitter based syntax highlighting with
/// excellent language support including native Nix highlighting.
///
/// # Errors
///
/// Returns an error if the Syntastica highlighter fails to initialize.
pub fn create_syntastica_manager(
  syntax_queries_dir: Option<&Path>,
) -> SyntaxResult<SyntaxManager> {
  let highlighter = Box::new(SyntasticaHighlighter::new(syntax_queries_dir)?);
  let config = SyntaxConfig {
    default_theme: Some("one-dark".to_string()),
    ..Default::default()
  };
  Ok(SyntaxManager::new(highlighter, config))
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_is_extends_query() {
    assert!(is_extends_query(";; extends\n(foo) @bar"));
    assert!(is_extends_query(";;extends\n(foo) @bar"));
    assert!(!is_extends_query("(foo) @bar"));
    assert!(!is_extends_query(""));
    assert!(!is_extends_query("; extends")); // single semicolon is a comment, not the directive
  }

  #[test]
  fn test_rewrite_any_of_basic() {
    let input = r#"((identifier) @_name (#any-of? @_name "foo" "bar"))"#;
    let output = rewrite_any_of_predicates(input);
    assert!(output.contains("#match?"));
    assert!(output.contains("@_name"));
    assert!(output.contains("^(foo|bar)$"));
    assert!(!output.contains("#any-of?"));
  }

  #[test]
  fn test_rewrite_any_of_multiple() {
    let input = r#"
      ((identifier) @a (#any-of? @a "x" "y"))
      ((identifier) @b (#any-of? @b "p" "q" "r"))
    "#;
    let output = rewrite_any_of_predicates(input);
    assert_eq!(output.matches("#match?").count(), 2);
    assert!(!output.contains("#any-of?"));
    assert!(output.contains("^(x|y)$"));
    assert!(output.contains("^(p|q|r)$"));
  }

  #[test]
  fn test_rewrite_any_of_regex_escaping() {
    let input = r#"((identifier) @a (#any-of? @a "foo.bar" "baz"))"#;
    let output = rewrite_any_of_predicates(input);
    assert!(output.contains("foo\\.bar"));
  }

  #[test]
  fn test_rewrite_any_of_no_match_passthrough() {
    let input = "(foo) @bar (#eq? @bar \"baz\")";
    let output = rewrite_any_of_predicates(input);
    assert_eq!(input, output);
  }

  #[test]
  fn test_rewrite_any_of_nvf_nix_query() {
    // Matches the actual query from nvf's nix.nix
    let input = r#"
;; extends

((apply_expression
  function: (variable_expression
    name: (identifier) @_func
    (#any-of? @_func "mkLuaInline" "entryAnywhere"))
  argument: (indented_string_expression
    (string_fragment) @injection.content))
(#set! injection.language "lua")
(#set! injection.combined))
"#;
    let output = rewrite_any_of_predicates(input);
    assert!(!output.contains("#any-of?"));
    assert!(
      output.contains("#match? @_func \"^(mkLuaInline|entryAnywhere)$\"")
    );
    // Non-any-of predicates must be preserved
    assert!(output.contains("#set! injection.language"));
    assert!(output.contains("#set! injection.combined"));
    assert!(output.contains(";; extends"));
  }
}