From efe7eea195e7810069319030dcddc831cdd06ac2 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:36:11 +0200 Subject: fix(lsp): update format filter for nightly (#2773) --- lua/lvim/lsp/utils.lua | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) (limited to 'lua/lvim/lsp/utils.lua') diff --git a/lua/lvim/lsp/utils.lua b/lua/lvim/lsp/utils.lua index d0e36241..2a23e501 100644 --- a/lua/lvim/lsp/utils.lua +++ b/lua/lvim/lsp/utils.lua @@ -130,45 +130,43 @@ function M.setup_codelens_refresh(client, bufnr) end ---filter passed to vim.lsp.buf.format ----gives higher priority to null-ls ----@param clients table clients attached to a buffer +---always selects null-ls if it's available and caches the value per buffer +---@param client table client attached to a buffer ---@return table chosen clients -function M.format_filter(clients) - return vim.tbl_filter(function(client) - local status_ok, formatting_supported = pcall(function() - return client.supports_method "textDocument/formatting" - end) - -- give higher prio to null-ls - if status_ok and formatting_supported and client.name == "null-ls" then - return "null-ls" - else - return status_ok and formatting_supported and client.name - end - end, clients) +function M.format_filter(client) + local filetype = vim.bo.filetype + local n = require "null-ls" + local s = require "null-ls.sources" + local method = n.methods.FORMATTING + local avalable_sources = s.get_available(filetype, method) + + if #avalable_sources > 0 then + return client.name == "null-ls" + else + return true + end end ---Provide vim.lsp.buf.format for nvim <0.8 ---@param opts table function M.format(opts) - opts = opts or { filter = M.format_filter } + opts = opts or {} + opts.filter = opts.filter or M.format_filter if vim.lsp.buf.format then return vim.lsp.buf.format(opts) end local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() - local clients = vim.lsp.buf_get_clients(bufnr) + + local clients = vim.lsp.get_active_clients { + id = opts.id, + bufnr = bufnr, + name = opts.name, + } if opts.filter then - clients = opts.filter(clients) - elseif opts.id then - clients = vim.tbl_filter(function(client) - return client.id == opts.id - end, clients) - elseif opts.name then - clients = vim.tbl_filter(function(client) - return client.name == opts.name - end, clients) + clients = vim.tbl_filter(opts.filter, clients) end clients = vim.tbl_filter(function(client) -- cgit v1.2.3 From dec21bbab6cf9102e236806e20273d08f32f8716 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sun, 31 Jul 2022 14:27:59 +0200 Subject: feat(lsp): bind formatexpr and omnifunc by default (#2865) --- lua/lvim/lsp/utils.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lua/lvim/lsp/utils.lua') diff --git a/lua/lvim/lsp/utils.lua b/lua/lvim/lsp/utils.lua index 2a23e501..fa1ac6d9 100644 --- a/lua/lvim/lsp/utils.lua +++ b/lua/lvim/lsp/utils.lua @@ -132,18 +132,20 @@ end ---filter passed to vim.lsp.buf.format ---always selects null-ls if it's available and caches the value per buffer ---@param client table client attached to a buffer ----@return table chosen clients +---@return boolean if client matches function M.format_filter(client) local filetype = vim.bo.filetype local n = require "null-ls" local s = require "null-ls.sources" local method = n.methods.FORMATTING - local avalable_sources = s.get_available(filetype, method) + local avalable_formatters = s.get_available(filetype, method) - if #avalable_sources > 0 then + if #avalable_formatters > 0 then return client.name == "null-ls" - else + elseif client.supports_method "textDocument/formatting" then return true + else + return false end end @@ -159,6 +161,7 @@ function M.format(opts) local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() + ---@type table|nil local clients = vim.lsp.get_active_clients { id = opts.id, bufnr = bufnr, -- cgit v1.2.3