Permalink
Browse files

Add code formatting via `format-code`

Closes #274
  • Loading branch information...
christoph-frick authored and tpope committed Aug 3, 2016
1 parent 02ef84d commit 2193122e13fdf9e9af30475d8d9a90746234c1d8
Showing with 42 additions and 0 deletions.
  1. +42 −0 plugin/fireplace.vim
@@ -1599,6 +1599,48 @@ augroup fireplace_go_to_file
autocmd FileType clojure call s:set_up_go_to_file()
augroup END

" Section: Formatting

function! fireplace#format(lnum, count, char) abort
let reg_save = @@
let sel_save = &selection
let cb_save = &clipboard
try
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
" ignore leading empty lines
let lnum = a:lnum
let l:count = a:count
while l:count >= 0
if getline(lnum) =~# '^\s*$'
let lnum += 1
let l:count -= 1
else
break
endif
endwhile
if a:count

This comment has been minimized.

@christoph-frick

christoph-frick May 5, 2017

Author Contributor

Should that be l:count?

This comment has been minimized.

@tpope

tpope May 5, 2017

Owner

Yep. Your mistake not mine right? :)

This comment has been minimized.

@christoph-frick

christoph-frick May 5, 2017

Author Contributor

Yeah, sorry. All that a: and l: got me confused...

silent exe "normal! " . string(lnum) . "ggV" . string(l:count-1) . "jy"
let response = fireplace#message({'op': 'format-code', 'code': @@})[0]
if !empty(get(response, 'formatted-code'))
let @@ = substitute(get(response, 'formatted-code'), '^\n\+', '', 'g')
if @@ !~# '^\n*$'
normal! gvp
endif
endif
endif
finally
let @@ = reg_save
let &selection = sel_save
let &clipboard = cb_save
endtry
endfunction

augroup fireplace_formatting
autocmd!
autocmd FileType clojure
\ setlocal formatexpr=fireplace#format(v:lnum,v:count,v:char)
augroup END

" Section: Documentation

function! s:Lookup(ns, macro, arg) abort

0 comments on commit 2193122

Please sign in to comment.