From 41d9c4778a8dc0439afee9c9000d7fa5b4b8be17 Mon Sep 17 00:00:00 2001 From: Adam Macumber Date: Wed, 9 Oct 2019 15:44:55 -0400 Subject: Toggle the autoformat setting based on config file presence. --- autoload/prettier.vim | 10 ++++++++++ plugin/prettier.vim | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/autoload/prettier.vim b/autoload/prettier.vim index e66f4b7..f55fb21 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) + for config_file in a:config_files + if filereadable(findfile(config_file, '.;')) + return 1 + endif + endfor + return 0 +endfunction 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 (PrettierCliPath) :PrettierCliPath 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 -- cgit v1.3 From 263d34324b5ad7f9c25f0d56db8d64c43c9c9d58 Mon Sep 17 00:00:00 2001 From: Adam Macumber Date: Wed, 9 Oct 2019 15:58:07 -0400 Subject: Update documentation to include autoformat_config_* options. --- doc/prettier.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 > -- cgit v1.3 From 10fc8cf75bf9630533cabe87b96f9943b8b5aed2 Mon Sep 17 00:00:00 2001 From: Atom Mac Date: Sat, 12 Oct 2019 20:57:38 -0400 Subject: Add autoformat_config_* options to README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index cf36367..f83fb6d 100644 --- a/README.md +++ b/README.md @@ -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. + +```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 -- cgit v1.3 From 59c4644692e10613b78e8746fe0832d33a2e1216 Mon Sep 17 00:00:00 2001 From: Atom Mac Date: Sun, 13 Oct 2019 09:51:05 -0400 Subject: Fix linting issue: autoloaded func needed 'abort' --- autoload/prettier.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/prettier.vim b/autoload/prettier.vim index f55fb21..d4c3171 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -80,7 +80,7 @@ function! prettier#Prettier(...) abort endfunction " Set autoformat toggle based on whether config file was found. -function! prettier#IsConfigPresent(config_files) +function! prettier#IsConfigPresent(config_files) abort for config_file in a:config_files if filereadable(findfile(config_file, '.;')) return 1 -- cgit v1.3 From e144afaa17a5c4069c9b86558a2831e729429073 Mon Sep 17 00:00:00 2001 From: Atom Mac Date: Sun, 13 Oct 2019 10:04:00 -0400 Subject: Clarify that autoformat will be overridden by config_present --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f83fb6d..f8c7253 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ 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. +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 -- cgit v1.3