1
0
Fork 0

detect correct formatter

This commit is contained in:
Luca Bilke 2022-12-20 18:14:15 +01:00
parent becace1ab8
commit d86c6ac9f4
3 changed files with 38 additions and 16 deletions

View file

@ -57,7 +57,7 @@ M.whichkey = {
l = {
name = "LSP",
a = { function() vim.lsp.buf.code_action() end, "Code Action" },
f = { function() vim.lsp.buf.format { async = true } end, "Format" },
f = { function() require("funcs").format { async = true } end, "Format" },
j = { function() vim.diagnostic.goto_next() end, "Next Diagnostic" },
k = { function() vim.diagnostic.goto_prev() end, "Prev Diagnostic" },
l = { function() vim.lsp.codelens.run() end, "CodeLens Action" },

View file

@ -22,6 +22,18 @@ function M.lazy_load(tb)
})
end
function M.on_file_open(plugin_name)
M.lazy_load {
events = { "BufRead", "BufWinEnter", "BufNewFile" },
augroup_name = "BeLazyOnFileOpen" .. plugin_name,
plugin = plugin_name,
condition = function()
local file = vim.fn.expand "%"
return file ~= "NvimTree_1" and file ~= "[packer]" and file ~= ""
end,
}
end
function M.gitsigns()
vim.api.nvim_create_autocmd({ "BufRead" }, {
group = vim.api.nvim_create_augroup("GitSignsLazyLoad", { clear = true }),
@ -37,18 +49,6 @@ function M.gitsigns()
})
end
function M.on_file_open(plugin_name)
M.lazy_load {
events = { "BufRead", "BufWinEnter", "BufNewFile" },
augroup_name = "BeLazyOnFileOpen" .. plugin_name,
plugin = plugin_name,
condition = function()
local file = vim.fn.expand "%"
return file ~= "NvimTree_1" and file ~= "[packer]" and file ~= ""
end,
}
end
function M.bootstrap()
local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
@ -100,6 +100,28 @@ function M.map(section)
end
end
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 available_formatters = s.get_available(filetype, method)
if #available_formatters > 0 then
return client.name == "null-ls"
elseif client.supports_method "textDocument/formatting" then
return true
else
return false
end
end
function M.format(opts)
opts = opts or {}
opts.filter = opts.filter or M.format_filter
return vim.lsp.buf.format(opts)
end
-- Modified version of a function stolen from LunarVim
function M.buf_kill(kill_command, bufnr, force)
kill_command = kill_command or "bd"

View file

@ -14,8 +14,8 @@ local diagnostics = null_ls.builtins.diagnostics
null_ls.setup {
debug = false,
sources = {
-- formatting.black.with { extra_args = { "--fast" } },
-- formatting.stylua,
-- diagnostics.flake8,
formatting.black.with { extra_args = { "--fast" } },
-- formatting.stylua,
-- diagnostics.flake8,
},
}