diff options
Diffstat (limited to 'autoload/prettier/utils')
| -rw-r--r-- | autoload/prettier/utils/buffer.vim | 70 | ||||
| -rw-r--r-- | autoload/prettier/utils/quickfix.vim | 22 | ||||
| -rw-r--r-- | autoload/prettier/utils/shim.vim | 8 |
3 files changed, 100 insertions, 0 deletions
diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim new file mode 100644 index 0000000..7bc7ab1 --- /dev/null +++ b/autoload/prettier/utils/buffer.vim @@ -0,0 +1,70 @@ +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 + + " 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 _' + + " replace all lines from the current buffer with output from prettier + let l:idx = 0 + for l:line in l:newBuffer + silent! call append(l:idx, l:line) + let l:idx += 1 + endfor + + " delete trailing newline introduced by the above append procedure + silent! execute '$delete _' + + " Restore view + 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) + noautocmd 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 +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 + +" Adapted from https://github.com/farazdagi/vim-go-ide +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 + 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/autoload/prettier/utils/quickfix.vim b/autoload/prettier/utils/quickfix.vim new file mode 100644 index 0000000..b4766a7 --- /dev/null +++ b/autoload/prettier/utils/quickfix.vim @@ -0,0 +1,22 @@ +" We use this flag so that we ensure only clearing quickfix if it was created by prettier itself +let s:prettier_quickfix_open = 0 + +function! prettier#utils#quickfix#close() abort + " close quickfix if it is opened + if s:prettier_quickfix_open + call setqflist([], 'r') + cclose + let s:prettier_quickfix_open = 0 + endif +endfunction + +function! prettier#utils#quickfix#open(errors, focus) abort + let s:prettier_quickfix_open = 1 + let l:winnr = winnr() + call setqflist(a:errors, 'r') + botright copen + if !a:focus + " Return the cursor back to the main buffer. + exe l:winnr . 'wincmd w' + endif +endfunction diff --git a/autoload/prettier/utils/shim.vim b/autoload/prettier/utils/shim.vim new file mode 100644 index 0000000..228829e --- /dev/null +++ b/autoload/prettier/utils/shim.vim @@ -0,0 +1,8 @@ +" Backwards compatable version of shiftwidth() +function! prettier#utils#shim#shiftwidth() abort + if exists('*shiftwidth') + return shiftwidth() + else + return &shiftwidth + endif +endfunction |
