aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--autoload/prettier.vim10
-rw-r--r--doc/prettier.txt10
-rw-r--r--plugin/prettier.vim21
4 files changed, 53 insertions, 0 deletions
diff --git a/README.md b/README.md
index cf36367..f8c7253 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. 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