Skip to content

WIP: Fix format before save #11

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

Merged
merged 1 commit into from
Aug 7, 2017
Merged
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ Auto formatting is disabled by default, so you have to enable it in your `.vimrc
```vim
" Enable rufo (RUby FOrmat)
let g:rufo_auto_formatting = 1

" Enable format before saving
let g:rufo_format_before_saving = 1
```

Next time when you save the file code will be formatted

## Partial formatting

For formatting some part of the code select lines with Shift+V and call `:Rufo` command
Expand Down
82 changes: 37 additions & 45 deletions autoload/rufo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,62 @@ function! rufo#Format(start_line, end_line, count) abort
end
endf

function! rufo#FormatBeforeSave() abort
if g:rufo_format_before_saving
call s:format_file()
endif
endf

function! rufo#AutoFormat() abort
if g:rufo_auto_formatting
call s:format_file()
endif
endf

function! s:format_file() abort
let l:filename = expand('%')

let l:curw = {}
try
mkview!
catch
let l:curw = winsaveview()
endtry
let l:current_view = winsaveview()

let l:out = system('rufo ' . l:filename)
try | silent undojoin | catch | endtry
silent edit!
let &syntax = &syntax

if s:formatting_failed(l:out)
let [l:start_line, l:end_line] = [1, '$']
let [l:formatting_failed, l:out] = s:format(l:start_line, l:end_line)
if l:formatting_failed
call s:show_error(l:out)
else
call s:replace(l:start_line, l:end_line, l:out)
silent exec l:end_line . 'delete _'
endif

call winrestview(l:curw)
call winrestview(l:current_view)
endf

function! s:format_selection(start_line, end_line) abort
let l:selection = getline(a:start_line, a:end_line)
let l:tempfile = tempname()
let l:last_line_is_empty_line_between_code_blocks = getline(a:end_line) == '' && getline(a:end_line + 1) != ''

call writefile(l:selection, l:tempfile)
let l:out = system('rufo ' . l:tempfile)

if s:formatting_failed(l:out)
let [l:formatting_failed, l:out] = s:format(a:start_line, a:end_line)
if l:formatting_failed
call s:show_error(l:out)
return
endif
else
call s:replace(a:start_line, a:end_line, l:out)

let l:result = readfile(l:tempfile)
silent exec a:start_line . ',' . a:end_line . ' delete'
call append(a:start_line - 1, l:result)
call delete(l:tempfile)
" restore indentation
silent exec 'normal! ' . a:start_line . 'GV' . a:end_line . 'G='

" restore indentation
silent exec 'normal! ' . a:start_line . 'GV' . a:end_line . 'G='
" keep empty line between code blocks
if l:last_line_is_empty_line_between_code_blocks
call append(a:end_line - 1, '')
endif
endif
endf

" keep empty line between code blocks
if l:last_line_is_empty_line_between_code_blocks
call append(a:end_line - 1, '')
function! s:format(start_line, end_line) abort
let l:buffer_number = bufnr(s:default_buffer_name)
if buffer_exists(l:buffer_number)
silent exec l:buffer_number . 'bdelete'
endif

let l:selection = join(getline(a:start_line, a:end_line), '\n')
let l:out = systemlist('echo ' . shellescape(l:selection) . '| rufo')
return [s:formatting_failed(l:out), l:out]
endf

function! s:formatting_failed(message) abort
let l:message = split(a:message, '\n')
return empty(l:message) ? 0 : l:message[0] !~ 'Format'
return a:message[0] =~ 'Error'
endf

function! s:show_error(message) abort
let l:message = split(a:message, '\n')
let l:buffer_position = get(s:buffer_positions, g:rufo_errors_buffer_position, s:buffer_positions.bottom)
let l:buffer_number = bufnr(s:default_buffer_name)

Expand All @@ -99,13 +87,17 @@ function! s:show_error(message) abort
endif

setlocal modifiable
silent exec '0,' . line('$') . 'delete'
call append(0, l:message)
call s:replace(1, line('$'), a:message)
else
silent exec l:buffer_position . ' new ' . s:default_buffer_name
call append(0, l:message)
call append(0, a:message)
setlocal buftype=nofile bufhidden=hide nobuflisted noswapfile
endif
silent exec line('$') . 'delete'
silent exec line('$') . 'delete _'
setlocal nomodifiable
endf

func! s:replace(start_line, end_line, text) abort
silent exec a:start_line . ',' . a:end_line . 'delete _'
call append(a:start_line - 1, a:text)
endfunc
15 changes: 3 additions & 12 deletions plugin/rufo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ endif

let g:rufo_loaded = 1

if !exists('g:rufo_format_before_saving')
let g:rufo_format_before_saving = 0
end

if !exists('g:rufo_auto_formatting')
let g:rufo_auto_formatting = 0
end
Expand All @@ -23,17 +19,12 @@ function! s:init_commands()
command! RufoToggle let g:rufo_auto_formatting = !g:rufo_auto_formatting
endf

augroup RufoFormatBeforeSave
augroup RufoCommands
autocmd!
autocmd BufWritePre *.rb silent! call rufo#FormatBeforeSave()
autocmd FileType ruby call s:init_commands()
augroup END

augroup RufoAutoFormat
autocmd!
autocmd BufWritePost *.rb silent! call rufo#AutoFormat()
augroup END

augroup RufoCommands
autocmd!
autocmd FileType ruby call s:init_commands()
autocmd BufWritePre *.rb silent! call rufo#AutoFormat()
augroup END