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
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

"" Editing Helper
" Vim Multiple Cursors
Plug 'terryma/vim-multiple-cursors'

" Vim Surround: Quickly surround stuff
Plug 'tpope/vim-surround'

"" Coding Assistance
" Ale: Runs fast and works well.
Plug 'dense-analysis/ale'

" Better HTML PHP support
Plug 'beanworks/vim-phpfmt'

" php.vim: PHP syntax higlighting and indent
Plug 'StanAngeloff/php.vim'

" PHP Namespace: Helps to keep track of namespaces and autcompletes them
Plug 'arnaud-lb/vim-php-namespace', {'for': 'php'}

"" Debugger
" v-debug: PHP Debugger with xdebug
Plug 'vim-vdebug/vdebug'

"" GUI
" NERDTree: Sidebar file tree
Plug 'scrooloose/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'

" MINIBUFXPL: A nice buffer explorer
Plug 'fholgado/minibufexpl.vim'

" Airline: Some file infos
Plug 'vim-airline/vim-airline'

"" Indexing
" ctrlp.vim: Quickly find files by name without navigating
Plug 'kien/ctrlp.vim'

" Ferret: Quickly finds file content
Plug 'wincent/ferret'

" Initialize plugin system
call plug#end()

"" NERDTree config
" Autostart NERDTree on start
autocmd vimenter * NERDTree | wincmd w
let NERDTreeShowHidden=1

" Close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

"" ALE config
" Define fixers
let g:ale_fixers = {
\   '*': ['remove_trailing_lines', 'trim_whitespace'],
\   'javascript': ['eslint'],
\   'php': ['phpcbf'],
\   'rust': ['rustfmt'],
\}

" Run linter on save
let g:ale_fix_on_save = 1

let g:ale_linters = {
\   'rust': ['rls', 'cargo']
\}

" Enable completion where available.
let g:ale_completion_enabled = 1
set omnifunc=ale#completion#OmniFunc

"" Gutentags
" Auto generate ctags on startup
autocmd! User vim-gutentags call gutentags#setup_gutentags()

"" PHP
" PHP Namespace: Automatically use fully qualified name
function! IPhpExpandClass()
    call PhpExpandClass()
    call feedkeys('a', 'n')
endfunction
autocmd FileType php inoremap <Leader>e <Esc>:call IPhpExpandClass()<CR>
autocmd FileType php noremap <Leader>e :call PhpExpandClass()<CR>

"" vdebug config
" Set xdebug config
let g:vdebug_options = {'ide_key': 'PHPSTORM'}
let g:vdebug_options = {'break_on_open': 0}
let g:vdebug_options = {'server': '172.17.0.1'}
let g:vdebug_options = {'port': '9002'}

" Configure paths
let g:vdebug_options["path_maps"] = { "/var/www/html": getcwd() }

" Don't break on open
let g:vdebug_options['break_on_open'] = 0

"" CtrlP config
" Self explainatory
let g:ctrlp_max_files=0
let g:ctrlp_max_depth=40
let g:ctrlp_custom_ignore = 'node_modules\|DS_Store\|git'

"" Vim Config
" Some autocompletion settings.
set completeopt+=longest,menuone,menu,preview

" Add clipboard support
set clipboard=unnamedplus

" Intendation
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent

let mapleader=" "
