Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support functions in format options #52

Merged
merged 1 commit into from
Sep 5, 2022
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
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ Use your favourite plugin manager to install.

```lua
-- init.lua
require("packer").startup(
function()
use "lukas-reineke/lsp-format.nvim"
end
)
require("packer").startup(function()
use "lukas-reineke/lsp-format.nvim"
end)
```

#### Example with Plug
Expand All @@ -52,7 +50,7 @@ To use LSP-format, you have to run the setup function, and pass the `on_attach`

```lua
require("lsp-format").setup {}
require "lspconfig".gopls.setup { on_attach = require "lsp-format".on_attach }
require("lspconfig").gopls.setup { on_attach = require("lsp-format").on_attach }
```

or
Expand All @@ -61,11 +59,11 @@ or
require("lsp-format").setup {}

local on_attach = function(client)
require "lsp-format".on_attach(client)
require("lsp-format").on_attach(client)

-- ... custom code ...
end
require "lspconfig".gopls.setup { on_attach = on_attach }
require("lspconfig").gopls.setup { on_attach = on_attach }
```

That's it, saving a buffer will format it now.
Expand Down Expand Up @@ -114,9 +112,11 @@ vim.cmd [[cabbrev wq execute "Format sync" <bar> wq]]

## FAQ

### How is it different to `autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()`?
### How is it different to `autocmd BufWritePre <buffer> lua vim.lsp.buf.format()`?

The main difference is that LSP-format.nvim is async by default. It will format on save, _without blocking the editor_.
When the formatting is done, LSP-format.nvim will only change the buffer if it
didn't change since the time formatting was called.
And it adds some convenience with disable commands and format options.
But the end result is the same.

Expand All @@ -125,20 +125,27 @@ But the end result is the same.
You can pass the format options into the `setup` function, or as arguments to the `:Format` command.
How the format options look like depends on the LSP server you are using.

The format options can either be string, number, boolean, or a function that
resolves to those.

As an example, [mattn/efm-langserver](https://github.com/mattn/efm-langserver) uses `${}` template syntax with which you can
define your own options `${--flag:lua_variable_name}`.

```lua
require "lsp-format".setup {
typescript = { tab_width = 4 },
require("lsp-format").setup {
typescript = {
tab_width = function()
return vim.opt.shiftwidth:get()
end,
},
yaml = { tab_width = 2 },
}
local prettier = {
formatCommand = [[prettier --stdin-filepath ${INPUT} ${--tab-width:tab_width}]],
formatStdin = true,
}
require "lspconfig".efm.setup {
on_attach = require "lsp-format".on_attach,
require("lspconfig").efm.setup {
on_attach = require("lsp-format").on_attach,
init_options = { documentFormatting = true },
settings = {
languages = {
Expand All @@ -149,5 +156,5 @@ require "lspconfig".efm.setup {
}
```

Now Typescript gets formatted with 4 and YAML with 2 spaces by default.
Now Typescript gets formatted with what `shiftwidth` is set to, and YAML with 2 spaces by default.
And you can run `:Format tab_width=8` to overwrite the setting and format with 8 spaces.
80 changes: 10 additions & 70 deletions doc/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ CONTENTS *lsp-format*
2. Setup |lsp-format-setup|
3. Special Format Options |lsp-format-special-format-options|
4. Commands |lsp-format-commands|
5. Changelog |lsp-format-changelog|
6. License |lsp-format-license|
5. License |lsp-format-license|

==============================================================================
1. INTRODUCTION *lsp-format-introduction*
Expand All @@ -39,6 +38,8 @@ function to each LSP that should use it.

The setup functions takes one optional argument that maps |filetypes| to
format options.
Format options can be strings, numbers, booleans, or functions that resolve
to strings, numbers, or booleans.


==============================================================================
Expand All @@ -53,7 +54,12 @@ exclude *lsp-format-exclude*
require "lsp-format".setup {
go = {
exclude = { "gopls" }
}
},
typescript = {
tab_width = function()
return vim.opt.shiftwidth:get()
end,
},
}


Expand Down Expand Up @@ -133,73 +139,7 @@ force *lsp-format-force*
If no filetype is given, it uses the global state.

==============================================================================
5. CHANGELOG *lsp-format-changelog*

2.4.1
Fix `force` option when `sync` is true

2.4.0
Add `force` option

2.3.3
Use "vim.lsp.log" for logging
Don't attach servers that don't support formatting

2.3.2
Continue with the next client when one client has an error

2.3.1
Fix clear autogroup only for buffer

2.3.0
Add `sync` option
Fix overwriting global format options from command
Fix empty queue when excluding all clients
Fix wrong index when excluding clients

2.2.3
Don't format with client if `on_attach` was not called for that client

2.2.2
Don't overwrite the global format handler

2.2.1
Fix `exclude` command argument when calling with more than one value

2.2.0
Add `exclude` option

2.1.2
* Fix error logging

2.1.1
* Don't overwrite the buffer when currently in insert mode

2.1.0
* Sequentially format with all attached LSP server
* Add special `order` format option

2.0.1
* Add notes to README

2.0.0
* Removed formatting from the plugin itself, and use LSP instead.

1.1.2
* Removed debug print

1.1.1
* Fixed wrong directory generation when file is in root of path
Thanks for the help by @ndreas

1.1.0
* Added `tempfile_dir` option

1.0.0
* First release

==============================================================================
6. LICENSE *lsp-format-license*
5. LICENSE *lsp-format-license*

The MIT Licence
http://www.opensource.org/licenses/mit-license.php
Expand Down
5 changes: 5 additions & 0 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ M.format = function(options)

local bufnr = vim.api.nvim_get_current_buf()
local format_options = vim.deepcopy(M.format_options[vim.bo.filetype] or {})
for key, option in pairs(format_options) do
if type(option) == "function" then
format_options[key] = option()
end
end
for _, option in ipairs(options.fargs or {}) do
local key, value = unpack(vim.split(option, "="))
format_options[key] = M._parse_value(key, value)
Expand Down