From 097cf106e340cd9d6b3f438d2b76329cc7173b51 Mon Sep 17 00:00:00 2001 From: Achal Dave Date: Mon, 2 Nov 2015 16:45:58 -0500 Subject: [PATCH 1/3] Fixes #61: Catch yapf errors instead of overwriting file --- autoload/codefmt.vim | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index a9b356b..11b059c 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -437,12 +437,17 @@ function! codefmt#GetYAPFFormatter() abort let l:cmd = [l:executable, '--lines=' . a:startline . '-' . a:endline] let l:input = join(l:lines, "\n") - let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0) - let l:formatted = split(l:result.stdout, "\n") + try + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() + let l:formatted = split(l:result.stdout, "\n") + + let l:full_formatted = l:formatted - let l:full_formatted = l:formatted + call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) + catch /ERROR(ShellError):/ + call maktaba#error#Shout('Error formatting file: %s', v:exception) + endtry - call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) endfunction return l:formatter From a5c8fda8cd3859d07420af82c0f11ef91ed2de66 Mon Sep 17 00:00:00 2001 From: Achal Dave Date: Mon, 2 Nov 2015 16:55:54 -0500 Subject: [PATCH 2/3] Delete unnecessary reassignment --- autoload/codefmt.vim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index 11b059c..b9bd5c9 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -441,9 +441,7 @@ function! codefmt#GetYAPFFormatter() abort let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() let l:formatted = split(l:result.stdout, "\n") - let l:full_formatted = l:formatted - - call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) catch /ERROR(ShellError):/ call maktaba#error#Shout('Error formatting file: %s', v:exception) endtry From 41ed647af34feb043cbed32c794c237a75cec17d Mon Sep 17 00:00:00 2001 From: Achal Dave Date: Mon, 2 Nov 2015 17:43:17 -0500 Subject: [PATCH 3/3] Handle yapf returning -2 yapf only returns an error code of 0 if it does not modify the file. If it does modify the file, it returns error code 2, which causes maktaba to throw a ShellError. Now, we just manually check the error code instead of attempting to catch the exception. --- autoload/codefmt.vim | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index b9bd5c9..1cd9bca 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -437,15 +437,14 @@ function! codefmt#GetYAPFFormatter() abort let l:cmd = [l:executable, '--lines=' . a:startline . '-' . a:endline] let l:input = join(l:lines, "\n") - try - let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call() - let l:formatted = split(l:result.stdout, "\n") - - call maktaba#buffer#Overwrite(1, line('$'), l:formatted) - catch /ERROR(ShellError):/ - call maktaba#error#Shout('Error formatting file: %s', v:exception) - endtry + let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0) + if v:shell_error == 1 " Indicates an error with parsing + call maktaba#error#Shout('Error formatting file: %s', l:result.stderr) + return + endif + let l:formatted = split(l:result.stdout, "\n") + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) endfunction return l:formatter