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
2 changes: 1 addition & 1 deletion integrations/logseq-native-modes/catalog/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const native = new Map(Object.entries({
PostgreSQL: "stream Sql.pg_sql",
SQL: "modern (Codemirror_ml_lang_sql.sql ())",
SQLite: "stream Sql.sqlite",
Slim: "modern (Codemirror_ml_lang_haml_slim.slim ())",
Slim: "modern (Codemirror_ml_lang_haml_slim.slim ~code_languages ())",
Tornado: "modern (Codemirror_ml_lang_django_tornado.tornado ())",
LaTeX: "stream Stex.stex_math",
Twig: "modern (Codemirror_ml_lang_twig.twig ~base:(Codemirror_ml_lang_html.html ()) ())",
Expand Down
2 changes: 1 addition & 1 deletion integrations/logseq-native-modes/catalog/mode_data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ let entries ~code_languages provider =
(fun () -> stream Sieve.sieve);
provider "Slim" ["slim"]
["text/x-slim"; "application/x-slim"] ["slim"] None
(fun () -> modern (Codemirror_ml_lang_haml_slim.slim ()));
(fun () -> modern (Codemirror_ml_lang_haml_slim.slim ~code_languages ()));
provider "Smalltalk" ["smalltalk"]
["text/x-stsrc"] ["st"] None
(fun () -> stream Smalltalk.smalltalk);
Expand Down
29 changes: 29 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 @@ -31,6 +31,34 @@ let check_markdown_fences () =
; "markdown", "", "text", []
])

let check_slim_markdown_filter () =
let open Highlight.Tags in
let highlighter =
Highlight.Highlighter.of_tags
[ [heading], "heading"; [link], "link"; [quote], "quote"
; [keyword], "keyword"; [number], "number"; [tag_name], "tag" ]
in
let source =
"section\n markdown:\n # Slim heading\n\n"
^ " [🚀 Link](https://example.com)\n > Quote\n\n"
^ " ```js\n const answer = 42;\n ```\n\n"
^ " ```slim\n javascript:\n throw 99;\n ```\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);
List.iter
(fun ((style, chunk) as expected) ->
if not (List.mem expected !spans) then
failwith (Printf.sprintf "Missing %s highlight for %S in Slim" style chunk))
[ "heading", "# Slim heading"; "link", "[🚀 Link](https://example.com)"
; "quote", "> Quote"; "keyword", "const"; "number", "42"
; "keyword", "throw"; "number", "99"; "tag", "p" ]

let check_lisp_brackets () =
let module State = Codemirror_ml_state in
let module View = Codemirror_ml_view.Editor_view in
Expand Down Expand Up @@ -165,4 +193,5 @@ let () =
List.iter (fun mode -> ignore (parse mode "\n"))
(Logseq_native_modes.names ());
check_markdown_fences ();
check_slim_markdown_filter ();
check_lisp_brackets ()
4 changes: 3 additions & 1 deletion integrations/logseq-native-modes/lang-haml-slim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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 and CSS filters mount the corresponding native grammars.
- 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.
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
@@ -1,4 +1,4 @@
let haml_language = Haml.language
let haml ?base () = Template.with_html haml_language base
let slim_language = Slim.language
let slim ?base () = Template.with_html slim_language base
let slim ?base ?code_languages () = Template.with_html ?code_languages slim_language base
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ val slim_language : Codemirror_ml_language.Language.t

val slim :
?base:Codemirror_ml_language.Language.Support.t ->
?code_languages:Codemirror_ml_lang_markdown.code_languages ->
unit ->
Codemirror_ml_language.Language.Support.t
(** Highlight Slim and its JavaScript and CSS filters. Embedded markup uses the
base language (HTML by default). *)
(** 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. *)
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 @@ -6,6 +6,7 @@
codemirror-ml.lang-html
codemirror-ml.lang-css
codemirror-ml.lang-javascript
codemirror-ml.lang-markdown
codemirror-ml.language
logseq-native-modes.legacy-modes
codemirror-ml.lezer_common
Expand Down
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 @@ -12,8 +12,9 @@ let filter line =
| "html" -> Some (size, Html)
| "javascript" -> Some (size, Javascript)
| "css" -> Some (size, Css)
| "markdown" -> Some (size, Markdown)
| "sass" | "scss" | "less" | "styl" | "coffee"
| "asciidoc" | "markdown" | "textile" | "creole" | "wiki" | "mediawiki"
| "asciidoc" | "textile" | "creole" | "wiki" | "mediawiki"
| "rdoc" | "builder" | "nokogiri" | "erb" | "plain" ->
Some (size, Text)
| _ -> None
Expand Down
62 changes: 49 additions & 13 deletions integrations/logseq-native-modes/lang-haml-slim/template.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ let html_region = node "HtmlRegion" 10 []
let ruby_region = node "RubyRegion" 11 []
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 ruby_types =
let open Highlight.Tags in
Expand All @@ -53,7 +55,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
type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown
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 @@ -268,7 +270,8 @@ let html line =
type region = {
body : body;
min_indent : int;
from : int;
mutable markdown_indent : int option;
mutable from : int;
mutable to_ : int;
mutable children : (int * Tree.child) list;
ruby : ruby option;
Expand All @@ -281,18 +284,36 @@ let region_type = function
| Ruby -> ruby_region
| Javascript -> javascript_region
| Css -> css_region
| Markdown -> markdown_region

let next_column line index width =
if at line index '\t' then width + tab_size - (width mod tab_size)
else width + 1

let column line =
let rec columns i width =
if i = line.cursor then width
else
let step =
if at line i '\t' then tab_size - (width mod tab_size) else 1
in
columns (i + 1) (width + step)
else columns (i + 1) (next_column line i width)
in
columns 0 0

let markdown_line line body ~depth ~blank =
if not blank then (
let indent =
match body.markdown_indent with
| Some indent -> indent
| None ->
body.markdown_indent <- Some depth;
depth
in
let rec offset i width =
if width >= indent then i
else offset (i + 1) (next_column line i width)
in
line.cursor <- offset 0 0);
(* Keep line breaks in the overlay and omit only the template indentation. *)
emit line markdown_text line.width

let read_line input from limit =
let rec find from =
if from >= limit then limit
Expand Down Expand Up @@ -360,13 +381,18 @@ let parser root rule after_group =
let depth = column line in
let blank = line.cursor = line.limit in
(match !region with
| Some body when (not blank) && depth < body.min_indent ->
| Some body
when (not blank)
&& depth < Option.value ~default:body.min_indent body.markdown_indent ->
close_region ()
| _ -> ());
let line_node () = Tree.Node (tree line_type line.nodes line.width) in
match !region with
| Some body ->
(if blank then
(* 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
else if blank then
Option.iter
(fun ruby -> ruby_mode.blank_line ruby.state 2)
body.ruby
Expand Down Expand Up @@ -404,6 +430,7 @@ let parser root rule after_group =
{
body;
min_indent;
markdown_indent = None;
from = after;
to_ = after;
children = [];
Expand Down Expand Up @@ -437,10 +464,8 @@ let parser root rule after_group =
| None, _ when !cursor >= !ceiling -> done_ ()
| None, [] -> done_ ()
| None, range :: tail when !cursor >= range.Range.to_ -> (
(* Markdown fences can omit a margin between included lines. *)
ranges := tail;
close_region ();
pending := None;
group := None;
match tail with
| next :: _ when next.Range.from < !ceiling ->
cursor := next.from;
Expand Down Expand Up @@ -487,14 +512,22 @@ let css_parser =
Language.Lr_language.language Codemirror_ml_lang_css.css_language
|> Language.parser

let with_html template base =
let with_html ?code_languages template base =
let base =
match base with Some base -> base | None -> Lazy.force default_html
in
let parser = Language.parser (Language.Support.language base) in
let overlay node =
if Syntax_node.node_type node == html_text then Mix.Include else Mix.Skip
in
let markdown_parser =
lazy
(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
in
let wrap =
Mix.parse_mixed (fun node _ ->
let type_ = Syntax_node.node_type node in
Expand All @@ -504,6 +537,9 @@ let with_html template base =
Some { Mix.parser = javascript_parser; overlay = None; bracketed = false }
else if type_ == css_region then
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 }
else None)
in
let mixed = Parser.with_wrapper (Language.parser template) wrap in
Expand Down
3 changes: 2 additions & 1 deletion integrations/logseq-native-modes/lang-haml-slim/template.mli
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
type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown
type action = Finish | Indented of body | Following_text of int | Ruby_line

val peek : ?offset:int -> line -> char -> bool
Expand Down Expand Up @@ -30,6 +30,7 @@ val language :
Codemirror_ml_language.Language.t

val with_html :
?code_languages:Codemirror_ml_lang_markdown.code_languages ->
Codemirror_ml_language.Language.t ->
Codemirror_ml_language.Language.Support.t option ->
Codemirror_ml_language.Language.Support.t
Loading