Skip to content

Commit

Permalink
Adding presets
Browse files Browse the repository at this point in the history
- adding facebook presets as a configurable option to overwrite prettier
defaults
  • Loading branch information
mitermayer committed May 16, 2018
1 parent bce2e04 commit 98845cd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion autoload/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function! prettier#Prettier(...) abort
let l:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {})

if l:execCmd != -1
let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(l:config)
let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(prettier#resolver#preset#build(l:config))

" close quickfix if it is opened
call prettier#utils#quickfix#close()
Expand Down
13 changes: 13 additions & 0 deletions autoload/prettier/presets/fb.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
" Return facebook style config overwrite presets
function! prettier#presets#fb#config() abort
return {
\ 'bracketSpacing': 'false',
\ 'jsxBracketSameLine': 'true',
\ 'printWidth': 80,
\ 'parser': 'flow',
\ 'singleQuote': 'true',
\ 'tabWidth': 2,
\ 'trailingComma': 'all',
\ 'useTabs': 'false',
\ }
endfunction
14 changes: 14 additions & 0 deletions autoload/prettier/resolver/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ function! prettier#resolver#config#buildCliArgs(config) abort
\ s:Flag_tab_width(a:config) . ' ' .
\ s:Flag_print_width(a:config) . ' ' .
\ s:Flag_parser(a:config) . ' ' .
\ ' --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=' .
Expand All @@ -17,6 +29,8 @@ function! prettier#resolver#config#buildCliArgs(config) abort
\ ' --no-editorconfig '.
\ ' --loglevel error '.
\ ' --stdin '
" TODO
" check to see if --no-editorconfig is still needed
return l:cmd
endfunction

Expand Down
8 changes: 8 additions & 0 deletions autoload/prettier/resolver/preset.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
" Build config using predefined preset
function! prettier#resolver#preset#build(fileTypeConfigOverwrites) abort
if ( g:prettier#preset#config ==# 'fb' )
return extend(prettier#presets#fb#config(), a:fileTypeConfigOverwrites)
endif

return a:fileTypeConfigOverwrites
endfunction
34 changes: 32 additions & 2 deletions plugin/prettier.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ let g:prettier#exec_cmd_async = get(g:, 'prettier#exec_cmd_async', 0)
" when having formatting errors will open the quickfix by default
let g:prettier#quickfix_enabled = get(g:, 'prettier#quickfix_enabled', 1)

" Don't leave the quicklist focused on error.
let g:prettier#quickfix_auto_focus = get(g:, 'prettier#quickfix_auto_focus', 1)

" default|fb
" Use prettier defaults
let g:prettier#preset#config = get(g:,'prettier#preset#config', 'default')

" => Prettier CLI config
" Max line length that prettier will wrap on: a number or 'auto' (use
" textwidth).
Expand Down Expand Up @@ -57,8 +64,31 @@ let g:prettier#config#config_precedence = get(g:, 'prettier#config#config_preced
" default: 'preserve'
let g:prettier#config#prose_wrap = get(g:, 'prettier#config#prose_wrap', 'preserve')

" Don't leave the quicklist focused on error.
let g:prettier#quickfix_auto_focus = get(g:, 'prettier#quickfix_auto_focus', 1)
" print semicolons
" default: 'true'
let g:prettier#config#semi = get(g:,'prettier#config#semi', 'true')

" Use single quotes instead of double quotes.
" default: 'false'
let g:prettier#config#single_quote = get(g:,'prettier#config#single_quote', 'false')

" print spaces between brackets
" default: 'true'
let g:prettier#config#bracket_spacing = get(g:,'prettier#config#bracket_spacing', 'true')

" put > on the last line instead of new line
" default: 'false'
let g:prettier#config#jsx_bracket_same_line = get(g:,'prettier#config#jsx_bracket_same_line', 'false')

" avoid wrapping a single arrow function param in parens
" avoid|always
" default: 'avoid'
let g:prettier#config#arrow_parens = get(g:,'prettier#config#arrow_parens', 'avoid')

" Print trailing commas wherever possible when multi-line.
" none|es5|all
" default: 'none'
let g:prettier#config#trailing_comma = get(g:,'prettier#config#trailing_comma', 'none')

" synchronous by default
command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, <line1>, <line2>)
Expand Down

1 comment on commit 98845cd

@mitermayer
Copy link
Member Author

@mitermayer mitermayer commented on 98845cd May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @docwhat

Added support for presets, this way we can have the old prettier defaults for users that wanted it by simply including let g:prettier#preset#config = 'fb' on their .vimrc

Please sign in to comment.