The Go Blog

Use Neovim as Swift IDE

bantana
23 April 2017

ctags

brew install ctags

sourcekitten

brew install sourcekitten

swiftlint and swiftformat

brew install swiftlint
brew install swiftformat

$HOME/.ctags file

--langmap=vim:+(vimrc)

--exclude=.git
--exclude=.config
--exclude=.cache
--exclude=.build
--exclude=YouCompleteMe
--exclude=nginx
--exclude=.svn
--exclude=.hg
--exclude=min
--exclude=vendor
--exclude=\*.min.\*
--exclude=\*.map
--exclude=\*.swp
--exclude=\*.bak
--exclude=\*.pyc
--exclude=\*.class
--exclude=\*.sln
--exclude=\*.Master
--exclude=\*.csproj
--exclude=\*.csproj.user
--exclude=\*.cache
--exclude=\*.dll
--exclude=\*.pdb
--exclude=tags
--exclude=cscope.\*
--exclude=\*.tar.\*
--exclude=node_modules
--exclude=bower_components

--langdef=swift
--langmap=swift:.swift
--regex-swift=/[[:<:]]class[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/c,class/*/
--regex-swift=/[[:<:]]enum[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/e,enum/*/
--regex-swift=/[[:<:]]func[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/f,function/*/
--regex-swift=/[[:<:]]let[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/l,constant/*/
--regex-swift=/[[:<:]]protocol[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/p,protocol/*/
--regex-swift=/[[:<:]]struct[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/s,struct/*/
--regex-swift=/[[:<:]]typealias[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/t,typealias/*/
--regex-swift=/[[:<:]]var[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/v,variable/*/
--regex-swift=/[[:<:]]extension[[:>:]][[:space:]]+([[:alnum:]_]+)/\1/E,extension/*/

neovim ~/.config/nvim/init.vim

use github.com/Shougo/dein.vim plugins Manager

  • neovim dein plugins " ale " language checker call dein#add('w0rp/ale')
" swiftlang
call dein#add('Shougo/deoplete.nvim')
call dein#add('mitsuse/autocomplete-swift')
call dein#add('landaire/deoplete-swift')
call dein#add('kballard/vim-swift')
call dein#add('keith/swift.vim')
call dein#add('aciidb0mb3r/SwiftDoc.vim')
call dein#add('cfdrake/ultisnips-swift')

" quickrun
call dein#add('thinca/vim-quickrun')

" colorscheme
call dein#add('chriskempson/base16-vim')
call dein#add('morhetz/gruvbox')
call dein#add('ryanoasis/vim-devicons')
  • colorscheme " colorscheme ------------------------------ {{{ let $NVIM_TUI_ENABLE_TRUE_COLOR=1
set background=dark
if has("gui_vimr")
  colorscheme base16-default-dark
else
  "colorscheme Tomorrow-Night
  colorscheme gruvbox
endif
" }}}
  • about ctags:
" ctags for swift -------------------------- {{{
let g:tagbar_type_swift = {
  \ 'ctagstype': 'swift',
  \ 'kinds' : [
    \ 'e:Enums',
    \ 't:Typealiases',
    \ 'p:Protocols',
    \ 's:Structs',
    \ 'c:Classes',
    \ 'f:Functions',
    \ 'v:Variables',
    \ 'E:Extensions',
    \ 'l:Constants',
  \ ],
  \ 'sort' : 0
  \ }
" }}}
  • swiftformat
" swiftformat ---------------------------- {{{
  nnoremap <leader>F :!swiftformat %<cr>
" }}}
  • swift command
" swift command --------------------------- {{{
au FileType swift nmap <leader>r :QuickRun swiftrun<cr>
au FileType swift nmap <leader>t :QuickRun swifttest<cr>
" }}}
  • swift autocomplete
" autocomplete for swift ------------------- {{{
autocmd FileType swift imap <buffer> <C-k> <Plug>(autocomplete_swift_jump_to_placeholder)
" }}}
  • swift CLighter Xcode
" CLighter Xcode --------------------------- {{{
" Config for CLighter
if has('nvim')
    let g:clamp_autostart = 1
    let g:clamp_libclang_file = '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib'
else
    let g:clighter8_autostart = 1
    let g:clighter8_libclang_file = '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib'
  endif
" }}}
  • swift source_kitten
" swift source_kitten ---------------------- {{{
let g:deoplete#sources#swift#source_kitten_binary = '/usr/local/bin/sourcekitten'
let g:deoplete#sources#swift#daemon_autostart = 1
" }}}
  • statusline
" statusline ------------------------------- {{{
set statusline=...%{MyGitStatus()}...

function MyGitStatus() abort
  let staged = gina#component#status#staged()
  let unstaged = gina#component#status#unstaged()
  let conflicted = gina#component#status#conflicted()
  let branched = gina#component#repo#branch()
  return printf(
        \ 'b: %s, s: %s, u: %s, c: %s',
        \ branched,
        \ staged,
        \ unstaged,
        \ conflicted,
        \)
endfunction
"set statusline=
set statusline=[%n]\                           "buffernr
set statusline+=%<%F\                          "File+path
set statusline+=%y\                            "FileType
set statusline+=%{ALEGetStatusLine()}\         "ale status"
"set statusline+=%{fugitive#statusline()}\      "fugitive#statusline"
set statusline+=%{MyGitStatus()}
set statusline+=%=                             "right alignment
set statusline+=[%{strlen(&fenc)?&fenc:&enc}]  "file encoding
set statusline+=%-16(\ %l,%c-%v\ %)\%P         "cursor row, col, Percentage
" }}}

swiftrun shell script

~/bin/swiftrun

#!/usr/bin/env bash
set -e
err() {
    echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
declare -r execname=.build/debug/$(basename $(pwd))
if [[ -f ${execname} ]]; then
   rm -f ${execname}
fi

declare -r result=$(swift build)
if [[ -z $(swift build) ]]; then
  echo "build success!"
  if    [[ -f ${execname} ]]; then
    echo "running ${execname}"
    echo
    ${execname}
  fi
else
  err "retun ${result}"
fi

~/bin/swifttest

#!/usr/bin/env bash
swift test
#

shortcuts

on vim

:help split

C-f PageDown
C-b PageUp
C-w |
C-w =
C-w r

Related articles