"""""""""""""""""""""""""""""""""""""

" Capella .vimrc
"""""""""""""""""""""""""""""""""""""
set nocompatible
filetype off

" Install vim-plug if not present
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif

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

" ------Plugins-------
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'scrooloose/nerdtree'
Plug 'tpope/vim-fugitive'
Plug 'kristijanhusak/vim-multiple-cursors'
Plug 'tpope/vim-surround'
Plug 'jistr/vim-nerdtree-tabs'
Plug 'iwonbigbro/vim-hybrid'
Plug 'dense-analysis/ale'
Plug 'junegunn/fzf'
Plug 'junegunn/fzf.vim'
Plug 'sheerun/vim-polyglot'
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
Plug 'tpope/vim-commentary'

call plug#end()
filetype plugin indent on

if has('syntax') && !exists('g:syntax_on')
  syntax enable
endif


"""""""""""""""""""""""""""""""""""""
" Configuration Section
"""""""""""""""""""""""""""""""""""""

set laststatus=2
set ruler
set showcmd
set wildmenu
set autoread
set backspace=indent,eol,start

" Show line numbers
set number
set relativenumber
set noruler
set cursorline
set scrolloff=8
set hidden
set noerrorbells
set signcolumn=yes
set updatetime=100

" Encoding
set encoding=utf-8


" Set Proper Tabs
set tabstop=4
set shiftwidth=4
set smarttab
set expandtab
set listchars=tab:▒░,trail:▓
set list

" Search
set hlsearch
set incsearch
set ignorecase
set smartcase
nnoremap <silent> <Esc><Esc> :let @/=""<CR>

" Clipboard
set clipboard=unnamedplus

" Theme and Styling
set t_Co=256
set background=dark

" Vim-Airline Configuration
let g:airline#extensions#tabline#enabled = 1
let g:airline_theme='badwolf'
let g:airline_powerline_fonts = 1

set guifont=Source\ Code\ Pro\ for\ Powerline
set t_Co=256

let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'

"""""""""""""""""""""""""""""""""""""
" Colors 
"""""""""""""""""""""""""""""""""""""
colorscheme hybrid
hi MatchParen cterm=bold ctermbg=none ctermfg=yellow
hi ColorColumn ctermbg=232 guibg=#2c2d27

autocmd FileType nerdtree setlocal colorcolumn&
let &colorcolumn="".join(range(81,999),",")

set splitright

let g:NERDTreeDirArrows=0

"""""""""""""""""""""""""""""""""""""
" Golang
"""""""""""""""""""""""""""""""""""""
" Optional: Auto-detect templates within generic .html files
function! DetectGoHtmlTmpl()
    if expand('%:e') == "html" && search("{{") != 0
        setfiletype gohtmltmpl
    endif
endfunction

augroup filetypedetect
    au BufRead,BufNewFile *.html call DetectGoHtmlTmpl()
augroup END

autocmd Filetype go command! -bang A call go#alternate#Switch(<bang>0, 'edit')
autocmd Filetype go command! -bang AV call go#alternate#Switch(<bang>0, 'vsplit')
autocmd Filetype go command! -bang AS call go#alternate#Switch(<bang>0, 'split')
autocmd Filetype go command! -bang AT call go#alternate#Switch(<bang>0, 'tabe')
map <C-l> :cnext<CR>
map <C-k> :cprevious<CR>
map gd :ALEGoToDefinition<CR>

nnoremap <leader>a :cclose<CR>
autocmd FileType go nmap <leader>b  <Plug>(go-build)
autocmd FileType go nmap <leader>r  <Plug>(go-run)

" Persistent undo
set undofile
set undodir=~/.vim/undodir

set spell spelllang=en_gb
hi SpellBad cterm=underline ctermfg=NONE ctermbg=NONE gui=undercurl guisp=red
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
let g:ale_echo_msg_format = '[%linter%]% [code]% %s'

let g:ale_sign_error = '✗'
let g:ale_sign_warning = '⚠️'
let g:ale_sign_column_always = 1

let g:ale_fixers = {
\   'go': ['gofmt', 'gofumpt', 'goimports', 'golines', 'trim_whitespace'],
\   'html': ['prettier', 'trim_whitespace'],
\   'gohtmltmpl': ['prettier'],
\   'python': [
\       'autoflake',
\       'ruff',
\       'ruff_format',
\       'add_blank_lines_for_python_control_statements'
\   ]
\}


let g:ale_linters= {
\   'go': ['gopls', 'golangci-lint'],
\   'python': ['pyright','ruff']
\}
let g:ale_python_auto_pipenv = 1

call ale#linter#Define('mycc', {
\   'name': 'mycc',
\   'lsp': 'stdio',
\   'executable': '/home/capella/repos/gitlab.com/capella/mycc/final/mycc-lsp',
\   'command': '%e',
\   'project_root': '/home/capella/repos/gitlab.com/capella/mycc/programs',
\})


