enc: optimize lsp config & optimize foramat on save

This commit is contained in:
asep.komarudin 2024-06-25 05:41:42 +07:00
parent 618b816d52
commit a3143f9ae0
12 changed files with 437 additions and 322 deletions

View file

@ -156,10 +156,10 @@ pcode.active_javascript_config = {
jest_command = "npm test -- ",
jest_config = "jest.config.mjs",
}
pcode.active_php_config = false
pcode.active_php_config = true
pcode.active_golang_config = false
pcode.active_python_config = false
pcode.active_cpp_config = false
pcode.active_cpp_config = true
pcode.active_java_config = {
active = false,
project = "gradle", -- gradle or maven

View file

@ -10,9 +10,9 @@ local cmd, _ = pcall(require, "user.autocommands")
if cmd then
require("user.autocommands")
end
local onsave, _ = pcall(require, "user.format_onsave")
if onsave then
require("user.format_onsave")
end
-- local onsave, _ = pcall(require, "user.format_onsave")
-- if onsave then
-- require("user.format_onsave")
-- end
vim.g.pcode_icons = require("user.icons")
return {}

View file

@ -1,30 +1,110 @@
return {
{
"neovim/nvim-lspconfig",
lazy = true,
event = "BufRead",
cmd = {
"LspInfo",
"LspInstall",
"LspUninstall",
"williamboman/mason-lspconfig.nvim",
dependencies = {
{
"neovim/nvim-lspconfig",
lazy = true,
event = "BufRead",
cmd = {
"LspInfo",
"LspInstall",
"LspUninstall",
},
config = function()
require("lspconfig.ui.windows").default_options.border = "rounded"
end,
},
{
"williamboman/mason.nvim",
lazy = true,
cmd = {
"Mason",
"MasonInstall",
"MasonUninstall",
"MasonUninstallAll",
"MasonLog",
},
opts = function()
local icons = vim.g.pcode_icons.ui
return {
ui = {
-- border = "none",
border = icons.Border,
icons = {
package_pending = icons.DotCircle,
package_installed = icons.CheckCircle,
package_uninstalled = icons.BlankCircle,
},
keymaps = {
-- Keymap to expand a server in the UI
toggle_server_expand = "<CR>",
-- Keymap to install the server under the current cursor position
install_server = "i",
-- Keymap to reinstall/update the server under the current cursor position
update_server = "u",
-- Keymap to check for new version for the server under the current cursor position
check_server_version = "c",
-- Keymap to update all installed servers
update_all_servers = "U",
-- Keymap to check which installed servers are outdated
check_outdated_servers = "C",
-- Keymap to uninstall a server
uninstall_server = "X",
},
},
log_level = vim.log.levels.INFO,
max_concurrent_installers = 4,
}
end,
config = function(_, opts)
require("mason").setup(opts)
end,
},
},
config = function()
require("lspconfig.ui.windows").default_options.border = "rounded"
event = "BufReadPre",
opts = function()
local servers = { "lua_ls" }
local mason_install = pcode.mason_ensure_installed or {}
vim.list_extend(servers, mason_install)
return {
ensure_installed = servers,
automatic_installation = true,
}
end,
},
{
"williamboman/mason.nvim",
lazy = true,
dependencies = { "williamboman/mason-lspconfig.nvim" },
cmd = {
"Mason",
"MasonInstall",
"MasonUninstall",
"MasonUninstallAll",
"MasonLog",
},
config = function()
require("user.lsp")
config = function(_, opts)
require("mason-lspconfig").setup(opts)
local option = {}
local unregis_lsp = pcode.unregister_lsp or {}
require("mason-lspconfig").setup_handlers({
function(server_name) -- default handler (optional)
local capabilities = require("user.lsp.handlers").capabilities
if server_name == "clangd" then
capabilities.offsetEncoding = { "utf-16" }
end
local is_skip = false
local my_index = idxOf(unregis_lsp, server_name)
if my_index ~= nil then
is_skip = true
end
if not is_skip then
option = {
on_attach = require("user.lsp.handlers").on_attach,
capabilities = capabilities,
}
server_name = vim.split(server_name, "@")[1]
local require_ok, conf_opts = pcall(require, "user.lsp.settings." .. server_name)
if require_ok then
option = vim.tbl_deep_extend("force", conf_opts, option)
end
require("lspconfig")[server_name].setup(option)
end
end,
})
require("user.lsp.handlers").setup()
end,
},
}

View file

@ -32,8 +32,17 @@ return {
debug = false,
sources = sources,
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = 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,
@ -41,10 +50,24 @@ return {
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,

View file

@ -28,6 +28,15 @@ _G.all_trim = function(s)
return s:match("^%s*(.-)%s*$")
end
_G.idxOf = function(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
return nil
end
-- run if rust config true
if pcode.active_rust_config then
table.insert(pcode.mason_ensure_installed, "rust_analyzer")

View file

@ -1,71 +1,71 @@
local run = 0
local frmt = pcode.format_on_save or 0
if frmt == 1 then
run = 1
else
run = 0
end
local buf_clients = vim.lsp.get_clients()
if next(buf_clients) == nil then
run = 0
end
if run == 1 then
-- 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
--
-- vim.cmd([[
-- augroup _lsp
-- autocmd!
-- " autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter}
-- autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=pcode.format_timeout_ms or 5000 ,filter=FORMAT_FILTER}
-- augroup 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 boolean if client matches
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
vim.api.nvim_create_autocmd("BufWritePre", {
group = "lsp_format_on_save",
pattern = "*",
callback = function()
vim.lsp.buf.format({ timeout_ms = pcode.format_timeout_ms or 5000, filter = FORMAT_FILTER })
end,
})
else
vim.schedule(function()
pcall(function()
vim.api.nvim_clear_autocmds({ group = "lsp_format_on_save" })
end)
end)
end
-- local run = 0
-- local frmt = pcode.format_on_save or 0
-- if frmt == 1 then
-- run = 1
-- else
-- run = 0
-- end
--
-- local buf_clients = vim.lsp.get_clients()
-- if next(buf_clients) == nil then
-- run = 0
-- end
--
-- if run == 1 then
-- -- 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
-- --
-- -- vim.cmd([[
-- -- augroup _lsp
-- -- autocmd!
-- -- " autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter}
-- -- autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=pcode.format_timeout_ms or 5000 ,filter=FORMAT_FILTER}
-- -- augroup 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 boolean if client matches
-- 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
-- vim.api.nvim_create_autocmd("BufWritePre", {
-- group = "lsp_format_on_save",
-- pattern = "*",
-- callback = function()
-- vim.lsp.buf.format({ timeout_ms = pcode.format_timeout_ms or 5000, filter = FORMAT_FILTER })
-- end,
-- })
-- else
-- vim.schedule(function()
-- pcall(function()
-- vim.api.nvim_clear_autocmds({ group = "lsp_format_on_save" })
-- end)
-- end)
-- end

View file

@ -1,6 +1,6 @@
local status_ok, lsp_installer = pcall(require, "nvim-lsp-installer")
if not status_ok then
return
return
end
local lspconfig = require("lspconfig")
@ -10,23 +10,23 @@ local lspconfig = require("lspconfig")
local servers = {}
local installer = pcode.lsp_installer or {}
for _, client in pairs(installer) do
table.insert(servers, client)
table.insert(servers, client)
end
lsp_installer.setup({
ensure_installed = servers,
ensure_installed = servers,
})
for _, server in pairs(servers) do
local opts = {
on_attach = require("user.lsp.handlers").on_attach,
capabilities = require("user.lsp.handlers").capabilities,
}
local has_custom_opts, server_custom_opts = pcall(require, "user.lsp.settings." .. server)
if has_custom_opts then
opts = vim.tbl_deep_extend("force", opts, server_custom_opts)
end
lspconfig[server].setup(opts)
local opts = {
on_attach = require("user.lsp.handlers").on_attach,
capabilities = require("user.lsp.handlers").capabilities,
}
local has_custom_opts, server_custom_opts = pcall(require, "user.lsp.settings." .. server)
if has_custom_opts then
opts = vim.tbl_deep_extend("force", opts, server_custom_opts)
end
lspconfig[server].setup(opts)
end
-- lspconfig.yamlls.setup({

View file

@ -1,9 +1,9 @@
local status_ok, _ = pcall(require, "lspconfig")
if not status_ok then
return
end
require("user.lsp.mason_cfg")
--require("user.lsp.config") -- ini hanya untuk windows supaya jdtls jalan, kalau pakai linu x remark saja
require("user.lsp.handlers").setup()
--require("user.lsp.null-lscfg")
-- local status_ok, _ = pcall(require, "lspconfig")
-- if not status_ok then
-- return
-- end
--
-- require("user.lsp.mason_cfg")
-- --require("user.lsp.config") -- ini hanya untuk windows supaya jdtls jalan, kalau pakai linu x remark saja
-- require("user.lsp.handlers").setup()
-- --require("user.lsp.null-lscfg")

View file

@ -1,110 +1,110 @@
local servers = {
"lua_ls",
}
local function idxOf(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
return nil
end
local mason_install = pcode.mason_ensure_installed or {}
for _, client in pairs(mason_install) do
table.insert(servers, client)
end
local unregis_lsp = pcode.unregister_lsp or {}
local icons = vim.g.pcode_icons.ui
local settings = {
ui = {
-- border = "none",
border = icons.Border,
icons = {
package_pending = icons.DotCircle,
package_installed = icons.CheckCircle,
package_uninstalled = icons.BlankCircle,
},
keymaps = {
-- Keymap to expand a server in the UI
toggle_server_expand = "<CR>",
-- Keymap to install the server under the current cursor position
install_server = "i",
-- Keymap to reinstall/update the server under the current cursor position
update_server = "u",
-- Keymap to check for new version for the server under the current cursor position
check_server_version = "c",
-- Keymap to update all installed servers
update_all_servers = "U",
-- Keymap to check which installed servers are outdated
check_outdated_servers = "C",
-- Keymap to uninstall a server
uninstall_server = "X",
},
},
log_level = vim.log.levels.INFO,
max_concurrent_installers = 4,
}
require("mason").setup(settings)
-- * buka remark ini jika akan menggunakan list serverrs diatas dan remark config dibawah
require("mason-lspconfig").setup({
ensure_installed = servers,
automatic_installation = true,
})
-- local servers = {
-- "lua_ls",
-- }
--
-- * buka remark ini jika ingin menjalankan dengan cara install dan remark config diatas (pilih satu)
-- require("mason-lspconfig").setup()
local lspconfig_status_ok, lspconfig = pcall(require, "lspconfig")
if not lspconfig_status_ok then
return
end
local opts = {}
require("mason-lspconfig").setup_handlers({
function(server_name) -- default handler (optional)
local capabilities = require("user.lsp.handlers").capabilities
if server_name == "clangd" then
capabilities.offsetEncoding = { "utf-16" }
end
local is_skip = false
local my_index = idxOf(unregis_lsp, server_name)
if my_index ~= nil then
is_skip = true
end
if not is_skip then
opts = {
on_attach = require("user.lsp.handlers").on_attach,
capabilities = capabilities,
}
server_name = vim.split(server_name, "@")[1]
local require_ok, conf_opts = pcall(require, "user.lsp.settings." .. server_name)
if require_ok then
opts = vim.tbl_deep_extend("force", conf_opts, opts)
end
lspconfig[server_name].setup(opts)
end
end,
-- Next, you can provide targeted overrides for specific servers.
-- ["rust_analyzer"] = function()
-- require("rust-tools").setup({})
-- end,
-- ["lua_ls"] = function()
-- lspconfig.sumneko_lua.setup({
-- settings = {
-- Lua = {
-- diagnostics = {
-- globals = { "vim" },
-- },
-- },
-- },
-- })
-- end,
})
-- local function idxOf(array, value)
-- for i, v in ipairs(array) do
-- if v == value then
-- return i
-- end
-- end
-- return nil
-- end
--
-- local mason_install = pcode.mason_ensure_installed or {}
-- for _, client in pairs(mason_install) do
-- table.insert(servers, client)
-- end
--
-- local unregis_lsp = pcode.unregister_lsp or {}
-- local icons = vim.g.pcode_icons.ui
--
-- local settings = {
-- ui = {
-- -- border = "none",
-- border = icons.Border,
-- icons = {
-- package_pending = icons.DotCircle,
-- package_installed = icons.CheckCircle,
-- package_uninstalled = icons.BlankCircle,
-- },
-- keymaps = {
-- -- Keymap to expand a server in the UI
-- toggle_server_expand = "<CR>",
-- -- Keymap to install the server under the current cursor position
-- install_server = "i",
-- -- Keymap to reinstall/update the server under the current cursor position
-- update_server = "u",
-- -- Keymap to check for new version for the server under the current cursor position
-- check_server_version = "c",
-- -- Keymap to update all installed servers
-- update_all_servers = "U",
-- -- Keymap to check which installed servers are outdated
-- check_outdated_servers = "C",
-- -- Keymap to uninstall a server
-- uninstall_server = "X",
-- },
-- },
-- log_level = vim.log.levels.INFO,
-- max_concurrent_installers = 4,
-- }
--
-- require("mason").setup(settings)
-- -- * buka remark ini jika akan menggunakan list serverrs diatas dan remark config dibawah
-- require("mason-lspconfig").setup({
-- ensure_installed = servers,
-- automatic_installation = true,
-- })
-- --
-- -- * buka remark ini jika ingin menjalankan dengan cara install dan remark config diatas (pilih satu)
-- -- require("mason-lspconfig").setup()
--
-- local lspconfig_status_ok, lspconfig = pcall(require, "lspconfig")
-- if not lspconfig_status_ok then
-- return
-- end
--
-- local opts = {}
--
-- require("mason-lspconfig").setup_handlers({
-- function(server_name) -- default handler (optional)
-- local capabilities = require("user.lsp.handlers").capabilities
-- if server_name == "clangd" then
-- capabilities.offsetEncoding = { "utf-16" }
-- end
-- local is_skip = false
-- local my_index = idxOf(unregis_lsp, server_name)
-- if my_index ~= nil then
-- is_skip = true
-- end
-- if not is_skip then
-- opts = {
-- on_attach = require("user.lsp.handlers").on_attach,
-- capabilities = capabilities,
-- }
--
-- server_name = vim.split(server_name, "@")[1]
--
-- local require_ok, conf_opts = pcall(require, "user.lsp.settings." .. server_name)
-- if require_ok then
-- opts = vim.tbl_deep_extend("force", conf_opts, opts)
-- end
-- lspconfig[server_name].setup(opts)
-- end
-- end,
-- -- Next, you can provide targeted overrides for specific servers.
-- -- ["rust_analyzer"] = function()
-- -- require("rust-tools").setup({})
-- -- end,
-- -- ["lua_ls"] = function()
-- -- lspconfig.sumneko_lua.setup({
-- -- settings = {
-- -- Lua = {
-- -- diagnostics = {
-- -- globals = { "vim" },
-- -- },
-- -- },
-- -- },
-- -- })
-- -- end,
-- })

View file

@ -1,84 +1,84 @@
local null_ls_status_ok, null_ls = pcall(require, "null-ls")
if not null_ls_status_ok then
return
end
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
local formatting = null_ls.builtins.formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
local diagnostics = null_ls.builtins.diagnostics
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
local sources = {}
local ensure_installed = {}
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
-- load data null-ls
local nullls_data = pcode.null_ls_ensure_installed or {}
for _, nullls in pairs(nullls_data) do
table.insert(ensure_installed, nullls)
end
local mason_ok, mason_null_ls = pcall(require, "mason-null-ls")
if mason_ok then
mason_null_ls.setup({
ensure_installed = ensure_installed,
})
end
local run = 0
local frmt = pcode.format_on_save or 0
if frmt == 1 then
run = 1
end
if run == 1 then
null_ls.setup({
debug = false,
ensure_installed = ensure_installed,
sources = sources,
--sources = {
--formatting.prettier.with({ extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" } }),
--formatting.prettier,
-- formatting.prettierd,
-- formatting.black.with({ extra_args = { "--fast" } }),
-- formatting.stylua,
-- formatting.eslint_d,
-- formatting.google_java_format,
-- formatting.phpcbf,
-- formatting.clang_format,
-- diagnostics.flake8
-- diagnostics.eslint_d,
--},
on_attach = function(client, bufnr)
--if client.resolved_capabilities.document_formatting then
--vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.format{async=true}")
--end
if client.supports_method("textDocument/formatting") then
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 })
-- vim.lsp.buf.formatting_sync()
end,
})
end
end,
})
else
null_ls.setup({
debug = false,
ensure_installed = ensure_installed,
sources = sources,
})
end
-- local null_ls_status_ok, null_ls = pcall(require, "null-ls")
-- if not null_ls_status_ok then
-- return
-- end
--
-- -- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
-- local formatting = null_ls.builtins.formatting
-- -- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
-- local diagnostics = null_ls.builtins.diagnostics
--
-- local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
--
-- local sources = {}
-- local ensure_installed = {}
--
-- 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
--
-- -- load data null-ls
-- local nullls_data = pcode.null_ls_ensure_installed or {}
-- for _, nullls in pairs(nullls_data) do
-- table.insert(ensure_installed, nullls)
-- end
--
-- local mason_ok, mason_null_ls = pcall(require, "mason-null-ls")
-- if mason_ok then
-- mason_null_ls.setup({
-- ensure_installed = ensure_installed,
-- })
-- end
--
-- local run = 0
-- local frmt = pcode.format_on_save or 0
-- if frmt == 1 then
-- run = 1
-- end
--
-- if run == 1 then
-- null_ls.setup({
-- debug = false,
-- ensure_installed = ensure_installed,
-- sources = sources,
-- --sources = {
-- --formatting.prettier.with({ extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" } }),
-- --formatting.prettier,
-- -- formatting.prettierd,
-- -- formatting.black.with({ extra_args = { "--fast" } }),
-- -- formatting.stylua,
-- -- formatting.eslint_d,
-- -- formatting.google_java_format,
-- -- formatting.phpcbf,
-- -- formatting.clang_format,
-- -- diagnostics.flake8
-- -- diagnostics.eslint_d,
-- --},
--
-- on_attach = function(client, bufnr)
-- --if client.resolved_capabilities.document_formatting then
-- --vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.format{async=true}")
-- --end
-- if client.supports_method("textDocument/formatting") then
-- 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 })
-- -- vim.lsp.buf.formatting_sync()
-- end,
-- })
-- end
-- end,
-- })
-- else
-- null_ls.setup({
-- debug = false,
-- ensure_installed = ensure_installed,
-- sources = sources,
-- })
-- end

View file

@ -122,6 +122,8 @@ return {
table.insert(buf_client_names, "lsp_fmt")
end
end
elseif not status and supported_formatters and #supported_formatters == 0 then
table.insert(buf_client_names, "lsp_fmt")
end
-- add linter