" This is Venomade's vimrc

" Install Vim Plug
let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
  silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

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

" Maybe replace with fzf in future
Plug 'ctrlpvim/ctrlp.vim'

" COC because YCM is too hard
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Pretty Colors
Plug 'jackguo380/vim-lsp-cxx-highlight'
"put this into coc-settings.json
"{
    "suggest.noselect": true,
    "clangd.semanticHighlighting": true,
    "coc.preferences.semanticTokensHighlights": false
"}

" λ Mathematical Crowbars λ
Plug 'neovimhaskell/haskell-vim'

" I'm too lazy to actually program
Plug 'Exafunction/codeium.vim'

" Getting Gruuvy
Plug 'dkasak/gruvbox'

" Space then... ummm
Plug 'liuchengxu/vim-which-key'

" Ahhh a lovely #00ff00 field
Plug 'chrisbra/Colorizer'

" Customize the flight path
Plug 'vim-airline/vim-airline'

"          \("^_^")/
Plug 'LunarWatcher/auto-pairs'

" So take me back in time!!!
Plug 'farmergreg/vim-lastplace'

" One  please! Oooh and a   too!
" Plug 'ryanoasis/vim-devicons'

call plug#end()

" Colorscheme
colorscheme gruvbox
set bg=dark

" Undo History
set undofile
set undodir=~/.vim/undodir

" Common Settings
syntax on
filetype plugin indent on
set ts=2 sts=2 sw=4 et ai si
set nu rnu
set nowrap
set formatoptions-=cro
set splitbelow splitright
set encoding=UTF-8
set shell=zsh
set ignorecase
set smartcase
set incsearch

" Cursor Shapes
let &t_SI = "\<Esc>[6 q"
let &t_SR = "\<Esc>[4 q"
let &t_EI = "\<Esc>[2 q"

" Terminal Title
autocmd BufEnter * let &titlestring = '' . expand("%:t")
set title

" Airline Config
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#codeium#enabled = 1
let g:airline#extensions#tabline#formatter = 'unique_tail'
let g:airline_powerline_fonts = 1
let g:airline#extensions#coc#enabled = 1
let g:airline#extensions#coc#show_coc_status = 1


" Haskell Highlighting
let g:haskell_enable_quantification = 1   " to enable highlighting of `forall`
let g:haskell_enable_recursivedo = 1      " to enable highlighting of `mdo` and `rec`
let g:haskell_enable_arrowsyntax = 1      " to enable highlighting of `proc`
let g:haskell_enable_pattern_synonyms = 1 " to enable highlighting of `pattern`
let g:haskell_enable_typeroles = 1        " to enable highlighting of type roles
let g:haskell_enable_static_pointers = 1  " to enable highlighting of `static`
let g:haskell_backpack = 1                " to enable highlighting of backpack keywords

" Color Highlighting
set termguicolors
let g:colorizer_auto_color = 1

" AutoPairs
let g:AutoPairsMapBS = 1

" Recentre view on slow move
nnoremap j jzz
nnoremap k kzz

" Same for mouse
set mouse=a
map <ScrollWheelDown> 2jzz
map <ScrollWheelUp> 2kzz

" Leader Key
let mapleader = " "

" WhichKey
nnoremap <silent> <leader> :WhichKey '<Space>'<CR>

" Quick split movement
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

" Terminal Buffer
map <leader>t :term<CR>

" Fuzzy Find
map <leader>ff :CtrlP<CR>
map <leader>fb :CtrlPBuffer<CR>
map <leader>fa :CtrlPMixed<CR>

" Copy and Paste from System Clipboard
noremap <Leader>y "*y
noremap <Leader>p "*p

" Quick Add/Remove Semicolon
map <leader>; :s/\v(.)$/\=submatch(1)==';' ? '' : submatch(1).';'<CR>

" Buffers
map <leader>bk :bd<CR>
map <leader>bn :bn<CR>
map <leader>bp :bp<CR>

" Clang Build Individual File (No Includes)
" map <leader>bf :!clang -Werror % -o out/%:r && ./out/%:r <CR>

" Haskell Run File
map <leader>rh :!runhaskell %<CR>

" Remove Trailing Whitespaces on Save
autocmd BufWritePre * %s/\s\+$//e

" Codeium Config
let g:codeium_disable_bindings = 1
inoremap <silent><script><expr> <C-space> codeium#Accept()

" ----------
" COC Config
" ----------
set encoding=utf-8

set nobackup "DANGER
set nowritebackup "But it's needed :/

set updatetime=300
set signcolumn=yes

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

inoremap <silent><expr> <c-@> coc#refresh()

" GoTo code navigation
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Use K to show documentation in preview window
nnoremap <silent> K :call ShowDocumentation()<CR>

function! ShowDocumentation()
  if CocAction('hasProvider', 'hover')
    call CocActionAsync('doHover')
  else
    call feedkeys('K', 'in')
  endif
endfunction

" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming
nmap <leader>cn <Plug>(coc-rename)

" Formatting selected code
xmap <leader>cf  <Plug>(coc-format-selected)
nmap <leader>cf  <Plug>(coc-format-selected)

augroup mygroup
  autocmd!
  " Setup formatexpr specified filetype(s)
  autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  " Update signature help on jump placeholder
  autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

" Applying code actions to the selected code block
" Example: `<leader>aap` for current paragraph
xmap <leader>caa  <Plug>(coc-codeaction-selected)
nmap <leader>caa  <Plug>(coc-codeaction-selected)

" Remap keys for applying code actions at the cursor position
nmap <leader>cac  <Plug>(coc-codeaction-cursor)
" Remap keys for apply code actions affect whole buffer
nmap <leader>cs  <Plug>(coc-codeaction-source)
" Apply the most preferred quickfix action to fix diagnostic on the current line
nmap <leader>qf  <Plug>(coc-fix-current)

" Remap keys for applying refactor code actions
nmap <silent> <leader>cre <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>crs  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>crs  <Plug>(coc-codeaction-refactor-selected)

" Run the Code Lens action on the current line
nmap <leader>cl  <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Remap <C-f> and <C-b> to scroll float windows/popups
if has('nvim-0.4.0') || has('patch-8.2.0750')
  nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
  inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
  inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
  vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif

" Use CTRL-S for selections ranges
" Requires 'textDocument/selectionRange' support of language server
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)

" Add `:Format` command to format current buffer
command! -nargs=0 Format :call CocActionAsync('format')

" Add `:Fold` command to fold current buffer
command! -nargs=? Fold :call     CocAction('fold', <f-args>)

" Add `:OR` command for organize imports of the current buffer
command! -nargs=0 OR   :call     CocActionAsync('runCommand', 'editor.action.organizeImport')

" Add (Neo)Vim's native statusline support
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

" Mappings for CoCList
" Show all diagnostics
nnoremap <silent><nowait> <space>cd  :<C-u>CocList diagnostics<cr>
" Manage extensions
nnoremap <silent><nowait> <space>ce  :<C-u>CocList extensions<cr>
" Show commands
nnoremap <silent><nowait> <space>cc  :<C-u>CocList commands<cr>
" Find symbol of current document
nnoremap <silent><nowait> <space>co  :<C-u>CocList outline<cr>
" Search workspace symbols
nnoremap <silent><nowait> <space>cs  :<C-u>CocList -I symbols<cr>
" Do default action for next item
nnoremap <silent><nowait> <space>cj  :<C-u>CocNext<CR>
" Do default action for previous item
nnoremap <silent><nowait> <space>ck  :<C-u>CocPrev<CR>
" Resume latest coc list
nnoremap <silent><nowait> <space>cp  :<C-u>CocListResume<CR>
