From ae6dca60d0827815639fb9ccf7b5a5ad0e998505 Mon Sep 17 00:00:00 2001 From: tea Date: Mon, 19 Aug 2024 11:59:14 +0200 Subject: [PATCH] nvim: rewrite config some more --- .config/nvim/lua/my/functions.lua | 95 +++++++++++ .config/nvim/lua/my/plugins/filetypes.lua | 64 ++++++++ .config/nvim/lua/my/plugins/lsp.lua | 15 ++ .config/nvim/lua/my/plugins/plugins.lua | 71 +------- .config/nvim/lua/my/plugins/tools.lua | 191 ++++++++++++++++++++++ 5 files changed, 371 insertions(+), 65 deletions(-) create mode 100644 .config/nvim/lua/my/plugins/filetypes.lua create mode 100644 .config/nvim/lua/my/plugins/lsp.lua create mode 100644 .config/nvim/lua/my/plugins/tools.lua diff --git a/.config/nvim/lua/my/functions.lua b/.config/nvim/lua/my/functions.lua index 5044d6f..8a2ae08 100644 --- a/.config/nvim/lua/my/functions.lua +++ b/.config/nvim/lua/my/functions.lua @@ -1,3 +1,98 @@ local M = {} +function M.has_api_level(desired) + return vim.version().api_level >= desired +end + +function M.nerdfont_installed() + if vim.fn.executable('fc-list') == 1 and + os.execute([[fc-list -q 'Symbols Nerd Font']]) == 0 then + return true + end + return false +end + +function M.map(mode, shortcut, command, description, buffer) + local opts = { noremap = true, silent = true } + opts.desc = description or nil + opts.buffer = buffer or nil + vim.keymap.set(mode, shortcut, command, opts) +end + +-- Files that could indicate the project's root directory +M.project_root_markers = { + '.git', '.hg', '.svn', '.bzr', '_darcs', + '.projectile', '.luarc.json', '.editorconfig', 'Cargo.toml' +} + +function M.get_project_root() + local lsp_root = vim.lsp.buf.list_workspace_folders()[1] + if lsp_root ~= nil then + return lsp_root + end + + local path = vim.api.nvim_buf_get_name(0) + local sep = '/' + + repeat + path = path:gsub(string.format('%s[^%s]*$', sep, sep), '') + for _, marker in ipairs(M.project_root_markers) do + if path ~= os.getenv('HOME') and io.open(path .. sep .. marker) then + return path + end + end + until path == '' + + return nil +end + +function M.not_firenvim() + return vim.g.started_by_firenvim == nil +end + +function M.lsp_status() + local status = '' + for _, msg in ipairs(require('lsp-status/messaging').messages()) do + if msg.progress then + status = '⏳' + elseif msg.status then + -- clangd parsing includes and whatnot + if msg.content ~= 'idle' then + status = '⌛' + end + end + end + + return status +end + +function M.lsp_sig_status() + if not vim.lsp.buf.server_ready() then + return "" + end + + local width = vim.o.columns / 3 + if width < 40 then + return "" + end + if width > 50 then + width = width * 1.5 + end + + local label = require('lsp_signature').status_line().label + if label:len() > width then + label = label:sub(1, math.floor(width)) .. '…' + end + + return label +end + +function M.current_function() + local curfun = vim.b.lsp_current_function + if curfun and curfun ~= '' then + return '(' .. curfun .. ')' + end + return '' +end + return M diff --git a/.config/nvim/lua/my/plugins/filetypes.lua b/.config/nvim/lua/my/plugins/filetypes.lua new file mode 100644 index 0000000..2d6b071 --- /dev/null +++ b/.config/nvim/lua/my/plugins/filetypes.lua @@ -0,0 +1,64 @@ +local has_api_level = require('my.functions').has_api_level + +return { + { 'https://github.com/nvim-treesitter/nvim-treesitter', + version = '*', + build = [[:TSUpdateSync]], + opts = { + ensure_installed = 'all', + ignore_install = { 'comment', 'help' }, + sync_install = true, + highlight = { + enable = true, + additional_vim_regex_highlighting = { 'cpp' } + } + }, + }, + { 'https://github.com/JoosepAlviste/nvim-ts-context-commentstring', + dependencies = 'nvim-treesitter', + enabled = has_api_level(11), + opts = { + enable = true, + enable_autocmd = false + }, + init = function() + vim.g.skip_ts_context_commentstring_module = true + end, + }, + { 'https://github.com/folke/todo-comments.nvim', + version = '*', + dependencies = { 'plenary.nvim' }, + enabled = has_api_level(11), + opts = { + signs = false, + highlight = { + keyword = 'bg', + after = '' + } + }, + init = function() + vim.g.vimtex_compiler_latexmk_engines = { _ = '-xelatex' } + end, + }, + { 'https://github.com/powerman/vim-plugin-AnsiEsc' }, + { 'https://github.com/lervag/vimtex', + version = '*', + enabled = has_api_level(11) and vim.fn.executable('xelatex') == 1 + }, + { 'https://github.com/NvChad/nvim-colorizer.lua', + opts = { + filetypes = { '*' }, + user_default_options = { + mode = 'virtualtext', + rgb_fn = true, + hsl_fn = true, + RRGGBBAA = true, + } + }, + }, + { 'https://github.com/gennaro-tedesco/nvim-jqx', + version = '*', + ft = { "json", "yaml" }, + }, + { 'https://github.com/isobit/vim-caddyfile' }, +} diff --git a/.config/nvim/lua/my/plugins/lsp.lua b/.config/nvim/lua/my/plugins/lsp.lua new file mode 100644 index 0000000..6b99e3e --- /dev/null +++ b/.config/nvim/lua/my/plugins/lsp.lua @@ -0,0 +1,15 @@ +-- TODO: configure +return { + { 'https://github.com/neovim/nvim-lspconfig', + version = '*', + dependencies = 'cmp-nvim-lsp', + }, + { 'https://github.com/lukas-reineke/lsp-format.nvim', + version = '*', + }, + { 'https://github.com/ray-x/lsp_signature.nvim' }, + { 'https://github.com/p00f/clangd_extensions.nvim' }, + { 'https://github.com/simrat39/rust-tools.nvim', + dependencies = 'nvim-lspconfig', + } +} diff --git a/.config/nvim/lua/my/plugins/plugins.lua b/.config/nvim/lua/my/plugins/plugins.lua index 93a3de1..bc6ce3a 100644 --- a/.config/nvim/lua/my/plugins/plugins.lua +++ b/.config/nvim/lua/my/plugins/plugins.lua @@ -1,12 +1,15 @@ -local function has_api_level(desired) - return vim.version().api_level >= desired -end +local has_api_level = require('my.functions').has_api_level +-- NOTE: next line is a variable, not a function +local nerdfont_installed = require('my.functions').nerdfont_installed() return { -- common dependencies { 'https://github.com/nvim-lua/plenary.nvim', lazy = true }, + { 'https://github.com/kyazdani42/nvim-web-devicons', + enabled = nerdfont_installed + }, -- basics { 'https://github.com/owozsh/amora', @@ -34,66 +37,4 @@ return { } end, }, - - -- filetypes - { 'https://github.com/nvim-treesitter/nvim-treesitter', - version = '*', - build = [[:TSUpdateSync]], - opts = { - ensure_installed = 'all', - ignore_install = { 'comment', 'help' }, - sync_install = true, - highlight = { - enable = true, - additional_vim_regex_highlighting = { 'cpp' } - } - }, - }, - { 'https://github.com/JoosepAlviste/nvim-ts-context-commentstring', - dependencies = 'nvim-treesitter', - enabled = has_api_level(11), - opts = { - enable = true, - enable_autocmd = false - }, - init = function() - vim.g.skip_ts_context_commentstring_module = true - end, - }, - { 'https://github.com/folke/todo-comments.nvim', - version = '*', - dependencies = { 'plenary.nvim' }, - enabled = has_api_level(11), - opts = { - signs = false, - highlight = { - keyword = 'bg', - after = '' - } - }, - init = function() - vim.g.vimtex_compiler_latexmk_engines = { _ = '-xelatex' } - end, - }, - { 'https://github.com/powerman/vim-plugin-AnsiEsc' }, - { 'https://github.com/lervag/vimtex', - version = '*', - enabled = has_api_level(11) and vim.fn.executable('xelatex') == 1 - }, - { 'https://github.com/NvChad/nvim-colorizer.lua', - opts = { - filetypes = { '*' }, - user_default_options = { - mode = 'virtualtext', - rgb_fn = true, - hsl_fn = true, - RRGGBBAA = true, - } - }, - }, - { 'https://github.com/gennaro-tedesco/nvim-jqx', - version = '*', - ft = { "json", "yaml" }, - }, - { 'https://github.com/isobit/vim-caddyfile' }, } diff --git a/.config/nvim/lua/my/plugins/tools.lua b/.config/nvim/lua/my/plugins/tools.lua new file mode 100644 index 0000000..180e8b0 --- /dev/null +++ b/.config/nvim/lua/my/plugins/tools.lua @@ -0,0 +1,191 @@ +local map = require('my.functions').map +local not_firenvim = require('my.functions').not_firenvim + +-- TODO: enable projections and dap plugins +return { + { 'https://github.com/folke/which-key.nvim', + version = '*', + }, + { 'https://github.com/nvim-telescope/telescope.nvim', + dependencies = { + 'plenary.nvim', + 'nvim-treesitter', + 'nvim-web-devicons', + }, + config = function() + local telescope = require('telescope') + local t_actions = require('telescope.actions') + -- telescope.load_extension('projections') + -- telescope.load_extension('dap') + telescope.load_extension('fzf') + + telescope.setup({ + mappings = { + i = { + [""] = t_actions.which_key, + }, + n = { + ["?"] = t_actions.which_key, + } + }, + defaults = { + file_ignore_patterns = { '^\\.git', '^fun' }, + }, + pickers = { + find_files = { + -- show hidden files by default + find_command = { "rg", "--files", "--hidden", "--glob", "!.git/*" } + } + } + }) + + local t_builtin = require('telescope.builtin') + local get_project_root = require('my.functions').get_project_root + require('which-key').add({{ 't', group = 'telescope' }}) + + map('n', 'tb', t_builtin.buffers, 'buffers') + map('n', 'tf', function() + t_builtin.find_files({ cwd = get_project_root() }) + end, 'files') + map('n', 'tF', t_builtin.find_files, 'files in cwd') + map('n', 'to', t_builtin.oldfiles, 'recently opened files') + map('n', 'tg', function() + t_builtin.live_grep({ cwd = get_project_root() }) + end, 'Live-grep') + map('n', 'tm', t_builtin.man_pages, 'man pages') + map('n', 'tr', t_builtin.registers, 'registers') + map('n', 'gh', t_builtin.git_bcommits, 'history of this file') + map('n', '', t_builtin.diagnostics, 'show diagnostics') + -- map('n', 'tp', telescope.extensions.projections.projections, 'projects') + map('n', 'tt', + require("telescope._extensions.todo-comments").exports.todo, + 'display TODO comments in project') + map('n', 'tn', telescope.extensions.notify.notify, 'notifications') + end, + }, + { 'https://github.com/nvim-telescope/telescope-fzf-native.nvim', + build = 'make', + }, + { 'https://github.com/kyazdani42/nvim-tree.lua', + version = '*', + dependencies = { 'nvim-web-devicons' }, + config = function() + require("nvim-tree").setup({ + filters = { + dotfiles = false, + custom = { '^\\.git' } + }, + sync_root_with_cwd = true, + respect_buf_cwd = true, + update_focused_file = { + enable = true, + update_root = true + }, + renderer = { + icons = { + show = { + file = nerdfont_installed, + folder = nerdfont_installed, + folder_arrow = nerdfont_installed, + git = nerdfont_installed + } + } + } + }) + + map('n', '', require("nvim-tree.api").tree.toggle, 'open nvim-tree') + map('i', '', require("nvim-tree.api").tree.toggle, 'open nvim-tree') + end, + }, + { 'https://github.com/nvim-lualine/lualine.nvim', + dependencies = { + 'nvim-web-devicons', + 'https://github.com/nvim-lua/lsp-status.nvim', + }, + init = function() + vim.o.showmode = false + end, + opts = { + options = { + icons_enabled = nerdfont_installed, + component_separators = {}, + section_separators = {}, + extensions = {}, + globalstatus = true + }, + sections = { + lualine_a = { { 'mode' } }, + lualine_b = { + { 'branch', icons_enabled = true }, + { 'diff' }, + { require('my.functions').lsp_status, padding = 0 }, + { 'diagnostics' } + }, + lualine_c = { + { + 'filename', + path = 1, + newfile_status = true, + cond = not_firenvim + }, + { + require('my.functions').current_function, + padding = 0, + color = 'StatusLineNC' + } + }, + lualine_x = { + { + require('my.functions').lsp_sig_status, + color = 'StatusLineNC' + }, + { 'filetype' } + }, + lualine_y = { { 'progress' } }, + lualine_z = { { 'location' }, { '"🍄"', padding = 0 }}, + }, + inactive_winbar = { + lualine_c = { { 'filename', cond = not_firenvim } }, + }, + }, + }, + { 'https://codeberg.org/tastytea/bug-reference.nvim', + keys = { + { 'B', 'BugReference', + desc = 'open bug under cursor' }, + }, + }, + { 'https://github.com/dhruvasagar/vim-table-mode', + version = '*', + init = function() + vim.g.table_mode_map_prefix = 'T' + require('which-key').add({{ 'T', group = 'Table mode' }}) + end, + }, + { 'https://github.com/stevearc/dressing.nvim', + opts = { + input = { + relative = 'win', + min_width = { 60, 0.4 }, + }, + }, + }, + { 'https://github.com/rcarriga/nvim-notify', + version = '*', + opts = { + on_open = function(win) + vim.api.nvim_win_set_option(win, 'wrap', true) -- does not work + end, + stages = 'static', + }, + init = function() + vim.notify = require('notify') + end, + }, + { 'https://github.com/gaoDean/autolist.nvim', + version = '*', + }, + { 'https://github.com/LittleMorph/copyright-updater.nvim', + event = 'BufModifiedSet', + }, +}