From 6f6865164e81b807a96aae090acbc65e95dda701 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 2 Dec 2026 03:45:58 -0800 Subject: [PATCH 1/3] Highlight SCSS filters in native Slim --- .../catalog/test/test_mode_catalog.ml | 25 ++++++++++++ .../lang-haml-slim/README.md | 6 +-- .../codemirror_ml_lang_haml_slim.mli | 6 +-- .../logseq-native-modes/lang-haml-slim/dune | 1 + .../lang-haml-slim/slim.ml | 3 +- .../lang-haml-slim/template.ml | 39 +++++++++++++------ .../lang-haml-slim/template.mli | 2 +- 7 files changed, 62 insertions(+), 20 deletions(-) diff --git a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml index 2e2eb9b..24115ec 100644 --- a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml +++ b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml @@ -59,6 +59,30 @@ let check_slim_markdown_filter () = ; "quote", "> Quote"; "keyword", "const"; "number", "42" ; "keyword", "throw"; "number", "99"; "tag", "p" ] +let check_slim_scss_filter () = + let open Highlight.Tags in + let highlighter = + Highlight.Highlighter.of_tags + [ [variable_name], "variable"; [color], "color" + ; [property_name], "property"; [tag_name], "tag" ] + in + let source = + "section 😀\n scss:\n $accent: #c00;\n" + ^ " .card { color: $accent; }\n p Back to Slim" + in + let text = Input.Text.of_utf8 source in + let spans = ref [] in + Highlight.highlight_tree (parse "Slim" source) [highlighter] + ~put_style:(fun first last style -> + let chunk = Input.Text.slice text ~from:first ~to_:last in + spans := (style, Input.Text.to_wtf8 chunk) :: !spans); + let expected = + [ "tag", "section"; "variable", "$accent"; "color", "#c00" + ; "property", "color"; "variable", "$accent"; "tag", "p" ] + in + if List.rev !spans <> expected then + failwith "Slim SCSS should highlight the filter body and resume tag styling after dedent" + let check_lisp_brackets () = let module State = Codemirror_ml_state in let module View = Codemirror_ml_view.Editor_view in @@ -194,4 +218,5 @@ let () = (Logseq_native_modes.names ()); check_markdown_fences (); check_slim_markdown_filter (); + check_slim_scss_filter (); check_lisp_brackets () diff --git a/integrations/logseq-native-modes/lang-haml-slim/README.md b/integrations/logseq-native-modes/lang-haml-slim/README.md index eb44edf..93e315a 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/README.md +++ b/integrations/logseq-native-modes/lang-haml-slim/README.md @@ -24,9 +24,9 @@ modern language API and syntax-tree highlighter. wrapped and bare attributes, Ruby switches and interpolation, and indented text and Ruby regions. Wrapped attribute lists can span lines in both languages, including HAML's parenthesized attributes. -- Slim's JavaScript, CSS, and Markdown filters mount the corresponding native - grammars. Markdown starts at the first content line's indentation, and the - Logseq catalog resolves fenced languages inside it. +- Slim's JavaScript, CSS, Markdown, and SCSS filters mount the corresponding + native grammars. Markdown and SCSS use the first content line's indentation + as the filter margin. The Logseq catalog resolves fenced Markdown languages. Other filters beyond Ruby, HTML, and HAML's preserve filter are plain text. Individual Ruby attribute values are line-local. Spaces in a value are supported inside strings and balanced brackets. At the top level a space ends diff --git a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli index 6ef5b45..3b160da 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli +++ b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli @@ -18,6 +18,6 @@ val slim : ?code_languages:Codemirror_ml_lang_markdown.code_languages -> unit -> Codemirror_ml_language.Language.Support.t -(** Highlight Slim and its JavaScript, CSS, and Markdown filters. Embedded - markup uses the base language (HTML by default). [code_languages] resolves - fenced language names inside Markdown filters. *) +(** Highlight Slim and its JavaScript, CSS, Markdown, and SCSS filters. + Embedded markup uses the base language (HTML by default). [code_languages] + resolves fenced language names inside Markdown filters. *) diff --git a/integrations/logseq-native-modes/lang-haml-slim/dune b/integrations/logseq-native-modes/lang-haml-slim/dune index fe3167c..016356b 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/dune +++ b/integrations/logseq-native-modes/lang-haml-slim/dune @@ -7,6 +7,7 @@ codemirror-ml.lang-css codemirror-ml.lang-javascript codemirror-ml.lang-markdown + codemirror-ml.lang-sass codemirror-ml.language logseq-native-modes.legacy-modes codemirror-ml.lezer_common diff --git a/integrations/logseq-native-modes/lang-haml-slim/slim.ml b/integrations/logseq-native-modes/lang-haml-slim/slim.ml index 991d5bb..8080700 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -13,7 +13,8 @@ let filter line = | "javascript" -> Some (size, Javascript) | "css" -> Some (size, Css) | "markdown" -> Some (size, Markdown) - | "sass" | "scss" | "less" | "styl" | "coffee" + | "scss" -> Some (size, Scss) + | "sass" | "less" | "styl" | "coffee" | "asciidoc" | "textile" | "creole" | "wiki" | "mediawiki" | "rdoc" | "builder" | "nokogiri" | "erb" | "plain" -> Some (size, Text) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 6e76da0..d930dda 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -34,6 +34,8 @@ let javascript_region = node "JavaScriptRegion" 12 [] let css_region = node "CSSRegion" 13 [] let markdown_region = node "MarkdownRegion" 14 [] let markdown_text = node "MarkdownText" 15 [] +let scss_region = node "SCSSRegion" 16 [] +let scss_text = node "SCSSText" 17 [] let ruby_types = let open Highlight.Tags in @@ -55,7 +57,7 @@ let ruby_types = |> List.mapi (fun i (style, name, tag) -> (style, node name (20 + i) [ tag ])) type mark = Tag | Attribute | Keyword | Marker | Comment -type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown +type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown | Scss type action = Finish | Indented of body | Following_text of int | Ruby_line type ruby = { state : Ruby_mode.state; mutable closes : char list } type ruby_group = { ruby : ruby; closing : char } @@ -270,7 +272,7 @@ let html line = type region = { body : body; min_indent : int; - mutable markdown_indent : int option; + mutable content_indent : int option; mutable from : int; mutable to_ : int; mutable children : (int * Tree.child) list; @@ -285,6 +287,7 @@ let region_type = function | Javascript -> javascript_region | Css -> css_region | Markdown -> markdown_region + | Scss -> scss_region let next_column line index width = if at line index '\t' then width + tab_size - (width mod tab_size) @@ -297,13 +300,13 @@ let column line = in columns 0 0 -let markdown_line line body ~depth ~blank = +let filter_line type_ line body ~depth ~blank = if not blank then ( let indent = - match body.markdown_indent with + match body.content_indent with | Some indent -> indent | None -> - body.markdown_indent <- Some depth; + body.content_indent <- Some depth; depth in let rec offset i width = @@ -312,7 +315,7 @@ let markdown_line line body ~depth ~blank = in line.cursor <- offset 0 0); (* Keep line breaks in the overlay and omit only the template indentation. *) - emit line markdown_text line.width + emit line type_ line.width let read_line input from limit = let rec find from = @@ -383,7 +386,7 @@ let parser root rule after_group = (match !region with | Some body when (not blank) - && depth < Option.value ~default:body.min_indent body.markdown_indent -> + && depth < Option.value ~default:body.min_indent body.content_indent -> close_region () | _ -> ()); let line_node () = Tree.Node (tree line_type line.nodes line.width) in @@ -391,7 +394,10 @@ let parser root rule after_group = | Some body -> (* A nested parser starts at the first included source position. *) if body.children = [] then body.from <- from; - (if body.body = Markdown then markdown_line line body ~depth ~blank + (if body.body = Markdown then + filter_line markdown_text line body ~depth ~blank + else if body.body = Scss then + filter_line scss_text line body ~depth ~blank else if blank then Option.iter (fun ruby -> ruby_mode.blank_line ruby.state 2) @@ -430,7 +436,7 @@ let parser root rule after_group = { body; min_indent; - markdown_indent = None; + content_indent = None; from = after; to_ = after; children = []; @@ -512,6 +518,10 @@ let css_parser = Language.Lr_language.language Codemirror_ml_lang_css.css_language |> Language.parser +let scss_parser = + Language.Lr_language.language Codemirror_ml_lang_sass.sass_language + |> Language.parser + let with_html ?code_languages template base = let base = match base with Some base -> base | None -> Lazy.force default_html @@ -525,8 +535,8 @@ let with_html ?code_languages template base = (Codemirror_ml_lang_markdown.markdown ?code_languages () |> Language.Support.language |> Language.parser) in - let markdown_overlay node = - if Syntax_node.node_type node == markdown_text then Mix.Include else Mix.Skip + let filter_overlay type_ node = + if Syntax_node.node_type node == type_ then Mix.Include else Mix.Skip in let wrap = Mix.parse_mixed (fun node _ -> @@ -539,7 +549,12 @@ let with_html ?code_languages template base = Some { Mix.parser = css_parser; overlay = None; bracketed = false } else if type_ == markdown_region then Some { Mix.parser = Lazy.force markdown_parser; - overlay = Some (Mix.Predicate markdown_overlay); bracketed = false } + overlay = Some (Mix.Predicate (filter_overlay markdown_text)); + bracketed = false } + else if type_ == scss_region then + Some { Mix.parser = scss_parser; + overlay = Some (Mix.Predicate (filter_overlay scss_text)); + bracketed = false } else None) in let mixed = Parser.with_wrapper (Language.parser template) wrap in diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.mli b/integrations/logseq-native-modes/lang-haml-slim/template.mli index ae14650..c214e6f 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.mli +++ b/integrations/logseq-native-modes/lang-haml-slim/template.mli @@ -2,7 +2,7 @@ type line type mark = Tag | Attribute | Keyword | Marker | Comment -type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown +type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown | Scss type action = Finish | Indented of body | Following_text of int | Ruby_line val peek : ?offset:int -> line -> char -> bool From 690ce0e96008eb43854043976e1ea23de31b72d8 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 2 Dec 2026 03:57:53 -0800 Subject: [PATCH 2/3] Preserve Slim indentation when typing SCSS braces --- .../catalog/test/test_mode_catalog.ml | 33 +++++++++++++++++++ .../logseq-native-modes/lang-haml-slim/dune | 3 +- .../lang-haml-slim/template.ml | 6 ++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml index 24115ec..2343cca 100644 --- a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml +++ b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml @@ -83,6 +83,38 @@ let check_slim_scss_filter () = if List.rev !spans <> expected then failwith "Slim SCSS should highlight the filter body and resume tag styling after dedent" +let check_slim_scss_input () = + let open Codemirror_ml_state in + let language = Option.get (Logseq_native_modes.find "Slim") in + let prefix = "section\n scss:\n #{\n $accent\n " in + let suffix = " { color: red; }\n p Back to Slim" in + let caret = String.length prefix in + let state = + Editor_state.create + ~doc:(Change_set.String (Utf16.of_utf8 (prefix ^ suffix))) + ~selection:(Editor_selection.single caret) + ~extensions: + (Extension.group + [ Language.extension language + ; Codemirror_ml_language.Indent.indent_on_input () ]) + () + in + let changes = + Change_set.Edit + { from = caret + ; to_ = None + ; insert = Some (Change_set.String (Utf16.of_utf8 "}")) + } + in + let input = + Transaction.spec ~changes ~selection:(Editor_selection.single (caret + 1)) + ~user_event:"input.type" () + in + let state = Transaction.state (Editor_state.update state [input]) in + let expected = Utf16.of_utf8 (prefix ^ "}" ^ suffix) in + if not (Utf16.equal (Text.to_string (Editor_state.doc state)) expected) then + failwith "Typing a closing brace must preserve the Slim SCSS filter margin" + let check_lisp_brackets () = let module State = Codemirror_ml_state in let module View = Codemirror_ml_view.Editor_view in @@ -219,4 +251,5 @@ let () = check_markdown_fences (); check_slim_markdown_filter (); check_slim_scss_filter (); + check_slim_scss_input (); check_lisp_brackets () diff --git a/integrations/logseq-native-modes/lang-haml-slim/dune b/integrations/logseq-native-modes/lang-haml-slim/dune index 016356b..726e90a 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/dune +++ b/integrations/logseq-native-modes/lang-haml-slim/dune @@ -7,10 +7,11 @@ codemirror-ml.lang-css codemirror-ml.lang-javascript codemirror-ml.lang-markdown - codemirror-ml.lang-sass codemirror-ml.language logseq-native-modes.legacy-modes codemirror-ml.lezer_common codemirror-ml.lezer_highlight + codemirror-ml.lezer_lr + codemirror-ml.lezer_sass logseq-native-modes.streamparser) (private_modules template haml slim)) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index d930dda..44e7802 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -518,9 +518,9 @@ let css_parser = Language.Lr_language.language Codemirror_ml_lang_css.css_language |> Language.parser -let scss_parser = - Language.Lr_language.language Codemirror_ml_lang_sass.sass_language - |> Language.parser +(* The filter uses only the grammar. Standalone SCSS can reindent typed + closing braces past the Slim filter margin. *) +let scss_parser = Lezer_lr.Parse.as_parser Lezer_sass.parser let with_html ?code_languages template base = let base = From 331925c71cd1a996c6580c6660df88c89e40655f Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 2 Dec 2026 04:02:21 -0800 Subject: [PATCH 3/3] Keep SCSS comment syntax in Slim filters --- .../logseq-native-modes/catalog/test/dune | 1 + .../catalog/test/test_mode_catalog.ml | 24 +++++++++++++++++++ .../logseq-native-modes/lang-haml-slim/dune | 1 - .../lang-haml-slim/template.ml | 17 ++++++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/integrations/logseq-native-modes/catalog/test/dune b/integrations/logseq-native-modes/catalog/test/dune index c0d1059..a587559 100644 --- a/integrations/logseq-native-modes/catalog/test/dune +++ b/integrations/logseq-native-modes/catalog/test/dune @@ -3,6 +3,7 @@ (modes native) (libraries logseq-native-modes + codemirror-ml.commands codemirror-ml.language codemirror-ml.lezer_common codemirror-ml.lezer_highlight diff --git a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml index 2343cca..1b933fe 100644 --- a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml +++ b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml @@ -115,6 +115,29 @@ let check_slim_scss_input () = if not (Utf16.equal (Text.to_string (Editor_state.doc state)) expected) then failwith "Typing a closing brace must preserve the Slim SCSS filter margin" +let check_slim_scss_block_comment () = + let open Codemirror_ml_state in + let language = Option.get (Logseq_native_modes.find "Slim") in + let prefix = "section\n scss:\n .card { " in + let selected = "color: red;" in + let suffix = " }\n p Back to Slim" in + let from = String.length prefix in + let state = + Editor_state.create + ~doc:(Change_set.String (Utf16.of_utf8 (prefix ^ selected ^ suffix))) + ~selection:(Editor_selection.single from ~head:(from + String.length selected)) + ~extensions:(Language.extension language) + () + in + let result = ref state in + let changed = + Codemirror_ml_commands.Comment.toggle_block_comment ~state + ~dispatch:(fun tr -> result := Transaction.state tr) + in + let expected = Utf16.of_utf8 (prefix ^ "/* " ^ selected ^ " */" ^ suffix) in + if not changed || not (Utf16.equal (Text.to_string (Editor_state.doc !result)) expected) + then failwith "Slim SCSS must support block comments inside the filter" + let check_lisp_brackets () = let module State = Codemirror_ml_state in let module View = Codemirror_ml_view.Editor_view in @@ -252,4 +275,5 @@ let () = check_slim_markdown_filter (); check_slim_scss_filter (); check_slim_scss_input (); + check_slim_scss_block_comment (); check_lisp_brackets () diff --git a/integrations/logseq-native-modes/lang-haml-slim/dune b/integrations/logseq-native-modes/lang-haml-slim/dune index 726e90a..7d28364 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/dune +++ b/integrations/logseq-native-modes/lang-haml-slim/dune @@ -11,7 +11,6 @@ logseq-native-modes.legacy-modes codemirror-ml.lezer_common codemirror-ml.lezer_highlight - codemirror-ml.lezer_lr codemirror-ml.lezer_sass logseq-native-modes.streamparser) (private_modules template haml slim)) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 44e7802..50f2d6f 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -518,9 +518,20 @@ let css_parser = Language.Lr_language.language Codemirror_ml_lang_css.css_language |> Language.parser -(* The filter uses only the grammar. Standalone SCSS can reindent typed - closing braces past the Slim filter margin. *) -let scss_parser = Lezer_lr.Parse.as_parser Lezer_sass.parser +let scss_parser = + let data = + Language.Data.set Language.Data.comment_tokens + { line = Some (Text.of_utf8 "//") + ; block = Some { opening = Text.of_utf8 "/*"; closing = Text.of_utf8 "*/" } + } + Language.Data.empty + in + (* Keep SCSS comment syntax without the standalone brace reindentation, + which can move the input outside the filter margin. *) + Lezer_sass.parser + |> Language.Lr_language.define ~name:"scss" ~language_data:data + |> Language.Lr_language.language + |> Language.parser let with_html ?code_languages template base = let base =