跳至主要内容

Vim

Flow 的编辑器集成主要通过 语言服务器协议 实现。有 许多 Vim LSP 客户端 可供选择,例如 ALE

ALE

Vim 8+ 和 NeoVim 的异步 Lint 引擎 (ALE) 插件,vim-ale,是一个通用的代码风格检查引擎,支持 Flow 和许多其他工具。

安装

按照 ALE 自述文件中的 说明 进行操作。

配置 ALE 以对 JavaScript 文件使用 flow-language-server 代码风格检查器

" In ~/.vim/ftplugin/javascript.vim, or somewhere similar.

" Enables only Flow for JavaScript. See :ALEInfo for a list of other available
" linters. NOTE: the `flow` linter uses an old API; prefer `flow-language-server`.
let b:ale_linters = ['flow-language-server']

" Or in ~/.vim/vimrc:
let g:ale_linters = {
\ 'javascript': ['flow-language-server'],
\}

coc.nvim-neovim

Coc 是 vim8 & neovim 的智能感知引擎。

设置

set nocompatible
filetype off

" install coc.nvim using Plug or preffered plugin manager
call plug#begin('~/.vim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

filetype plugin indent on

" ======= coc settings
set updatetime=300
set shortmess+=c

" Use leader T to show documentation in preview window
nnoremap <leader>t :call <SID>show_documentation()<CR>


function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('&lt;cword&gt;')
else
call CocAction('doHover')
endif
endfunction

" instead of having ~/.vim/coc-settings.json
let s:LSP_CONFIG = {
\ 'flow': {
\ 'command': exepath('flow'),
\ 'args': ['lsp'],
\ 'filetypes': ['javascript', 'javascriptreact'],
\ 'initializationOptions': {},
\ 'requireRootPattern': 1,
\ 'settings': {},
\ 'rootPatterns': ['.flowconfig']
\ }
\}

let s:languageservers = {}
for [lsp, config] in items(s:LSP_CONFIG)
let s:not_empty_cmd = !empty(get(config, 'command'))
if s:not_empty_cmd | let s:languageservers[lsp] = config | endif
endfor

if !empty(s:languageservers)
call coc#config('languageserver', s:languageservers)
endif

LanguageClient-neovim

在 Vim 中添加对 Flow 支持的另一种方法是使用 LanguageClient-neovim

  • 支持 vim 8 和 neovim
  • 将补全添加到 omnifunc
  • 在保存时检查 JavaScript 文件中的类型错误
  • 查找光标下的类型

要求

  • 要求安装 Flow 并将其添加到您的路径中。
  • 要求包含 JavaScript 文件的项目使用 flow init 初始化。
  • 要求 JavaScript 文件在顶部使用 / @flow / 标记。

Pathogen

cd ~/.vim/bundle
git clone git://github.com/autozimu/LanguageClient-neovim.git

NeoBundle

将此添加到您的 ~/.vimrc

  NeoBundleLazy 'autozimu/LanguageClient-neovim', {
\ 'autoload': {
\ 'filetypes': 'javascript'
\ }}

使用 flow-bin 进行 Flow 构建步骤

  NeoBundleLazy 'autozimu/LanguageClient-neovim', {
\ 'autoload': {
\ 'filetypes': 'javascript'
\ },
\ 'build': {
\ 'mac': 'npm install -g flow-bin',
\ 'unix': 'npm install -g flow-bin'
\ }}

VimPlug

  Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh && npm install -g flow-bin',
\ }

设置

let g:LanguageClient_rootMarkers = {
\ 'javascript': ['.flowconfig', 'package.json']
\ }
let g:LanguageClient_serverCommands={
\ 'javascript': ['flow', 'lsp'],
\ 'javascript.jsx': ['flow', 'lsp']
\}

" check the type under cursor w/ leader T
nnoremap <leader>t :call LanguageClient_textDocument_hover()<CR>
nnoremap <leader>y :call LanguageClient_textDocument_definition()<CR>