" Vimrc
" Set encoding
set encoding=utf-8

scriptencoding utf-8

"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

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

" Make sure you use single quotes

Plug 'tpope/vim-sensible'
Plug 'tpope/vim-fugitive' | Plug 'junegunn/gv.vim'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-unimpaired'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-dispatch'
Plug 'tpope/vim-endwise'
Plug 'tpope/vim-rails'
Plug 'tpope/vim-bundler', { 'for': 'ruby' }
Plug 'tpope/vim-haml', { 'for': 'haml' }
" Plug 'Valloric/YouCompleteMe', { 'do': './install.py --gocode-completer' }
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'
Plug 'kana/vim-smartchr'
Plug 'kana/vim-smartinput'
Plug 'kana/vim-textobj-user' | Plug 'rhysd/vim-textobj-ruby'
Plug 'tmux-plugins/vim-tmux'
Plug 'tmux-plugins/vim-tmux-focus-events'
Plug 'christoomey/vim-tmux-navigator'
Plug 'vim-ruby/vim-ruby'
Plug 'skwp/vim-rspec', { 'for': 'ruby' }
Plug 'fatih/vim-go'
" Plug 'scrooloose/syntastic'
Plug 'editorconfig/editorconfig-vim'
Plug 'junegunn/vim-easy-align'
Plug 'nelstrom/vim-visual-star-search'
Plug 'airblade/vim-gitgutter'
Plug 'itchyny/lightline.vim'
Plug 'rizzatti/dash.vim'
Plug 'tomasr/molokai'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --no-key-bindings' }
Plug 'junegunn/fzf.vim'
Plug 'justinmk/vim-dirvish'
Plug 'mattn/emmet-vim'
Plug 'Glench/Vim-Jinja2-Syntax'
Plug 'janko-m/vim-test'
Plug 'othree/yajs.vim'
Plug 'skywind3000/asyncrun.vim'
Plug 'mhinz/vim-startify'
Plug 'elixir-lang/vim-elixir'
Plug 'slashmili/alchemist.vim'
Plug 'pearofducks/ansible-vim'
Plug 'w0rp/ale'

Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'

Plug 'Shougo/deoplete-rct'
Plug 'Shougo/neco-syntax'

Plug 'mileszs/ack.vim'
Plug 'mbbill/undotree'
Plug 'cohama/agit.vim'

if has('nvim')
  Plug 'autozimu/LanguageClient-neovim', {
    \ 'branch': 'next',
    \ 'do': 'bash install.sh',
    \ }
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
  Plug 'direnv/direnv.vim'
  Plug 'kassio/neoterm'
else
  Plug 'prabirshrestha/async.vim'
  Plug 'prabirshrestha/vim-lsp'
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif

" Add plugins to &runtimepath
call plug#end()

" Auto command group setting
augroup MyAutoCmd
  autocmd!
augroup END

" <Leader> is now '<space>' not '\'
nnoremap <Space> <Nop>
let mapleader = ' '

" source ~/.vim/settings/neobundle.vim

" % to bounce from do to end etc.
runtime! macros/matchit.vim

" Basic configuration, showing absolute line number of
" current line and ruler
" Show relative number with GUI only
if has('gui_running')
  set relativenumber
endif
set number
set ruler

" Syntax enabling
syntax on

" Show mode and command
set showmode
set showcmd

" Don't beep
set visualbell

" Store command histories
set history=1000

" Folding
set foldmethod=syntax
set foldenable
set foldlevel=2
set foldopen=hor,insert,search

" Whitespace stuff
set wrap
set linebreak
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set list listchars=tab:\ \ ,trail:ยท

" Tab completion
set wildmode=list:longest,list:full
set wildignore&
set wildignore+=*.o,*.obj,.git,*.rbc,*.class,.svn,vendor/gems/*

" Searching
set hlsearch
set incsearch
set ignorecase
set smartcase

" Use English as default help language
set helplang=en,ja

" Use modeline overrides
set modeline
set modelines=10

" Swapfile and backup
set swapfile
set directory=~/.vim/swap
set backup
set backupdir=~/.vim/backup

" Status bar
set laststatus=2

source ~/.vim/settings/bundle/lightline.vim

" Auto commands
if has('autocmd')
  " Remember last location in file
  autocmd MyAutoCmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
    \| exe "normal g'\"" | endif

  " md is markdown, not modula2!
  autocmd MyAutoCmd BufNewFile,BufRead *.md set filetype=markdown

  " ES6 is javascript
  autocmd MyAutoCmd BufNewFile,BufRead *.{es6,es6.js} set filetype=javascript

  " Close help with q
  autocmd MyAutoCmd FileType help nnoremap <silent> <buffer> q <C-W>c
  autocmd MyAutoCmd FileType qf nnoremap <silent> <buffer> q :cclose<CR>

  autocmd MyAutoCmd VimEnter *.txt,*.md,*.markdown setlocal spell
  autocmd MyAutoCmd FileType gitcommit setlocal spell

  autocmd MyAutoCmd FileType ruby setlocal iskeyword+=?
  autocmd MyAutoCmd FileType ruby setlocal iskeyword+=!
  autocmd MyAutoCmd FileType ruby setlocal iskeyword+=@-@

  " Auto load vimrc
  if has('gui_running')
    autocmd MyAutoCmd BufWritePost $MYVIMRC source $MYVIMRC | source $MYGVIMRC
          \ | redraw
    autocmd MyAutoCmd BufWritePost $MYGVIMRC source $MYGVIMRC | redraw
  else
    autocmd MyAutoCmd BufWritePost $MYVIMRC source $MYVIMRC | redraw
  endif
endif

" Easy help keymapping
" nnoremap <Leader>h :<C-u>help<Space>

" Edit vimrc keymappings
nnoremap <silent> <Leader>ev  :<C-u>edit $MYVIMRC<CR>
if has('gui_running')
  nnoremap <silent> <Leader>eg  :<C-u>edit $MYGVIMRC<CR>
endif

" colorscheme
set t_Co=256
color molokai
if !has('gui_running')
  hi Normal ctermbg=none
endif

" Save undo history after quitting
if has('persistent_undo')
  set undofile
  set undodir=~/.vim/undo
endif

" Ref.vim
let g:ref_use_vimproc = 1

" dash.vim configuration
if has('mac')
  nmap <silent> <Leader>d <Plug>DashSearch
endif

" Smartchr configuration
inoremap <expr> = smartchr#loop('=', '==', ' = ', ' == ' )

" CTags
set tags&
set tags+=gems.tags

" Unimpaired configuration
" Bubble single lines
nmap <C-Up> [e
nmap <C-Down> ]e
" Bubble multiple lines
vmap <C-Up> [egv
vmap <C-Down> ]egv

let g:startify_bookmarks = [ {'c': '~/.vimrc'}, '~/.zshrc' ]
let g:startify_skiplist = [
      \ 'COMMIT_EDITMSG',
      \ ]

" Disable <C-S> in insert mode
let g:surround_no_insert_mappings = 1

" Go
let g:go_dispatch_enabled = 1
let g:go_metalinter_autosave = 1

let test#strategy = "dispatch"

nmap <silent> <leader>t :TestNearest<CR>
nmap <silent> <leader>T :TestFile<CR>
nmap <silent> <leader>l :TestLast<CR>

nnoremap <Leader>f :<C-u>Files<CR>
nnoremap <Leader>b :<C-u>Buffers<CR>
nnoremap <Leader>hi :<C-u>History<CR>
nnoremap <Leader>d :<C-u>Dirvish<CR>

source ~/.vim/settings/bundle/easy-align.vim
source ~/.vim/settings/bundle/fugitive.vim
source ~/.vim/settings/bundle/rails.vim
" source ~/.vim/settings/bundle/syntastic.vim
" source ~/.vim/settings/bundle/ycm.vim
source ~/.vim/settings/bundle/ultisnips.vim

hi Visual           guifg=NONE        guibg=#262D51     gui=NONE      ctermfg=NONE        ctermbg=darkgray    cterm=NONE

set hidden

if has('nvim')
  let g:LanguageClient_serverCommands = {
        \ 'ruby': ['solargraph', 'socket', '--port', '0'],
        \ }

  " Automatically start language servers.
  let g:LanguageClient_autoStart = 1
else
  if executable('language_server-ruby')
    autocmd User lsp_setup call lsp#register_server({
          \ 'name': 'language_server-ruby',
          \ 'cmd': {server_info->['language_server-ruby']},
          \ 'whitelist': ['ruby'],
          \ })
    autocmd FileType ruby setlocal omnifunc=lsp#complete
  endif
endif

let g:deoplete#enable_at_startup = 1
call deoplete#custom#source('ultisnips', 'matchers', ['matcher_fuzzy'])
call deoplete#custom#source('LanguageClient',
            \ 'min_pattern_length',
            \ 2)
call deoplete#custom#option('sources', {
      \ 'ruby': ['syntax', 'buffer', 'member', 'omni', 'LanguageClient', 'ultisnips', 'around'],
      \})
autocmd CompleteDone * silent! pclose!

let g:ale_fixers = {
\   'javascript': ['prettier'],
\   'ruby': ['rubocop'],
\}
let g:ale_enabled = 1
let g:ale_fix_on_save = 1
let g:ale_ruby_rubocop_executable='bundle'

" External rc files
" Include vimrc for mac
" Include user's local vim config
if filereadable(expand('~/.vimrc.local'))
  source ~/.vimrc.local
endif