summaryrefslogtreecommitdiff
path: root/nvim/init.vim
blob: e6931ca04865e725063ec5802047d772e6690402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"line numbers
set nu
"unix newlines
set ff=unix
"set the highlight column at 80
set colorcolumn=80
" display spaces, tabs, and newlines
set list
set listchars=tab:→\ ,space:·,nbsp:␣,trail:•,eol,precedes:«,extends:»
set noexpandtab

" Kevin's lint
set expandtab
set softtabstop=4
set shiftwidth=4

" Clipboard
set clipboard+=unnamedplus

" Set wrap
set wrap

"2 lines of cmd, needed for echodoc
set cmdheight=2
if has('win32')
	source $VIMRUNTIME/mswin.vim
endif
if has('mac')
	let g:neovide_input_macos_option_key_is_meta = 'only_left'
endif
let g:python3_host_prog='python3'

call plug#begin('~/.vim/plugged')

"Formatting and navigation
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'scrooloose/nerdcommenter'
Plug 'godlygeek/tabular'
" 
" "Git
Plug 'xuyuanp/nerdtree-git-plugin'
Plug 'tpope/vim-fugitive'
" 
" "Fuzzy file search
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
 
" "tab completion
Plug 'mason-org/mason.nvim'
Plug 'mason-org/mason-lspconfig.nvim'
Plug 'mfussenegger/nvim-lint'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
"Plug 'Shougo/denite.nvim'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
"Plug 'Shougo/echodoc.vim'
Plug 'deoplete-plugins/deoplete-jedi'
Plug 'neovim/nvim-lspconfig'
Plug 'deoplete-plugins/deoplete-lsp'

" Snippits
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'

" keymap help
Plug 'folke/which-key.nvim'

" Python
Plug 'raimon49/requirements.txt.vim'

" Scala tab completion
Plug 'nvim-lua/plenary.nvim'
Plug 'scalameta/nvim-metals'

" CSV formatting
Plug 'chrisbra/csv.vim'

" Lua, Moonscript, Teal
Plug 'xolox/vim-misc'
Plug 'xolox/vim-lua-ftplugin'
"Plug 'VaiN474/vim-etlua'
Plug 'leafo/moonscript-vim'
Plug 'teal-language/vim-teal'
"Plug 'S1M0N38/love2d.nvim'

" Treesitter, higlihting
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}

" Coq
"Plug 'whonore/Coqtail'

" GLSL
"Plug 'tikhomirov/vim-glsl'

" Supercollider
"Plug 'davidgranstrom/scnvim'

"Color schemes
Plug 'mhartington/oceanic-next'
Plug 'vim-scripts/Wombat'
Plug 'alessandroyorba/despacio'
Plug 'alessandroyorba/alduin'
Plug 'tomasr/molokai'
Plug 'sainnhe/vim-color-forest-night'
Plug 'sts10/vim-pink-moon'
Plug 'severij/vadelma'
Plug 'kkpmw/sacredforest-vim'
Plug 'jsit/toast.vim'
Plug 'sainnhe/edge'
Plug 'kjssad/quantum.vim'
Plug 'kyoz/purify'
Plug 'joshdick/onedark.vim'
Plug 'cocopon/iceberg.vim'
Plug 'wolf-dog/sceaduhelm.vim'
Plug 'danilo-augusto/vim-afterglow'
Plug 'kamwitsta/mythos'
Plug 'https://github.com/gbprod/nord.nvim'
Plug 'ghifarit53/tokyonight-vim'
Plug 'scottmckendry/cyberdream.nvim'
Plug 'nyoom-engineering/oxocarbon.nvim'
Plug 'rebelot/kanagawa.nvim'

"Icons, requires a nerd font
Plug 'ryanoasis/vim-devicons'

"AI, requires ollama, curl, and plenary
"on
Plug 'nomnivore/ollama.nvim'
Plug 'nvim-lualine/lualine.nvim'

" Terraform
Plug 'hashivim/vim-terraform'
call plug#end()

set background=dark
let colorschemes = ['OceanicNext',  'afterglow', 'alduin', 'nord', 'edge', 'desert', 'despacio', 'onedark', 'evening', 'everforest', 'habamax', 'iceberg', 'oxocarbon', 'lunaperche', 'molokai', 'moon', 'mythos', 'cyberdream', 'orange-moon', 'pink-moon', 'quantum', 'retrobox', 'sceaduhelm', 'kanagawa', 'sorbet', 'toast', 'tokyonight', 'vadelma', 'yellow-moon', 'zaibatsu']

