Open 0xC0FFEE opened 7 years ago
Currently, visual selection works by sending the selected text to the stdin of the formatter.
The resulting output can vary greatly depending on the formatter.
For example with js-beautify
and running :Neoformat
after selecting lines 3 and 4 of the following code:
function main()
{
var a = 1;
const b = false;
}
Will not make any change since js-beautify
maintains the indentation.
This is more because visual selection is intended to not take into account the surrounding area, since you could select an entire block or just format the whole buffer. Otherwise, doing things like formatting code embedded in a markdown document would give weird results because it would run into invalid syntax.
The current way of formatting individual parts of a file is totally unusable for me. In my workflow I only format lines which I modified previously, to leave all other lines in this file unchanged. This behavior is possible to implement as seen in clang-format.py or vim-clang-format.
@Alok formatting embedded code is an edge case for me, which should be handled separately.
So ideally, instead of just using the current selection, Neoformat would use the entire buffer, but only update the selected area?
This would be the naive way to implement this and I'm not really sure if a line-wise mapping from pre-formatted to post-formatted content is always possible. clang-format OTOH provides the command line argument --lines
which specifies the exact part(s) of the file which should be formatted. This makes the (maybe problematic) mapping of lines superfluous.
Edit: I'm not sure if other formatters support a similar option, but this is the only sane implementation I can think of to support partial formatting.
It looks like this comes down to an issue of which workflow is to be preferred as the default behavior. I'm in favor of formatting only what was passed in the visual selection because it's more in line with how STDIN-based formatters are meant to work, and also because that happens to work well for embedded filetypes.
I'm interested in this as well - formatting a few lines at a time is what I'm comfortable with right now.
I could see it working exactly how it does now with one simple addition:
prettier-standard
is what I'm using for javascript (via autocmd FileType javascript set formatprg=prettier-standard\ --stdin
) and this is basically what I do manually to make things work.
This works well for me for several months, but after a update in 2018-03-01, Neoformat switch to bad with a result same as Same as 0xC0FFEE commented on Jan 13, 2017, can not format visula selection.
Having run into issues with other formatters which require the whole input and don't work well with just a bit of visual mode selection, have you guys come up with any solution or workaround to this?
Thank you for your great plugin, i'm really enjoying it so far. But I encountered a possible shortcoming. The c++ code below was formatted with clang-format (via normal mode
:Neoformat
) and the result is perfectly fine.But as soon as I try to format a visual selection the formatting is off. Below is the result of only formatting line 3 in visual mode by calling
:Neoformat
:If line 3 and 4 are selected:
If lines 2 to 5 are selected the formatting matches listing 1 :+1:. my guess is that the visual selection is formatted out of any surrounding context which leads to incorrect results.