Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ There are a couple special format options that LSP-format uses.
Alternatively, you can also just not call `on_attach` for the clients you don't want to use for
formatting.

#### `only` format option

`only` is a table of the only LSP servers that should format the buffer.

#### `order` format option

`order` is a table that determines the order formatting is requested from the LSP server.
Expand Down
14 changes: 14 additions & 0 deletions doc/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ exclude *lsp-format-exclude*
}


------------------------------------------------------------------------------
only *lsp-format-only*

`only` is a table of client names to limit formatting to.

Example: >

require "lsp-format".setup {
go = {
only = { "gopls" }
},
}


------------------------------------------------------------------------------
order *lsp-format-order*

Expand Down
28 changes: 20 additions & 8 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local parse_value = function(key, value)
if not value then
return true
end
if key == "order" or key == "exclude" then
if key == "order" or key == "exclude" or key == "only" then
return vim.split(value, ",")
end
local int_value = tonumber(value)
Expand Down Expand Up @@ -123,13 +123,25 @@ M.format = function(options)
end

local clients = {}
for _, client in ipairs(get_clients { bufnr = bufnr }) do
if
client
and not vim.tbl_contains(format_options.exclude or {}, client.name)
and vim.tbl_contains(M.buffers[bufnr] or {}, client.id)
then
table.insert(clients, client)
if #(format_options.only or {}) > 0 then
for _, client in ipairs(get_clients { bufnr = bufnr }) do
if
client
and vim.tbl_contains(format_options.only, client.name)
and vim.tbl_contains(M.buffers[bufnr] or {}, client.id)
then
table.insert(clients, client)
end
end
else
for _, client in ipairs(get_clients { bufnr = bufnr }) do
if
client
and not vim.tbl_contains(format_options.exclude or {}, client.name)
and vim.tbl_contains(M.buffers[bufnr] or {}, client.id)
then
table.insert(clients, client)
end
end
end

Expand Down