"Randomly select a colorscheme based on current directory. Each project gets
"it's own color- usually.
let cs = colorschemes[str2nr(sha256(execute('pwd'))[1:5],16) % len(colorschemes)]
execute('colorscheme ' . cs)

"deoplete
"let g:deoplete#enable_at_startup = 1
"call deoplete#custom#option("min_pattern_length", 1)
"call deoplete#custom#option("smart_case", 1)
"imap <expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
"imap <expr><CR> pumvisible() ? "\<CR>\<Plug>AutoPairsReturn" : "\<CR>"
"imap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<S-TAB>"
"imap <expr><CR> pumvisible() ? deoplete#close_popup() : "\<CR>\<Plug>AutoPairsReturn"

" echodoc
"let g:echodoc#enable_at_startup=1
"let g:echodoc#type="floating"
"highlight link EchoDocFloat Pmenu

"python
"let g:deoplete#sources#jedi#python_path='python3'
"let g:deoplete#sources#jedi#enable_short_types=1
"let g:deoplete#sources#jedi#show_docstring=1
"call deoplete#custom#source('jedi','max_info_width',0)
"set completeopt-=preview

"lua
let g:lua_compiler_name='lua'
"let g:lua_compiler_name='lua'
let g:lua_complete_dynamic=1
let g:lua_complete_keywords=0
"let g:deoplete#lsp#handler_enabled=v:true
"let g:deoplete#lsp#use_icons_for_candidates=v:true

"au BufReadPost *.busted set syntax=lua
"au BufReadPost *.spec set syntax=lua
"au BufReadPost *.etlua set syntax=html
"Overwrite xolox:ftplugin's "Help" with shift-k
au BufReadPost *.lua unmap <buffer> K
"au BufReadPost *.lua eval b:undo_ftplugin
"au BufReadPost *.lua let g:lua_complete_omni=1
" GLSL
au BufReadPost *.vs,*.fs set ft=glsl

"scala
augroup lsp
  au!
  au FileType java,scala,sbt lua m=require("metals"); mc=m.bare_config(); mc.init_options.statusBarProvider="on"; m.initialize_or_attach(mc)
augroup end

"NERDTree config
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
	\ quit | endif


nnoremap <C-\> :NERDTreeToggle<Cr>
nnoremap <C-r> :NERDTreeFind<Cr>
let NERDTreeMapHelp='y'
let NERDTreeMapJumpLastChild='y'
let NERDTreeMapJumpFirstChild='y'

"NERDCommenter config
nnoremap <C-/> :call nerdcommenter#Comment('x', 'toggle')<CR>
vnoremap <C-/> :call nerdcommenter#Comment('x', 'toggle')<CR>

"Fuzzy file search
let g:fzf_layout = { 'window': {'width': 0.9, 'height': 0.6} }
noremap <M-p> :call fzf#run(fzf#wrap({'sink': 'tabe', 'left': '20%', 'source': 'git ls-files'}))<Cr>
noremap <C-p> :call fzf#run(fzf#wrap({'sink': 'vsplit', 'left': '50%', 'source': 'git ls-files'}))<Cr>

"Set leader
let mapleader=";"

"Fuzzy git checkout branch
noremap <leader>c :call fzf#run(fzf#wrap({'sink': 'Git checkout', 'left':'20%', 'source': 'git branch \| tr -d " "' }))<Cr>
noremap <leader>a :call fzf#run(fzf#wrap({'sink': 'git checkout', 'left':'50%', 'source': 'ag --nogroup --column --color --', 'preview-window':'up','preview':expand('<sfile>:p:h:h:h').'/bin/preview.sh'}))<Cr>

" which-key, helps finding keybinds
noremap <leader>w lua require("which-key").show({})

"Fuzzy sbt
function! s:VTerm(command)
	vsplit
	termopen(a:command)
endfunction

"ctrl+s saves
nmap <C-s> :w<Cr>

"For some reason this is different between linux and windows
if has('win32')
	vmap <C-/> <leader>c<Space>
else
	vmap <C-_> <leader>c<Space>
endif


"Shortcut to modify vim config
nnoremap <leader>ec :tabe $MYVIMRC<Cr>

"Move around quickly
nnoremap J 5j
nnoremap K 5k
nnoremap H 5h
nnoremap L 5l

vnoremap J 5j
vnoremap K 5k
vnoremap H 5h
vnoremap L 5l

"Navigate splits
tnoremap <M-h> <C-\><C-n><C-w>h
tnoremap <M-j> <C-\><C-n><C-w>j
tnoremap <M-k> <C-\><C-n><C-w>k
tnoremap <M-l> <C-\><C-n><C-w>l

