Skip to content

Commit

Permalink
Chore(nvim): format using lua-format
Browse files Browse the repository at this point in the history
tags:
    lua-format, code-formatting
  • Loading branch information
PrashanthaTP committed Dec 12, 2021
1 parent 4249938 commit 714d03e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 108 deletions.
20 changes: 11 additions & 9 deletions .config/nvim/lua/plugins/nvim-lspconfig/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local nvim_lsp = require("lspconfig")
local utils = require("plugins.nvim-lspconfig.utils")
local on_attach = utils.on_attach

-- Use a loop to conveniently call 'setup' on multiple servers and
-- map buffer local keybindings when the language server attaches
local servers = { "pyright", "clangd", "cmake", "tsserver", "cssls", "sumneko_lua" }

local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())

local servers = {
"pyright", "clangd", "cmake", "tsserver", "cssls", "sumneko_lua"
}
local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp
.protocol
.make_client_capabilities())
--[[
for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup({
Expand All @@ -19,21 +20,22 @@ for _, lsp in ipairs(servers) do
})
end
--]]
vim.lsp.set_log_level("debug")
-- vim.lsp.set_log_level("debug")
require("plugins.nvim-lspconfig.servers.pyright").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.clangd").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.cmake").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.cssls").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.sumneko_lua").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.sumneko_lua").setup(on_attach,
capabilities)
require("plugins.nvim-lspconfig.servers.tsserver").setup(on_attach, capabilities)
require("plugins.nvim-lspconfig.servers.efm").setup(on_attach, capabilities)
--require("plugins.nvim-lspconfig.servers.diagnosticls").setup(on_attach,capabilities)
--
function _G.updateLocationList()
vim.lsp.diagnostic.set_loclist({ open_loclist = false })
vim.lsp.diagnostic.set_loclist({open_loclist = false})
end

---reference : https://rafaelleru.github.io/blog/quickfix-autocomands/
--- reference for autocmd : https://github.com/neovim/neovim/issues/14090#issuecomment-979424077
vim.cmd([[
augroup UPDATE_LOCATION_LIST_WITH_LSP_LINTS
autocmd!
Expand Down
185 changes: 86 additions & 99 deletions .config/nvim/lua/plugins/nvim-lspconfig/utils.lua
Original file line number Diff line number Diff line change
@@ -1,112 +1,99 @@
local lspconfig = require("lspconfig")
local t = require("utils").replace_termcodes
local M = {}

M.set_keymappings = function(bufnr)
local buf_set_keymap = function(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end

local opts = { noremap = true, silent = true }

-- See `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", t("<C-k>"), "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
buf_set_keymap("n", t("<space>wa"), "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
buf_set_keymap("n", t("<space>wr"), "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
buf_set_keymap("n", t("<space>wl"), "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
buf_set_keymap("n", t("<space>D"), "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
buf_set_keymap("n", t("<space>rn"), "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", t("<space>ca"), "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", t("<space>e"), "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
buf_set_keymap("n", t("<space>q"), "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
buf_set_keymap("n", t("<space>wf"), "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
-- formatting
local buf_set_keymap = function(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local opts = {noremap = true, silent = true}
-- See `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", t("<C-k>"), "<cmd>lua vim.lsp.buf.signature_help()<CR>",
opts)
buf_set_keymap("n", t("<space>wa"),
"<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
buf_set_keymap("n", t("<space>wr"),
"<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
buf_set_keymap("n", t("<space>wl"),
"<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>",
opts)
buf_set_keymap("n", t("<space>D"),
"<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
buf_set_keymap("n", t("<space>rn"), "<cmd>lua vim.lsp.buf.rename()<CR>",
opts)
buf_set_keymap("n", t("<space>ca"),
"<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", t("<space>e"),
"<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>",
opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>",
opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>",
opts)
buf_set_keymap("n", t("<space>q"),
"<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
-- buf_set_keymap("n", t("<space>wf"), "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
-- formatting
end

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
M.on_attach = function(client, bufnr)
local buf_set_option = function(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
-- Enable completion triggered by <c-x><c-o>
buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
-- Mappings.
M.set_keymappings(bufnr)

--[[ if client.name == 'tsserver' then
local buf_set_option = function(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
-- Enable completion triggered by <c-x><c-o>
buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
-- Mappings.
M.set_keymappings(bufnr)
--[[ if client.name == 'tsserver' then
client.resolved_capabilities.document_formatting = false
end
--]]
if client.resolved_capabilities.document_formatting then
vim.api.nvim_command([[augroup FORMAT]])
vim.api.nvim_command([[autocmd! * <buffer>]])
vim.api.nvim_command([[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]])
vim.api.nvim_command([[augroup end]])
end
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
virtual_text = { spacing = 4, prefix = "" },
signs = true,
underline = true,
update_in_insert = true,
})
--protocol.SymbolKind = { }
vim.lsp.protocol.completionItemKind = {
"", -- Text
"", -- Method
"", -- Function
"", -- Constructor
"", -- Field
"", -- Variable
"", -- Class
"", -- Interface
"", -- Module
"", -- Property
"", -- Unit
"", -- Value
"", -- Enum
"", -- Keyword
"", -- Snippet
"", -- Color
"", -- File
"", -- Reference
"", -- Folder
"", -- EnumMember
"", -- Constant
"", -- Struct
"", -- Event
"", -- Operator
"", -- TypeParameter
}
end

--[==[== on attach add formatting functionality
--
if client.resolved_capabilities.document_formatting then
vim.api.nvim_command([[augroup FORMAT]])
vim.api.nvim_command([[autocmd! * <buffer>]])
vim.api.nvim_command([[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]])
vim.api.nvim_command([[augroup end]])
end
--]==]
local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())

M.get_capabilities = function()
local _capabilities = nil
if capabilities == nil then
_capabilities = vim.lsp.protocol.make_client_capabilities()
_capabilities.textDocument.completion.completionItem.snippetSupport = true
else
_capabilities = capabilities
end
return _capabilities
if client.resolved_capabilities.document_formatting then
vim.api.nvim_command([[augroup FORMAT]])
vim.api.nvim_command([[autocmd! * <buffer>]])
vim.api.nvim_command(
[[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]])
vim.api.nvim_command([[augroup end]])
end
vim.lsp.handlers["textDocument/publishDiagnostics"] =
vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
virtual_text = {spacing = 4, prefix = ""},
signs = true,
underline = true,
update_in_insert = false
})
-- protocol.SymbolKind = { }
vim.lsp.protocol.completionItemKind = {
"", -- Text
"", -- Method
"", -- Function
"", -- Constructor
"", -- Field
"", -- Variable
"", -- Class
"", -- Interface
"", -- Module
"", -- Property
"", -- Unit
"", -- Value
"", -- Enum
"", -- Keyword
"", -- Snippet
"", -- Color
"", -- File
"", -- Reference
"", -- Folder
"", -- EnumMember
"", -- Constant
"", -- Struct
"", -- Event
"", -- Operator
"" -- TypeParameter
}
end

return M

0 comments on commit 714d03e

Please sign in to comment.