From a34b66f9a4a8f4fb8e270ebfa9c8836fdb8381c1 Mon Sep 17 00:00:00 2001 From: Angelo Fallaria Date: Tue, 13 Feb 2024 11:41:25 +0800 Subject: feat: add ReScript formatter (#293) --- README.md | 1 + lua/conform/formatters/rescript-format.lua | 40 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 lua/conform/formatters/rescript-format.lua diff --git a/README.md b/README.md index cd20376..5efb108 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ You can view this list in vim with `:help conform-formatters` - [pretty-php](https://github.com/lkrms/pretty-php) - The opinionated PHP code formatter. - [puppet-lint](https://github.com/puppetlabs/puppet-lint) - Check that your Puppet manifests conform to the style guide. - [reorder-python-imports](https://github.com/asottile/reorder-python-imports) - Rewrites source to reorder python imports +- [rescript-format](https://github.com/rescript-lang/rescript-compiler) - The built-in ReScript formatter. - [rubocop](https://github.com/rubocop/rubocop) - Ruby static code analyzer and formatter, based on the community Ruby style guide. - [rubyfmt](https://github.com/fables-tales/rubyfmt) - Ruby Autoformatter! (Written in Rust) - [ruff_fix](https://beta.ruff.rs/docs/) - An extremely fast Python linter, written in Rust. Fix lint errors. diff --git a/lua/conform/formatters/rescript-format.lua b/lua/conform/formatters/rescript-format.lua new file mode 100644 index 0000000..806b380 --- /dev/null +++ b/lua/conform/formatters/rescript-format.lua @@ -0,0 +1,40 @@ +-- The formatter expects one of [.res | .resi | .ml | .mli] passed as +-- the value to the '-stdin' argument. +local valid_extensions = { + res = true, + resi = true, + ml = true, + mli = true, +} + +local default_extension = "res" + +---@type conform.FileFormatterConfig +return { + meta = { + url = "https://rescript-lang.org/", + description = "The built-in ReScript formatter.", + }, + command = "rescript", + args = function(self, ctx) + local extension = vim.fn.fnamemodify(ctx.filename, ":e") + + local is_invalid_extension = valid_extensions[extension] == nil + if is_invalid_extension then + extension = default_extension + end + + return { + "format", + "-stdin", + "." .. extension, + } + end, + stdin = true, + + require_cwd = true, + cwd = require("conform.util").root_file({ + "rescript.json", + "bsconfig.json", + }), +} -- cgit v1.2.3-70-g09d2