From 2a35a3511bc3d74e7e41014362f432a5f02a70e2 Mon Sep 17 00:00:00 2001 From: Bogdan Manole Date: Thu, 14 Jun 2018 18:23:46 +0300 Subject: [PATCH 1/2] Add --quiet to autocomplete and get_type --- autoload/flowcomplete.vim | 2 +- autoload/flowcomplete.vim._old | 74 ++++++++++++ plugin/flow.vim | 2 +- plugin/flow.vim._old | 205 +++++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 autoload/flowcomplete.vim._old create mode 100644 plugin/flow.vim._old diff --git a/autoload/flowcomplete.vim b/autoload/flowcomplete.vim index 30ee293..1c76774 100644 --- a/autoload/flowcomplete.vim +++ b/autoload/flowcomplete.vim @@ -34,7 +34,7 @@ function! flowcomplete#Complete(findstart, base) " Pass the buffer to flow. let buffer = join(lines, "\n") - let command = g:flow#flowpath.' autocomplete "'.expand('%:p').'"' + let command = g:flow#flowpath.' autocomplete --quiet "'.expand('%:p').'"' let result = system(command, buffer) if result =~ '^Error: not enough type information to autocomplete' || diff --git a/autoload/flowcomplete.vim._old b/autoload/flowcomplete.vim._old new file mode 100644 index 0000000..30ee293 --- /dev/null +++ b/autoload/flowcomplete.vim._old @@ -0,0 +1,74 @@ +" Vim completion script +" +" This source code is licensed under the BSD-style license found in the +" LICENSE file in the toplevel directory of this source tree. An additional +" grant of patent rights can be found in the PATENTS file in the same +" directory. + +" Magical flow autocomplete token. +let s:autotok = 'AUTO332' + +" Omni findstart phase. +function! s:FindStart() + let line = getline('.') + let start = col('.') - 1 + + while start >= 0 && line[start - 1] =~ '[a-zA-Z_0-9\x7f-\xff$]' + let start -= 1 + endwhile + return start +endfunction + +function! flowcomplete#Complete(findstart, base) + if a:findstart + return s:FindStart() + endif + + let lnum = line('.') + let cnum = col('.') + let lines = getline(1, '$') + + " Insert the base and magic token into the current line. + let curline = lines[lnum - 1] + let lines[lnum - 1] = curline[:cnum - 1] . a:base . s:autotok . curline[cnum :] + + " Pass the buffer to flow. + let buffer = join(lines, "\n") + let command = g:flow#flowpath.' autocomplete "'.expand('%:p').'"' + let result = system(command, buffer) + + if result =~ '^Error: not enough type information to autocomplete' || + \ result =~ '^Could not find file or directory' + return [] + endif + + let matches = [] + + " Parse the flow output. + for line in split(result, "\n") + if empty(line) | continue | endif + + let entry = {} + let space = stridx(line, ' ') + let word = line[:space - 1] + let type = line[space + 1 :] + + " Skip matches that don't start with the base" + if (stridx(word, a:base) != 0) | continue | endif + + " This is pretty hacky. We're using regexes to recognize the different + " kind of matches. Really in the future we should somehow consume the json + " output + if type =~ '^(.*) =>' + let entry = { 'word': word, 'kind': a:base, 'menu': type } + elseif type =~ '^[class:' + let entry = { 'word': word, 'kind': 'c', 'menu': type } + else + let entry = { 'word': word, 'kind': 'v', 'menu': type } + endif + + call add(matches, entry) + endfor + + return matches +endfunction diff --git a/plugin/flow.vim b/plugin/flow.vim index 73ff2a7..c49d67f 100644 --- a/plugin/flow.vim +++ b/plugin/flow.vim @@ -104,7 +104,7 @@ endfunction function! flow#get_type() let pos = line('.').' '.col('.') let path = ' --path '.fnameescape(expand('%')) - let cmd = g:flow#flowpath.' type-at-pos '.pos.path + let cmd = g:flow#flowpath.' type-at-pos --quiet '.pos.path let stdin = join(getline(1,'$'), "\n") let output = 'FlowType: '.system(cmd, stdin) diff --git a/plugin/flow.vim._old b/plugin/flow.vim._old new file mode 100644 index 0000000..73ff2a7 --- /dev/null +++ b/plugin/flow.vim._old @@ -0,0 +1,205 @@ +" flow.vim - Flow typechecker integration for vim + +if exists("g:loaded_flow") + finish +endif +let g:loaded_flow = 1 + +" Configuration switches: +" - enable: Typechecking is done on :w. +" - autoclose: Quickfix window closes automatically when there are no errors. +" - errjmp: Jump to errors after typechecking; default off. +" - qfsize: Let the plugin control the quickfix window size. +" - flowpath: Path to the flow executable - default is flow in path +" - showquickfix Show the quickfix window +if !exists("g:flow#enable") + let g:flow#enable = 1 +endif +if !exists("g:flow#autoclose") + let g:flow#autoclose = 0 +endif +if !exists("g:flow#errjmp") + let g:flow#errjmp = 0 +endif +if !exists("g:flow#qfsize") + let g:flow#qfsize = 1 +endif +if !exists("g:flow#flowpath") + let g:flow#flowpath = "flow" +endif +if !exists("g:flow#timeout") + let g:flow#timeout = 2 +endif +if !exists("g:flow#showquickfix") + let g:flow#showquickfix = 1 +endif + +" Require the flow executable. +if !executable(g:flow#flowpath) + finish +endif + +" flow error format. +let s:flow_errorformat = '%EFile "%f"\, line %l\, characters %c-%.%#,%Z%m,' +" flow from editor. +let s:flow_from = '--from vim' + + +" Call wrapper for flow. +function! FlowClientCall(cmd, suffix, ...) + " Invoke typechecker. + " We also concatenate with the empty string because otherwise + " cgetexpr complains about not having a String argument, even though + " type(flow_result) == 1. + let command = g:flow#flowpath.' '.a:cmd.' '.s:flow_from.' '.a:suffix + + let flow_result = a:0 > 0 ? system(command, a:1) : system(command) + + " Handle the server still initializing + if v:shell_error == 1 + echohl WarningMsg + echomsg 'Flow server is still initializing...' + echohl None + cclose + return 0 + endif + + " Handle timeout + if v:shell_error == 3 + echohl WarningMsg + echomsg 'Flow timed out, please try again!' + echohl None + cclose + return 0 + endif + + return flow_result +endfunction + +" Main interface functions. +function! flow#typecheck() + " Flow current outputs errors to stderr and gets fancy with single character + " files + let flow_result = FlowClientCall('--timeout '.g:flow#timeout.' --retry-if-init false "'.expand('%:p').'"', '2> /dev/null') + let old_fmt = &errorformat + let &errorformat = s:flow_errorformat + + if g:flow#errjmp + cexpr flow_result + else + cgetexpr flow_result + endif + + if g:flow#showquickfix + if g:flow#autoclose + botright cwindow + else + botright copen + endif + endif + let &errorformat = old_fmt +endfunction + +" Get the Flow type at the current cursor position. +function! flow#get_type() + let pos = line('.').' '.col('.') + let path = ' --path '.fnameescape(expand('%')) + let cmd = g:flow#flowpath.' type-at-pos '.pos.path + let stdin = join(getline(1,'$'), "\n") + + let output = 'FlowType: '.system(cmd, stdin) + let output = substitute(output, '\n$', '', '') + echo output +endfunction + +" Toggle auto-typecheck. +function! flow#toggle() + if g:flow#enable + let g:flow#enable = 0 + else + let g:flow#enable = 1 + endif +endfunction + +" Jump to Flow definition for the current cursor position +function! flow#jump_to_def() + let pos = line('.').' '.col('.') + let path = ' --path '.fnameescape(expand('%')) + let stdin = join(getline(1,'$'), "\n") + let flow_result = FlowClientCall('get-def --quiet '.pos.path, '', stdin) + " Output format is: + " File: "/path/to/file", line 1, characters 1-11 + + " Flow returns a single line-feed if no result + if strlen(flow_result) == 1 + echo 'No definition found' + return 1 + endif + + let parts = split(flow_result, ",") + if len(parts) < 2 + echo 'cannot find definition' + return 1 + endif + + " File: "/path/to/file" => /path/to/file + let file = substitute(substitute(parts[0], '"', '', 'g'), 'File ', '', '') + + " line 1 => 1 + let row = split(parts[1], " ")[1] + + " characters 1-11 => 1 + let col = 0 + if len(parts) == 3 + let col = split(split(parts[2], " ")[1], "-")[0] + endif + + " File - means current file + if filereadable(file) || file == '-' + if file != '-' + execute 'edit' file + endif + call cursor(row, col) + end +endfunction + +" Open importers of current file in quickfix window +function! flow#get_importers() + let flow_result = FlowClientCall('get-importers "'.expand('%').'" --strip-root', '') + let importers = split(flow_result, '\n')[1:1000] + + let l:flow_errorformat = '%f' + let old_fmt = &errorformat + let &errorformat = l:flow_errorformat + + if g:flow#errjmp + cexpr importers + else + cgetexpr importers + endif + + if g:flow#autoclose + botright cwindow + else + botright copen + endif + let &errorformat = old_fmt +endfunction + + +" Commands and auto-typecheck. +command! FlowToggle call flow#toggle() +command! FlowMake call flow#typecheck() +command! FlowType call flow#get_type() +command! FlowJumpToDef call flow#jump_to_def() +command! FlowGetImporters call flow#get_importers() + +au BufWritePost *.js,*.jsx if g:flow#enable | call flow#typecheck() | endif + + +" Keep quickfix window at an adjusted height. +function! AdjustWindowHeight(minheight, maxheight) + exe max([min([line("$"), a:maxheight]), a:minheight]) . "wincmd _" +endfunction + +au FileType qf if g:flow#qfsize | call AdjustWindowHeight(3, 10) | endif From 29802bcd003f53c951cd1c3d60b26fc065ef114f Mon Sep 17 00:00:00 2001 From: Bogdan Manole Date: Thu, 14 Jun 2018 18:33:25 +0300 Subject: [PATCH 2/2] Add --quiet to autocomplete and get_type --- autoload/flowcomplete.vim._old | 74 ------------ plugin/flow.vim._old | 205 --------------------------------- 2 files changed, 279 deletions(-) delete mode 100644 autoload/flowcomplete.vim._old delete mode 100644 plugin/flow.vim._old diff --git a/autoload/flowcomplete.vim._old b/autoload/flowcomplete.vim._old deleted file mode 100644 index 30ee293..0000000 --- a/autoload/flowcomplete.vim._old +++ /dev/null @@ -1,74 +0,0 @@ -" Vim completion script -" -" This source code is licensed under the BSD-style license found in the -" LICENSE file in the toplevel directory of this source tree. An additional -" grant of patent rights can be found in the PATENTS file in the same -" directory. - -" Magical flow autocomplete token. -let s:autotok = 'AUTO332' - -" Omni findstart phase. -function! s:FindStart() - let line = getline('.') - let start = col('.') - 1 - - while start >= 0 && line[start - 1] =~ '[a-zA-Z_0-9\x7f-\xff$]' - let start -= 1 - endwhile - return start -endfunction - -function! flowcomplete#Complete(findstart, base) - if a:findstart - return s:FindStart() - endif - - let lnum = line('.') - let cnum = col('.') - let lines = getline(1, '$') - - " Insert the base and magic token into the current line. - let curline = lines[lnum - 1] - let lines[lnum - 1] = curline[:cnum - 1] . a:base . s:autotok . curline[cnum :] - - " Pass the buffer to flow. - let buffer = join(lines, "\n") - let command = g:flow#flowpath.' autocomplete "'.expand('%:p').'"' - let result = system(command, buffer) - - if result =~ '^Error: not enough type information to autocomplete' || - \ result =~ '^Could not find file or directory' - return [] - endif - - let matches = [] - - " Parse the flow output. - for line in split(result, "\n") - if empty(line) | continue | endif - - let entry = {} - let space = stridx(line, ' ') - let word = line[:space - 1] - let type = line[space + 1 :] - - " Skip matches that don't start with the base" - if (stridx(word, a:base) != 0) | continue | endif - - " This is pretty hacky. We're using regexes to recognize the different - " kind of matches. Really in the future we should somehow consume the json - " output - if type =~ '^(.*) =>' - let entry = { 'word': word, 'kind': a:base, 'menu': type } - elseif type =~ '^[class:' - let entry = { 'word': word, 'kind': 'c', 'menu': type } - else - let entry = { 'word': word, 'kind': 'v', 'menu': type } - endif - - call add(matches, entry) - endfor - - return matches -endfunction diff --git a/plugin/flow.vim._old b/plugin/flow.vim._old deleted file mode 100644 index 73ff2a7..0000000 --- a/plugin/flow.vim._old +++ /dev/null @@ -1,205 +0,0 @@ -" flow.vim - Flow typechecker integration for vim - -if exists("g:loaded_flow") - finish -endif -let g:loaded_flow = 1 - -" Configuration switches: -" - enable: Typechecking is done on :w. -" - autoclose: Quickfix window closes automatically when there are no errors. -" - errjmp: Jump to errors after typechecking; default off. -" - qfsize: Let the plugin control the quickfix window size. -" - flowpath: Path to the flow executable - default is flow in path -" - showquickfix Show the quickfix window -if !exists("g:flow#enable") - let g:flow#enable = 1 -endif -if !exists("g:flow#autoclose") - let g:flow#autoclose = 0 -endif -if !exists("g:flow#errjmp") - let g:flow#errjmp = 0 -endif -if !exists("g:flow#qfsize") - let g:flow#qfsize = 1 -endif -if !exists("g:flow#flowpath") - let g:flow#flowpath = "flow" -endif -if !exists("g:flow#timeout") - let g:flow#timeout = 2 -endif -if !exists("g:flow#showquickfix") - let g:flow#showquickfix = 1 -endif - -" Require the flow executable. -if !executable(g:flow#flowpath) - finish -endif - -" flow error format. -let s:flow_errorformat = '%EFile "%f"\, line %l\, characters %c-%.%#,%Z%m,' -" flow from editor. -let s:flow_from = '--from vim' - - -" Call wrapper for flow. -function! FlowClientCall(cmd, suffix, ...) - " Invoke typechecker. - " We also concatenate with the empty string because otherwise - " cgetexpr complains about not having a String argument, even though - " type(flow_result) == 1. - let command = g:flow#flowpath.' '.a:cmd.' '.s:flow_from.' '.a:suffix - - let flow_result = a:0 > 0 ? system(command, a:1) : system(command) - - " Handle the server still initializing - if v:shell_error == 1 - echohl WarningMsg - echomsg 'Flow server is still initializing...' - echohl None - cclose - return 0 - endif - - " Handle timeout - if v:shell_error == 3 - echohl WarningMsg - echomsg 'Flow timed out, please try again!' - echohl None - cclose - return 0 - endif - - return flow_result -endfunction - -" Main interface functions. -function! flow#typecheck() - " Flow current outputs errors to stderr and gets fancy with single character - " files - let flow_result = FlowClientCall('--timeout '.g:flow#timeout.' --retry-if-init false "'.expand('%:p').'"', '2> /dev/null') - let old_fmt = &errorformat - let &errorformat = s:flow_errorformat - - if g:flow#errjmp - cexpr flow_result - else - cgetexpr flow_result - endif - - if g:flow#showquickfix - if g:flow#autoclose - botright cwindow - else - botright copen - endif - endif - let &errorformat = old_fmt -endfunction - -" Get the Flow type at the current cursor position. -function! flow#get_type() - let pos = line('.').' '.col('.') - let path = ' --path '.fnameescape(expand('%')) - let cmd = g:flow#flowpath.' type-at-pos '.pos.path - let stdin = join(getline(1,'$'), "\n") - - let output = 'FlowType: '.system(cmd, stdin) - let output = substitute(output, '\n$', '', '') - echo output -endfunction - -" Toggle auto-typecheck. -function! flow#toggle() - if g:flow#enable - let g:flow#enable = 0 - else - let g:flow#enable = 1 - endif -endfunction - -" Jump to Flow definition for the current cursor position -function! flow#jump_to_def() - let pos = line('.').' '.col('.') - let path = ' --path '.fnameescape(expand('%')) - let stdin = join(getline(1,'$'), "\n") - let flow_result = FlowClientCall('get-def --quiet '.pos.path, '', stdin) - " Output format is: - " File: "/path/to/file", line 1, characters 1-11 - - " Flow returns a single line-feed if no result - if strlen(flow_result) == 1 - echo 'No definition found' - return 1 - endif - - let parts = split(flow_result, ",") - if len(parts) < 2 - echo 'cannot find definition' - return 1 - endif - - " File: "/path/to/file" => /path/to/file - let file = substitute(substitute(parts[0], '"', '', 'g'), 'File ', '', '') - - " line 1 => 1 - let row = split(parts[1], " ")[1] - - " characters 1-11 => 1 - let col = 0 - if len(parts) == 3 - let col = split(split(parts[2], " ")[1], "-")[0] - endif - - " File - means current file - if filereadable(file) || file == '-' - if file != '-' - execute 'edit' file - endif - call cursor(row, col) - end -endfunction - -" Open importers of current file in quickfix window -function! flow#get_importers() - let flow_result = FlowClientCall('get-importers "'.expand('%').'" --strip-root', '') - let importers = split(flow_result, '\n')[1:1000] - - let l:flow_errorformat = '%f' - let old_fmt = &errorformat - let &errorformat = l:flow_errorformat - - if g:flow#errjmp - cexpr importers - else - cgetexpr importers - endif - - if g:flow#autoclose - botright cwindow - else - botright copen - endif - let &errorformat = old_fmt -endfunction - - -" Commands and auto-typecheck. -command! FlowToggle call flow#toggle() -command! FlowMake call flow#typecheck() -command! FlowType call flow#get_type() -command! FlowJumpToDef call flow#jump_to_def() -command! FlowGetImporters call flow#get_importers() - -au BufWritePost *.js,*.jsx if g:flow#enable | call flow#typecheck() | endif - - -" Keep quickfix window at an adjusted height. -function! AdjustWindowHeight(minheight, maxheight) - exe max([min([line("$"), a:maxheight]), a:minheight]) . "wincmd _" -endfunction - -au FileType qf if g:flow#qfsize | call AdjustWindowHeight(3, 10) | endif