Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues when running multiple formatters and one formatter add/removes lines #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions autoload/neoformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort

let formatters_failed = []
let formatters_changed = []
let end_line = a:end_line
for formatter in formatters

if &formatprg != '' && split(&formatprg)[0] ==# formatter
Expand Down Expand Up @@ -75,7 +76,7 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort
continue
endif

let stdin = getbufline(bufnr('%'), a:start_line, a:end_line)
let stdin = getbufline(bufnr('%'), a:start_line, end_line)
let original_buffer = getbufline(bufnr('%'), 1, '$')

call neoformat#utils#log(stdin)
Expand Down Expand Up @@ -108,12 +109,15 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort
call neoformat#utils#log_file_content(cmd.stderr_log)
endif
if process_ran_succesfully
" 1. append the lines that are before and after the formatterd content
let lines_after = getbufline(bufnr('%'), a:end_line + 1, '$')
" 1. append the lines that are before and after the formatted content
let lines_after = getbufline(bufnr('%'), end_line + 1, '$')
let lines_before = getbufline(bufnr('%'), 1, a:start_line - 1)

let new_buffer = lines_before + stdout + lines_after
if new_buffer !=# original_buffer
" previous formatter may have added or removed lines which moves
" the end_line for the next formatter
let end_line = end_line + len(stdout) - len(stdin)

call s:deletelines(len(new_buffer), line('$'))

Expand Down