From 7048c8cc4a821a7b6d99f3ef8ff5da3dc69dd62e Mon Sep 17 00:00:00 2001 From: mitermayer Date: Wed, 2 May 2018 22:33:06 -0700 Subject: Refactoring executable resolver out into its own module - Refactoring the executable resolver into its own module for making it easier to test --- autoload/prettier.vim | 89 ++------------------------------------------------- 1 file changed, 3 insertions(+), 86 deletions(-) (limited to 'autoload/prettier.vim') diff --git a/autoload/prettier.vim b/autoload/prettier.vim index 2708caa..c6b89fd 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -16,7 +16,7 @@ let s:prettier_job_running = 0 let s:prettier_quickfix_open = 0 function! prettier#PrettierCliPath() abort - let l:execCmd = s:Get_Prettier_Exec() + let l:execCmd = prettier#resolver#executable#getPath() if l:execCmd != -1 echom l:execCmd @@ -26,7 +26,7 @@ function! prettier#PrettierCliPath() abort endfunction function! prettier#PrettierCli(user_input) abort - let l:execCmd = s:Get_Prettier_Exec() + let l:execCmd = prettier#resolver#executable#getPath() if l:execCmd != -1 let l:out = system(l:execCmd. ' ' . a:user_input) @@ -37,7 +37,7 @@ function! prettier#PrettierCli(user_input) abort endfunction function! prettier#Prettier(...) abort - let l:execCmd = s:Get_Prettier_Exec() + let l:execCmd = prettier#resolver#executable#getPath() let l:async = a:0 > 0 ? a:1 : 0 let l:startSelection = a:0 > 1 ? a:2 : 1 let l:endSelection = a:0 > 2 ? a:3 : line('$') @@ -341,89 +341,6 @@ function! s:Get_Prettier_Exec_Args(config) abort return l:cmd endfunction -" By default we will search for the following -" => user defined prettier cli path from vim configuration file -" => locally installed prettier inside node_modules on any parent folder -" => globally installed prettier -" => vim-prettier prettier installation -" => if all fails suggest install -function! s:Get_Prettier_Exec() abort - let l:user_defined_exec_path = fnamemodify(g:prettier#exec_cmd_path, ':p') - if executable(l:user_defined_exec_path) - return l:user_defined_exec_path - endif - - let l:local_exec = s:Get_Prettier_Local_Exec() - if executable(l:local_exec) - return fnameescape(l:local_exec) - endif - - let l:global_exec = s:Get_Prettier_Global_Exec() - if executable(l:global_exec) - return fnameescape(l:global_exec) - endif - - let l:plugin_exec = s:Get_Prettier_Plugin_Exec() - if executable(l:plugin_exec) - return fnameescape(l:plugin_exec) - endif - - return -1 -endfunction - -function! s:Get_Prettier_Local_Exec() abort - return s:Get_Exec(getcwd()) -endfunction - -function! s:Get_Prettier_Global_Exec() abort - return s:Get_Exec() -endfunction - -function! s:Get_Prettier_Plugin_Exec() abort - return s:Get_Exec(s:root_dir) -endfunction - -function! s:Get_Exec(...) abort - let l:rootDir = a:0 > 0 ? a:1 : 0 - let l:exec = -1 - - if isdirectory(l:rootDir) - let l:dir = s:Traverse_Dir_Search(l:rootDir) - if l:dir != -1 - let l:exec = s:Get_Path_To_Exec(l:dir) - endif - else - let l:exec = s:Get_Path_To_Exec() - endif - - return l:exec -endfunction - -function! s:Get_Path_To_Exec(...) abort - let l:rootDir = a:0 > 0 ? a:1 : -1 - let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : '' - return l:dir . 'prettier' -endfunction - -function! s:Traverse_Dir_Search(rootDir) abort - let l:root = a:rootDir - let l:dir = 'node_modules' - - while 1 - let l:search_dir = l:root . '/' . l:dir - if isdirectory(l:search_dir) - return l:search_dir - endif - - let l:parent = fnamemodify(l:root, ':h') - if l:parent == l:root - return -1 - endif - - let l:root = l:parent - endwhile -endfunction - function! s:Prettier_Parse_Error(errors) abort call prettier#logging#error#log('PARSING_ERROR') if g:prettier#quickfix_enabled && prettier#bridge#parser#onError(a:errors) -- cgit v1.3