Skip to content

Commit

Permalink
Create package
Browse files Browse the repository at this point in the history
  • Loading branch information
v0idpwn committed Jan 23, 2022
0 parents commit 837a967
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs,zig}"],
plugins: [ZiglerFormat.Plugin]
]
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
zigler_format-*.tar

# Temporary files, for example, from tests.
/tmp/
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ZiglerFormat

Format Zig NIFs with ease!

## Installation

The package can be installed by adding `zigler_format` to your list of
dependencies in `mix.exs`:

```elixir
def deps do
[
{:zigler_format, "~> 0.1.0"}
]
end
```

Then, you can add `ZiglerFormat.Plugin` to your `.formatter.exs` file, as in:

```elixir
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs,zig}"],
plugins: [ZiglerFormat.Plugin]
]
```

It is also recommended to add `zig` in allowed extensions.
23 changes: 23 additions & 0 deletions lib/zigler_format.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ZiglerFormat do
use Zig, link_libc: true

~Z"""
/// nif: format_string/1
fn format_string(env: beam.env, source_code_binary: beam.term) !beam.term {
const source_code: []u8 = try beam.get_char_slice(env, source_code_binary);
var tree = try std.zig.parse(beam.allocator, source_code);
defer tree.deinit(beam.allocator);
// no-op if parsing errors
if (tree.errors.len == 0) {
const formatted = try tree.render(beam.allocator);
defer beam.allocator.free(formatted);
return beam.make_slice(env, formatted);
} else {
return source_code_binary;
}
}
"""
end
11 changes: 11 additions & 0 deletions lib/zigler_format/plugin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule ZiglerFormat.Plugin do
@behaviour Mix.Tasks.Format

def features(_opts) do
[sigils: [:Z], extensions: [".zig"]]
end

def format(contents, _opts) do
ZiglerFormat.format_string(contents)
end
end
27 changes: 27 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule ZiglerFormat.MixProject do
use Mix.Project

def project do
[
app: :zigler_format,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:zigler, "~> 0.8"}
]
end
end
10 changes: 10 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%{
"earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"},
"ex_doc": {:hex, :ex_doc, "0.27.3", "d09ed7ab590b71123959d9017f6715b54a448d76b43cf909eb0b2e5a78a977b2", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "ee60b329d08195039bfeb25231a208749be4f2274eae42ce38f9be0538a2f2e6"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"},
"zigler": {:hex, :zigler, "0.8.1", "51e0433916c44d90c0fa728e5d186c354d45bb67da51052933837c551e243e5f", [:mix], [{:ex_doc, "~> 0.23", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "3c3a702dcc4fd1f0346b609925b023814cf10f20b927285f9598160dce20d8cc"},
}
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
12 changes: 12 additions & 0 deletions test/zigler_format_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule ZiglerFormatTest do
use ExUnit.Case
doctest ZiglerFormat

test "format_string with valid input" do
assert "var a = 1;\n" == ZiglerFormat.format_string("var a = 1;");
end

test "format_string with invalid input does a no-op" do
assert "var a = ;" == ZiglerFormat.format_string("var a = ;");
end
end

0 comments on commit 837a967

Please sign in to comment.