Skip to content

Commit

Permalink
[NVIM] lsp setup
Browse files Browse the repository at this point in the history
  • Loading branch information
goncrust committed Dec 3, 2023
1 parent f47fccd commit fea84e6
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .config/nvim/lua/goncrust/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ require("goncrust.plugins.treesitter")
require("goncrust.plugins.nvim-tree")
require("goncrust.plugins.startify")
vim.cmd("source $HOME/.config/nvim/lua/goncrust/plugins/airline.vim")
require("goncrust.plugins.lsp-config")

-- other config
vim.opt.hidden = true -- Required to keep multiple buffers open
vim.opt.wrap = false -- Display long lines as one line
vim.opt.encoding = 'utf-8' -- The encoding displayed
vim.opt.pumheight = 9 -- Popup menu height
vim.opt.pumheight = 9 -- Popup menu height
vim.opt.fileencoding = 'utf-8' -- The encoding written to a file
vim.opt.ruler = true -- Show the cursor position all the time
vim.opt.cmdheight = 2 -- More space for displaying messages
Expand Down
6 changes: 6 additions & 0 deletions .config/nvim/lua/goncrust/packer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ return require('packer').startup(function(use)

use("vim-airline/vim-airline")
use("vim-airline/vim-airline-themes")

use {
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"neovim/nvim-lspconfig"
}
end)
74 changes: 74 additions & 0 deletions .config/nvim/lua/goncrust/plugins/lsp-config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = { "clangd", "pyright", "jdtls",
"html", "jsonls", "tsserver", "cssls", "lua_ls",
"rust_analyzer", "ltex", "marksman", "sqlls", "yamlls" }
})

-- Setup language servers.
local lspconfig = require('lspconfig')
lspconfig.clangd.setup {}
lspconfig.pyright.setup {}
lspconfig.jdtls.setup {}
lspconfig.html.setup {}
lspconfig.jsonls.setup {}
lspconfig.tsserver.setup {}
lspconfig.cssls.setup {}
lspconfig.lua_ls.setup {}
lspconfig.rust_analyzer.setup {
-- Server-specific settings. See `:help lspconfig-setup`
settings = {
['rust-analyzer'] = {},
},
}
lspconfig.ltex.setup {}
lspconfig.marksman.setup {}
lspconfig.sqlls.setup {}
lspconfig.yamlls.setup {}


-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float)
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
vim.keymap.set('n', '<space>w', vim.diagnostic.setloclist)

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('UserLspConfig', {}),
callback = function(ev)
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf }
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
vim.keymap.set('n', '<space>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, opts)
vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
vim.keymap.set('n', '<space>f', function()
vim.lsp.buf.format { async = true }
end, opts)
end,
})

-- format on save
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = buffer,
callback = function()
vim.lsp.buf.format { async = false }
end
})
41 changes: 34 additions & 7 deletions .config/nvim/lua/goncrust/plugins/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,40 @@ require("nvim-tree").setup({
vim.api.nvim_set_keymap("n", "<leader>f", ":NvimTreeToggle<cr>", {})

-- autoclose
vim.o.confirm = true
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("NvimTreeClose", {clear = true}),
callback = function()
local layout = vim.api.nvim_call_function("winlayout", {})
if layout[1] == "leaf" and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(layout[2]), "filetype") == "NvimTree" and layout[3] == nil then vim.cmd("quit") end
end
local function tab_win_closed(winnr)
local api = require"nvim-tree.api"
local tabnr = vim.api.nvim_win_get_tabpage(winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr)
local buf_info = vim.fn.getbufinfo(bufnr)[1]
local tab_wins = vim.tbl_filter(function(w) return w~=winnr end, vim.api.nvim_tabpage_list_wins(tabnr))
local tab_bufs = vim.tbl_map(vim.api.nvim_win_get_buf, tab_wins)
if buf_info.name:match(".*NvimTree_%d*$") then -- close buffer was nvim tree
-- Close all nvim tree on :q
if not vim.tbl_isempty(tab_bufs) then -- and was not the last window (not closed automatically by code below)
api.tree.close()
end
else -- else closed buffer was normal buffer
if #tab_bufs == 1 then -- if there is only 1 buffer left in the tab
local last_buf_info = vim.fn.getbufinfo(tab_bufs[1])[1]
if last_buf_info.name:match(".*NvimTree_%d*$") then -- and that buffer is nvim tree
vim.schedule(function ()
if #vim.api.nvim_list_wins() == 1 then -- if its the last buffer in vim
vim.cmd "quit" -- then close all of vim
else -- else there are more tabs open
vim.api.nvim_win_close(tab_wins[1], true) -- then close only the tab
end
end)
end
end
end
end

vim.api.nvim_create_autocmd("WinClosed", {
callback = function ()
local winnr = tonumber(vim.fn.expand("<amatch>"))
vim.schedule_wrap(tab_win_closed(winnr))
end,
nested = true
})

-- startup
Expand Down
7 changes: 3 additions & 4 deletions .config/nvim/lua/goncrust/plugins/startify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ vim.g.startify_lists = {

-- Startify bookmarks
vim.g.startify_bookmarks = {
{ c = '~/.xmonad/xmonad.hs' },
{ n = '~/.config/nvim/init.vim' },
{ n = '~/.config/nvim' },
{ z = '~/.zshrc' },
'/first-hdd/dev',
'/first-hdd/dev/SchoolProjects',
{ d = '/home/goncrust/Documents/dev' },
{ s = '/home/goncrust/Documents/School' },
}

-- Use Unicode in Startify fortune
Expand Down
15 changes: 15 additions & 0 deletions .config/nvim/plugin/packer_compiled.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ end
time([[try_loadstring definition]], false)
time([[Defining packer_plugins]], true)
_G.packer_plugins = {
["mason-lspconfig.nvim"] = {
loaded = true,
path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/mason-lspconfig.nvim",
url = "https://github.com/williamboman/mason-lspconfig.nvim"
},
["mason.nvim"] = {
loaded = true,
path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/mason.nvim",
url = "https://github.com/williamboman/mason.nvim"
},
["nvim-lspconfig"] = {
loaded = true,
path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/nvim-lspconfig",
url = "https://github.com/neovim/nvim-lspconfig"
},
["nvim-tree.lua"] = {
loaded = true,
path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/nvim-tree.lua",
Expand Down

0 comments on commit fea84e6

Please sign in to comment.