From 1c7f17ea30b80fbba8716900147267f61b68c2c8 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 9 Oct 2023 20:25:38 -0400 Subject: [PATCH] neovim lsp --- home-config/home-configuration.scm | 3 +- home-config/nix-home-manager/home.nix | 1 + home-config/nvim/init.lua | 7 ++ home-config/nvim/init.vim | 36 --------- home-config/nvim/lua/keybinds.lua | 18 +++++ home-config/nvim/lua/lsp.lua | 73 +++++++++++++++++++ home-config/nvim/lua/plugin.lua | 28 +++++++ home-config/nvim/lua/theme.lua | 22 ++++++ home-config/nvim/lua/tree.lua | 101 ++++++++++++++++++++++++++ home-config/nvim/lua/treesitter.lua | 30 ++++++++ home-config/nvim/lua/vimopt.lua | 13 ++++ 11 files changed, 295 insertions(+), 37 deletions(-) create mode 100644 home-config/nvim/init.lua delete mode 100644 home-config/nvim/init.vim create mode 100644 home-config/nvim/lua/keybinds.lua create mode 100644 home-config/nvim/lua/lsp.lua create mode 100644 home-config/nvim/lua/plugin.lua create mode 100644 home-config/nvim/lua/theme.lua create mode 100644 home-config/nvim/lua/tree.lua create mode 100644 home-config/nvim/lua/treesitter.lua create mode 100644 home-config/nvim/lua/vimopt.lua diff --git a/home-config/home-configuration.scm b/home-config/home-configuration.scm index 0b6b8a1..b4414f9 100644 --- a/home-config/home-configuration.scm +++ b/home-config/home-configuration.scm @@ -49,6 +49,7 @@ "nasm" ;; devlopment packages "rust" ;; packages usefull for coding and devlopment "gcc-toolchain" + "clang-toolchain" "racket" "python" "node" @@ -117,7 +118,7 @@ (zprofile (list (local-file "zsh/zprofile"))))) (service home-xdg-configuration-files-service-type - `(("nvim/init.vim" ,(local-file "nvim/init.vim")) + `(("nvim" ,(local-file "nvim" #:recursive? #t)) ("sway" ,(local-file "sway" #:recursive? #t)) ("waybar" ,(local-file "waybar" #:recursive? #t)) ("alacritty" ,(local-file "alacritty" #:recursive? #t)) diff --git a/home-config/nix-home-manager/home.nix b/home-config/nix-home-manager/home.nix index 289be29..b313b38 100644 --- a/home-config/nix-home-manager/home.nix +++ b/home-config/nix-home-manager/home.nix @@ -41,6 +41,7 @@ lua lua52Packages.dkjson lua52Packages.luaposix + lua-language-server ]; # Let Home Manager install and manage itself. diff --git a/home-config/nvim/init.lua b/home-config/nvim/init.lua new file mode 100644 index 0000000..fd623bf --- /dev/null +++ b/home-config/nvim/init.lua @@ -0,0 +1,7 @@ +require('plugin') +require('theme') +require('vimopt') +require('tree') +require('keybinds') +require('treesitter') +require('lsp') diff --git a/home-config/nvim/init.vim b/home-config/nvim/init.vim deleted file mode 100644 index 45b705b..0000000 --- a/home-config/nvim/init.vim +++ /dev/null @@ -1,36 +0,0 @@ -call plug#begin() - -Plug 'itchyny/lightline.vim' - -Plug 'folke/tokyonight.nvim', { 'branch': 'main' } - -call plug#end() - -syntax on -filetype plugin indent on - -colorscheme tokyonight-moon -let g:lightline = {'colorscheme': 'tokyonight'} - -set mouse=v -set mouse=a -set clipboard=unnamedplus -set autoindent -set hlsearch -set tabstop=4 -set shiftwidth=0 -set expandtab -set number -set cc=80 -set encoding=utf-8 -set nofoldenable -set laststatus=2 -set noshowmode -set modeline -set mouse=a -set ttyfast -set colorcolumn=0 - -hi Normal guibg=NONE ctermbg=NONE -hi LineNr term=bold cterm=bold ctermfg=2 guifg=White guibg=NONE - diff --git a/home-config/nvim/lua/keybinds.lua b/home-config/nvim/lua/keybinds.lua new file mode 100644 index 0000000..cea6cfe --- /dev/null +++ b/home-config/nvim/lua/keybinds.lua @@ -0,0 +1,18 @@ +local opts = { noremap = true, silent = true } +local keymap = vim.api.nvim_set_keymap + +vim.g.mapleader = ' ' +vim.g.maplocalleader = ' ' + +keymap('', '', '', opts) +keymap('n', 'e', ':NvimTreeToggle %', opts) + +local builtin = require('telescope.builtin') +local keymap = vim.keymap.set + +keymap('n', 'ff', builtin.find_files, {}) +keymap('n', 'fg', builtin.live_grep, {}) +keymap('n', 'fb', builtin.buffers, {}) +keymap('n', 'fh', builtin.help_tags, {}) +keymap('n', 'u', vim.cmd.UndotreeToggle) +keymap('n', 'gs', vim.cmd.Git) diff --git a/home-config/nvim/lua/lsp.lua b/home-config/nvim/lua/lsp.lua new file mode 100644 index 0000000..af62047 --- /dev/null +++ b/home-config/nvim/lua/lsp.lua @@ -0,0 +1,73 @@ +local lsp = require('lsp-zero') + +lsp.preset('recommended') + +require('mason').setup({}) +require('mason-lspconfig').setup({ + ensure_installed = { + 'tsserver', + 'eslint', + 'rust_analyzer', + 'clangd', + 'html', + 'cssls', + 'pyright', + 'vimls', + 'yamlls', + 'jdtls', + 'bashls', + 'lua_ls' + }, + handlers = { + lsp.default_setup, + }, +}) + +local cmp = require('cmp') + +local cmp_select = {behavior = cmp.SelectBehavior.Select} +local cmp_mappings = cmp.mapping.preset.insert({ + [''] = cmp.mapping.select_prev_item(cmp_select), + [''] = cmp.mapping.select_next_item(cmp_select), + [''] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping.complete(), +}) + +cmp_mappings[''] = nil +cmp_mappings[''] = nil + +cmp.setup({ + mapping = cmp_mappings +}) + +lsp.set_preferences({ + suggest_lsp_servers = false, + sign_icons = { + error = 'E', + warn = 'W', + hint = 'H', + info = 'I' + } +}) + +lsp.on_attach(function(client, bufnr) + local opts = {buffer = bufnr, remap = false} + local keymap = vim.keymap.set + + keymap("n", "gd", function() vim.lsp.buf.definition() end, opts) + keymap("n", "K", function() vim.lsp.buf.hover() end, opts) + keymap("n", "vws", function() vim.lsp.buf.workspace_symbol() end, opts) + keymap("n", "vd", function() vim.diagnostic.open_float() end, opts) + keymap("n", "[d", function() vim.diagnostic.goto_next() end, opts) + keymap("n", "]d", function() vim.diagnostic.goto_prev() end, opts) + keymap("n", "vca", function() vim.lsp.buf.code_action() end, opts) + keymap("n", "vrr", function() vim.lsp.buf.references() end, opts) + keymap("n", "vrn", function() vim.lsp.buf.rename() end, opts) + keymap("i", "", function() vim.lsp.buf.signature_help() end, opts) +end) + +lsp.setup() + +vim.diagnostic.config({ + virtual_text = true +}) diff --git a/home-config/nvim/lua/plugin.lua b/home-config/nvim/lua/plugin.lua new file mode 100644 index 0000000..f477d85 --- /dev/null +++ b/home-config/nvim/lua/plugin.lua @@ -0,0 +1,28 @@ +local Plug = vim.fn['plug#'] + +vim.call('plug#begin') + +Plug('nvim-tree/nvim-web-devicons') +Plug('nvim-tree/nvim-tree.lua') +Plug('nvim-lualine/lualine.nvim') +Plug('ryanoasis/vim-devicons') +Plug('SirVer/ultisnips') +Plug('honza/vim-snippets') +Plug('preservim/nerdcommenter') +Plug('mhinz/vim-startify') +Plug('neoclide/coc.nvim', {branch = 'release'}) +Plug('nvim-telescope/telescope.nvim', {tag = '0.1.3' }) +Plug('nvim-treesitter/nvim-treesitter', {run = ':TSUpdate'}) +Plug('nvim-lua/plenary.nvim') +Plug('mbbill/undotree') +Plug('catppuccin/nvim', {as = 'catppuccin'}) +Plug('tpope/vim-fugitive') +Plug('neovim/nvim-lspconfig') +Plug('hrsh7th/nvim-cmp') +Plug('hrsh7th/cmp-nvim-lsp') +Plug('L3MON4D3/LuaSnip') +Plug('williamboman/mason.nvim') +Plug('williamboman/mason-lspconfig.nvim') +Plug('VonHeikemen/lsp-zero.nvim', {branch = 'v3.x'}) + +vim.call('plug#end') diff --git a/home-config/nvim/lua/theme.lua b/home-config/nvim/lua/theme.lua new file mode 100644 index 0000000..e10d0e2 --- /dev/null +++ b/home-config/nvim/lua/theme.lua @@ -0,0 +1,22 @@ +local options = { + termguicolors = true +} + +local colorscheme = "catppuccin" +local ok, _ = pcall(vim.cmd, "colorscheme " .. colorscheme) +vim.o.background = "dark" -- or "light" for light mode +if not ok then + vim.notify("colorscheme " .. colorscheme .. " not found!") + return +end + +require('lualine').setup { + options = { + theme = colorscheme, + icons_enabled = true, + globalstatus = true, + }, +} + +vim.api.nvim_set_hl(0, "Normal", { bg = "none" }) +vim.api.nvim_set_hl(0, "NormalFloat", { bg = "none" }) diff --git a/home-config/nvim/lua/tree.lua b/home-config/nvim/lua/tree.lua new file mode 100644 index 0000000..0214392 --- /dev/null +++ b/home-config/nvim/lua/tree.lua @@ -0,0 +1,101 @@ +vim.g.loaded_netrw = 1 +vim.g.loaded_netrwPlugin = 1 + +local function on_attach(bufnr) + local api = require("nvim-tree.api") + + local function opts(desc) + return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } + end + + local function edit_or_open() + local node = api.tree.get_node_under_cursor() + + if node.nodes ~= nil then + -- expand or collapse folder + api.node.open.edit() + else + -- open file + api.node.open.edit() + -- Close the tree if file was opened + api.tree.close() + end + end + + api.config.mappings.default_on_attach(bufnr) + vim.keymap.set("n", "", edit_or_open, opts("Open")) +end + +require("nvim-tree").setup({ + sort_by = "case_sensitive", + view = { + width = 30, + }, + renderer = { + group_empty = true, + }, + actions = { + use_system_clipboard = true, + change_dir = { + enable = true, + global = false, + restrict_above_cwd = false, + }, + expand_all = { + max_folder_discovery = 300, + exclude = {}, + }, + file_popup = { + open_win_config = { + col = 1, + row = 1, + relative = "cursor", + border = "shadow", + style = "minimal", + }, + }, + open_file = { + window_picker = { + enable = false, + picker = "default", + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", + exclude = { + filetype = { "notify", "lazy", "qf", "diff", "fugitive", "fugitiveblame" }, + buftype = { "nofile", "terminal", "help" }, + }, + } + }, + remove_file = { + close_window = true, + }, + + }, + tab = { + sync = { + open = false, + close = false, + ignore = {}, + }, + }, + git = { + enable = true, + ignore = false, + show_on_dirs = true, + show_on_open_dirs = true, + timeout = 200, + }, + filters = { + dotfiles = false, + git_clean = false, + no_buffer = false, + custom = { "node_modules", "\\.cache" }, + exclude = {}, + }, + update_cwd = true, + respect_buf_cwd = true, + update_focused_file = { + enable = true, + update_cwd = true + }, + on_attach = on_attach +}) diff --git a/home-config/nvim/lua/treesitter.lua b/home-config/nvim/lua/treesitter.lua new file mode 100644 index 0000000..268ec0e --- /dev/null +++ b/home-config/nvim/lua/treesitter.lua @@ -0,0 +1,30 @@ +local lsps = { + "c", + "lua", + "rust", + "typescript", + "javascript", + "python", + "vim", + "vimdoc", + "query" +}; + +require('nvim-treesitter.configs').setup { + ensure_installed = lsps, + sync_install = false, + auto_install = true, + ignore_install = { "javascript" }, + highlight = { + enable = true, + disable = { "c", "rust" }, + disable = function(lang, buf) + local max_filesize = 100 * 1024 -- 100 KB + local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf)) + if ok and stats and stats.size > max_filesize then + return true + end + end, + additional_vim_regex_highlighting = false, + }, +} diff --git a/home-config/nvim/lua/vimopt.lua b/home-config/nvim/lua/vimopt.lua new file mode 100644 index 0000000..ac1a689 --- /dev/null +++ b/home-config/nvim/lua/vimopt.lua @@ -0,0 +1,13 @@ +local set = vim.opt +set.tabstop = 4 +set.softtabstop = 4 +set.shiftwidth = 4 +set.mouse = "a" +set.clipboard = "unnamedplus" +set.hlsearch = true +set.autoindent = true +set.ttyfast = true +set.cc = '80' +set.number = true +set.relativenumber = true +set.rnu = true