From 7ac46762931442491b13401411a1b4eb65272099 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Thu, 12 Sep 2019 23:54:51 +0000 Subject: issues/184-fixing-undo-step - Adding support for require-pragma - Our previous undo implementation tried to be naive on implementing require-pragma, and created some undo bugs due to it. This diff enables us to use the prettier upstream support for require-pragma - On subsequent PR's we will completetly phase out the previous undo implementation in favour to this one in order to fix bug https://github.com/prettier/vim-prettier/issues/184 --- README.md | 4 ++++ autoload/prettier/resolver/config.vim | 2 ++ doc/prettier.txt | 4 ++++ plugin/prettier.vim | 3 +++ 4 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 04e2243..ee2ff31 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,10 @@ let g:prettier#config#prose_wrap = 'preserve' " css|strict|ignore " default: 'css' let g:prettier#config#html_whitespace_sensitivity = 'css' + +" false|true +" default: 'false' +let g:prettier#config#require_pragma = 'false' ``` ### REQUIREMENT(S) diff --git a/autoload/prettier/resolver/config.vim b/autoload/prettier/resolver/config.vim index 029bec4..b22f11a 100644 --- a/autoload/prettier/resolver/config.vim +++ b/autoload/prettier/resolver/config.vim @@ -28,6 +28,8 @@ function! prettier#resolver#config#resolve(config, hasSelection, start, end) abo \ ' --html-whitespace-sensitivity ' . \ get(a:config, 'htmlWhitespaceSensitivity', g:prettier#config#html_whitespace_sensitivity) . \ ' --stdin-filepath="'.simplify(expand('%:p')).'"' . + \ ' --require-pragma=' . + \ get(a:config, 'requirePragma', g:prettier#config#require_pragma) . \ ' --loglevel error '. \ ' --stdin ' return l:cmd diff --git a/doc/prettier.txt b/doc/prettier.txt index 0a8dc30..11ed194 100644 --- a/doc/prettier.txt +++ b/doc/prettier.txt @@ -185,6 +185,10 @@ However they can be configured by: " css|strict|ignore let g:prettier#config#html_whitespace_sensitivity = 'css' + + " false|true + " default: 'false' + let g:prettier#config#require_pragma = 'false' < ============================================================================== REQUIREMENT(S) *vim-prettier-requirements* diff --git a/plugin/prettier.vim b/plugin/prettier.vim index 19b4002..d3bee5a 100644 --- a/plugin/prettier.vim +++ b/plugin/prettier.vim @@ -97,6 +97,9 @@ let g:prettier#config#arrow_parens = get(g:,'prettier#config#arrow_parens', 'avo " default: 'none' let g:prettier#config#trailing_comma = get(g:,'prettier#config#trailing_comma', 'none') +" restrict itself to only format files that contain a special comment @prettier or @format +let g:prettier#config#require_pragma= get(g:, 'prettier#config#require_pragma', 'false') + " synchronous by default command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, , , g:prettier#partial_format) -- cgit v1.3 From 3dc6684302ff2749570d938e1d49862db0e2648c Mon Sep 17 00:00:00 2001 From: mitermayer Date: Fri, 13 Sep 2019 21:38:53 +0000 Subject: issues/184-fixing-undo-step - Removing previous naive implementation of requirePragma - This commit ensures that we use `prettier` default way to identify pragmas for the auto save and remove our previous naive implementation --- autoload/prettier.vim | 33 ++++++--------------------------- ftplugin/html.vim | 7 ------- plugin/prettier.vim | 7 +++++++ 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/autoload/prettier.vim b/autoload/prettier.vim index 3241f48..e66f4b7 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -34,33 +34,9 @@ function! prettier#PrettierCli(user_input) abort endif endfunction -" Allows @format pragma support +" Allows @format and @prettier pragma support upon saving function! prettier#Autoformat(...) abort - let l:curPos = getpos('.') - let l:maxLineLookup = 50 - let l:maxTimeLookupMs = 500 - let l:pattern = '@format' - let l:search = @/ - let l:winview = winsaveview() - - " we need to move selection to the top before looking up to avoid - " scanning a very long file - call cursor(1, 1) - - " Search starting at the start of the document - if search(l:pattern, 'n', l:maxLineLookup, l:maxTimeLookupMs) > 0 - " autoformat async - call prettier#Prettier(1) - endif - - " Restore the selection and if greater then before it defaults to end - call cursor(l:curPos[1], l:curPos[2]) - - " Restore view - call winrestview(l:winview) - - " Restore search - let @/=l:search + call prettier#Prettier(1, 1, line('$'), 0, { 'requirePragma': 'true'}) endfunction " Main prettier command @@ -71,9 +47,12 @@ function! prettier#Prettier(...) abort let l:endSelection = a:0 > 2 ? a:3 : line('$') let l:hasSelection = a:0 > 2 ? 1 : 0 let l:partialFormat = a:0 > 3 && a:4 ? a:4 : 0 - let l:config = getbufvar(bufnr('%'), 'prettier_ft_default_args', {}) let l:partialFormatEnabled = l:hasSelection && l:partialFormat + let l:overWrite = a:0 > 4 ? a:5 : {} + let l:bufferConfig = getbufvar(bufnr('%'), 'prettier_ft_default_args', {}) + let l:config = extend(l:bufferConfig, l:overWrite) + if l:execCmd != -1 " TODO " => we should make sure we can resolve --range-start and --range-end when required diff --git a/ftplugin/html.vim b/ftplugin/html.vim index d2f059d..58ab258 100644 --- a/ftplugin/html.vim +++ b/ftplugin/html.vim @@ -5,10 +5,3 @@ if &filetype !~# 'markdown' \ 'parser': 'html', \ } endif - -augroup Prettier - autocmd! - if g:prettier#autoformat - autocmd BufWritePre *.html call prettier#Autoformat() - endif -augroup end diff --git a/plugin/prettier.vim b/plugin/prettier.vim index d3bee5a..aafa7a2 100644 --- a/plugin/prettier.vim +++ b/plugin/prettier.vim @@ -136,3 +136,10 @@ nnoremap (PrettierVersion) :PrettierVersion nnoremap (PrettierCli) :PrettierCli nnoremap (PrettierCliVersion) :PrettierCliVersion nnoremap (PrettierCliPath) :PrettierCliPath + +augroup Prettier + autocmd! + if g:prettier#autoformat + autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html call prettier#Autoformat() + endif +augroup end -- cgit v1.3