avh4 / elm-format

elm-format formats Elm source code according to a standard set of rules based on the official Elm Style Guide
BSD 3-Clause "New" or "Revised" License
1.31k stars 145 forks source link

Vim Integration seems to mess with how vim redraws the screen after a save. #93

Open cpdean opened 8 years ago

cpdean commented 8 years ago

I'm using elm-format ( elm-format 0.1-alpha3-dev ) with vim like this:

autocmd BufWritePost *.elm silent execute "!elm-format --yes %" | edit! | set filetype=elm

But on saving my display gets kind of messed up. Not really sure if this is a vim bug or something to do with elm-format, but when the format command exits the file gets redrawn sort of weird, everything gets shifted up a line. here's a screenshot of before saving:

conrad_ _tmux_ _176x56

And now after saving:

broke

I can fix my screen by running :redraw!.

redrawn

I've found that I can get around this issue by adding > /dev/null to the end of the format command:

autocmd BufWritePost *.elm silent execute "!elm-format --yes % > /dev/null" | edit! | set filetype=elm

I feel like there is a more correct fix out there, or there is something else wrong with my vim configuration. Why is this happening? What's the right way to fix this?

avh4 commented 8 years ago

I don't know vim that well, but I will take a look after I get the alpha3 release ready.

If anyone knows vim better, please chime in.

@Janiczek any thoughts on this?

Janiczek commented 8 years ago

I am admittedly not very well versed in Vimscript, and don't see the problem @cpdean describes on my vim setup.

Possible causes I can think of:

  1. vim 3rd party packages mess something up
  2. elm-format outputs something (as seen by the > /dev/null addition)

ad 1:

ad 2: I don't really see what could elm-format be outputting that trips up vim.

Well-formatted file:

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes formatted.elm | xxd
0000000: 5072 6f63 6573 7369 6e67 2066 696c 6520  Processing file 
0000010: 666f 726d 6174 7465 642e 656c 6d0a       formatted.elm.

stdout of badly formatted file:

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes _input.elm 2>/dev/null | xxd
0000000: 5072 6f63 6573 7369 6e67 2066 696c 6520  Processing file 
0000010: 5f69 6e70 7574 2e65 6c6d 0a45 5252 4f52  _input.elm.ERROR
0000020: 530a                                     S.

stderr of badly formatted file: (see the <ESC>[36m coloring codes)

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes _input.elm 1>/dev/null 2>tmp; xxd tmp
0000000: 1b5b 3336 6d2d 2d20 5359 4e54 4158 2050  .[36m-- SYNTAX P
0000010: 524f 424c 454d 202d 2d2d 2d2d 2d2d 2d2d  ROBLEM ---------
0000020: 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d  ----------------
0000030: 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d  ----------------
0000040: 2d2d 2d2d 2d2d 2d2d 2d2d 203c 6c6f 6361  ---------- <loca
0000050: 7469 6f6e 3e0a 0a1b 5b30 6d49 2072 616e  tion>...[0mI ran
0000060: 2069 6e74 6f20 736f 6d65 7468 696e 6720   into something 
0000070: 756e 6578 7065 6374 6564 2077 6865 6e20  unexpected when 
0000080: 7061 7273 696e 6720 796f 7572 2063 6f64  parsing your cod
0000090: 6521 0a0a 0a49 2061 6d20 6c6f 6f6b 696e  e!...I am lookin
00000a0: 6720 666f 7220 6f6e 6520 6f66 2074 6865  g for one of the
00000b0: 2066 6f6c 6c6f 7769 6e67 2074 6869 6e67   following thing
00000c0: 733a 0a0a 2020 2020 656e 6420 6f66 2069  s:..    end of i
00000d0: 6e70 7574 0a20 2020 2077 6869 7465 7370  nput.    whitesp
00000e0: 6163 650a 0a0a                           ace...
cpdean commented 8 years ago

Thanks for those trouble shooting tips. I'll go through those some time. Also, I should just test this in different terminals and outside of tmux as that could be messing with vim as well.

On Tue, Jan 5, 2016, 06:47 Martin Janiczek notifications@github.com wrote:

I am admittedly not very well versed in Vimscript, and don't see the problem @cpdean https://github.com/cpdean describes.

Possible causes I can think of:

  1. vim 3rd party packages mess something up
  2. elm-format outputs something (as seen by the > /dev/null addition)

ad 1:

  • either you can try to selectively disable plugins in .vim/ and lines in .vimrc and see if the problem persists and which one is the culprit
    • to see if you'll get anywhere with this approach, try vim -u NONE which runs barebones vim and prepare the format-on-save with :autocmd BufWritePost ... yourself. does the error still happen?
  • or maybe if you don't feel like it, share your .vim/ and .vimrc config and I can try to see if I reproduce the bug and do the pruning

ad 2: I don't really see what could elm-format be outputting that trips up vim.

  • Maybe that's some plugin not handling the file refresh well?
  • Or maybe it's that elm-format writes to stderr on error?
  • Or maybe the [36m and [0m coloring codes in stderr trip it up?

Well-formatted file:

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes formatted.elm | xxd 0000000: 5072 6f63 6573 7369 6e67 2066 696c 6520 Processing file 0000010: 666f 726d 6174 7465 642e 656c 6d0a formatted.elm.

stdout of badly formatted file:

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes _input.elm 2>/dev/null | xxd 0000000: 5072 6f63 6573 7369 6e67 2066 696c 6520 Processing file 0000010: 5f69 6e70 7574 2e65 6c6d 0a45 5252 4f52 _input.elm.ERROR 0000020: 530a S.

stderr of badly formatted file: (see the [36m coloring codes)

martin@janiczek-lenovo /var/www/html/elm-format $ elm-format --yes _input.elm 1>/dev/null 2>tmp; xxd tmp 0000000: 1b5b 3336 6d2d 2d20 5359 4e54 4158 2050 .[36m-- SYNTAX P 0000010: 524f 424c 454d 202d 2d2d 2d2d 2d2d 2d2d ROBLEM --------- 0000020: 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d ---------------- 0000030: 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d 2d2d ---------------- 0000040: 2d2d 2d2d 2d2d 2d2d 2d2d 203c 6c6f 6361 ---------- <loca 0000050: 7469 6f6e 3e0a 0a1b 5b30 6d49 2072 616e tion>...[0mI ran 0000060: 2069 6e74 6f20 736f 6d65 7468 696e 6720 into something 0000070: 756e 6578 7065 6374 6564 2077 6865 6e20 unexpected when 0000080: 7061 7273 696e 6720 796f 7572 2063 6f64 parsing your cod 0000090: 6521 0a0a 0a49 2061 6d20 6c6f 6f6b 696e e!...I am lookin 00000a0: 6720 666f 7220 6f6e 6520 6f66 2074 6865 g for one of the 00000b0: 2066 6f6c 6c6f 7769 6e67 2074 6869 6e67 following thing 00000c0: 733a 0a0a 2020 2020 656e 6420 6f66 2069 s:.. end of i 00000d0: 6e70 7574 0a20 2020 2077 6869 7465 7370 nput. whitesp 00000e0: 6163 650a 0a0a ace...

— Reply to this email directly or view it on GitHub https://github.com/avh4/elm-format/issues/93#issuecomment-168979475.

Janiczek commented 8 years ago

@romanzolotarev has been using elm-format in Vim this way:

autocmd BufWrite *.elm setlocal filetype=elm
autocmd BufWritePost *.elm silent execute '!elm-format --yes %' | edit! | setlocal filetype=elm | redraw!

Maybe the more defensive the better?

romanzolotarev commented 8 years ago

@Janiczek thanks for mention. This is kind of hacky solution. Not sure if running elm-format on every save is a good idea.

I would prefer to have Elm linter integrated with https://github.com/scrooloose/syntastic.

cpdean commented 8 years ago

I can add that to the list of things to try.

something i've noticed right off the bat is getting stuff from stderr still comes through my way, so it'll tell me if there are compile errors right away.

granted the error message will be sprinkled into my code because of the redraw errors but at least i know something i've done is wrong

On Wed, Jan 6, 2016 at 7:56 PM, Martin Janiczek notifications@github.com wrote:

@romanzolotarev https://github.com/romanzolotarev has been using elm-format in Vim this way:

autocmd BufWrite .elm setlocal filetype=elmautocmd BufWritePost .elm silent execute '!elm-format --yes %' | edit! | setlocal filetype=elm | redraw!

Maybe the more defensive the better?

— Reply to this email directly or view it on GitHub https://github.com/avh4/elm-format/issues/93#issuecomment-169512445.

avh4 commented 8 years ago

I am seeing this on a bare vim installation Vi IMproved 7.3. I'm not sure there's a good fix for the autocmd. I think the output could be hidden by redirecting stderr to /dev/null, but then the error messages would be hidden if there's a syntax error when parsing the file. I don't see anything obvious to be done for elm-format-0.2.

I think think ideally we should have Vim support instructions that use some Vim plugin that can handle console output and display it in a reasonable way.

jergason commented 8 years ago

I can second the recommendation for redraw!. This made it in to my .vimrc file:

augroup format_elm " {
  autocmd BufWritePost *.elm silent execute "!elm-format --yes %" | edit! | redraw!| setlocal ft=elm
augroup END " }
mgold commented 8 years ago

That works for me (vim 7.4), except when there's a compiler error which is flashed quickly and then your code isn't formatted. Piping to /dev/null doesn't help.

leafgarland commented 8 years ago

Vim has commands for filtering files, so you might find it works better than changing the source file and reloading. The command below will filter the whole file. As it is a command it can be undone, repeated and will not change the file on disk. There is no special handling of errors, so any error text will replace your buffer but at least you can read it, undo and fix.

:%!elm-format --stdin

There is also Vim's equalprg option which can be used to format text using an external program but this is perhaps less useful with elm-format as elm-format doesn't yet handle snippets of code, only whole files.

:setlocal equalprg=elm-format\ --stdin

Then you can use = command over the whole buffer to format.

ahstro commented 6 years ago

The commands found in here seems to work fine for me in Vim 7.4 and Neovim 0.2.0, and I've never had a trouble neither with elm-vim nor Neoformat, which I assume will be the solution most people default to.