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..692fd1c 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -36,6 +36,7 @@ let rec element line = content line and content line = + spaces line; if peek line '=' then ruby_switch line else if peek line ':' then ( mark line Marker 1; @@ -69,4 +70,4 @@ let parse line = Indented body | None -> element line -let language = language ~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 fa0a4f7..9511d47 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -55,6 +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 { closing : char; splats : bool } type line = { text : Text.t; @@ -62,7 +65,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,50 +185,57 @@ 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 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 +let rec scan_attributes ~splats line closing = spaces line; + if at_end line then + 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 '=' -> + mark line Attribute size; + spaces line; + mark line Marker (if starts line "==" then 2 else 1); + spaces line; + value (); + scan_attributes ~splats line closing + | _ when splats && peek line '*' -> + mark line Marker 1; + (match closing with + | None -> ruby_value line + | Some _ -> value ()); + scan_attributes ~splats line closing + | Some _ when size > 0 -> + mark line Attribute size; + scan_attributes ~splats line closing + | Some _ -> + skip line 1; + scan_attributes ~splats line closing + | None -> () + +let attributes ~bare line = + if bare then spaces line; let rec wrappers () = match closer (code line line.cursor) with | Some close -> @@ -233,10 +243,9 @@ 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 ~splats:bare line (Some close)); + if not bare && Option.is_none line.group then wrappers () + | None -> if bare then scan_attributes ~splats:true line None in wrappers () @@ -374,7 +383,10 @@ 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 { 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 5989465..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 @@ -100,4 +100,23 @@ let () = (148, 157, "comment"); (161, 163, "tag"); (218, 222, "tag"); + ]; + List.iter + (fun (name, support, source, expected, unhighlighted) -> + check ~unhighlighted name support source expected) + [ + ( "multiline HAML attributes", Provider.haml (), + "😀\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 *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"); (28, 33, "variable"); + (43, 53, "attribute"); (54, 63, "string"); (72, 75, "property"); + (105, 107, "tag") ], + [ (92, 104) ] ); ]