From a7669555a8bce9a6a980ea6e5cae7088cdfab5d0 Mon Sep 17 00:00:00 2001 From: TheK4n Date: Sun, 15 Jan 2023 19:07:48 +0300 Subject: [PATCH] nvim ref to lua --- .gitignore | 2 + Makefile | 14 +-- sub/nvim/after/plugin/auto-save.lua | 40 +++++++ sub/nvim/after/plugin/cmp.lua | 51 +++++++++ sub/nvim/after/plugin/colorscheme.lua | 3 + sub/nvim/after/plugin/gitsigns.lua | 5 + sub/nvim/after/plugin/lspconfig.lua | 50 +++++++++ sub/nvim/after/plugin/lualine.lua | 5 + sub/nvim/after/plugin/luasnip.lua | 4 + sub/nvim/after/plugin/nvim-autopairs.lua | 5 + sub/nvim/after/plugin/nvim-treesitter.lua | 8 ++ sub/nvim/after/plugin/nvim-ts-autotag.lua | 6 ++ sub/nvim/after/plugin/rainbow.lua | 10 ++ sub/nvim/after/plugin/telescope.lua | 5 + sub/nvim/init.lua | 4 + sub/nvim/lua/base/base.lua | 85 +++++++++++++++ sub/nvim/lua/base/maps.lua | 125 ++++++++++++++++++++++ sub/nvim/lua/base/plugins.lua | 45 ++++++++ sub/nvim/lua/base/sourcer.lua | 5 + 19 files changed, 463 insertions(+), 9 deletions(-) create mode 100644 sub/nvim/after/plugin/auto-save.lua create mode 100644 sub/nvim/after/plugin/cmp.lua create mode 100644 sub/nvim/after/plugin/colorscheme.lua create mode 100644 sub/nvim/after/plugin/gitsigns.lua create mode 100644 sub/nvim/after/plugin/lspconfig.lua create mode 100644 sub/nvim/after/plugin/lualine.lua create mode 100644 sub/nvim/after/plugin/luasnip.lua create mode 100644 sub/nvim/after/plugin/nvim-autopairs.lua create mode 100644 sub/nvim/after/plugin/nvim-treesitter.lua create mode 100644 sub/nvim/after/plugin/nvim-ts-autotag.lua create mode 100644 sub/nvim/after/plugin/rainbow.lua create mode 100644 sub/nvim/after/plugin/telescope.lua create mode 100644 sub/nvim/init.lua create mode 100644 sub/nvim/lua/base/base.lua create mode 100644 sub/nvim/lua/base/maps.lua create mode 100644 sub/nvim/lua/base/plugins.lua create mode 100644 sub/nvim/lua/base/sourcer.lua diff --git a/.gitignore b/.gitignore index 852274b..fac7a65 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ sub/bash/bashrc.d/* sub/zsh/zshrc.d/* !sub/zsh/zshrc.d/00_test.sh sub/vim/tmp/ + +sub/nvim/plugin diff --git a/Makefile b/Makefile index 779e0a9..c14bbbf 100644 --- a/Makefile +++ b/Makefile @@ -36,18 +36,14 @@ alacritty: mkdir -p ~/.config/alacritty ln -s $(PWD)/sub/alacritty/alacritty.yml ~/.config/alacritty/ -vim: +nvim: @echo "sudo pacman -S npm ctags fzf glow; mkdir ~/.npm-global; npm config set prefix '~/.npm-global'; npm install -g pyright" echo "set editing-mode vi" >> ~/.inputrc - test -d ~/.vim || \ - ln -s $(PWD)/sub/vim ~/.vim - ln -s $(PWD)/light/.vimrc ~/.vimrc - mkdir -p ~/.config/nvim/lua - ln -s ~/.vim/vimrc ~/.config/nvim/init.vim + test -d ~/.config/nvim || \ + ln -s $(PWD)/sub/nvim ~/.config/nvim ln -s $(PWD)/functions/vim_askpass_helper ~/.local/bin - ln -s $(PWD)/sub/vim/init.lua ~/.config/nvim/lua/init.lua - git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim - nvim +PluginInstall +qall + git clone --depth 1 https://github.com/wbthomason/packer.nvim ~/.local/share/nvim/site/pack/packer/start/packer.nvim + nvim +PackerCompile +PackerClean +PackerInstall +PackerUpdate +qall ssh: cat $(PWD)/sub/ssh/config >> ~/.ssh/config diff --git a/sub/nvim/after/plugin/auto-save.lua b/sub/nvim/after/plugin/auto-save.lua new file mode 100644 index 0000000..4c7c36f --- /dev/null +++ b/sub/nvim/after/plugin/auto-save.lua @@ -0,0 +1,40 @@ + +local status, autosave = pcall(require, "auto-save") +if (not status) then return end + +autosave.setup( +{ + enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) + execution_message = { + message = function() -- message to print on save + return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S")) + end, + dim = 0.18, -- dim the color of `message` + cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea + }, + trigger_events = {"InsertLeave", "TextChanged"}, -- vim events that trigger auto-save. See :h events + -- function that determines whether to save the current buffer or not + -- return true: if buffer is ok to be saved + -- return false: if it's not ok to be saved + condition = function(buf) + local fn = vim.fn + local utils = require("auto-save.utils.data") + + if + fn.getbufvar(buf, "&modifiable") == 1 and + utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then + return true -- met condition(s), can save + end + return false -- can't save + end, + write_all_buffers = false, -- write all buffers when the current one meets `condition` + debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds + callbacks = { -- functions to be executed at different intervals + enabling = nil, -- ran when enabling auto-save + disabling = nil, -- ran when disabling auto-save + before_asserting_save = nil, -- ran before checking `condition` + before_saving = nil, -- ran before doing the actual save + after_saving = nil -- ran after doing the actual save + } +} +) diff --git a/sub/nvim/after/plugin/cmp.lua b/sub/nvim/after/plugin/cmp.lua new file mode 100644 index 0000000..b09b7b6 --- /dev/null +++ b/sub/nvim/after/plugin/cmp.lua @@ -0,0 +1,51 @@ + +local status, cmp = pcall(require, "cmp") +if (not status) then return end + + +-- nvim-cmp setup +local cmp = require 'cmp' +cmp.setup { + completion = { + autocomplete = false + }, + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = function(fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n') + elseif luasnip.expand_or_jumpable() then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-expand-or-jump', true, true, true), '') + else + fallback() + end + end, + [''] = function(fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n') + elseif luasnip.jumpable(-1) then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-jump-prev', true, true, true), '') + else + fallback() + end + end, + }, + sources = { + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, +} diff --git a/sub/nvim/after/plugin/colorscheme.lua b/sub/nvim/after/plugin/colorscheme.lua new file mode 100644 index 0000000..10fccee --- /dev/null +++ b/sub/nvim/after/plugin/colorscheme.lua @@ -0,0 +1,3 @@ + + +vim.api.nvim_exec('colorscheme gruvbox', true) diff --git a/sub/nvim/after/plugin/gitsigns.lua b/sub/nvim/after/plugin/gitsigns.lua new file mode 100644 index 0000000..05777eb --- /dev/null +++ b/sub/nvim/after/plugin/gitsigns.lua @@ -0,0 +1,5 @@ + +local status, gitsigns = pcall(require, "gitsigns") +if (not status) then return end + +gitsigns.setup() diff --git a/sub/nvim/after/plugin/lspconfig.lua b/sub/nvim/after/plugin/lspconfig.lua new file mode 100644 index 0000000..baa7c33 --- /dev/null +++ b/sub/nvim/after/plugin/lspconfig.lua @@ -0,0 +1,50 @@ + +local status, nvim_lsp = pcall(require, "lspconfig") +if (not status) then return end + + +-- Use an on_attach function to only map the following keys +-- after the language server attaches to the current buffer +local on_attach = function(client, bufnr) + + local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end + local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end + + -- Enable completion triggered by + buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') + + -- Mappings. + local opts = { noremap=true, silent=true } + + -- See `:help vim.lsp.*` for documentation on any of the below functions + buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) + buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) + buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + buf_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) + buf_set_keymap('n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) + buf_set_keymap('n', 'e', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) + buf_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev()', opts) + buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) + buf_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) + buf_set_keymap('n', 'f', 'lua vim.lsp.buf.formatting()', opts) + +end + +-- Use a loop to conveniently call 'setup' on multiple servers and +-- map buffer local keybindings when the language server attaches +local servers = { 'pyright' } +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, + flags = { + debounce_text_changes = 150, + } + } +end diff --git a/sub/nvim/after/plugin/lualine.lua b/sub/nvim/after/plugin/lualine.lua new file mode 100644 index 0000000..f3ce4d6 --- /dev/null +++ b/sub/nvim/after/plugin/lualine.lua @@ -0,0 +1,5 @@ + +local status, lualine = pcall(require, "lualine") +if (not status) then return end + +lualine.setup() diff --git a/sub/nvim/after/plugin/luasnip.lua b/sub/nvim/after/plugin/luasnip.lua new file mode 100644 index 0000000..52761f1 --- /dev/null +++ b/sub/nvim/after/plugin/luasnip.lua @@ -0,0 +1,4 @@ + +local status, luasnip = pcall(require, "luasnip") +if (not status) then return end + diff --git a/sub/nvim/after/plugin/nvim-autopairs.lua b/sub/nvim/after/plugin/nvim-autopairs.lua new file mode 100644 index 0000000..d3c7655 --- /dev/null +++ b/sub/nvim/after/plugin/nvim-autopairs.lua @@ -0,0 +1,5 @@ + +local status, nvim_autopairs = pcall(require, "nvim_autopairs") +if (not status) then return end + +nvim_autopairs.setup() diff --git a/sub/nvim/after/plugin/nvim-treesitter.lua b/sub/nvim/after/plugin/nvim-treesitter.lua new file mode 100644 index 0000000..66e42f6 --- /dev/null +++ b/sub/nvim/after/plugin/nvim-treesitter.lua @@ -0,0 +1,8 @@ + +local status, nvim_treesitter = pcall(require, "nvim_treesitter") +if (not status) then return end + +nvim_treesitter.setup { + auto_install = true, +} + diff --git a/sub/nvim/after/plugin/nvim-ts-autotag.lua b/sub/nvim/after/plugin/nvim-ts-autotag.lua new file mode 100644 index 0000000..f9cb28d --- /dev/null +++ b/sub/nvim/after/plugin/nvim-ts-autotag.lua @@ -0,0 +1,6 @@ + +local status, nvim_ts_autotag = pcall(require, "nvim_ts_autotag") +if (not status) then return end + + +nvim_ts_autotag.setup() diff --git a/sub/nvim/after/plugin/rainbow.lua b/sub/nvim/after/plugin/rainbow.lua new file mode 100644 index 0000000..e7d2027 --- /dev/null +++ b/sub/nvim/after/plugin/rainbow.lua @@ -0,0 +1,10 @@ + +vim.api.nvim_exec([[ + let g:rainbow_active = 1 +]], true) + +vim.api.nvim_create_autocmd('FileType', + { + pattern = {"*"}, + command = 'RainbowToggle' + }) diff --git a/sub/nvim/after/plugin/telescope.lua b/sub/nvim/after/plugin/telescope.lua new file mode 100644 index 0000000..a5cf702 --- /dev/null +++ b/sub/nvim/after/plugin/telescope.lua @@ -0,0 +1,5 @@ + +local status, telescope = pcall(require, "telescope") +if (not status) then return end + +telescope.load_extension('fzf') diff --git a/sub/nvim/init.lua b/sub/nvim/init.lua new file mode 100644 index 0000000..560a6b6 --- /dev/null +++ b/sub/nvim/init.lua @@ -0,0 +1,4 @@ + +require("base.sourcer") + + diff --git a/sub/nvim/lua/base/base.lua b/sub/nvim/lua/base/base.lua new file mode 100644 index 0000000..e4a9d75 --- /dev/null +++ b/sub/nvim/lua/base/base.lua @@ -0,0 +1,85 @@ + +vim.opt.ruler = true + + +vim.scriptencoding = 'utf-8' +vim.opt.encoding = 'utf-8' +vim.opt.fileencoding = 'utf-8' + +vim.opt.clipboard = 'unnamedplus' +vim.opt.shell = 'bash' + +vim.opt.ttimeoutlen = 0 + +-- vim.opt.compatible = false + +vim.opt.expandtab = true +vim.opt.smarttab = true +vim.opt.tabstop = 4 + + +vim.opt.softtabstop = 4 +vim.opt.shiftwidth = 4 +vim.opt.autoindent = true +vim.opt.smartindent = true +vim.opt.wrap = false + +vim.opt.ttyfast = true +vim.opt.autoread = true + + +vim.opt.errorbells = false +vim.opt.visualbell = false +vim.opt.showcmd = true +vim.opt.showtabline = 2 + +vim.opt.smartcase = true +vim.opt.incsearch = true + +vim.opt.mousehide = true +vim.opt.mouse = 'a' + +vim.opt.colorcolumn = '81' +vim.opt.scrolloff = 7 + +vim.opt.backup = true +vim.opt.swapfile = false +vim.opt.undofile = true +vim.opt.history = 1000 +vim.opt.undoreload = 1000 +vim.opt.backupdir = '~/.vim/tmp/backup/' +vim.opt.undodir = '~/.vim/tmp/undo/' +vim.opt.directory = '~/.vim/tmp/swap/' + +vim.api.nvim_exec([[ +function! MakeDirIfNoExists(path) + if !isdirectory(expand(a:path)) + call mkdir(expand(a:path), "p") + endif +endfunction + + +" make this dirs if no exists previously +silent! call MakeDirIfNoExists(&undodir) +silent! call MakeDirIfNoExists(&backupdir) +silent! call MakeDirIfNoExists(&directory) +]], true) + +vim.opt.ffs = 'unix,mac' + +vim.opt.path:append { '**' } -- Finding files - Search down into subfolders + +vim.opt.cursorline = true + + + +vim.cmd([[let &t_SI.="\e[5 q"]]) +vim.cmd([[let &t_SR.="\e[3 q"]]) +vim.cmd([[let &t_EI.="\e[1 q"]]) + + +vim.cmd([[let g:netrw_banner = 0]]) -- hide banner +vim.cmd([[let g:netrw_liststyle = 3]]) -- tree instead of plain view +vim.cmd([[let g:netrw_browse_split = 0]]) +vim.cmd([[let g:netrw_winsize = 15]]) +vim.cmd([[let g:netrw_keepdir = 0]]) diff --git a/sub/nvim/lua/base/maps.lua b/sub/nvim/lua/base/maps.lua new file mode 100644 index 0000000..c0c3feb --- /dev/null +++ b/sub/nvim/lua/base/maps.lua @@ -0,0 +1,125 @@ +local keymap = vim.keymap + + +vim.g.mapleader = ',' + + +vim.api.nvim_exec([[ + " like tabdo but restore the current tab + function! TabDo(command) + let currTab=tabpagenr() + execute 'tabdo ' . a:command + execute 'tabn ' . currTab + endfunction + + " like bufdo but restore the current buffer + function! BufDo(command) + let currBuff=bufnr("%") + execute 'bufdo ' . a:command + execute 'buffer ' . currBuff + endfunction + + " like windo but restore the current window + function! WinDo(command) + let currwin=winnr() + execute 'windo ' . a:command + execute currwin . 'wincmd w' + endfunction +]], true) + +keymap.set('n', 'c', [[:call TabDo('set cursorline!')]], {silent = true}) + +keymap.set('n', '/', [[:set invhlsearch]], {silent = true}) + +keymap.set('i', 'jk', '') +keymap.set('i', 'ол', '') + + + +-- x to blackhole +keymap.set('n', 'x', '"_x') + +-- Increment/decrement +keymap.set('n', '+', '') +keymap.set('n', '-', '') + +-- Select all +keymap.set('n', '', 'ggG') + + +-- Scroll tabs +keymap.set("n", '', ':tabnext') +keymap.set("n", '', ':tabprev') + + +-- Kill current buffer +keymap.set("n", 'qq', ':bd!') +-- Quick exit without saving +keymap.set("n", 'qa', ':qa!') + + +keymap.set("n", 'eh', ':set list!') +vim.opt.listchars=[[tab:→\ ,eol:↵,trail:·,extends:↷,precedes:↶]] + + +-- Tags panes (ctags required) +keymap.set("n", 't', ':TagbarToggle') + + +-- Telescope +keymap.set("n", 'ff', 'Telescope find_files') +keymap.set("n", 'fg', 'Telescope live_grep') + + +-- Expand %% to dirname of current file in command line +keymap.set("c", '%%', [[getcmdtype() == ':' ? expand('%:h').'/' : '%%']], {expr = true}) + + +-- Save from root +vim.api.nvim_create_user_command('Sw', [[execute 'silent! write !SUDO_ASKPASS=$(which vim_askpass_helper) sudo -A tee % >/dev/null']], {}) + + + +function create_autocmd_filetype(func) + create_autocmd = vim.api.nvim_create_autocmd + + create_autocmd("BufEnter", + { pattern = '*', callback = func} + ) +end + +function set_keymap_run_script() + if vim.bo.filetype == 'python' then + cmd_string = string.format([[:tabnew %% :terminal %s %% :set nocursorline number norelativenumber G ]], 'python3') + keymap.set("n", "rr", cmd_string) + end +end + +create_autocmd_filetype(set_keymap_run_script) + + + + +-- Toggle line number style +vim.opt.number = true +vim.opt.relativenumber = true + +vim.api.nvim_exec([[ +function! ToggleRelativeAbsoluteNumber() + if !&number && !&relativenumber + set number + set norelativenumber + elseif &number && !&relativenumber + set nonumber + set relativenumber + elseif !&number && &relativenumber + set number + set relativenumber + elseif &number && &relativenumber + set nonumber + set norelativenumber + endif +endfunction +]], true) + +keymap.set('n', 'l', [[:call TabDo('call ToggleRelativeAbsoluteNumber()') ]], {silent = true}) diff --git a/sub/nvim/lua/base/plugins.lua b/sub/nvim/lua/base/plugins.lua new file mode 100644 index 0000000..4948373 --- /dev/null +++ b/sub/nvim/lua/base/plugins.lua @@ -0,0 +1,45 @@ +return require('packer').startup(function(use) + -- Packer can manage itself + use 'wbthomason/packer.nvim' + + use 'VundleVim/Vundle.vim' + + use { + 'nvim-lualine/lualine.nvim', + requires = { 'kyazdani42/nvim-web-devicons', opt = true } + } + + use 'morhetz/gruvbox' + use 'tpope/vim-surround' + use 'tpope/vim-commentary' + use 'ap/vim-css-color' + use 'preservim/tagbar' + use 'preservim/vimux' + use 'rbgrouleff/bclose.vim' + use 'frazrepo/vim-rainbow' + + -- requiered patch your font + use 'ryanoasis/vim-devicons' + use 'powerman/vim-plugin-ruscmd' -- Russian navigation + + use 'Pocco81/auto-save.nvim' + use 'windwp/nvim-autopairs' + + -- pyright + use 'neovim/nvim-lspconfig' + use 'hrsh7th/nvim-cmp' + use 'hrsh7th/cmp-nvim-lsp' + use 'saadparwaiz1/cmp_luasnip' + use 'L3MON4D3/LuaSnip' + use 'nvim-lua/plenary.nvim' + + use { + 'nvim-telescope/telescope.nvim', + requires = { 'nvim-telescope/telescope-fzf-native.nvim', run = 'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build', opt = false } + } + use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate' } + + use 'lewis6991/gitsigns.nvim' + use 'windwp/nvim-ts-autotag' + +end) diff --git a/sub/nvim/lua/base/sourcer.lua b/sub/nvim/lua/base/sourcer.lua new file mode 100644 index 0000000..bf1b36b --- /dev/null +++ b/sub/nvim/lua/base/sourcer.lua @@ -0,0 +1,5 @@ + +require("base.base") +require("base.maps") +require("base.plugins") +