From 92297e340647c33e21373eaff007d0a7bb4eb34b Mon Sep 17 00:00:00 2001 From: "Tejasvi S. Tomar" <45873379+tejasvi@users.noreply.github.com> Date: Wed, 26 Jan 2022 16:23:32 +0530 Subject: [PATCH] Parsimonious buffer updates Update only the lines which need updating. Prevents unnecessary computations for plugins which listen to buffer change events. --- autoload/clang_format.vim | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/autoload/clang_format.vim b/autoload/clang_format.vim index f4ff380..7d6aaf7 100644 --- a/autoload/clang_format.vim +++ b/autoload/clang_format.vim @@ -260,13 +260,19 @@ function! clang_format#replace_ranges(ranges, ...) abort endif let winview = winsaveview() - let splitted = split(formatted, '\n', 1) silent! undojoin - if line('$') > len(splitted) - execute len(splitted) .',$delete' '_' + let line_num = 0 + for line in split(formatted, '\n', 1) + let line_num += 1 + if line != getline(line_num) + call setline(line_num, line) + endif + endfor + if line('$') > line_num + execute (line_num + 1) .',$delete' '_' endif - call setline(1, splitted) + call winrestview(winview) call setpos('.', pos_save) endfunction