@@ -24,34 +24,53 @@ endif
2424if ! exists (" g:flow#qfsize" )
2525 let g: flow #qfsize = 1
2626endif
27- if ! exists (" g:flow#flowpath" )
28- let g: flow #flowpath = " flow"
29- endif
3027if ! exists (" g:flow#timeout" )
3128 let g: flow #timeout = 2
3229endif
3330if ! exists (" g:flow#showquickfix" )
3431 let g: flow #showquickfix = 1
3532endif
3633
37- " Require the flow executable.
38- if ! executable (g: flow #flowpath)
39- finish
40- endif
41-
4234" flow error format.
4335let s: flow_errorformat = ' %EFile "%f"\, line %l\, characters %c-%.%#,%Z%m,'
4436" flow from editor.
4537let s: flow_from = ' --from vim'
4638
39+ function ! <SID> GetFlowExecutable ()
40+ if exists (" g:flow#flowpath" )
41+ return g: flow #flowpath
42+ else
43+ " Search for a local version of flow
44+ let s: npm_local_flowpath = finddir (" node_modules" , " .;" ) . " /.bin/flow"
45+ if filereadable (s: npm_local_flowpath )
46+ return s: npm_local_flowpath
47+ else
48+ " fallback to global instance
49+ return " flow"
50+ endif
51+ endif
52+ endfunction
53+
54+ function ! flow#SaveGetFlowExecutable ()
55+ let a: flow_executable = <SID> GetFlowExecutable ()
56+ if ! executable (a: flow_executable )
57+ echohl WarningMsg
58+ echomsg ' No Flow executable found.'
59+ echohl None
60+ finish
61+ else
62+ return a: flow_executable
63+ endif
64+ endfunction
65+
4766
4867" Call wrapper for flow.
4968function ! <SID> FlowClientCall (cmd, suffix, ... )
5069 " Invoke typechecker.
5170 " We also concatenate with the empty string because otherwise
5271 " cgetexpr complains about not having a String argument, even though
5372 " type(flow_result) == 1.
54- let command = g: flow #flowpath .' ' .a: cmd .' ' .s: flow_from .' ' .a: suffix
73+ let command = flow#SaveGetFlowExecutable () .' ' .a: cmd .' ' .s: flow_from .' ' .a: suffix
5574
5675 let flow_result = a: 0 > 0 ? system (command , a: 1 ) : system (command )
5776
@@ -104,7 +123,7 @@ endfunction
104123function ! flow#get_type ()
105124 let pos = line (' .' ).' ' .col (' .' )
106125 let path = ' --path ' .fnameescape (expand (' %' ))
107- let cmd = g: flow #flowpath .' type-at-pos ' .pos.path
126+ let cmd = flow#SaveGetFlowExecutable () .' type-at-pos ' .pos.path
108127 let stdin = join (getline (1 ,' $' ), " \n " )
109128
110129 let output = ' FlowType: ' .system (cmd, stdin)
0 commit comments