diff options
| author | Mitermayer Reis <mitermayer.reis@gmail.com> | 2020-01-08 13:08:21 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-08 13:08:21 -0800 |
| commit | f4e31d67aa1e0658c1a19a26e4bad9fc4691eab9 (patch) | |
| tree | e5f9a74d33ad77cfbf77a19f44eee828b1aedce8 | |
| parent | 9c216df3a57d35dc90222473a3438697624c9a4f (diff) | |
| parent | e144afaa17a5c4069c9b86558a2831e729429073 (diff) | |
| download | vim-prettier-f4e31d67aa1e0658c1a19a26e4bad9fc4691eab9.tar.xz | |
Merge pull request #212 from atomdmac/toggle-autoformat-by-config
Toggle the autoformat setting based on config file presence.
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | autoload/prettier.vim | 10 | ||||
| -rw-r--r-- | doc/prettier.txt | 10 | ||||
| -rw-r--r-- | plugin/prettier.vim | 21 |
4 files changed, 53 insertions, 0 deletions
@@ -133,6 +133,18 @@ Enable auto formatting of files that have "@format" or "@prettier" tag let g:prettier#autoformat = 1 ``` +Toggle the `g:prettier#autoformat` setting based on whether a config file can be found in the current directory or any parent directory. Note that this will override the `g:prettier#autoformat` setting! + +```vim +let g:prettier#autoformat_config_present = 1 +``` + +A list containing all config file names to search for when using the `g:prettier#autoformat_config_present` option. + +```vim +let g:prettier#autoformat_config_files = [...] +``` + Set the prettier CLI executable path ```vim diff --git a/autoload/prettier.vim b/autoload/prettier.vim index e66f4b7..d4c3171 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -78,3 +78,13 @@ function! prettier#Prettier(...) abort call prettier#logging#error#log('EXECUTABLE_NOT_FOUND_ERROR') endif endfunction + +" Set autoformat toggle based on whether config file was found. +function! prettier#IsConfigPresent(config_files) abort + for config_file in a:config_files + if filereadable(findfile(config_file, '.;')) + return 1 + endif + endfor + return 0 +endfunction diff --git a/doc/prettier.txt b/doc/prettier.txt index c2b38fd..b56f6e1 100644 --- a/doc/prettier.txt +++ b/doc/prettier.txt @@ -114,6 +114,16 @@ CONFIGURATION *vim-prettier-configuration* Enable auto formatting of files that have "@format" or "@prettier" tag > let g:prettier#autoformat = 1 + +Enable auto formatting of files based on whether a Prettier configuration file has been +found in the project directory or any parent directories. +> + let g:prettier#autoformat_config_present = 1 + +Configuration file names to search for when attempting to find an appropriate +Prettier configuration file for the current project. +> + let g:prettier#autoformat_config_files = [...] < Set the prettier CLI executable path > diff --git a/plugin/prettier.vim b/plugin/prettier.vim index 83656d3..4ffa9cb 100644 --- a/plugin/prettier.vim +++ b/plugin/prettier.vim @@ -20,6 +20,19 @@ let g:loaded_prettier = 1 " autoformating disabled by default upon saving let g:prettier#autoformat = get(g:, 'prettier#autoformat', 0) +" whether to turn autoformatting on if a prettier config file is found +let g:prettier#autoformat_config_present = get(g:, 'prettier#autoformat_config_present', 0) + +" prettier config files to search current directory and parent directories for +let g:prettier#autoformat_config_files = get(g:, 'prettier#autoformat_config_files', [ + \'.prettierrc', + \'.prettierrc.yml', + \'.prettierrc.yaml', + \'.prettierrc.js', + \'.prettierrc.config.js', + \'.prettierrc.json' + \'.prettierrc.toml']) + " path to prettier cli let g:prettier#exec_cmd_path = get(g:, 'prettier#exec_cmd_path', 0) @@ -139,6 +152,14 @@ nnoremap <silent> <Plug>(PrettierCliPath) :PrettierCliPath<CR> augroup Prettier autocmd! + if g:prettier#autoformat_config_present + if prettier#IsConfigPresent(g:prettier#autoformat_config_files) + let g:prettier#autoformat = 1 + else + let g:prettier#autoformat = 0 + endif + endif + if g:prettier#autoformat autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html noautocmd | call prettier#Autoformat() endif |
