From 1dd4288818b5386c1e9f64a36a640bdb1ea3b207 Mon Sep 17 00:00:00 2001 From: jpsouzasilva Date: Mon, 14 Jan 2019 03:08:35 -0300 Subject: [PATCH 1/2] Introduce formatter option 'indent_output' --- autoload/neoformat.vim | 27 +++++++++++++++++++++++++-- doc/neoformat.txt | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index 7891c83d..e3d04e02 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(a: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,25 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort endif endfunction +function! s:indent_output(output, lines_before) + " 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 From 9850f017c897d74b830384ec0665b0e04099e158 Mon Sep 17 00:00:00 2001 From: jpsouzasilva Date: Mon, 14 Jan 2019 20:54:42 -0300 Subject: [PATCH 2/2] Fix bug with variable name --- autoload/neoformat.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index e3d04e02..a989e3d0 100644 --- a/autoload/neoformat.vim +++ b/autoload/neoformat.vim @@ -112,7 +112,7 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort let lines_after = getbufline(bufnr('%'), a:end_line + 1, '$') let lines_before = getbufline(bufnr('%'), 1, a:start_line - 1) - if get(a:definition, 'indent_output') + if get(definition, 'indent_output') let stdout = s:indent_output(stdout, lines_before) endif let new_buffer = lines_before + stdout + lines_after @@ -150,6 +150,10 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort 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