LoginSignup
2
2

More than 1 year has passed since last update.

Nim言語で開発するときのNeovimの設定

Last updated at Posted at 2022-09-20

はじめに

この記事はNimアドベントカレンダー20229日目の記事です。

NeovimでNimを書くときのプラグイン設定メモを書き下します。

Nimのインストール

choosenimというNimの仮想環境構築ツールがあります。 1 2

$ curl https://nim-lang.org/choosenim/init.sh -sSf -o /tmp/init.sh &&\
    chmod +x /tmp/init.sh &&\
    /tmp/init.sh -y &&\
    rm /tmp/init.sh
$ export PATH="/home/u1and0/.nimble/bin:${PATH}"
$ choosenim -y stable

nimlsp

Languageサーバー入れたいが、どうなっていたか...3

$ sudo pacman -Syu --noconfirm go zeromq &&\
    go install github.com/mattn/efm-langserver@latest
$ nimble refresh &&\
    nimble -y install nimlsp inim jupyternim

pacman -S nim でも一式入ってくる(nimgrep nimsuggest 等)
nimble install nimlsp は用意されているが、pacmanでnimをインストールしたから?
nimlspのインストールに失敗する。
choosenimのほうがいいのか。

Jupyter Notebookと連携

Zeromqが必要らしい?45

Instal Nim language sever & Repl
Zeromq いる?? jupyternimと一緒に同梱されているみたい
Installing zmq@>= 1.2.1 & < 1.3.0
Downloading https://github.com/nim-lang/nim-zmq using git
Verifying dependencies for zmq@1.2.1
Installing zmq@1.2.1
Success: zmq installed successfully.

autocmd

autocmdの設定を書きます。

.config/nvim/autocmd.rc.vim
if executable("nim")
    augroup MyNimCmd
        " nimファイル開いたときにfiletypeを勝手にnimにしてくれないのでわざわざautocmdで書く
        autocmd BufNewFile,BufRead *.nim,*.nimble set filetype=nim
        " 保存するたびにフォーマッタによる整形
        autocmd BufWritePost *.nim,*.nimble !nimpretty %
        " 実行
        autocmd FileType nim command! -nargs=* NimRun :sp <Bar> term nim c -r <args> %
        " ビルド
        autocmd FileType nim command! -nargs=* NimBuild :sp <Bar> term nim c <args> %
        " ライブラリとしてビルド(Pythonとの連携なんかに使う)
        autocmd FileType nim command! -nargs=* NimBuildLib :!nim c --tlsEmulation:off --app:lib --out:%:t:r.so <args> %
        " Space + R で実行
        autocmd FileType nim nnoremap <buffer> <Leader>r :NimRun<CR>
        " Space + B でビルド
        autocmd FileType nim nnoremap <buffer> <Leader>b :NimBuild<CR>
    augroup END
endif

NimをインストールしたときにNim開発のサポートツール6が入っているので、シンタックスハイライト以外のことは、autocmdの設定次第で何とかなりそうです。

deinプラグイン

deinでインストールするプラグインです。
tomlファイルで管理しています。

.config/dein/lazy.toml
# Nim lang plugins
[[plugins]]
repo = 'zah/nim.vim'
on_ft = ['nim', 'nimble']
if = '''executable('nim')'''''
hook_add = '''
fun! JumpToDef()
 if exists("*GotoDefinition_" . &filetype)
     call GotoDefinition_{&filetype}()
  else
    exe "norm! \<C-]>"
  endif
endf
" Jump to tag
nn <M-g> :call JumpToDef()<cr>
ino <M-g> <esc>:call JumpToDef()<cr>
'''

[[plugins]]
repo = 'prabirshrestha/asyncomplete.vim'
hook_add = '''
let g:asyncomplete_smart_completion = 1
let g:asyncomplete_auto_popup = 1
'''

[[plugins]]
repo = 'alaviss/nim.nvim'
on_ft = ['nim', 'nimble']
hook_add = '''
au User asyncomplete_setup call asyncomplete#register_source({
  \ 'name': 'nim',
  \ 'whitelist': ['nim'],
  \ 'completor': {opt, ctx -> nim#suggest#sug#GetAllCandidates(
  \     {start, candidates -> asyncomplete#complete(opt['name'], ctx, start, candidates)})}
  \ })
'''
  1. 'zah/nim.vim'7はシンタックスハイライトと自動インデント。
  2. 'alaviss/nim.nvim'8は補完。
  3. 'prabirshrestha/asyncomplete.vim'9は 'alaviss/nim.nvim'の動作に必要です。

Kでヘルプホバーが出てくれたり出てくれなかったりすることがあります。
やや補完が効きすぎで打ちにくいですが、 補完自体は速いので、ハマるとサクサク打てて気持ちが良いです。
Definitionはnimsuggestでジャンプしてくれるようですが、ジャンプしてくれてたりしてくれなかったりすることがあります。

各プラグインのREADMEをさっと読んだくらいで、細かくいじってはいません。

  1. choosenimでnim環境構築

  2. choosenim

  3. nimlsp

  4. INim

  5. JupyternotebookでNimを使い,グラフの可視化まで

  6. ~/.nimble/bin下にあるこのへんだろうか nimgrep, nimlsp, nimpretty, nimsuggest

  7. https://github.com/zah/nim.vim

  8. https://github.com/alaviss/nim.nvim

  9. https://github.com/prabirshrestha/asyncomplete.vim

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2