Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions integrations/logseq-native-modes/lang-haml-slim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion integrations/logseq-native-modes/lang-haml-slim/slim.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
94 changes: 53 additions & 41 deletions integrations/logseq-native-modes/lang-haml-slim/template.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ 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;
limit : int;
width : int;
mutable cursor : int;
mutable nodes : (int * Tree.child) list;
mutable group : ruby_group option;
mutable group : group option;
}

let code line at =
Expand Down Expand Up @@ -182,61 +185,67 @@ 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 ->
if close = '}' && not bare then
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 ()

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ] );
]
Loading