From 65509ccde131812cc327fefb09740a845ee5d0e0 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Wed, 11 Nov 2026 09:41:56 +0700 Subject: [PATCH] editor: add plain language with no grammar --- crates/editor/src/highlight.rs | 2 ++ crates/editor/src/utils.rs | 3 ++- crates/editor/tests/code.rs | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/highlight.rs b/crates/editor/src/highlight.rs index cf3d109..8fa1b91 100644 --- a/crates/editor/src/highlight.rs +++ b/crates/editor/src/highlight.rs @@ -26,6 +26,7 @@ struct Highlighter { /// A language source can highlight as. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Language { + Plain, Rust, JavaScript, TypeScript, @@ -163,6 +164,7 @@ fn grammar_for(language: Language) -> Option<(tree_sitter::Language, &'static st Language::TypeScript => typescript_grammar(), Language::JavaScript => javascript_grammar(), Language::Python => python_grammar(), + Language::Plain => None, } } diff --git a/crates/editor/src/utils.rs b/crates/editor/src/utils.rs index 285a2d1..7ddf56d 100644 --- a/crates/editor/src/utils.rs +++ b/crates/editor/src/utils.rs @@ -52,9 +52,10 @@ impl Role { #[cfg(feature = "markdown")] impl Language { - /// Maps a fence info string to a bundled grammar. + /// Maps a fence info string to a language. pub(crate) fn from_fence_info(info: &str) -> Option { match info.to_ascii_lowercase().as_str() { + "plain" | "text" | "txt" => Some(Language::Plain), "rust" | "rs" => Some(Language::Rust), "typescript" | "ts" => Some(Language::TypeScript), "javascript" | "js" => Some(Language::JavaScript), diff --git a/crates/editor/tests/code.rs b/crates/editor/tests/code.rs index 22f1376..5027f13 100644 --- a/crates/editor/tests/code.rs +++ b/crates/editor/tests/code.rs @@ -42,6 +42,21 @@ fn a_rust_snippet_gets_its_roles_from_the_grammar() { ); } +#[test] +fn plain_language_never_applies_grammar_roles() { + let source = "// a comment\nfn main() {\n let greeting = \"hello\";\n}\n"; + let code = highlight(source, Language::Plain); + + assert_eq!(code.lines.len(), 5, "a trailing newline ends an empty line"); + assert!( + code.lines + .iter() + .flat_map(|line| line.spans.iter()) + .all(|span| span.role == Role::Text), + "plain renders every span as text, whatever the source looks like" + ); +} + #[test] fn a_capture_spanning_newlines_styles_both_lines() { let source = "let text = \"a\nb\";\n";