nnoremap <M-h> <C-\><C-n><C-w>h
nnoremap <M-j> <C-\><C-n><C-w>j
nnoremap <M-k> <C-\><C-n><C-w>k
nnoremap <M-l> <C-\><C-n><C-w>l

"Resize splits
"noremap <C-h> :vertical resize -5<Cr>
"noremap <C-l> :vertical resize +5<Cr>
"noremap <C-j> :res +5<Cr>
"noremap <C-k> :res -5<Cr>

function! s:HasNeighbor(direction)
	return winnr() != winnr(a:direction)
endfunction

function! s:PushResize(direction, amount)
	if a:direction == 'l' || a:direction == 'h'
		let l:cmd = 'vertical resize'
	else
		let l:cmd = 'resize'
	endif

	if s:HasNeighbor(a:direction)
		let l:amount = '+'.a:amount
	else
		let l:amount = '-'.a:amount
	endif

	echo l:cmd . ' ' . l:amount
	execute l:cmd . ' ' . l:amount
	set cmdheight=2
endfunction

nnoremap <C-h> :call <SID>PushResize('h',5)<CR>
nnoremap <C-j> :call <SID>PushResize('j',5)<CR>
nnoremap <C-k> :call <SID>PushResize('k',5)<CR>
nnoremap <C-l> :call <SID>PushResize('l',5)<CR>

