From e5fb9a21e3d1c7161ba9afa092e908a03494d7ab Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Thu, 3 Oct 2024 01:32:19 +0300 Subject: [PATCH] feat(nvim): another configuration tweak round added snippets! --- config/nvim/lua/plugins/comment.lua | 9 ++ config/nvim/lua/plugins/conform.lua | 84 ++++++++++++++++ config/nvim/lua/plugins/flash.lua | 39 ++++++++ config/nvim/lua/plugins/harpoon.lua | 47 +++++++++ config/nvim/lua/plugins/lsp-saga.lua | 21 ++++ config/nvim/lua/plugins/neogen.lua | 8 ++ config/nvim/lua/plugins/refactoring.lua | 8 ++ config/nvim/lua/plugins/surround.lua | 8 ++ config/nvim/lua/plugins/todo-comments.lua | 8 ++ config/nvim/lua/plugins/treesj.lua | 9 ++ config/nvim/lua/plugins/ufo.lua | 116 ++++++++++++++++++++++ config/nvim/lua/plugins/vim-sleuth.lua | 3 + 12 files changed, 360 insertions(+) create mode 100644 config/nvim/lua/plugins/comment.lua create mode 100644 config/nvim/lua/plugins/conform.lua create mode 100644 config/nvim/lua/plugins/flash.lua create mode 100644 config/nvim/lua/plugins/harpoon.lua create mode 100644 config/nvim/lua/plugins/lsp-saga.lua create mode 100644 config/nvim/lua/plugins/neogen.lua create mode 100644 config/nvim/lua/plugins/refactoring.lua create mode 100644 config/nvim/lua/plugins/surround.lua create mode 100644 config/nvim/lua/plugins/todo-comments.lua create mode 100644 config/nvim/lua/plugins/treesj.lua create mode 100644 config/nvim/lua/plugins/ufo.lua create mode 100644 config/nvim/lua/plugins/vim-sleuth.lua diff --git a/config/nvim/lua/plugins/comment.lua b/config/nvim/lua/plugins/comment.lua new file mode 100644 index 0000000..5f37fd1 --- /dev/null +++ b/config/nvim/lua/plugins/comment.lua @@ -0,0 +1,9 @@ +-- Commenting +-- "gc" to comment visual regions/lines +-- https://github.com/numToStr/Comment.nvim +return { + 'numToStr/Comment.nvim', + version = '*', + event = { 'BufRead', 'BufNewFile' }, + opts = {}, +} diff --git a/config/nvim/lua/plugins/conform.lua b/config/nvim/lua/plugins/conform.lua new file mode 100644 index 0000000..e25edaa --- /dev/null +++ b/config/nvim/lua/plugins/conform.lua @@ -0,0 +1,84 @@ +-- ── Formatting ────────────────────────────────────────────────────── +-- Lightweight yet powerful formatter plugin for Neovim +-- https://github.com/stevearc/conform.nvim +return { + 'stevearc/conform.nvim', + event = { 'BufWritePre' }, + cmd = { 'ConformInfo' }, + config = function() + -- Select first conform formatter that is available + ---@param bufnr integer + ---@param ... string + ---@return string + local function first(bufnr, ...) + local conform = require 'conform' + for i = 1, select('#', ...) do + local formatter = select(i, ...) + if conform.get_formatter_info(formatter, bufnr).available then + return formatter + end + end + return select(1, ...) + end + + require('conform').setup { + -- Enable or disable logging + notify_on_error = true, + -- Set the default formatter for all filetypes + default_formatter = 'injected', + -- Set the default formatter for all filetypes + default_formatter_opts = { + lsp_format = 'fallback', + -- Set the default formatter for all filetypes + -- formatter = 'injected', + -- Set the default formatter for all filetypes + -- formatter_opts = {}, + }, + formatters_by_ft = { + markdown = function(bufnr) + return { first(bufnr, 'prettierd', 'prettier'), 'injected' } + end, + javascript = function(bufnr) + return { first(bufnr, 'prettier', 'eslint'), 'injected' } + end, + lua = { 'stylua' }, + -- Conform will run multiple formatters sequentially + -- python = { 'isort', 'black', lsp_format = 'fallback' }, + -- You can customize some of the format options for the filetype (:help conform.format) + -- rust = { 'rustfmt', lsp_format = 'fallback' }, + }, + format_on_save = function(bufnr) + -- Disable autoformat on certain filetypes + local ignore_filetypes = { + 'c', + 'cpp', + 'sql', + 'java', + } + if vim.tbl_contains(ignore_filetypes, vim.bo[bufnr].filetype) then + return + end + -- Disable with a global or buffer-local variable + if + vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat + then + return + end + -- Disable autoformat for files in a certain path + local bufname = vim.api.nvim_buf_get_name(bufnr) + if bufname:match '/node_modules/' then return end + if bufname:match '/vendor/' then return end + if bufname:match '/dist/' then return end + if bufname:match '/build/' then return end + + return { timeout_ms = 500, lsp_format = 'fallback' } + end, + } + end, + init = function() + -- If you want the formatexpr, here is the place to set it + vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + end, + + +} diff --git a/config/nvim/lua/plugins/flash.lua b/config/nvim/lua/plugins/flash.lua new file mode 100644 index 0000000..9c0bc45 --- /dev/null +++ b/config/nvim/lua/plugins/flash.lua @@ -0,0 +1,39 @@ +return { + 'folke/flash.nvim', + event = 'VeryLazy', + opts = {}, + keys = { + { + 'zk', + mode = { 'n', 'x', 'o' }, + function() require('flash').jump() end, + desc = 'Flash', + }, + { + 'Zk', + mode = { 'n', 'x', 'o' }, + function() require('flash').treesitter() end, + desc = 'Flash Treesitter', + }, + { + 'r', + mode = 'o', + function() require('flash').remote() end, + desc = 'Remote Flash', + }, + { + 'R', + mode = { 'o', 'x' }, + function() require('flash').treesitter_search() end, + desc = 'Treesitter Search', + }, + { + '', + mode = { 'c' }, + function() require('flash').toggle() end, + desc = 'Toggle Flash Search', + }, + }, +} + +-- vim: ts=2 sts=2 sw=2 et diff --git a/config/nvim/lua/plugins/harpoon.lua b/config/nvim/lua/plugins/harpoon.lua new file mode 100644 index 0000000..206cf6d --- /dev/null +++ b/config/nvim/lua/plugins/harpoon.lua @@ -0,0 +1,47 @@ +-- Getting you where you want with the fewest keystrokes. +-- https://github.com/ThePrimeagen/harpoon +return { + 'ThePrimeagen/harpoon', + branch = 'harpoon2', + dependencies = { + 'nvim-lua/plenary.nvim', + 'nvim-telescope/telescope.nvim', + }, + config = function() + local harpoon = require 'harpoon' + harpoon:setup {} + + -- basic telescope configuration + local conf = require('telescope.config').values + local function toggle_telescope(harpoon_files) + local file_paths = {} + for _, item in ipairs(harpoon_files.items) do + table.insert(file_paths, item.value) + end + + require('telescope.pickers') + .new({}, { + prompt_title = 'Harpoon', + finder = require('telescope.finders').new_table { + results = file_paths, + }, + previewer = conf.file_previewer {}, + sorter = conf.generic_sorter {}, + }) + :find() + end + + vim.keymap.set( + 'n', + 'hw', + function() toggle_telescope(harpoon:list()) end, + { desc = 'Open harpoon window with telescope' } + ) + vim.keymap.set( + 'n', + 'ht', + function() harpoon.ui:toggle_quick_menu(harpoon:list()) end, + { desc = 'Open Harpoon Quick menu' } + ) + end, +} diff --git a/config/nvim/lua/plugins/lsp-saga.lua b/config/nvim/lua/plugins/lsp-saga.lua new file mode 100644 index 0000000..aec604f --- /dev/null +++ b/config/nvim/lua/plugins/lsp-saga.lua @@ -0,0 +1,21 @@ +-- improve neovim lsp experience +-- https://github.com/nvimdev/lspsaga.nvim +-- https://nvimdev.github.io/lspsaga/ +return { + 'nvimdev/lspsaga.nvim', + event = 'LspAttach', + dependencies = { + 'nvim-treesitter/nvim-treesitter', -- optional + 'nvim-tree/nvim-web-devicons', -- optional + }, + opts = { + code_action = { + show_server_name = true, + }, + diagnostic = { + keys = { + quit = { 'q', '' }, + }, + }, + }, +} diff --git a/config/nvim/lua/plugins/neogen.lua b/config/nvim/lua/plugins/neogen.lua new file mode 100644 index 0000000..46d3b75 --- /dev/null +++ b/config/nvim/lua/plugins/neogen.lua @@ -0,0 +1,8 @@ +-- A better annotation generator. +-- Supports multiple languages and annotation conventions. +-- https://github.com/danymat/neogen +return { + 'danymat/neogen', + version = '*', + opts = { enabled = true, snippet_engine = 'luasnip' }, +} diff --git a/config/nvim/lua/plugins/refactoring.lua b/config/nvim/lua/plugins/refactoring.lua new file mode 100644 index 0000000..cde56ed --- /dev/null +++ b/config/nvim/lua/plugins/refactoring.lua @@ -0,0 +1,8 @@ +-- The Refactoring library based off the Refactoring book by Martin Fowler +-- https://github.com/ThePrimeagen/refactoring.nvim +return { + 'ThePrimeagen/refactoring.nvim', + version = '*', + dependencies = { 'nvim-lua/plenary.nvim', 'nvim-treesitter/nvim-treesitter' }, + opts = {}, +} diff --git a/config/nvim/lua/plugins/surround.lua b/config/nvim/lua/plugins/surround.lua new file mode 100644 index 0000000..701788e --- /dev/null +++ b/config/nvim/lua/plugins/surround.lua @@ -0,0 +1,8 @@ +-- Add/change/delete surrounding delimiter pairs with ease. +-- https://github.com/kylechui/nvim-surround +return { + 'kylechui/nvim-surround', + version = '*', + event = 'VeryLazy', + opts = {}, +} diff --git a/config/nvim/lua/plugins/todo-comments.lua b/config/nvim/lua/plugins/todo-comments.lua new file mode 100644 index 0000000..7d9efb0 --- /dev/null +++ b/config/nvim/lua/plugins/todo-comments.lua @@ -0,0 +1,8 @@ +-- Highlight, list and search todo comments in your projects +-- https://github.com/folke/todo-comments.nvim +return { + 'folke/todo-comments.nvim', + version = '*', + dependencies = { 'nvim-lua/plenary.nvim' }, + opts = {}, +} diff --git a/config/nvim/lua/plugins/treesj.lua b/config/nvim/lua/plugins/treesj.lua new file mode 100644 index 0000000..0a7b802 --- /dev/null +++ b/config/nvim/lua/plugins/treesj.lua @@ -0,0 +1,9 @@ +-- Neovim plugin for splitting/joining blocks of code +-- https://github.com/Wansmer/treesj +return { + 'Wansmer/treesj', + dependencies = { 'nvim-treesitter/nvim-treesitter' }, + opts = { + use_default_keymaps = false, + }, +} diff --git a/config/nvim/lua/plugins/ufo.lua b/config/nvim/lua/plugins/ufo.lua new file mode 100644 index 0000000..a6480e7 --- /dev/null +++ b/config/nvim/lua/plugins/ufo.lua @@ -0,0 +1,116 @@ +-- Not UFO in the sky, but an ultra fold in Neovim. +-- https://github.com/kevinhwang91/nvim-ufo/ +return { + 'kevinhwang91/nvim-ufo', + version = '*', + dependencies = { + { 'neovim/nvim-lspconfig' }, + { 'kevinhwang91/promise-async' }, + { 'nvim-treesitter/nvim-treesitter' }, + { + -- Status column plugin that provides a configurable + -- 'statuscolumn' and click handlers. + -- https://github.com/luukvbaal/statuscol.nvim + 'luukvbaal/statuscol.nvim', + config = function() + local builtin = require 'statuscol.builtin' + require('statuscol').setup { + relculright = true, + segments = { + { + text = { builtin.foldfunc }, + click = 'v:lua.ScFa', + }, + { + sign = { + namespace = { 'diagnostic/signs' }, + maxwidth = 2, + -- auto = true, + }, + click = 'v:lua.ScSa', + }, + { + text = { builtin.lnumfunc, ' ' }, + click = 'v:lua.ScLa', + }, + }, + } + end, + }, + }, + config = function() + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities.textDocument.foldingRange = { + dynamicRegistration = false, + lineFoldingOnly = true, + } + local language_servers = require('lspconfig').util.available_servers() -- or list servers manually like {'gopls', 'clangd'} + for _, ls in ipairs(language_servers) do + require('lspconfig')[ls].setup { + capabilities = capabilities, + -- you can add other fields for setting up lsp server in this table + } + end + + require('ufo').setup { + open_fold_hl_timeout = 150, + close_fold_kinds_for_ft = { 'imports', 'comment' }, + preview = { + win_config = { + border = { '', '─', '', '', '', '─', '', '' }, + winhighlight = 'Normal:Folded', + winblend = 0, + }, + mappings = { + scrollU = '', + scrollD = '', + jumpTop = '[', + jumpBot = ']', + }, + }, + + provider_selector = function(_, _, _) -- bufnr, filetype, buftype + return { 'treesitter', 'indent' } + end, + + -- fold_virt_text_handler + -- + -- This handler is called when the fold text is too long to fit in the window. + -- It is expected to truncate the text and return a new list of virtual text. + -- + ---@param virtText table The current virtual text list. + ---@param lnum number The line number of the first line in the fold. + ---@param endLnum number The line number of the last line in the fold. + ---@param width number The width of the window. + ---@param truncate function Truncate function + ---@return table + fold_virt_text_handler = function(virtText, lnum, endLnum, width, truncate) + local newVirtText = {} + local suffix = (' 󰁂 %d '):format(endLnum - lnum) + local sufWidth = vim.fn.strdisplaywidth(suffix) + local targetWidth = width - sufWidth + local curWidth = 0 + for _, chunk in ipairs(virtText) do + local chunkText = chunk[1] + local chunkWidth = vim.fn.strdisplaywidth(chunkText) + if targetWidth > curWidth + chunkWidth then + table.insert(newVirtText, chunk) + else + chunkText = truncate(chunkText, targetWidth - curWidth) + local hlGroup = chunk[2] + table.insert(newVirtText, { chunkText, hlGroup }) + chunkWidth = vim.fn.strdisplaywidth(chunkText) + -- str width returned from truncate() may less than 2nd argument, need padding + if curWidth + chunkWidth < targetWidth then + suffix = suffix .. (' '):rep(targetWidth - curWidth - chunkWidth) + end + break + end + curWidth = curWidth + chunkWidth + end + table.insert(newVirtText, { suffix, 'MoreMsg' }) + return newVirtText + end, + } + end, +} diff --git a/config/nvim/lua/plugins/vim-sleuth.lua b/config/nvim/lua/plugins/vim-sleuth.lua new file mode 100644 index 0000000..a6ff93b --- /dev/null +++ b/config/nvim/lua/plugins/vim-sleuth.lua @@ -0,0 +1,3 @@ +-- Detect tabstop and shiftwidth automatically +-- https://github.com/tpope/vim-sleuth +return { 'tpope/vim-sleuth' }