diff options
| author | Adam Macumber <adam.macumber@sharpnotions.com> | 2019-10-09 15:44:55 -0400 |
|---|---|---|
| committer | Adam Macumber <adam.macumber@sharpnotions.com> | 2019-10-09 15:46:41 -0400 |
| commit | 41d9c4778a8dc0439afee9c9000d7fa5b4b8be17 (patch) | |
| tree | e08d7407ca84f46e37601d25e218ff9ce32c8be4 | |
| parent | 9c216df3a57d35dc90222473a3438697624c9a4f (diff) | |
| download | vim-prettier-41d9c4778a8dc0439afee9c9000d7fa5b4b8be17.tar.xz | |
Toggle the autoformat setting based on config file presence.
| -rw-r--r-- | autoload/prettier.vim | 10 | ||||
| -rw-r--r-- | plugin/prettier.vim | 21 |
2 files changed, 31 insertions, 0 deletions
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 <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 |
