dawnbeen / c_formatter_42

C language formatter for 42 norminette
GNU General Public License v3.0
176 stars 18 forks source link

Add exception handling #24

Closed cacharle closed 2 years ago

cacharle commented 2 years ago

Any exception during the execution of the formatter should restore the current file to it's previous state.

The downside of this is that the formatter will fail silently if there is an error.

One solution to that could be to add a line at the beginning or end of the file with the error (can't be seen by the user if he isn't looking at those lines, but still better than silent).

keyhr commented 2 years ago

The python program already emits errors messages when catching exception, so I'm not sure but I think handling with the messages is users' (such as plugins using this package) responsibilities. What do you think?

For example, vscode run on save extension, which we're using a solution to use the formatter in vscode, allows me to see the messages while executing. (It doesn't automatically appear though)

Screen Shot 2022-02-04 at 11 07 35
keyhr commented 2 years ago

I made a simple example of handling them in vim script:

function! s:c_formatter_42()
    let filepath = expand('%:p')
    redir => message
        silent execute '!python3 -m c_formatter_42 ' . filepath
    redir END
    if v:shell_error
        echo message
    endif
endfunction

augroup fmt
    autocmd!
    autocmd BufWritePost *.[ch] call s:c_formatter_42() | edit
augroup END
cacharle commented 2 years ago

I also think it should be the extensions's job to detect errors but I didn't knew runOnSave could detect them (thought it would just overwrite the file contects with the error)

Thank you for the vimscript example.

keyhr commented 2 years ago

thought it would just overwrite the file contects with the error

That's actually my fault. I guess that you tried my run-on-save config, which makes it just overwrites the contents even if on error. (I had assumed that the formatter can't modify the file content, and used pipes to replace file contents with stdout)

"cmd": "python3 -m c_formatter_42 < ${file} | tee _cfdump && cat _cfdump | tee ${file} && rm -f _cfdump""cmd": "python3 -m c_formatter_42 ${file}"