Skip to content
This repository has been archived by the owner on Feb 9, 2025. It is now read-only.

Commit

Permalink
classify HTML attributes correctly, add extra_syntax_folders config
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 6, 2025
1 parent 5b169ee commit d12ba01
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 14 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## v0.1.1

### Bug fixes

* Properly classify HTML attributes as `:name_attribute` instead of `:name_entity`

### Enhancements

* Allow to configure extra syntaxes to load at compile time through the `:extra_syntax_folders` configuration option. These are also registered into Makeup.

## v0.1.0

Initial release.
15 changes: 11 additions & 4 deletions lib/makeup_syntect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,24 @@ defmodule MakeupSyntect do
# Should be called during application startup.
def initialize_default_syntax_set(folders \\ []) do
syntax_set =
initialize_syntaxes_from_folders([
Application.app_dir(:makeup_syntect, "priv/extra_syntaxes") | folders
])
initialize_syntaxes_from_folders(
[
Application.app_dir(:makeup_syntect, "priv/extra_syntaxes")
] ++ Application.get_env(:makeup_syntect, :extra_syntax_folders, []) ++ folders
)

:persistent_term.put(@syntax_set_key, syntax_set)
end

@doc false
def supported_syntaxes(), do: :erlang.nif_error(:nif_not_loaded)
def supported_syntaxes() do
get_supported_syntaxes(:persistent_term.get(@syntax_set_key, nil))
end

# NIF implementations
@doc false
def do_tokenize(_text, _language, _syntax_set), do: :erlang.nif_error(:nif_not_loaded)

@doc false
def get_supported_syntaxes(_syntax_set), do: :erlang.nif_error(:nif_not_loaded)
end
11 changes: 9 additions & 2 deletions native/makeup_syntect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ fn map_scope_to_token_type(
"generic_heading"
} else if scope_name.contains("name.namespace") {
"name_namespace"
} else if scope_name.contains("entity.other.attribute-name") {
// https://www.sublimetext.com/docs/scope_naming.html#entity
"name_attribute"
} else if scope_name.contains("other") {
"name_entity"
} else {
Expand Down Expand Up @@ -316,8 +319,12 @@ fn do_tokenize(
}

#[rustler::nif]
fn supported_syntaxes() -> NifResult<Vec<SyntaxInfo>> {
let syntax_set = two_face::syntax::extra_newlines();
fn get_supported_syntaxes(syntax_set_resource: Option<ResourceArc<SyntaxSetResource>>) -> NifResult<Vec<SyntaxInfo>> {
let syntax_set = if let Some(resource) = syntax_set_resource {
resource.0.lock().unwrap().clone()
} else {
two_face::syntax::extra_newlines()
};

let syntaxes: Vec<SyntaxInfo> = syntax_set
.syntaxes()
Expand Down
7 changes: 4 additions & 3 deletions test/makeup_syntect_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule MakeupSyntectTest do
use ExUnit.Case
use ExUnit.Case, async: true

doctest MakeupSyntect

defp lex(text, opts \\ []) do
Expand All @@ -13,13 +14,13 @@ defmodule MakeupSyntectTest do
punctuation: "<?",
name_tag: "xml",
whitespace: " ",
name_entity: "version",
name_attribute: "version",
punctuation: "=",
punctuation: "\"",
string_double: "1.0",
punctuation: "\"",
whitespace: " ",
name_entity: "encoding",
name_attribute: "encoding",
punctuation: "=",
punctuation: "\"",
string_double: "UTF-8",
Expand Down
18 changes: 18 additions & 0 deletions test/syntaxes/extra_syntaxes_config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule MakeupSyntect.Syntaxes.ExtraSyntaxesConfigTest do
use ExUnit.Case, async: false

test "loads extra syntaxes" do
Application.put_env(:makeup_syntect, :extra_syntax_folders, [Path.join(__DIR__, "..")])
on_exit(fn -> Application.put_env(:makeup_syntect, :extra_syntax_folders, []) end)

MakeupSyntect.initialize_default_syntax_set()

assert MakeupSyntect.Lexer.lex("if foo {}", language: "Demo C") == [
{:keyword, %{scope: ["source.c", "keyword.control.c"], language: "demo c"}, "if"},
{:name, %{scope: ["source.c"], language: "demo c"}, " foo {}"}
]

assert %{name: "Demo C", codeblock_name: "demo_c"} =
MakeupSyntect.Syntaxes.all() |> Enum.find(&(&1.name == "Demo C"))
end
end
10 changes: 5 additions & 5 deletions test/syntaxes/heex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule MakeupSyntect.Syntaxes.HEExTest do
punctuation: "<",
name_tag: ".component",
whitespace: " ",
name_entity: "foo",
name_attribute: "foo",
punctuation: "=",
whitespace: " ",
punctuation: ">"
Expand All @@ -25,7 +25,7 @@ defmodule MakeupSyntect.Syntaxes.HEExTest do
punctuation: "<",
name_tag: ":slot",
whitespace: " ",
name_entity: "foo",
name_attribute: "foo",
punctuation: "=",
whitespace: " ",
punctuation: ">"
Expand All @@ -37,13 +37,13 @@ defmodule MakeupSyntect.Syntaxes.HEExTest do
{:punctuation, "<"},
{:name_tag, ".dynamic_component"},
{:whitespace, " "},
{:name_entity, "module"},
{:name_attribute, "module"},
{:punctuation, "="},
{:whitespace, " "},
{:name_entity, "function"},
{:name_attribute, "function"},
{:punctuation, "="},
{:whitespace, " "},
{:name_entity, "shared"},
{:name_attribute, "shared"},
{:punctuation, "="},
{:punctuation, "\""},
{:string_double, "Yay"},
Expand Down

0 comments on commit d12ba01

Please sign in to comment.