Skip to content

Commit

Permalink
docs(lsp): asynchronous format on save using lsp-format.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
VonHeikemen committed Apr 6, 2023
1 parent 3a2db14 commit 8ecb9cb
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion doc/md/lsp.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Language servers are configured and initialized using [nvim-lspconfig](https://g

Ever wondered what does lsp-zero does under the hood? Let me tell you.

First it adds data to an option called `capabilities` in lspconfig's defaults. This new data comes from [cmp-nvim-lsp](https://github.com/hrsh7th/cmp-nvim-lsp). They tell the language server what features [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) adds to the editor.
First it adds data to an option called `capabilities` in lspconfig's defaults. This new data comes from [cmp-nvim-lsp](https://github.com/hrsh7th/cmp-nvim-lsp). It tells the language server what features [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) adds to the editor.

Then it creates an autocommand on the event `LspAttach`. This autocommand will be triggered every time a language server is attached to a buffer. Is where all keybindings and commands are created.

Expand Down Expand Up @@ -234,6 +234,8 @@ You can choose one of these methods.

If you want to control exactly what language server is used to format a file call the function [.format_on_save()](https://github.com/VonHeikemen/lsp-zero.nvim/blob/v2.x/doc/md/api-reference.md#format_on_saveopts), this will allow you to associate a language server with a list of filetypes.

Note: [.format_on_save()](https://github.com/VonHeikemen/lsp-zero.nvim/blob/v2.x/doc/md/api-reference.md#format_on_saveopts) doesn't support async formatting.

```lua
local lsp = require('lsp-zero').preset({})

Expand Down Expand Up @@ -274,6 +276,27 @@ You could be more specific if you give the name of a server to [.buffer_autoform
lsp.buffer_autoformat({name = 'lua_ls'})
```

### I really, really want asynchronous format on save

Fine. But you'll have to use [lsp-format.nvim](https://github.com/lukas-reineke/lsp-format.nvim).

I always recommend having only one language server to format the buffer. This next example will show how to allow only a list of servers.

```lua
local lsp = require('lsp-zero').preset({})

lsp.on_attach(function(client, bufnr)
lsp.default_keymaps({buffer = bufnr})

local allow_format = {'lua_ls', 'rust_analyzer'}
if vim.tbl_contains(allow_format, client.name) then
require('lsp-format').on_attach(client)
end
end)

lsp.setup()
```

## Format buffer using a keybinding

### Using built-in functions
Expand Down

0 comments on commit 8ecb9cb

Please sign in to comment.