"Navigate tabs
noremap <C-]> gt
noremap <C-[> gT

"Add a semicolon at the end of the line
vnoremap <C-;> :'<,'>s/\([^;]\)$/\1;/g<Cr>

"Vertical split and make
noremap <C-b> :vsplit term://make all<Cr>

"Start a terminal
function! s:Terminal()
    if filereadable(".terminal")
        execute feedkeys(":vsplit\<CR>:terminal\<CR>i./.terminal\<CR>")
    else
        execute feedkeys(":vsplit\<CR>:terminal\<CR>i")
    endif
endfunction
noremap <C-t> :call <SID>Terminal()<CR>

"Find and open in a new tab
noremap <C-f> :Ag<Cr>

"Restart vim (alt+1)
noremap <M-1> :source $MYVIMRC \| :PlugInstall<Cr>

filetype plugin indent on
syntax enable

"Unmap a bunch of stuff set by lunarvim
if has_key(environ(), "LUNARVIM_BASE_DIR")
	lua if lvim then lvim.builtin.which_key.mappings.e = nil end
	lua if lvim then lvim.lsp.buffer_mappings.normal_mode.K = nil end
	lua if lvim then lvim.builtin.bufferline.active = false end
	let $MYVIMRC=$LUNARVIM_BASE_DIR . "\\vimrc.vim"
	"nmap <C-Bslash> :NvimTreeToggle<CR>
	"vunmap <C-/>
	"vmap <C-/> :call nerdcommenter#NERDComment('x', 'toggle')<CR>
	"nnoremap <C-/> :call nerdcommenter#Comment('x', 'toggle')<CR>
endif

" mason.nvim - lsp running
lua << EOF
--[[
require("mason").setup({
    ui = {icons = {
        package_installed = "󰄳",
        package_pending = "󱑤",
        package_uninstalled = ""
    }}
})]]
require("mason").setup()

EOF
lua require("mason-lspconfig").setup({ensure_installed = {"lua_ls"}})

lua << EOF
    local cmp = require("cmp")
    local function has_words_before()
        local unpack = unpack or table.unpack
        local line, col = unpack(vim.api.nvim_win_get_cursor(0))
        return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
    end
    local function feedkey(key, mode)
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
    end
    cmp.setup({
        snippet= {
            expand = function(args)
                vim.fn["vsnip#anonymous"](args.body)
            end,
        },
        window = {
            completion = cmp.config.window.bordered(),
            documentation = cmp.config.window.bordered(),
        },
        mapping = cmp.mapping.preset.insert({
            --["<CR>"] = cmp.mapping.confirm({select=true}),
            ["<C-e>"] = cmp.mapping.abort(),
            ["<Tab>"] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_next_item()
                elseif vim.fn["vsnip#available"](1) == 1 then
                    feedkey("<Plug>(vsnip-expand-or-jump)", "")
                elseif has_words_before() then
                    cmp.complete()
                else
                    fallback()
                end
                end, {"i","s"}),
            ["<S-Tab>"] = cmp.mapping(function()
                if cmp.visible() then
                    cmp.select_prev_item()
                elseif vim.fn["vsnip#available"](1) == 1 then
                    feedkey("<Plug>(vsnip-jump-prev)", "")
                end
            end, {"i","s"}),
            ["<C-j>"] = cmp.mapping.scroll_docs(5),
            ["<C-k>"] = cmp.mapping.scroll_docs(-5),
        }),
        sources = cmp.config.sources({
                {name = "nvim_lsp"},
                {name = "vsnip" },
            },{
                {name = "buffer" },
        }),
    })
    local capabilities = require("cmp_nvim_lsp").default_capabilities()
    vim.api.nvim_create_autocmd("LspAttach", {
        desc = "LSP actions",
        callback = function(event)
            local opts = {buffer = event.buf}
            vim.keymap.set("n", "<f1>", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
        end,
    })
    require("lspconfig").lua_ls.setup{
        settings={
            Lua={
                runtime={
                    version="LuaJIT",
                    path="src/?.lua;src/?/init.lua;lib/3rd/?/init.lua;lib/3rd/?.lua"
                },
                completion = {enable = true, callSnippet = "Replace"},
                diagnostics = {
                    enable=true,
                },
                workspace = {
                    --"${3rd}/love2d/library",
                    --"${3rd}/busted/library",
                }
            }
        }
    }
    require('lspconfig').harper_ls.setup {
        settings = {
            ["harper-ls"] = {
                linters = {
                    SentenceCapitalization = false,
                    SpellCheck = false
                }
            }
        }
    }
EOF

lua << EOF
-- ollama.nvim integration
do
  local function ollama_loaded()
    return package.loaded.ollama and require("ollama").status ~= nil
  end
  local ollama_status = {
    IDLE = "🦙",
    WORKING = "🤖"
  }
  local function get_status_icon()
    return ollama_status[require("ollama").status()]
  end
  require("lualine").setup({sections = {lualine_x = {get_status_icon, ollama_loaded}}})
  require("ollama").config.prompts.fgen = {
    prompt = [[
Generate $ftype code that keeps all comments and follows this code:

```
$before
```

and could go before this code

```
$after
```

Respond EXACTLY in this format:
```$ftype
<your code>
```
]],
    action = "display_insert",
  }
  require("ollama").config.prompts.rewrite= {
    prompt = [[
Change this $ftype code to $input, keep any comments:

$sel

Respond EXACTLY in this format:
```$ftype
<your code>
```
]],
    action = "display_replace"
  }
  require("ollama").config.prompts.review = {
    prompt = [[
Take a look at this $ftype code and provide critique in the style of a code review with concrete hints on how to make the code easier to read or more pythonic.
Keep your review succinct. Respond EXACTLY in this format:

```$ftype
<your comments>
```

```
$buf
```

]],
    action = "display"
  }
end
EOF

" Open diagnostics (e.g. for harper)
nnoremap <leader>s :lua vim.diagnostic.open_float()<CR>

" Copy the current filename
nnoremap <leader>y :let @+ = expand('%')<CR>

" ollama.nvim - ai code assist

lua require("ollama").config.model = "codestral"
nnoremap <leader>gg :lua require("ollama").prompt("fgen")<CR>
vnoremap <leader>gh :lua require("ollama").prompt("Explain_Code")<CR>
vnoremap <leadeR>gG :lua require("ollama").prompt("rewrite")<CR>
nnoremap <leadeR>gt :lua require("ollama").prompt("Raw")<CR>
nnoremap <leadeR>gr :lua require("ollama").prompt("review")<CR>
lua << EOF
    vim.api.nvim_create_autocmd('FileType', {
      -- This handler will fire when the buffer's 'filetype' is "lua"
      pattern = 'lua',
      callback = function(ev)
        vim.lsp.start({
          name = 'lua-lsp',
          cmd = {'lua-language-server'},

          -- Set the "root directory" to the parent directory of the file in the
          -- current buffer (`ev.buf`) that contains either a "setup.py" or a
          -- "pyproject.toml" file. Files that share a root directory will reuse
          -- the connection to the same LSP server.
          root_dir = "src"
        })
      end,
    })
EOF

" love2d help

lua << EOF
    --[[
    require("love2d").setup{
        path_to_love_bin = "love",
        path_to_love_library = vim.fn.globpath(vim.o.runtimepath, "love2d/library"),
        restart_on_save = false,
    }
    ]]
EOF

" Acrisure

" Sqlfluf leader+sql
"nnoremap <leader>sql :let cfile=expand('%')<CR>:vsplit<CR>:terminal<CR>isqlfluff