1. 程式人生 > >Vim之程式碼非同步檢測外掛 ALE -- 實時檢查verilog等程式碼的正確性

Vim之程式碼非同步檢測外掛 ALE -- 實時檢查verilog等程式碼的正確性

Vim之程式碼非同步檢測外掛 ALE

前言

知名的 vim 程式碼檢測外掛主要是兩個

  • syntastic
  • neomake
  • ALE

ALE 雖是後起之秀,但目前是功能最強大的一個

  • 實時檢測。為了讓程式碼可以在編輯時進行實時的檢測,ale 的執行方式是將程式碼做為 stdin 匯入檢測工具(不支援的話使用臨時檔案),這樣做的好處是我們可以更早的發現錯誤。
  • 併發執行。ale 預設使用所有可用的檢測工具併發執行檢測,譬如說我們有時需要同時對 javascript 執行 eslint 以及 jscs。
  • 標識欄、狀態列以及命令列訊息支援。

安裝

Vim 8 on Unix

mkdir -p ~/.vim/pack/git-plugins/start
git clone https://github.com/w0rp/ale.git ~/.vim/pack/git-plugins/start/ale

NeoVim on Unix

mkdir -p ~/.local/share/nvim/site/pack/git-plugins/start
git clone https://github.com/w0rp/ale.git ~/.local/share/nvim/site/pack/git-plugins/start/ale

Vim 8 on Windows

## Run these commands in the "Git for Windows" Bash terminal
mkdir -p ~/vimfiles/pack/git-plugins/start
git clone https://github.com/w0rp/ale.git ~/vimfiles/pack/git-plugins/start/ale

Vundle

把下面行加入到,vimrc

Plugin 'w0rp/ale'

Linter

ale 的 linter 都要自己安裝還好系統一般都是有 gcc, python, gofmt 之類的需要額外安裝的大約有

  • vint: vimscript
  • mdl: markdown
  • iverilog: verilog

vint

安裝方法如下:

pip3 install vim-vint

mdl

安裝方法如下:

gem install mdl

iverilog

安裝 gperf, 下載原始碼: 連結

./configure && make && make install

下載 iverilog 原始碼:

git clone https://github.com/steveicarus/iverilog.git
cd iverilog
./autoconf.sh
./configure && make && make isntall

配置使用

"-----------------------------------------------------------------------------
" plugin - ale.vim
"-----------------------------------------------------------------------------
"keep the sign gutter open
let g:ale_sign_column_always = 1
let g:ale_sign_error = '>>'
let g:ale_sign_warning = '--'

" show errors or warnings in my statusline
let g:airline#extensions#ale#enabled = 1

" self-define statusline
"function! LinterStatus() abort
"    let l:counts = ale#statusline#Count(bufnr(''))
"
"    let l:all_errors = l:counts.error + l:counts.style_error
"    let l:all_non_errors = l:counts.total - l:all_errors
"
"    return l:counts.total == 0 ? 'OK' : printf(
"    \  '%dW %dE',
"    \  all_non_errors,
"    \  all_errors
"    \)
"endfunction
"set statusline=%{LinterStatus()}

" echo message
" %s is the error message itself
" %linter% is the linter name
" %severity is the severity type
" let g:ale_echo_msg_error_str = 'E'
" let g:ale_echo_msg_warning_str = 'W'
" let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'

" use quickfix list instead of the loclist
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 1

" only enable these linters
"let g:ale_linters = {
"\    'javascript': ['eslint']
"\}

nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-J> <Plug>(ale_next_wrap)

" run lint only on saving a file
" let g:ale_lint_on_text_changed = 'never'
" dont run lint on opening a file
" let g:ale_lint_on_enter = 0

"------------------------END ale.vim--------------------------------------

效果

a1a4db88-b29a-4b08-998d-29fd9f706d54可能看到因為第69,70,71, 73行的幾個模組定義沒有提供,所以左邊線上有紅色的>>游標定位到73行, 在下面命令列會給出具體的錯誤:

Unknown module type: pmu

總結

ALE可以讓你一邊編碼一邊實時檢查程式碼的語法問題,同時還完全不影響vim的效能。這可以極大提升你程式碼輸寫的正確性。