diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index 7891c83d..a989e3d0 100644 --- a/autoload/neoformat.vim +++ b/autoload/neoformat.vim @@ -102,17 +102,21 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort call neoformat#utils#log(v:shell_error) let process_ran_succesfully = index(cmd.valid_exit_codes, v:shell_error) != -1 - + if cmd.stderr_log != '' call neoformat#utils#log('stderr output redirected to file' . cmd.stderr_log) 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 + " 1. append the lines that are before and after the formatted content let lines_after = getbufline(bufnr('%'), a:end_line + 1, '$') let lines_before = getbufline(bufnr('%'), 1, a:start_line - 1) + if get(definition, 'indent_output') + let stdout = s:indent_output(stdout, lines_before) + endif let new_buffer = lines_before + stdout + lines_after + if new_buffer !=# original_buffer call s:deletelines(len(new_buffer), line('$')) @@ -145,6 +149,29 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort endif endfunction +function! s:indent_output(output, lines_before) + if empty(a:output) + return [] + endif + + " guess indent based on the previous line + let prev_line = a:lines_before[-1] + " see if the line is indented with tabs + let tab_count = matchend(prev_line, '^\t*') + + if tab_count > 0 + let indent_str = repeat("\t", tab_count + 1) + else + let indent_str = repeat(' ', matchend(prev_line, '^\s*') + &shiftwidth) + endif + + for i in range(0, len(a:output) - 1, 1) + let a:output[i] = indent_str . a:output[i] + endfor + + return a:output +endfunction + function! s:get_enabled_formatters(filetype) abort if &formatprg != '' && neoformat#utils#var('neoformat_try_formatprg') call neoformat#utils#log('adding formatprg to enabled formatters') diff --git a/doc/neoformat.txt b/doc/neoformat.txt index 6b3ff62c..87e60175 100644 --- a/doc/neoformat.txt +++ b/doc/neoformat.txt @@ -96,6 +96,7 @@ Options: used when the `path` is in the middle of a command | default: 0 | optional | `env` | list of environment variables to prepend to the command | default: [] | optional +| `indent_output` | wether or not to indent the output automatically, based on the previous line | default: 0 | optional | `valid_exit_codes` | list of valid exit codes for formatters who do not respect common unix practices | \[0] | optional