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
1 change: 1 addition & 0 deletions integrations/logseq-native-modes/catalog/test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(modes native)
(libraries
logseq-native-modes
codemirror-ml.commands
codemirror-ml.language
codemirror-ml.lezer_common
codemirror-ml.lezer_highlight
Expand Down
82 changes: 82 additions & 0 deletions integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,85 @@ 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_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_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
Expand Down Expand Up @@ -194,4 +273,7 @@ let () =
(Logseq_native_modes.names ());
check_markdown_fences ();
check_slim_markdown_filter ();
check_slim_scss_filter ();
check_slim_scss_input ();
check_slim_scss_block_comment ();
check_lisp_brackets ()
6 changes: 3 additions & 3 deletions integrations/logseq-native-modes/lang-haml-slim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
1 change: 1 addition & 0 deletions integrations/logseq-native-modes/lang-haml-slim/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
logseq-native-modes.legacy-modes
codemirror-ml.lezer_common
codemirror-ml.lezer_highlight
codemirror-ml.lezer_sass
logseq-native-modes.streamparser)
(private_modules template haml slim))
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 @@ -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)
Expand Down
50 changes: 38 additions & 12 deletions integrations/logseq-native-modes/lang-haml-slim/template.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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 =
Expand All @@ -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 =
Expand Down Expand Up @@ -383,15 +386,18 @@ 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
match !region with
| 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)
Expand Down Expand Up @@ -430,7 +436,7 @@ let parser root rule after_group =
{
body;
min_indent;
markdown_indent = None;
content_indent = None;
from = after;
to_ = after;
children = [];
Expand Down Expand Up @@ -512,6 +518,21 @@ let css_parser =
Language.Lr_language.language Codemirror_ml_lang_css.css_language
|> Language.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 "*/" }
}
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
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 =
match base with Some base -> base | None -> Lazy.force default_html
Expand All @@ -525,8 +546,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 _ ->
Expand All @@ -539,7 +560,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading