mirror of
https://github.com/pojokcodeid/nvim-lazy.git
synced 2025-06-21 08:35:48 +02:00
fix: format on save tidak respon jika menggunakan lsp format
This commit is contained in:
parent
2e64635d7e
commit
f5f74f80e0
6 changed files with 159 additions and 157 deletions
|
@ -38,12 +38,17 @@ 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
|
||||
|
||||
pcode.treesitter_ensure_installed = {}
|
||||
-- https://github.com/mfussenegger/nvim-lint
|
||||
-- https://github.com/stevearc/conform.nvim
|
||||
-- use conform and nvim-lint if set true
|
||||
pcode.disable_null_ls = true
|
||||
|
||||
pcode.treesitter_ensure_installed = {}
|
||||
pcode.tscontext = false
|
||||
-- ini hanya untuk lsp yg tidak support masson
|
||||
-- untuk referesi support language kunjungi link dibawah
|
||||
-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
|
||||
|
@ -65,7 +70,7 @@ pcode.mason_ensure_installed = { -- sebelumnya register_lsp
|
|||
-- tambahkan di bawah sini setelah melakukan :masoninstall
|
||||
}
|
||||
pcode.unregister_lsp = {
|
||||
-- "jdtls", -- tambahkan di bawah ini
|
||||
"jdtls", -- tambahkan di bawah ini
|
||||
}
|
||||
|
||||
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md
|
||||
|
@ -153,7 +158,7 @@ pcode.database = false
|
|||
pcode.rest_client = true
|
||||
|
||||
-- https://github.com/mfussenegger/nvim-dap
|
||||
pcode.nvim_dap = false -- not support for windows os (auto config mason-nvim-dap)
|
||||
pcode.nvim_dap = true -- not support for windows os (auto config mason-nvim-dap)
|
||||
|
||||
-- conefig special support test & dap
|
||||
pcode.active_rust_config = false
|
||||
|
@ -167,6 +172,6 @@ pcode.active_golang_config = false
|
|||
pcode.active_python_config = false
|
||||
pcode.active_cpp_config = false
|
||||
pcode.active_java_config = {
|
||||
active = false,
|
||||
active = true,
|
||||
project = "gradle", -- gradle or maven
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ return {
|
|||
{
|
||||
"nvimtools/none-ls.nvim",
|
||||
lazy = true,
|
||||
enabled = not (pcode.disable_null_ls or false),
|
||||
dependencies = {
|
||||
{
|
||||
"jayp0521/mason-null-ls.nvim",
|
||||
event = "VeryLazy",
|
||||
opts = {
|
||||
ensure_installed = pcode.null_ls_ensure_installed or {},
|
||||
automatic_setup = true,
|
||||
|
@ -17,65 +19,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,13 @@ 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
|
||||
-- disable if conform active
|
||||
local status, _ = pcall(require, "conform")
|
||||
if status then
|
||||
on_save = false
|
||||
end
|
||||
lsp_keymaps(bufnr, on_save)
|
||||
local status_ok, illuminate = pcall(require, "illuminate")
|
||||
if not status_ok then
|
||||
return
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
local null_ls = require("null-ls")
|
||||
local ok, null_ls = pcall(require, "null-ls")
|
||||
local M = {}
|
||||
|
||||
M.list_registered = function(filetype)
|
||||
if ok then
|
||||
local method = null_ls.methods.FORMATTING
|
||||
local registered_providers = require("user.utils.lsp").list_registered_providers_names(filetype)
|
||||
return registered_providers[method] or {}
|
||||
else
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
M.list_registered_all = function()
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
local null_ls = require("null-ls")
|
||||
local ok, null_ls = pcall(require, "null-ls")
|
||||
local M = {}
|
||||
|
||||
M.alternative_methods = {
|
||||
if ok then
|
||||
M.alternative_methods = {
|
||||
null_ls.methods.DIAGNOSTICS,
|
||||
null_ls.methods.DIAGNOSTICS_ON_OPEN,
|
||||
null_ls.methods.DIAGNOSTICS_ON_SAVE,
|
||||
}
|
||||
}
|
||||
else
|
||||
M.alternative_methods = {}
|
||||
end
|
||||
|
||||
M.linter_list_registered = function(filetype)
|
||||
if ok then
|
||||
local registered_providers = require("user.utils.lsp").list_registered_providers_names(filetype)
|
||||
local providers_for_methods = vim.iter(vim.tbl_map(function(m)
|
||||
return registered_providers[m] or {}
|
||||
end, M.alternative_methods))
|
||||
|
||||
return providers_for_methods
|
||||
else
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
@ -93,7 +93,8 @@ return {
|
|||
if type(msg) == "boolean" or #msg == 0 then
|
||||
return "LSP Inactive"
|
||||
end
|
||||
return msg
|
||||
-- return msg
|
||||
table.insert(buf_client_names, msg)
|
||||
end
|
||||
-- add client
|
||||
for _, client in pairs(buf_clients) do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue