Skip to content

Commit 42b1335

Browse files
author
Samir Benmendil
committed
Fix undo and partial format
1 parent 157cfe8 commit 42b1335

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Diff for: autoload/clang_format.vim

+14-8
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function! s:detect_style_file() abort
203203
endfunction
204204

205205
function! clang_format#format(line1, line2) abort
206-
let args = printf(' -lines=%d:%d', a:line1, a:line2)
206+
let args = ""
207207
if ! (g:clang_format#detect_style_file && s:detect_style_file())
208208
if g:clang_format#enable_fallback_style
209209
let args .= ' ' . s:shellescape(printf('-style=%s', s:make_style_options())) . ' '
@@ -219,8 +219,7 @@ function! clang_format#format(line1, line2) abort
219219
endif
220220
let args .= g:clang_format#extra_args
221221
let clang_format = printf('%s %s --', s:shellescape(g:clang_format#command), args)
222-
let source = join(getline(1, '$'), "\n")
223-
return s:system(clang_format, source)
222+
return s:system(clang_format, join(getline(a:line1, a:line2), "\n"))
224223
endfunction
225224
" }}}
226225

@@ -238,13 +237,20 @@ function! clang_format#replace(line1, line2, ...) abort
238237
let winview = winsaveview()
239238
let splitted = split(formatted, '\n', 1)
240239

241-
silent! undojoin
242-
if line('$') > len(splitted)
243-
execute len(splitted) .',$delete' '_'
244-
endif
245-
call setline(1, splitted)
240+
let fold_closed_save = foldclosed(line('.'))
241+
242+
let indent = indent(a:line1) / shiftwidth()
243+
silent exe a:line1.','a:line2.'delete _'
244+
245+
call append(a:line1 - 1, splitted)
246+
silent exe a:line1.";+".(len(splitted)-1).repeat('>', indent)
247+
246248
call winrestview(winview)
247249
call setpos('.', pos_save)
250+
251+
if fold_closed_save == -1
252+
foldopen!
253+
endif
248254
endfunction
249255
" }}}
250256

0 commit comments

Comments
 (0)