init.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. set mouse=a " enable mouse
  2. set encoding=utf-8
  3. set number
  4. set noswapfile
  5. set scrolloff=7
  6. set tabstop=4
  7. set softtabstop=4
  8. set shiftwidth=4
  9. set expandtab
  10. set autoindent
  11. set fileformat=unix
  12. filetype indent on " load filetype-specific indent files
  13. " for tabulation
  14. set smartindent
  15. set tabstop=2
  16. set expandtab
  17. set shiftwidth=2
  18. inoremap jk <esc>
  19. call plug#begin('~/.vim/plugged')
  20. Plug 'neovim/nvim-lspconfig'
  21. Plug 'hrsh7th/nvim-cmp'
  22. Plug 'hrsh7th/cmp-nvim-lsp'
  23. Plug 'saadparwaiz1/cmp_luasnip'
  24. Plug 'L3MON4D3/LuaSnip'
  25. " color schemas
  26. Plug 'morhetz/gruvbox' " colorscheme gruvbox
  27. Plug 'mhartington/oceanic-next' " colorscheme OceanicNext
  28. Plug 'kaicataldo/material.vim', { 'branch': 'main' }
  29. Plug 'ayu-theme/ayu-vim'
  30. Plug 'xiyaowong/nvim-transparent'
  31. Plug 'Pocco81/auto-save.nvim'
  32. Plug 'justinmk/vim-sneak'
  33. " JS/JSX/TS
  34. Plug 'pangloss/vim-javascript'
  35. Plug 'leafgarland/typescript-vim'
  36. Plug 'peitalin/vim-jsx-typescript'
  37. Plug 'maxmellon/vim-jsx-pretty'
  38. " TS from here https://jose-elias-alvarez.medium.com/configuring-neovims-lsp-client-for-typescript-development-5789d58ea9c
  39. Plug 'jose-elias-alvarez/null-ls.nvim'
  40. Plug 'jose-elias-alvarez/nvim-lsp-ts-utils'
  41. Plug 'nvim-lua/plenary.nvim'
  42. Plug 'prettier/vim-prettier', {
  43. \ 'do': 'npm install --frozen-lockfile --production',
  44. \ 'for': ['javascript', 'typescript', 'typescriptreact', 'javascriptreact', 'css', 'less', 'scss', 'json', 'graphql', 'markdown', 'vue', 'svelte', 'yaml', 'html'] }
  45. Plug 'bmatcuk/stylelint-lsp'
  46. Plug 'nvim-telescope/telescope.nvim', { 'tag': '0.1.0' }
  47. Plug 'nvim-telescope/telescope-fzf-native.nvim', { 'do': 'make' }
  48. " Convenient floating terminal window
  49. "Plug 'voldikss/vim-floaterm'
  50. call plug#end()
  51. " Leader bind to space
  52. let mapleader = ","
  53. " Netrw file explorer settings
  54. let g:netrw_banner = 0 " hide banner above files
  55. let g:netrw_liststyle = 3 " tree instead of plain view
  56. let g:netrw_browse_split = 3 " vertical split window when Enter pressed on file
  57. " Automatically format frontend files with prettier after file save
  58. let g:prettier#autoformat = 1
  59. let g:prettier#autoformat_require_pragma = 0
  60. " Disable quickfix window for prettier
  61. let g:prettier#quickfix_enabled = 0
  62. " Turn on vim-sneak
  63. let g:sneak#label = 1
  64. colorscheme gruvbox
  65. "colorscheme OceanicNext
  66. "let g:material_terminal_italics = 1
  67. " variants: default, palenight, ocean, lighter, darker, default-community,
  68. " palenight-community, ocean-community, lighter-community,
  69. " darker-community
  70. "let g:material_theme_style = 'darker'
  71. "colorscheme material
  72. if (has('termguicolors'))
  73. set termguicolors
  74. endif
  75. " variants: mirage, dark, dark
  76. "let ayucolor="mirage"
  77. "colorscheme ayu
  78. " turn off search highlight
  79. nnoremap ,<space> :nohlsearch<CR>
  80. lua << EOF
  81. -- Set completeopt to have a better completion experience
  82. vim.o.completeopt = 'menuone,noselect'
  83. -- luasnip setup
  84. local luasnip = require 'luasnip'
  85. local async = require "plenary.async"
  86. -- nvim-cmp setup
  87. local cmp = require 'cmp'
  88. cmp.setup {
  89. completion = {
  90. autocomplete = false
  91. },
  92. snippet = {
  93. expand = function(args)
  94. require('luasnip').lsp_expand(args.body)
  95. end,
  96. },
  97. mapping = {
  98. ['<C-p>'] = cmp.mapping.select_prev_item(),
  99. ['<C-n>'] = cmp.mapping.select_next_item(),
  100. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  101. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  102. ['<C-Space>'] = cmp.mapping.complete(),
  103. ['<C-e>'] = cmp.mapping.close(),
  104. ['<CR>'] = cmp.mapping.confirm {
  105. behavior = cmp.ConfirmBehavior.Replace,
  106. select = true,
  107. },
  108. ['<Tab>'] = function(fallback)
  109. if vim.fn.pumvisible() == 1 then
  110. vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n')
  111. elseif luasnip.expand_or_jumpable() then
  112. vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-expand-or-jump', true, true, true), '')
  113. else
  114. fallback()
  115. end
  116. end,
  117. ['<S-Tab>'] = function(fallback)
  118. if vim.fn.pumvisible() == 1 then
  119. vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-p>', true, true, true), 'n')
  120. elseif luasnip.jumpable(-1) then
  121. vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-jump-prev', true, true, true), '')
  122. else
  123. fallback()
  124. end
  125. end,
  126. },
  127. sources = {
  128. { name = 'nvim_lsp' },
  129. { name = 'luasnip' },
  130. },
  131. }
  132. local nvim_lsp = require('lspconfig')
  133. -- Use an on_attach function to only map the following keys
  134. -- after the language server attaches to the current buffer
  135. local on_attach = function(client, bufnr)
  136. local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  137. local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
  138. -- Enable completion triggered by <c-x><c-o>
  139. buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
  140. -- Mappings.
  141. local opts = { noremap=true, silent=true }
  142. -- See `:help vim.lsp.*` for documentation on any of the below functions
  143. buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  144. buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  145. buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
  146. buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  147. buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  148. buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  149. buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  150. buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  151. buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  152. buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  153. buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
  154. buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  155. buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
  156. buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
  157. buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
  158. buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
  159. buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
  160. end
  161. -- TS setup
  162. local buf_map = function(bufnr, mode, lhs, rhs, opts)
  163. vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts or {
  164. silent = true,
  165. })
  166. end
  167. nvim_lsp.tsserver.setup({
  168. on_attach = function(client, bufnr)
  169. client.resolved_capabilities.document_formatting = false
  170. client.resolved_capabilities.document_range_formatting = false
  171. local ts_utils = require("nvim-lsp-ts-utils")
  172. ts_utils.setup({})
  173. ts_utils.setup_client(client)
  174. buf_map(bufnr, "n", "gs", ":TSLspOrganize<CR>")
  175. buf_map(bufnr, "n", "gi", ":TSLspRenameFile<CR>")
  176. buf_map(bufnr, "n", "go", ":TSLspImportAll<CR>")
  177. on_attach(client, bufnr)
  178. end,
  179. })
  180. local null_ls = require("null-ls")
  181. null_ls.setup({
  182. sources = {
  183. null_ls.builtins.diagnostics.eslint,
  184. null_ls.builtins.code_actions.eslint,
  185. null_ls.builtins.formatting.prettier
  186. },
  187. on_attach = on_attach
  188. })
  189. -- Stylelint format after save
  190. require'lspconfig'.stylelint_lsp.setup{
  191. settings = {
  192. stylelintplus = {
  193. --autoFixOnSave = true,
  194. --autoFixOnFormat = true,
  195. }
  196. }
  197. }
  198. -- Use a loop to conveniently call 'setup' on multiple servers and
  199. -- map buffer local keybindings when the language server attaches
  200. local servers = { 'pyright', 'rust_analyzer' }
  201. for _, lsp in ipairs(servers) do
  202. nvim_lsp[lsp].setup {
  203. on_attach = on_attach,
  204. flags = {
  205. debounce_text_changes = 150,
  206. }
  207. }
  208. end
  209. EOF
  210. " Delete buffer while keeping window layout (don't close buffer's windows).
  211. " Version 2008-11-18 from http://vim.wikia.com/wiki/VimTip165
  212. if v:version < 700 || exists('loaded_bclose') || &cp
  213. finish
  214. endif
  215. let loaded_bclose = 1
  216. if !exists('bclose_multiple')
  217. let bclose_multiple = 1
  218. endif
  219. " Display an error message.
  220. function! s:Warn(msg)
  221. echohl ErrorMsg
  222. echomsg a:msg
  223. echohl NONE
  224. endfunction
  225. " Command ':Bclose' executes ':bd' to delete buffer in current window.
  226. " The window will show the alternate buffer (Ctrl-^) if it exists,
  227. " or the previous buffer (:bp), or a blank buffer if no previous.
  228. " Command ':Bclose!' is the same, but executes ':bd!' (discard changes).
  229. " An optional argument can specify which buffer to close (name or number).
  230. function! s:Bclose(bang, buffer)
  231. if empty(a:buffer)
  232. let btarget = bufnr('%')
  233. elseif a:buffer =~ '^\d\+$'
  234. let btarget = bufnr(str2nr(a:buffer))
  235. else
  236. let btarget = bufnr(a:buffer)
  237. endif
  238. if btarget < 0
  239. call s:Warn('No matching buffer for '.a:buffer)
  240. return
  241. endif
  242. if empty(a:bang) && getbufvar(btarget, '&modified')
  243. call s:Warn('No write since last change for buffer '.btarget.' (use :Bclose!)')
  244. return
  245. endif
  246. " Numbers of windows that view target buffer which we will delete.
  247. let wnums = filter(range(1, winnr('$')), 'winbufnr(v:val) == btarget')
  248. if !g:bclose_multiple && len(wnums) > 1
  249. call s:Warn('Buffer is in multiple windows (use ":let bclose_multiple=1")')
  250. return
  251. endif
  252. let wcurrent = winnr()
  253. for w in wnums
  254. execute w.'wincmd w'
  255. let prevbuf = bufnr('#')
  256. if prevbuf > 0 && buflisted(prevbuf) && prevbuf != btarget
  257. buffer #
  258. else
  259. bprevious
  260. endif
  261. if btarget == bufnr('%')
  262. " Numbers of listed buffers which are not the target to be deleted.
  263. let blisted = filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != btarget')
  264. " Listed, not target, and not displayed.
  265. let bhidden = filter(copy(blisted), 'bufwinnr(v:val) < 0')
  266. " Take the first buffer, if any (could be more intelligent).
  267. let bjump = (bhidden + blisted + [-1])[0]
  268. if bjump > 0
  269. execute 'buffer '.bjump
  270. else
  271. execute 'enew'.a:bang
  272. endif
  273. endif
  274. endfor
  275. execute 'bdelete'.a:bang.' '.btarget
  276. execute wcurrent.'wincmd w'
  277. endfunction
  278. command! -bang -complete=buffer -nargs=? Bclose call <SID>Bclose(<q-bang>, <q-args>)
  279. nnoremap <silent> <Leader>bd :Bclose<CR>
  280. map gn :bn<cr>
  281. map gp :bp<cr>
  282. map gw :Bclose<cr>
  283. " Run Python and C files by Ctrl+h
  284. autocmd FileType python map <buffer> <C-h> :w<CR>:exec '!python3.10' shellescape(@%, 1)<CR>
  285. autocmd FileType python imap <buffer> <C-h> <esc>:w<CR>:exec '!python3.10' shellescape(@%, 1)<CR>
  286. autocmd FileType c map <buffer> <C-h> :w<CR>:exec '!gcc' shellescape(@%, 1) '-o out; ./out'<CR>
  287. autocmd FileType c imap <buffer> <C-h> <esc>:w<CR>:exec '!gcc' shellescape(@%, 1) '-o out; ./out'<CR>
  288. autocmd FileType python set colorcolumn=79
  289. set relativenumber
  290. set rnu
  291. let g:transparent_enabled = v:true
  292. tnoremap <Esc> <C-\><C-n>
  293. " Telescope bindings
  294. nnoremap ,f <cmd>Telescope find_files<cr>
  295. nnoremap ,g <cmd>Telescope live_grep<cr>
  296. " Go to next or prev tab by H and L accordingly
  297. nnoremap H gT
  298. nnoremap L gt
  299. " Autosave plugin
  300. lua << EOF
  301. require("auto-save").setup({
  302. enabled = true,
  303. execution_message = {
  304. message = function() -- message to print on save
  305. return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
  306. end,
  307. dim = 0.18, -- dim the color of `message`
  308. cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
  309. },
  310. events = {"InsertLeave", "TextChanged"},
  311. conditions = {
  312. exists = true,
  313. filename_is_not = {},
  314. filetype_is_not = {},
  315. modifiable = true
  316. },
  317. write_all_buffers = false,
  318. on_off_commands = true,
  319. clean_command_line_interval = 0,
  320. debounce_delay = 135,
  321. })
  322. EOF
  323. " Telescope fzf plugin
  324. lua << EOF
  325. require('telescope').load_extension('fzf')
  326. EOF
  327. " Fast component creating for React app
  328. command CreateComponent :terminal '/Users/alexeygoloburdin/code/lms/frontend/createcomponent.py'
  329. " White colors for LSP messages in code
  330. set termguicolors
  331. hi DiagnosticError guifg=White
  332. hi DiagnosticWarn guifg=White
  333. hi DiagnosticInfo guifg=White
  334. hi DiagnosticHint guifg=White