From 314e4053ca3676dce8deceaa432667ab2f3a0058 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Fri, 4 May 2018 09:37:26 -0700 Subject: Moving formating ultils to buffer module - moving formating utils to buffer module to make it easier to interact and test --- autoload/prettier/utils/buffer.vim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 autoload/prettier/utils/buffer.vim (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim new file mode 100644 index 0000000..06fd7ff --- /dev/null +++ b/autoload/prettier/utils/buffer.vim @@ -0,0 +1,29 @@ +function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abort + " store view + let l:winview = winsaveview() + let l:newBuffer = prettier#utils#buffer#createBufferFromUpdatedLines(a:lines, a:startSelection, a:endSelection) + + " we should not replace contents if the newBuffer is empty + if empty(l:newBuffer) + return + endif + + " delete all lines on the current buffer + silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _' + + " replace all lines from the current buffer with output from prettier + call setline(1, l:newBuffer) + + " Restore view + call winrestview(l:winview) +endfunction + +" Returns 1 if content has changed +function! prettier#utils#buffer#willUpdatedLinesChangeBuffer(lines, start, end) abort + return getbufline(bufnr('%'), 1, line('$')) == prettier#utils#buffer#createBufferFromUpdatedLines(a:lines, a:start, a:end) ? 0 : 1 +endfunction + +" Returns a new buffer with lines replacing start and end of the contents of the current buffer +function! prettier#utils#buffer#createBufferFromUpdatedLines(lines, start, end) abort + return getbufline(bufnr('%'), 1, a:start - 1) + a:lines + getbufline(bufnr('%'), a:end + 1, '$') +endfunction -- cgit v1.3 From 0feb9565771ed0210e45e7226edc6d8185a8ce4f Mon Sep 17 00:00:00 2001 From: mitermayer Date: Fri, 4 May 2018 12:48:20 -0700 Subject: Creating buffer replace and save utils --- autoload/prettier/job/async/vim.vim | 12 ++---------- autoload/prettier/utils/buffer.vim | 6 ++++++ 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/autoload/prettier/job/async/vim.vim b/autoload/prettier/job/async/vim.vim index 0fd8f6c..4db6522 100644 --- a/autoload/prettier/job/async/vim.vim +++ b/autoload/prettier/job/async/vim.vim @@ -50,7 +50,7 @@ function! s:onClose(channel, startSelection, endSelection, bufferName) abort if (bufloaded(str2nr(a:bufferName))) try silent exec 'sp '. escape(bufname(bufnr(a:bufferName)), ' \') - call s:formatAndSave(l:out, a:startSelection, a:endSelection) + call prettier#utils#buffer#replaceAndSave(l:out, a:startSelection, a:endSelection) catch call prettier#logging#error#log('PARSING_ERROR', a:bufferName) finally @@ -61,17 +61,9 @@ function! s:onClose(channel, startSelection, endSelection, bufferName) abort endtry endif else - call s:formatAndSave(l:out, a:startSelection, a:endSelection) + call prettier#utils#buffer#replaceAndSave(l:out, a:startSelection, a:endSelection) endif let s:prettier_job_running = 0 endif endfunction - -" TODO -" make the buffer replace method accerpt an extra arg to -" decide if it should save it or not and delete this method -function! s:formatAndSave(lines, start, end) abort - call prettier#utils#buffer#replace(a:lines, a:start, a:end) - write -endfunction diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index 06fd7ff..1e11ddb 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -18,6 +18,12 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo call winrestview(l:winview) endfunction +" Replace and save the buffer +function! prettier#utils#buffer#replaceAndSave(lines, startSelection, endSelection) abort + call prettier#utils#buffer#replace(a:lines, a:startSelection, a:endSelection) + write +endfunction + " Returns 1 if content has changed function! prettier#utils#buffer#willUpdatedLinesChangeBuffer(lines, start, end) abort return getbufline(bufnr('%'), 1, line('$')) == prettier#utils#buffer#createBufferFromUpdatedLines(a:lines, a:start, a:end) ? 0 : 1 -- cgit v1.3 From ec6ede90f3b9948ed7063402189653f8d6721326 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Mon, 28 May 2018 20:53:30 -0700 Subject: Enabling partial formatting but still maintaining support for fragment formatting --- README.md | 2 +- autoload/prettier.vim | 18 +++++++++++++++++- autoload/prettier/resolver/config.vim | 14 +++++++++++++- autoload/prettier/resolver/preset.vim | 2 +- autoload/prettier/utils/buffer.vim | 21 +++++++++++++++++++++ plugin/prettier.vim | 15 +++++++++++++-- 6 files changed, 66 insertions(+), 6 deletions(-) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/README.md b/README.md index 442a821..d0ec620 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ By default it will auto format **javascript**, **typescript**, **less**, **scss**, **css**, **json**, **graphql** and **markdown** files if they have/support the "@format" pragma annotation in the header of the file. -![vim-prettier](/media/vim-prettier.gif?raw=true "vim-prettier") +![vim-prettier](/media/vim-prettier.gif?raw=true 'vim-prettier') ### INSTALL diff --git a/autoload/prettier.vim b/autoload/prettier.vim index fae1ef8..0e8ade3 100644 --- a/autoload/prettier.vim +++ b/autoload/prettier.vim @@ -69,14 +69,30 @@ function! prettier#Prettier(...) abort let l:async = a:0 > 0 ? a:1 : 0 let l:startSelection = a:0 > 1 ? a:2 : 1 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 if l:execCmd != -1 - let l:cmd = l:execCmd . prettier#resolver#config#buildCliArgs(prettier#resolver#preset#build(l:config)) + " TODO + " => we should make sure we can resolve --range-start and --range-end when required + " => when the above is required we should also update l:startSelection to '1' and l:endSelection to line('$') + let l:cmd = l:execCmd . prettier#resolver#config#resolve( + \ prettier#resolver#preset#resolve(l:config), + \ l:partialFormatEnabled, + \ l:startSelection, + \ l:endSelection) " close quickfix if it is opened call prettier#utils#quickfix#close() + " we will be using portion formatting, so we need to send entire buffer to prettier + if l:partialFormatEnabled + let l:startSelection = 1 + let l:endSelection = line('$') + endif + " format buffer call prettier#job#runner#run(l:cmd, l:startSelection, l:endSelection, l:async) else diff --git a/autoload/prettier/resolver/config.vim b/autoload/prettier/resolver/config.vim index 91fc247..eae6725 100644 --- a/autoload/prettier/resolver/config.vim +++ b/autoload/prettier/resolver/config.vim @@ -1,6 +1,6 @@ " By default we will default to our internal " configuration settings for prettier -function! prettier#resolver#config#buildCliArgs(config) abort +function! prettier#resolver#config#resolve(config, hasSelection, start, end) abort " Allow params to be passed as json format " convert bellow usage of globals to a get function o the params defaulting to global " TODO: Use a list, filter() and join() to get a nicer list of args. @@ -8,6 +8,7 @@ function! prettier#resolver#config#buildCliArgs(config) abort \ s:Flag_tab_width(a:config) . ' ' . \ s:Flag_print_width(a:config) . ' ' . \ s:Flag_parser(a:config) . ' ' . + \ s:Flag_range_delimiter(a:config, a:hasSelection, a:start, a:end) . ' ' . \ ' --semi=' . \ get(a:config, 'semi', g:prettier#config#semi) . \ ' --single-quote=' . @@ -34,6 +35,17 @@ function! prettier#resolver#config#buildCliArgs(config) abort return l:cmd endfunction +" Returns either '--range-start X --range-end Y' or an empty string. +function! s:Flag_range_delimiter(config, partialFormatEnabled, start, end) abort + if (!a:partialFormatEnabled) + return '' + endif + + let l:range = prettier#utils#buffer#getCharRange(a:start, a:end) + + return '--range-start=' . l:range[0] . ' --range-end=' . l:range[1] +endfunction + " Returns '--tab-width=NN' function! s:Flag_tab_width(config) abort let l:value = get(a:config, 'tabWidth', g:prettier#config#tab_width) diff --git a/autoload/prettier/resolver/preset.vim b/autoload/prettier/resolver/preset.vim index a0891d2..03f98ee 100644 --- a/autoload/prettier/resolver/preset.vim +++ b/autoload/prettier/resolver/preset.vim @@ -1,5 +1,5 @@ " Build config using predefined preset -function! prettier#resolver#preset#build(fileTypeConfigOverwrites) abort +function! prettier#resolver#preset#resolve(fileTypeConfigOverwrites) abort if ( g:prettier#preset#config ==# 'fb' ) return extend(prettier#presets#fb#config(), a:fileTypeConfigOverwrites) endif diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index 1e11ddb..0b3c619 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -33,3 +33,24 @@ endfunction function! prettier#utils#buffer#createBufferFromUpdatedLines(lines, start, end) abort return getbufline(bufnr('%'), 1, a:start - 1) + a:lines + getbufline(bufnr('%'), a:end + 1, '$') endfunction + +" Adapted from https://github.com/farazdagi/vim-go-ide +function! s:getCharPosition(line, col) + if &encoding !=# 'utf-8' + " On utf-8 enconding we can't just use bytes so we need to make sure we can count the + " characters, we do that by adding the text into a temporary buffer and counting the chars + let l:buf = a:line == 1 ? '' : (join(getline(1, a:line - 1), "\n") . "\n") + let l:buf .= a:col == 1 ? '' : getline('.')[:a:col - 2] + return len(iconv(l:buf, &encoding, 'utf-8')) + endif + " On non utf-8 the line byte should match the character + return line2byte(a:line) + (a:col - 2) +endfun + +" Returns [start, end] byte range when on visual mode +function! prettier#utils#buffer#getCharRange(startSelection, endSelection) abort + let l:range = [] + call add(l:range, s:getCharPosition(a:startSelection, col("'<"))) + call add(l:range, s:getCharPosition(a:endSelection, col("'>"))) + return l:range +endfunction diff --git a/plugin/prettier.vim b/plugin/prettier.vim index 2bb5a39..4a3db64 100644 --- a/plugin/prettier.vim +++ b/plugin/prettier.vim @@ -32,6 +32,9 @@ let g:prettier#quickfix_enabled = get(g:, 'prettier#quickfix_enabled', 1) " Don't leave the quicklist focused on error. let g:prettier#quickfix_auto_focus = get(g:, 'prettier#quickfix_auto_focus', 1) +" Send to prettier entire buffer and use --range-start and --range-end as delimter +let g:prettier#partial_format = get(g:, 'prettier#partial_format', 0) + " default|fb " Use prettier defaults let g:prettier#preset#config = get(g:,'prettier#preset#config', 'default') @@ -91,10 +94,10 @@ let g:prettier#config#arrow_parens = get(g:,'prettier#config#arrow_parens', 'avo let g:prettier#config#trailing_comma = get(g:,'prettier#config#trailing_comma', 'none') " synchronous by default -command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, , ) +command! -nargs=? -range=% Prettier call prettier#Prettier(g:prettier#exec_cmd_async, , , g:prettier#partial_format) " prettier async -command! -nargs=? -range=% PrettierAsync call prettier#Prettier(1, , ) +command! -nargs=? -range=% PrettierAsync call prettier#Prettier(1, , , g:prettier#partial_format) " prints vim-prettier version command! -nargs=? -range=% PrettierVersion echom '0.2.6' @@ -108,12 +111,20 @@ command! -nargs=? -range=% PrettierCliVersion call prettier#PrettierCli('--versi " prints prettier resolved cli path command! -nargs=? -range=% PrettierCliPath call prettier#PrettierCliPath() +" sends selected text to prettier cli for formatting +command! -nargs=? -range=% PrettierFragment call prettier#Prettier(g:prettier#exec_cmd_async, , , 0) + +" sends entire buffer to prettier cli but format just selection +command! -nargs=? -range=% PrettierPartial call prettier#Prettier(g:prettier#exec_cmd_async, , , 1) + " map command if !hasmapto('(Prettier)') && maparg('p', 'n') ==# '' nmap p (Prettier) endif nnoremap (Prettier) :Prettier nnoremap (PrettierAsync) :PrettierAsync +nnoremap (PrettierFragment) :PrettierFragment +nnoremap (PrettierPartial) :PrettierPartial nnoremap (PrettierVersion) :PrettierVersion nnoremap (PrettierCli) :PrettierCli nnoremap (PrettierCliVersion) :PrettierCliVersion -- cgit v1.3 From 33d19b72f4f97cccf68b1581f6d33b900bdb4b3c Mon Sep 17 00:00:00 2001 From: mitermayer Date: Sun, 3 Jun 2018 23:11:06 -0700 Subject: Adding documentation for prettier partial formatting --- README.md | 25 +++++++++++++++++++++++++ autoload/prettier/utils/buffer.vim | 2 +- doc/prettier.txt | 19 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/README.md b/README.md index d0ec620..7c3c775 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,22 @@ If your are on vim 8+ you can also trigger async formatting by: :PrettierAsync ``` +You can send to prettier your entire buffer but ensure that it formats only your selection. + +**note: ** differs from `:PrettierFragment` by sending the entire buffer to prettier, allowing identation level to be preserved, but it requires the whole file to be valid. + +```vim +:PrettierPartial +``` + +You can send to prettier your current selection as a fragment of same type as the file being edited. + +**note: ** differs from `:PrettierFragment` by sending only the current selection to prettier, this allows for faster formatting but wont preserve indentation. + +````vim +:PrettierFragment +``` + You can check what is the `vim-prettier` plugin version by: ```vim @@ -135,6 +151,13 @@ By default parsing errors will open the quickfix but can also be disabled let g:prettier#quickfix_enabled = 0 ``` +By default selection formatting will be running `:PrettierFragment` but we can set +`:PrettierPartial` as the default selection formatting by: + +```vim +let g:prettier#partial_format=1 +``` + By default we auto focus on the quickfix when there are errors but can also be disabled ```vim @@ -205,3 +228,5 @@ let g:prettier#config#prose_wrap = 'preserve' ### REQUIREMENT(S) If prettier installation can't be found no code formatting will happen +``` +```` diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index 0b3c619..258c615 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -35,7 +35,7 @@ function! prettier#utils#buffer#createBufferFromUpdatedLines(lines, start, end) endfunction " Adapted from https://github.com/farazdagi/vim-go-ide -function! s:getCharPosition(line, col) +function! s:getCharPosition(line, col) abort if &encoding !=# 'utf-8' " On utf-8 enconding we can't just use bytes so we need to make sure we can count the " characters, we do that by adding the text into a temporary buffer and counting the chars diff --git a/doc/prettier.txt b/doc/prettier.txt index 632c1d8..208cf22 100644 --- a/doc/prettier.txt +++ b/doc/prettier.txt @@ -89,7 +89,24 @@ You can check what is the resolved `prettier` cli path by: You can check what is the resolved `prettier` cli version by: > :PrettierCliVersion -< + +You can send to prettier your entire buffer but ensure that it +formats only your selection. + +**note: ** differs from `:PrettierFragment` by sending the entire buffer +to prettier, allowing identation level to be preserved, but it requires +the whole file to be valid. +> + :PrettierPartial + +You can send to prettier your current selection as a fragment of same type as +the file being edited. + +**note: ** differs from `:PrettierPartial` by sending only the current selection +to prettier, this allows for faster formatting but wont preserve indentation. +> + :PrettierFragment + ============================================================================== CONFIGURATION *vim-prettier-configuration* -- cgit v1.3 From cacdf22fdeef9998d304be2e6330d14b4cd1d3d7 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Sat, 14 Sep 2019 04:55:30 +0000 Subject: issues/184-fixing-undo-step - Writting should not trigger autocomands - when formatting files we should not trigger auto commands --- autoload/prettier/job/async/neovim.vim | 4 ++-- autoload/prettier/utils/buffer.vim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/autoload/prettier/job/async/neovim.vim b/autoload/prettier/job/async/neovim.vim index 1cffe5c..28ecb8a 100644 --- a/autoload/prettier/job/async/neovim.vim +++ b/autoload/prettier/job/async/neovim.vim @@ -73,7 +73,7 @@ function! s:onExit(status, info, out, err) abort try silent exec 'sp '. escape(bufname(a:info.buf_nr), ' \') call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out) - write + noautocmd write catch call prettier#logging#error#log('PARSING_ERROR') finally @@ -91,7 +91,7 @@ function! s:onExit(status, info, out, err) abort " TODO " we should be auto saving in order to work similar to vim8 call nvim_buf_set_lines(a:info.buf_nr, a:info.start, a:info.end, 0, l:out) - write + noautocmd write endif let s:prettier_job_running = 0 diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index c22fec7..d09c568 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -28,7 +28,7 @@ endfunction " Replace and save the buffer function! prettier#utils#buffer#replaceAndSave(lines, startSelection, endSelection) abort call prettier#utils#buffer#replace(a:lines, a:startSelection, a:endSelection) - write + noautocmd write endfunction " Returns 1 if content has changed -- cgit v1.3 From 87f217f953b64103e8bef48980c0f002aa0f00c7 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Sat, 14 Sep 2019 05:17:33 +0000 Subject: issues/184-fixing-undo-step - Fixing undo step - in order to bypass vimdefaults we can fake a change prior to formatting and merging that back to undostack operation --- autoload/prettier/utils/buffer.vim | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index d09c568..821abda 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -8,6 +8,12 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo return endif + " https://vim.fandom.com/wiki/Restore_the_cursor_position_after_undoing_text_change_made_by_a_script + " create a fake change entry and merge with undo stack prior to do formating + normal ix + normal x + try | silent undojoin | catch | endtry + " delete all lines on the current buffer silent! execute '%delete _' @@ -23,6 +29,7 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo " Restore view call winrestview(l:winview) + endfunction " Replace and save the buffer -- cgit v1.3 From 46fd3290d1c69fb394a79e5174e7c1aa69b885c0 Mon Sep 17 00:00:00 2001 From: mitermayer Date: Sat, 14 Sep 2019 05:27:16 +0000 Subject: issues/184-fixing-undo-step - fixing linting --- autoload/prettier/utils/buffer.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'autoload/prettier/utils/buffer.vim') diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim index 821abda..7bc7ab1 100644 --- a/autoload/prettier/utils/buffer.vim +++ b/autoload/prettier/utils/buffer.vim @@ -10,8 +10,8 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo " https://vim.fandom.com/wiki/Restore_the_cursor_position_after_undoing_text_change_made_by_a_script " create a fake change entry and merge with undo stack prior to do formating - normal ix - normal x + normal! ix + normal! x try | silent undojoin | catch | endtry " delete all lines on the current buffer -- cgit v1.3