mirror of
https://github.com/pojokcodeid/nvim-lazy.git
synced 2025-06-22 08:53:31 +02:00
enc: update format on save flow
This commit is contained in:
parent
2486c9e49e
commit
77ec605527
3 changed files with 114 additions and 137 deletions
|
@ -38,8 +38,8 @@ pcode.progress = 1
|
|||
-- 4 = off
|
||||
pcode.show_mode = 3
|
||||
|
||||
-- 1 ( format jalan) 0 (fromat off)
|
||||
pcode.format_on_save = 1
|
||||
-- true or false
|
||||
pcode.format_on_save = true
|
||||
pcode.format_timeout_ms = 5000
|
||||
|
||||
-- https://github.com/mfussenegger/nvim-lint
|
||||
|
|
|
@ -18,65 +18,7 @@ return {
|
|||
"nvimtools/none-ls-extras.nvim",
|
||||
},
|
||||
event = "InsertEnter",
|
||||
opts = function()
|
||||
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
||||
local sources = {}
|
||||
local data_ok, data_sources = pcall(require, "custom.null-ls")
|
||||
if data_ok then
|
||||
for _, cfg in pairs(data_sources.sources) do
|
||||
table.insert(sources, cfg)
|
||||
end
|
||||
end
|
||||
local on_save = pcode.format_on_save or 0
|
||||
if on_save == 1 then
|
||||
return {
|
||||
debug = false,
|
||||
sources = sources,
|
||||
on_attach = function(client, bufnr)
|
||||
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
|
||||
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 and client.name == "null-ls")
|
||||
or client.supports_method("textDocument/formatting")
|
||||
then
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, timeout_ms = pcode.format_timeout_ms or 5000 })
|
||||
end,
|
||||
})
|
||||
else
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, timeout_ms = pcode.format_timeout_ms or 5000, filter = nil })
|
||||
end,
|
||||
})
|
||||
end
|
||||
end,
|
||||
}
|
||||
else
|
||||
vim.schedule(function()
|
||||
pcall(function()
|
||||
vim.api.nvim_clear_autocmds({ group = "LspFormatting" })
|
||||
end)
|
||||
end)
|
||||
|
||||
return {
|
||||
debug = false,
|
||||
sources = sources,
|
||||
}
|
||||
end
|
||||
end,
|
||||
config = function(_, opts)
|
||||
require("null-ls").setup(opts)
|
||||
end,
|
||||
opts = {},
|
||||
config = true,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -62,7 +62,24 @@ local function attach_navic(client, bufnr)
|
|||
navic.attach(client, bufnr)
|
||||
end
|
||||
|
||||
local function lsp_keymaps(bufnr)
|
||||
function 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
|
||||
|
||||
-- stylua: ignore
|
||||
local function lsp_keymaps(bufnr, on_save)
|
||||
local opts = { noremap = true, silent = true }
|
||||
local keymap = vim.api.nvim_buf_set_keymap
|
||||
keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
|
||||
|
@ -80,6 +97,23 @@ local function lsp_keymaps(bufnr)
|
|||
keymap(bufnr, "n", "<leader>lr", "<cmd>lua vim.lsp.buf.rename()<cr>", opts)
|
||||
keymap(bufnr, "n", "<leader>ls", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
|
||||
keymap(bufnr, "n", "<leader>lq", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
|
||||
if on_save then
|
||||
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
||||
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, timeout_ms = pcode.format_timeout_ms or 5000, filter = FORMAT_FILTER })
|
||||
end,
|
||||
})
|
||||
else
|
||||
vim.schedule(function()
|
||||
pcall(function()
|
||||
vim.api.nvim_clear_autocmds({ group = "LspFormatting" })
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
M.on_attach = function(client, bufnr)
|
||||
attach_navic(client, bufnr)
|
||||
|
@ -96,7 +130,8 @@ M.on_attach = function(client, bufnr)
|
|||
vim.lsp.inlay_hint.enable(true)
|
||||
end
|
||||
|
||||
lsp_keymaps(bufnr)
|
||||
local on_save = pcode.format_on_save or false
|
||||
lsp_keymaps(bufnr, on_save)
|
||||
local status_ok, illuminate = pcall(require, "illuminate")
|
||||
if not status_ok then
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue