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
|
-- 4 = off
|
||||||
pcode.show_mode = 3
|
pcode.show_mode = 3
|
||||||
|
|
||||||
-- 1 ( format jalan) 0 (fromat off)
|
-- true or false
|
||||||
pcode.format_on_save = 1
|
pcode.format_on_save = true
|
||||||
pcode.format_timeout_ms = 5000
|
pcode.format_timeout_ms = 5000
|
||||||
|
|
||||||
-- https://github.com/mfussenegger/nvim-lint
|
-- https://github.com/mfussenegger/nvim-lint
|
||||||
|
|
|
@ -18,65 +18,7 @@ return {
|
||||||
"nvimtools/none-ls-extras.nvim",
|
"nvimtools/none-ls-extras.nvim",
|
||||||
},
|
},
|
||||||
event = "InsertEnter",
|
event = "InsertEnter",
|
||||||
opts = function()
|
opts = {},
|
||||||
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
config = true,
|
||||||
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,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ local M = {}
|
||||||
|
|
||||||
local status_cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
|
local status_cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
|
||||||
if not status_cmp_ok then
|
if not status_cmp_ok then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local lspvitualtext = pcode.lsp_virtualtext or false
|
local lspvitualtext = pcode.lsp_virtualtext or false
|
||||||
|
@ -13,95 +13,130 @@ M.capabilities.textDocument.completion.completionItem.snippetSupport = true
|
||||||
M.capabilities = cmp_nvim_lsp.default_capabilities(M.capabilities)
|
M.capabilities = cmp_nvim_lsp.default_capabilities(M.capabilities)
|
||||||
|
|
||||||
M.setup = function()
|
M.setup = function()
|
||||||
local signs = {
|
local signs = {
|
||||||
{ name = "DiagnosticSignError", text = icons.diagnostics.Error },
|
{ name = "DiagnosticSignError", text = icons.diagnostics.Error },
|
||||||
{ name = "DiagnosticSignWarn", text = icons.diagnostics.Warning },
|
{ name = "DiagnosticSignWarn", text = icons.diagnostics.Warning },
|
||||||
{ name = "DiagnosticSignHint", text = icons.diagnostics.Hint },
|
{ name = "DiagnosticSignHint", text = icons.diagnostics.Hint },
|
||||||
{ name = "DiagnosticSignInfo", text = icons.diagnostics.Info },
|
{ name = "DiagnosticSignInfo", text = icons.diagnostics.Info },
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sign in ipairs(signs) do
|
for _, sign in ipairs(signs) do
|
||||||
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
|
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
|
||||||
end
|
end
|
||||||
|
|
||||||
local config = {
|
local config = {
|
||||||
virtual_text = lspvitualtext, -- disable virtual text
|
virtual_text = lspvitualtext, -- disable virtual text
|
||||||
signs = {
|
signs = {
|
||||||
active = signs, -- show signs
|
active = signs, -- show signs
|
||||||
},
|
},
|
||||||
update_in_insert = true,
|
update_in_insert = true,
|
||||||
underline = true,
|
underline = true,
|
||||||
severity_sort = true,
|
severity_sort = true,
|
||||||
float = {
|
float = {
|
||||||
focusable = true,
|
focusable = true,
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
source = "always",
|
source = "always",
|
||||||
header = "",
|
header = "",
|
||||||
prefix = "",
|
prefix = "",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
vim.diagnostic.config(config)
|
vim.diagnostic.config(config)
|
||||||
|
|
||||||
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
|
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
|
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local function attach_navic(client, bufnr)
|
local function attach_navic(client, bufnr)
|
||||||
vim.g.navic_silence = true
|
vim.g.navic_silence = true
|
||||||
local status_ok, navic = pcall(require, "nvim-navic")
|
local status_ok, navic = pcall(require, "nvim-navic")
|
||||||
if not status_ok then
|
if not status_ok then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
navic.attach(client, bufnr)
|
navic.attach(client, bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function lsp_keymaps(bufnr)
|
function FORMAT_FILTER(client)
|
||||||
local opts = { noremap = true, silent = true }
|
local filetype = vim.bo.filetype
|
||||||
local keymap = vim.api.nvim_buf_set_keymap
|
local n = require("null-ls")
|
||||||
keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
|
local s = require("null-ls.sources")
|
||||||
keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
|
local method = n.methods.FORMATTING
|
||||||
keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
|
local available_formatters = s.get_available(filetype, method)
|
||||||
keymap(bufnr, "n", "gI", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
|
|
||||||
keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
|
if #available_formatters > 0 then
|
||||||
keymap(bufnr, "n", "gl", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
|
return client.name == "null-ls"
|
||||||
keymap(bufnr, "n", "<leader>lf", "<cmd>lua vim.lsp.buf.format{ async = true }<cr>", opts)
|
elseif client.supports_method("textDocument/formatting") then
|
||||||
keymap(bufnr, "n", "<leader>li", "<cmd>LspInfo<cr>", opts)
|
return true
|
||||||
keymap(bufnr, "n", "<leader>lI", "<cmd>Mason<cr>", opts)
|
else
|
||||||
keymap(bufnr, "n", "<leader>la", "<cmd>lua vim.lsp.buf.code_action()<cr>", opts)
|
return false
|
||||||
keymap(bufnr, "n", "<leader>lj", "<cmd>lua vim.diagnostic.goto_next({buffer=0})<cr>", opts)
|
end
|
||||||
keymap(bufnr, "n", "<leader>lk", "<cmd>lua vim.diagnostic.goto_prev({buffer=0})<cr>", opts)
|
end
|
||||||
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)
|
-- stylua: ignore
|
||||||
keymap(bufnr, "n", "<leader>lq", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
|
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)
|
||||||
|
keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
|
||||||
|
keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
|
||||||
|
keymap(bufnr, "n", "gI", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
|
||||||
|
keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
|
||||||
|
keymap(bufnr, "n", "gl", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>lf", "<cmd>lua vim.lsp.buf.format{ async = true }<cr>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>li", "<cmd>LspInfo<cr>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>lI", "<cmd>Mason<cr>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>la", "<cmd>lua vim.lsp.buf.code_action()<cr>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>lj", "<cmd>lua vim.diagnostic.goto_next({buffer=0})<cr>", opts)
|
||||||
|
keymap(bufnr, "n", "<leader>lk", "<cmd>lua vim.diagnostic.goto_prev({buffer=0})<cr>", opts)
|
||||||
|
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
|
end
|
||||||
M.on_attach = function(client, bufnr)
|
M.on_attach = function(client, bufnr)
|
||||||
attach_navic(client, bufnr)
|
attach_navic(client, bufnr)
|
||||||
if client.name == "tsserver" then
|
if client.name == "tsserver" then
|
||||||
client.server_capabilities.documentFormattingProvider = false
|
client.server_capabilities.documentFormattingProvider = false
|
||||||
end
|
end
|
||||||
|
|
||||||
if client.name == "lua_ls" then
|
if client.name == "lua_ls" then
|
||||||
client.server_capabilities.documentFormattingProvider = false
|
client.server_capabilities.documentFormattingProvider = false
|
||||||
end
|
end
|
||||||
|
|
||||||
if client.supports_method("textDocument/inlayHint") then
|
if client.supports_method("textDocument/inlayHint") then
|
||||||
-- vim.lsp.inlay_hint.enable(bufnr, true)
|
-- vim.lsp.inlay_hint.enable(bufnr, true)
|
||||||
vim.lsp.inlay_hint.enable(true)
|
vim.lsp.inlay_hint.enable(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
lsp_keymaps(bufnr)
|
local on_save = pcode.format_on_save or false
|
||||||
local status_ok, illuminate = pcall(require, "illuminate")
|
lsp_keymaps(bufnr, on_save)
|
||||||
if not status_ok then
|
local status_ok, illuminate = pcall(require, "illuminate")
|
||||||
return
|
if not status_ok then
|
||||||
end
|
return
|
||||||
illuminate.on_attach(client)
|
end
|
||||||
|
illuminate.on_attach(client)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue