Skip to content

Commit

Permalink
feat: add builtin support
Browse files Browse the repository at this point in the history
glepnir committed Mar 16, 2023
1 parent 14e5155 commit d629809
Showing 3 changed files with 114 additions and 74 deletions.
82 changes: 45 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -44,54 +44,62 @@ filetype = {
stdin -- boolean type when is true will send the buffer contents to stdin
ignore_patterns --table type when file name match one of it will ignore format
find -- string type search the config file that command used. if not find will not format
hook -- function type a hook run after async fmt invoked
lsp -- boolean type if enable it will run vim.lsp.buf.format with async = true
before -- function type a hook run before format
}
```

also you can use a command `EasyFormat` to format file.
Examples you can `clang-format` for c file like this

## Example configs

for c cpp go lua

```lua
require('easyformat').setup({
fmt_on_save = true,
```
c = {
cmd = 'clang-format',
args = { '-style=file', vim.api.nvim_buf_get_name(0) },
ignore_patterns = { 'neovim/*' },
find = '.clang-format',
stdin = false,
lsp = false,
before = function()
print('run before format')
end
},
cpp = {
cmd = 'clang-format',
args = { '-style=file', vim.api.nvim_buf_get_name(0) },
find = '.clang-format',
stdin = false,
lsp = false,
},
go = {
cmd = 'golines',
args = { '--max-len=80', vim.api.nvim_buf_get_name(0) },
stdin = false,
hook = function()
vim.lsp.buf.code_action({ context = { only = { 'source.organizeImports' } }, apply = true })
end,
lsp = true,
},
lua = {
cmd = 'stylua',
ignore_patterns = { '%pspec', 'neovim/*' },
find = '.stylua.toml',
args = { '-' },
stdin = true,
lsp = false,
},
})
```
builtin support filetypes

- c cpp `clang-format`
- rust `rustfmt`
- lua `stylua`
- go `golines`
- js/ts/react `prettier`

`require('easyformat.config').get_config` is an export function that you can use it go get the
default configs the param is filetypes can be `string | table`

you can use this code to get the default config then override it

```lua
local get_config = require('easyformat.config').get_config
local config = get_config('cpp')
-- then you can override this tool config and pass it to setup function
require('easyformat').setup({
cpp = config
})
```

## Command

use a command `EasyFormat` to format file.

## Example configs

- usage in my [config](https://github.com/glepnir/nvim)

```lua
local get_config = require('easyformat.config').get_config
local configs =
get_config({ 'c', 'cpp', 'lua', 'rust', 'go', 'javascriptreact', 'typescriptreact' })
local params = vim.tbl_extend('keep', {
fmt_on_save = true,
}, configs)
require('easyformat').setup(params)
```

## License
## License MIT
62 changes: 62 additions & 0 deletions lua/easyformat/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local M = {}

function M.get_config(fts)
local prettier = {
cmd = 'prettier',
args = { '--stdin-filepath', vim.api.nvim_buf_get_name(0) },
stdin = true,
}

local configs = {
c = {
cmd = 'clang-format',
args = { '-style=file', vim.api.nvim_buf_get_name(0) },
ignore_patterns = { 'neovim/*' },
find = '.clang-format',
stdin = false,
},
cpp = {
cmd = 'clang-format',
args = { '-style=file', vim.api.nvim_buf_get_name(0) },
ignore_patterns = { 'neovim/*' },
find = '.clang-format',
stdin = false,
},
rust = {
cmd = 'rustfmt',
args = {},
stdin = true,
},
go = {
cmd = 'golines',
args = { '--max-len=80', vim.api.nvim_buf_get_name(0) },
stdin = false,
before = function()
vim.lsp.buf.code_action({ context = { only = { 'source.organizeImports' } }, apply = true })
end,
},
lua = {
cmd = 'stylua',
ignore_patterns = { '%pspec', 'neovim/*' },
find = '.stylua.toml',
args = { '-' },
stdin = true,
},
typescript = prettier,
typescriptreact = prettier,
javascript = prettier,
javascriptreact = prettier,
}

if type(fts) == 'string' and configs[fts] then
return { fts = configs[fts] }
end

local res = {}
for _, ft in ipairs(fts) do
res[ft] = configs[ft]
end
return res
end

return M
44 changes: 7 additions & 37 deletions lua/easyformat/init.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
local api, lsp, fn = vim.api, vim.lsp, vim.fn
local api, fn = vim.api, vim.fn
local ef = {}
local fmt = require('easyformat.format')

local function get_lsp_client()
local current_buf = api.nvim_get_current_buf()
local clients = lsp.get_active_clients({ buffer = current_buf })
if next(clients) == nil then
return nil
end

for _, client in pairs(clients) do
local fts = client.config.filetypes
if
client.server_capabilities.documentFormattingProvider
and vim.tbl_contains(fts, vim.bo.filetype)
then
return client
end
end
end

local function searcher(match, bufnr)
if not match then
return true
@@ -35,15 +17,7 @@ local function searcher(match, bufnr)
return true
end

-- if the file is opened as a single just return false don't show a notify
local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
for _, client in pairs(clients or {}) do
if not client.config.root_dir then
return false
end
end

vim.notify('[EasyFormat] Does not find ' .. match .. ' in local', vim.log.levels.WARN)
print('[EasyFormat] Does not find ' .. match .. ' in local')
return false
end

@@ -69,6 +43,10 @@ local function do_fmt(buf)
return
end

if conf.before then
conf.before()
end

--ignore when have error diagnostics
if #vim.diagnostic.get(buf, { severity = 1 }) ~= 0 then
return
@@ -77,14 +55,6 @@ local function do_fmt(buf)
if searcher(conf.find, buf) then
fmt:init(vim.tbl_extend('keep', conf, { bufnr = buf }))
end

if conf.hook then
conf.hook()
end

if conf.lsp and get_lsp_client() then
lsp.buf.format({ async = true })
end
end

local function register_command()
@@ -94,7 +64,7 @@ local function register_command()
end

local function register_event(fts)
local group = api.nvim_create_augroup('EasyFormat with lsp and third tools', { clear = true })
local group = api.nvim_create_augroup('EasyFormat with third tools', { clear = true })
local function bufwrite(bufnr)
api.nvim_create_autocmd('BufWritePre', {
group = group,

0 comments on commit d629809

Please sign in to comment.