diff options
Diffstat (limited to 'autoload/prettier/resolver')
| -rw-r--r-- | autoload/prettier/resolver/config.vim | 98 | ||||
| -rw-r--r-- | autoload/prettier/resolver/executable.vim | 74 | ||||
| -rw-r--r-- | autoload/prettier/resolver/preset.vim | 8 |
3 files changed, 180 insertions, 0 deletions
diff --git a/autoload/prettier/resolver/config.vim b/autoload/prettier/resolver/config.vim new file mode 100644 index 0000000..b22f11a --- /dev/null +++ b/autoload/prettier/resolver/config.vim @@ -0,0 +1,98 @@ +" By default we will default to our internal +" configuration settings for prettier +function! prettier#resolver#config#resolve(config, hasSelection, start, end) abort + " Allow params to be passed as json format + " convert bellow usage of globals to a get function o the params defaulting to global + " TODO: Use a list, filter() and join() to get a nicer list of args. + let l:cmd = s:Flag_use_tabs(a:config) . ' ' . + \ s:Flag_tab_width(a:config) . ' ' . + \ s:Flag_print_width(a:config) . ' ' . + \ s:Flag_parser(a:config) . ' ' . + \ s:Flag_range_delimiter(a:config, a:hasSelection, a:start, a:end) . ' ' . + \ ' --semi=' . + \ get(a:config, 'semi', g:prettier#config#semi) . + \ ' --single-quote=' . + \ get(a:config, 'singleQuote', g:prettier#config#single_quote) . + \ ' --bracket-spacing=' . + \ get(a:config, 'bracketSpacing', g:prettier#config#bracket_spacing) . + \ ' --jsx-bracket-same-line=' . + \ get(a:config, 'jsxBracketSameLine', g:prettier#config#jsx_bracket_same_line) . + \ ' --arrow-parens=' . + \ get(a:config, 'arrowParens', g:prettier#config#arrow_parens) . + \ ' --trailing-comma=' . + \ get(a:config, 'trailingComma', g:prettier#config#trailing_comma) . + \ ' --config-precedence=' . + \ get(a:config, 'configPrecedence', g:prettier#config#config_precedence) . + \ ' --prose-wrap=' . + \ get(a:config, 'proseWrap', g:prettier#config#prose_wrap) . + \ ' --html-whitespace-sensitivity ' . + \ get(a:config, 'htmlWhitespaceSensitivity', g:prettier#config#html_whitespace_sensitivity) . + \ ' --stdin-filepath="'.simplify(expand('%:p')).'"' . + \ ' --require-pragma=' . + \ get(a:config, 'requirePragma', g:prettier#config#require_pragma) . + \ ' --loglevel error '. + \ ' --stdin ' + return l:cmd +endfunction + +" Returns either '--range-start X --range-end Y' or an empty string. +function! s:Flag_range_delimiter(config, partialFormatEnabled, start, end) abort + if (!a:partialFormatEnabled) + return '' + endif + + let l:range = prettier#utils#buffer#getCharRange(a:start, a:end) + + return '--range-start=' . l:range[0] . ' --range-end=' . l:range[1] +endfunction + +" Returns '--tab-width=NN' +function! s:Flag_tab_width(config) abort + let l:value = get(a:config, 'tabWidth', g:prettier#config#tab_width) + + if (l:value ==# 'auto') + let l:value = prettier#utils#shim#shiftwidth() + endif + + return '--tab-width=' . l:value +endfunction + +" Returns either '--use-tabs' or an empty string. +function! s:Flag_use_tabs(config) abort + let l:value = get(a:config, 'useTabs', g:prettier#config#use_tabs) + if (l:value ==# 'auto') + let l:value = &expandtab ? 'false' : 'true' + endif + + if ( l:value ==# 'true' ) + return ' --use-tabs' + else + return '' + endif +endfunction + +" Returns '--print-width=NN' or '' +function! s:Flag_print_width(config) abort + let l:value = get(a:config, 'printWidth', g:prettier#config#print_width) + + if (l:value ==# 'auto') + let l:value = &textwidth + endif + + if (l:value > 0) + return '--print-width=' . l:value + else + return '' + endif +endfunction + +" Returns '--parser=PARSER' or '' +function! s:Flag_parser(config) abort + let l:value = get(a:config, 'parser', g:prettier#config#parser) + + if (l:value !=# '') + return '--parser=' . l:value + else + return '' + endif +endfunction diff --git a/autoload/prettier/resolver/executable.vim b/autoload/prettier/resolver/executable.vim new file mode 100644 index 0000000..8d233ab --- /dev/null +++ b/autoload/prettier/resolver/executable.vim @@ -0,0 +1,74 @@ +let s:ROOT_DIR = fnamemodify(resolve(expand('<sfile>:p')), ':h') + +" 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! prettier#resolver#executable#getPath() 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:localExec = s:ResolveExecutable(getcwd()) + if executable(l:localExec) + return fnameescape(l:localExec) + endif + + let l:globalExec = s:ResolveExecutable() + if executable(l:globalExec) + return fnameescape(l:globalExec) + endif + + let l:pluginExec = s:ResolveExecutable(s:ROOT_DIR) + if executable(l:pluginExec) + return fnameescape(l:pluginExec) + endif + + return -1 +endfunction + +function! s:GetExecPath(...) abort + let l:rootDir = a:0 > 0 ? a:1 : -1 + let l:dir = l:rootDir != -1 ? l:rootDir . '/.bin/' : '' + return l:dir . 'prettier' +endfunction + +" Searches for the existence of a directory accross +" ancestral parents +function! s:TraverseAncestorDirSearch(rootDir) abort + let l:root = a:rootDir + let l:dir = 'node_modules' + + while 1 + let l:searchDir = l:root . '/' . l:dir + if isdirectory(l:searchDir) + return l:searchDir + 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:ResolveExecutable(...) abort + let l:rootDir = a:0 > 0 ? a:1 : 0 + let l:exec = -1 + + if isdirectory(l:rootDir) + let l:dir = s:TraverseAncestorDirSearch(l:rootDir) + if l:dir != -1 + let l:exec = s:GetExecPath(l:dir) + endif + else + let l:exec = s:GetExecPath() + endif + + return l:exec +endfunction diff --git a/autoload/prettier/resolver/preset.vim b/autoload/prettier/resolver/preset.vim new file mode 100644 index 0000000..03f98ee --- /dev/null +++ b/autoload/prettier/resolver/preset.vim @@ -0,0 +1,8 @@ +" Build config using predefined preset +function! prettier#resolver#preset#resolve(fileTypeConfigOverwrites) abort + if ( g:prettier#preset#config ==# 'fb' ) + return extend(prettier#presets#fb#config(), a:fileTypeConfigOverwrites) + endif + + return a:fileTypeConfigOverwrites +endfunction |
