{ config, pkgs, lib, home, ... }:

{ 
  nixpkgs.config.allowUnfree = true;

  home.packages = with pkgs; [ 
    nixfmt
    vscode
  ]; 

  programs.neovim = {
    enable      = true;
    extraConfig = ''
      " syntax
      syntax enable

      " color themes
      set termguicolors
      colorscheme molokai

      " wildcard mode
      set wildmode=longest:full,full

      " remapping popup menu (command autocompletion)
"      cnoremap <expr> <up> pumvisible() ? "<C-p>" : "<up>
"      cnoremap <expr> <down> pumvisible() ? "<C-n>" : "<down>"
"      cnoremap <expr> <CR> pumvisible() ? "<C-e>":"<CR>"
      " set line numbers
      set number

      " YouCompleteMe
      let g:ycm_key_list_stop_completion = ['<C-y>', '<CR>'] 
      let g:ycm_key_list_select_completion = ['<Up>', '<Down>', '<TAB>'] 

      " enable indent guides
      let g:indent_guides_enable_on_vim_startup = 1

      " Exit Vim if NERDTree is the only window left.
      autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
      \ quit | endif

      " Start NERDTree. If a file is specified, move the cursor to its window.
      autocmd StdinReadPre * let s:std_in=1
      autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif

      " Start NERDTree when Vim starts with a directory argument.
      autocmd StdinReadPre * let s:std_in=1
      autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
      \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif

      " Exit Vim if NERDTree is the only window left.
      autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
      \ quit | endif

      " Start interactive EasyAlign in visual mode (e.g. vipga)
      xmap ga <Plug>(EasyAlign)

      " Start interactive EasyAlign for a motion/text object (e.g. gaip)
      nmap ga <Plug>(EasyAlign)

      " Highlight row and column
      set cul
      set cuc

      " Fix for code not being aligned if between comment blocks
      set cindent cinkeys-=0#
      set expandtab shiftwidth=2 tabstop=2 softtabstop=2
    '';

    viAlias  = true;
    vimAlias = true;
    plugins  = with pkgs.vimPlugins; [
      vim-nix
      molokai
      YouCompleteMe
      vim-airline
      vim-airline-themes
      vim-lsp 
      vim-indent-guides
      vim-signify 
      nerdtree
      vim-easy-align
      vim-fugitive
      vim-yaml
      vim-autoformat
    ];
  };

}