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

[Do Not Merge] Pad formatter output based on the previous line #206

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 29 additions & 2 deletions autoload/neoformat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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('$'))
Expand Down Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions doc/neoformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down