From 922cbe0da5655caf29a39b9ac7c0f0a687524f3a Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 30 Nov 2026 10:02:11 -0800 Subject: [PATCH 1/4] Highlight multiline wrapped HAML and Slim attributes --- .../lang-haml-slim/README.md | 9 +- .../lang-haml-slim/slim.ml | 5 +- .../lang-haml-slim/template.ml | 86 ++++++++++--------- .../test/template_highlight_test.ml | 12 +++ 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/integrations/logseq-native-modes/lang-haml-slim/README.md b/integrations/logseq-native-modes/lang-haml-slim/README.md index c3d4190..c56e090 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/README.md +++ b/integrations/logseq-native-modes/lang-haml-slim/README.md @@ -22,10 +22,11 @@ modern language API and syntax-tree highlighter. - HAML supports tag selectors, attributes, inline Ruby, multiline Ruby hash attributes, and interpolation. Slim supports tag selectors, inline nesting, wrapped and bare attributes, Ruby switches and interpolation, and indented - text and Ruby regions. + text and Ruby regions. Wrapped attribute lists can span lines in both + languages, including HAML's parenthesized attributes. - Filters other than Ruby and HTML or HAML's preserve filter are plain text. - Multiline parenthesized HAML attributes and multiline wrapped Slim attributes - are currently line-local. Spaces in Ruby attribute values are supported inside - strings and balanced brackets. At the top level a space ends the value. + 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 + the value. - Tab indentation uses a width of four columns. The parser computes regions from the input on every parse. Indent and fold services are outside this provider. diff --git a/integrations/logseq-native-modes/lang-haml-slim/slim.ml b/integrations/logseq-native-modes/lang-haml-slim/slim.ml index 5d1d858..58ff6ef 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -32,6 +32,9 @@ let rec element line = mark line Marker 1; if peek line '<' || peek line '>' then mark line Marker 1); selectors line; + after_group line + +and after_group line = attributes ~bare:true line; content line @@ -69,4 +72,4 @@ let parse line = Indented body | None -> element line -let language = language ~name:"slim" ~comment:"/" parse +let language = language ~after_group ~name:"slim" ~comment:"/" parse diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index fa0a4f7..c1cb3e5 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -55,6 +55,7 @@ type body = Comments | Text | Html | Ruby 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 } +type group = Ruby_group of ruby_group | Attribute_group of char type line = { text : Text.t; @@ -62,7 +63,7 @@ type line = { width : int; mutable cursor : int; mutable nodes : (int * Tree.child) list; - mutable group : ruby_group option; + mutable group : group option; } let code line at = @@ -182,49 +183,51 @@ let ruby_tokens ruby stop line = in loop false -let resume_group ~multiline group line = +let resume_ruby_group ~multiline group line = ignore (ruby_tokens group.ruby (( = ) (Char.code group.closing)) line); if peek line group.closing then mark line Marker 1 - else if multiline then line.group <- Some group + else if multiline then line.group <- Some (Ruby_group group) let ruby_group ?(multiline = false) line ~closing = mark line Marker 1; - resume_group ~multiline { ruby = fresh_ruby (); closing } line + resume_ruby_group ~multiline { ruby = fresh_ruby (); closing } line let ruby_value line = ignore (ruby_tokens (fresh_ruby ()) is_space line) +let rec scan_attributes line closing = + spaces line; + if at_end line then + line.group <- Option.map (fun close -> Attribute_group close) closing + else + let size = name_length line ~offset:0 in + let equal = next_nonspace line (line.cursor + size) in + match closing with + | Some close when peek line close -> mark line Marker 1 + | _ when size > 0 && at line equal '=' -> + mark line Attribute size; + spaces line; + mark line Marker (if starts line "==" then 2 else 1); + spaces line; + let stop unit = + is_space unit + || unit = Char.code ',' + || Option.exists (fun close -> Char.code close = unit) closing + in + ignore (ruby_tokens (fresh_ruby ()) stop line); + scan_attributes line closing + | None when peek line '*' -> + mark line Marker 1; + ruby_value line; + scan_attributes line closing + | Some _ when size > 0 -> + mark line Attribute size; + scan_attributes line closing + | Some _ -> + skip line 1; + scan_attributes line closing + | None -> () + let attributes ~bare line = - let rec scan closing = - spaces line; - if line.cursor < line.limit then - let size = name_length line ~offset:0 in - let equal = next_nonspace line (line.cursor + size) in - match closing with - | Some close when peek line close -> mark line Marker 1 - | _ when size > 0 && at line equal '=' -> - mark line Attribute size; - spaces line; - mark line Marker (if starts line "==" then 2 else 1); - spaces line; - let stop unit = - is_space unit - || unit = Char.code ',' - || Option.exists (fun close -> Char.code close = unit) closing - in - ignore (ruby_tokens (fresh_ruby ()) stop line); - scan closing - | None when bare && peek line '*' -> - mark line Marker 1; - ruby_value line; - scan closing - | Some _ when size > 0 -> - mark line Attribute size; - scan closing - | Some _ -> - skip line 1; - scan closing - | None -> () - in spaces line; let rec wrappers () = match closer (code line line.cursor) with @@ -233,10 +236,11 @@ let attributes ~bare line = ruby_group ~multiline:true line ~closing:close else ( mark line Marker 1; - scan (Some close)); - spaces line; - wrappers () - | None -> if bare then scan None + scan_attributes line (Some close)); + if Option.is_none line.group then ( + spaces line; + wrappers ()) + | None -> if bare then scan_attributes line None in wrappers () @@ -374,7 +378,9 @@ let parser root rule after_group = match (!group, !pending) with | Some active, _ -> line.cursor <- 0; - resume_group ~multiline:true active line; + (match active with + | Ruby_group group -> resume_ruby_group ~multiline:true group line + | Attribute_group close -> scan_attributes line (Some close)); if line.group = None then after_group line else Finish | None, _ when blank -> Finish | None, Some _ -> Ruby_line diff --git a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml index 5989465..69d22d0 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml @@ -100,4 +100,16 @@ let () = (148, 157, "comment"); (161, 163, "tag"); (218, 222, "tag"); + ]; + List.iter + (fun (name, support, source, expected) -> check name support source expected) + [ + ( "multiline HAML attributes", Provider.haml (), + "😀\n%a(\n title=user.name\n aria-label=\"café 😀\"\n)= user.url\n%p end\n", + [ (9, 14, "attribute"); (20, 24, "property"); (27, 37, "attribute"); + (38, 47, "string"); (56, 59, "property"); (60, 62, "tag") ] ); + ( "multiline Slim attributes", Provider.slim (), + "😀\na[\n title=user.name\n\n aria-label=\"café 😀\"\n]= user.url\np end\n", + [ (8, 13, "attribute"); (19, 23, "property"); (27, 37, "attribute"); + (38, 47, "string"); (56, 59, "property"); (60, 61, "tag") ] ); ] From 2560e55035bbff6db8f374b595e7e0ce348eb552 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 30 Nov 2026 10:07:07 -0800 Subject: [PATCH 2/4] Resume template text after multiline attribute wrappers --- .../logseq-native-modes/lang-haml-slim/slim.ml | 6 ++---- .../lang-haml-slim/template.ml | 6 ++---- .../test/template_highlight_test.ml | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/integrations/logseq-native-modes/lang-haml-slim/slim.ml b/integrations/logseq-native-modes/lang-haml-slim/slim.ml index 58ff6ef..692fd1c 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -32,13 +32,11 @@ let rec element line = mark line Marker 1; if peek line '<' || peek line '>' then mark line Marker 1); selectors line; - after_group line - -and after_group line = attributes ~bare:true line; content line and content line = + spaces line; if peek line '=' then ruby_switch line else if peek line ':' then ( mark line Marker 1; @@ -72,4 +70,4 @@ let parse line = Indented body | None -> element line -let language = language ~after_group ~name:"slim" ~comment:"/" parse +let language = language ~after_group:content ~name:"slim" ~comment:"/" parse diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index c1cb3e5..2020689 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -228,7 +228,7 @@ let rec scan_attributes line closing = | None -> () let attributes ~bare line = - spaces line; + if bare then spaces line; let rec wrappers () = match closer (code line line.cursor) with | Some close -> @@ -237,9 +237,7 @@ let attributes ~bare line = else ( mark line Marker 1; scan_attributes line (Some close)); - if Option.is_none line.group then ( - spaces line; - wrappers ()) + if not bare && Option.is_none line.group then wrappers () | None -> if bare then scan_attributes line None in wrappers () diff --git a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml index 69d22d0..b2701cc 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml @@ -102,14 +102,20 @@ let () = (218, 222, "tag"); ]; List.iter - (fun (name, support, source, expected) -> check name support source expected) + (fun (name, support, source, expected, unhighlighted) -> + check ~unhighlighted name support source expected) [ ( "multiline HAML attributes", Provider.haml (), - "😀\n%a(\n title=user.name\n aria-label=\"café 😀\"\n)= user.url\n%p end\n", - [ (9, 14, "attribute"); (20, 24, "property"); (27, 37, "attribute"); - (38, 47, "string"); (56, 59, "property"); (60, 62, "tag") ] ); + "😀\n%a(title=\"Guide\"\n href=url\n aria-label=\"café 😀\"\n)= user.url\n\ + %p(title=\"Note\"\n id=note\n) (literal text\n%em end\n", + [ (6, 11, "attribute"); (12, 19, "string"); (22, 26, "attribute"); + (33, 43, "attribute"); (44, 53, "string"); (62, 65, "property"); + (108, 111, "tag") ], + [ (95, 107) ] ); ( "multiline Slim attributes", Provider.slim (), - "😀\na[\n title=user.name\n\n aria-label=\"café 😀\"\n]= user.url\np end\n", + "😀\na[\n title=user.name\n\n aria-label=\"café 😀\"\n]= user.url\n\ + p[\n id=note\n] [literal text\nem end\n", [ (8, 13, "attribute"); (19, 23, "property"); (27, 37, "attribute"); - (38, 47, "string"); (56, 59, "property"); (60, 61, "tag") ] ); + (38, 47, "string"); (56, 59, "property"); (89, 91, "tag") ], + [ (76, 88) ] ); ] From ac11f8a72af879fc9b7871b3bb1c536e63610f13 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 30 Nov 2026 10:07:25 -0800 Subject: [PATCH 3/4] Parse indexed Slim splats inside attribute wrappers --- .../lang-haml-slim/template.ml | 42 +++++++++++-------- .../test/template_highlight_test.ml | 9 ++-- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 2020689..528a936 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -55,7 +55,9 @@ type body = Comments | Text | Html | Ruby 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 } -type group = Ruby_group of ruby_group | Attribute_group of char +type group = + | Ruby_group of ruby_group + | Attribute_group of { closing : char; splats : bool } type line = { text : Text.t; @@ -194,13 +196,21 @@ let ruby_group ?(multiline = false) line ~closing = let ruby_value line = ignore (ruby_tokens (fresh_ruby ()) is_space line) -let rec scan_attributes line closing = +let rec scan_attributes ~splats line closing = spaces line; if at_end line then - line.group <- Option.map (fun close -> Attribute_group close) closing + line.group <- Option.map (fun closing -> Attribute_group { closing; splats }) closing else let size = name_length line ~offset:0 in let equal = next_nonspace line (line.cursor + size) in + let value () = + let stop unit = + is_space unit + || unit = Char.code ',' + || Option.exists (fun close -> Char.code close = unit) closing + in + ignore (ruby_tokens (fresh_ruby ()) stop line) + in match closing with | Some close when peek line close -> mark line Marker 1 | _ when size > 0 && at line equal '=' -> @@ -208,23 +218,18 @@ let rec scan_attributes line closing = spaces line; mark line Marker (if starts line "==" then 2 else 1); spaces line; - let stop unit = - is_space unit - || unit = Char.code ',' - || Option.exists (fun close -> Char.code close = unit) closing - in - ignore (ruby_tokens (fresh_ruby ()) stop line); - scan_attributes line closing - | None when peek line '*' -> + value (); + scan_attributes ~splats line closing + | _ when splats && peek line '*' -> mark line Marker 1; - ruby_value line; - scan_attributes line closing + value (); + scan_attributes ~splats line closing | Some _ when size > 0 -> mark line Attribute size; - scan_attributes line closing + scan_attributes ~splats line closing | Some _ -> skip line 1; - scan_attributes line closing + scan_attributes ~splats line closing | None -> () let attributes ~bare line = @@ -236,9 +241,9 @@ let attributes ~bare line = ruby_group ~multiline:true line ~closing:close else ( mark line Marker 1; - scan_attributes line (Some close)); + scan_attributes ~splats:bare line (Some close)); if not bare && Option.is_none line.group then wrappers () - | None -> if bare then scan_attributes line None + | None -> if bare then scan_attributes ~splats:true line None in wrappers () @@ -378,7 +383,8 @@ let parser root rule after_group = line.cursor <- 0; (match active with | Ruby_group group -> resume_ruby_group ~multiline:true group line - | Attribute_group close -> scan_attributes line (Some close)); + | Attribute_group { closing; splats } -> + scan_attributes ~splats line (Some closing)); if line.group = None then after_group line else Finish | None, _ when blank -> Finish | None, Some _ -> Ruby_line diff --git a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml index b2701cc..d21f674 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml @@ -113,9 +113,10 @@ let () = (108, 111, "tag") ], [ (95, 107) ] ); ( "multiline Slim attributes", Provider.slim (), - "😀\na[\n title=user.name\n\n aria-label=\"café 😀\"\n]= user.url\n\ + "😀\na[\n title=user.name\n\n *attrs[:link]\n aria-label=\"café 😀\"\n]= user.url\n\ p[\n id=note\n] [literal text\nem end\n", - [ (8, 13, "attribute"); (19, 23, "property"); (27, 37, "attribute"); - (38, 47, "string"); (56, 59, "property"); (89, 91, "tag") ], - [ (76, 88) ] ); + [ (8, 13, "attribute"); (19, 23, "property"); (28, 33, "variable"); + (43, 53, "attribute"); (54, 63, "string"); (72, 75, "property"); + (105, 107, "tag") ], + [ (92, 104) ] ); ] From e1cda2ac19318213006c8e98decdcd3807ec46a0 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 30 Nov 2026 10:07:55 -0800 Subject: [PATCH 4/4] Preserve bare Slim splat tokenization --- integrations/logseq-native-modes/lang-haml-slim/template.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 528a936..9511d47 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -222,7 +222,9 @@ let rec scan_attributes ~splats line closing = scan_attributes ~splats line closing | _ when splats && peek line '*' -> mark line Marker 1; - value (); + (match closing with + | None -> ruby_value line + | Some _ -> value ()); scan_attributes ~splats line closing | Some _ when size > 0 -> mark line Attribute size;