beautifier / js-beautify

Beautifier for javascript
https://beautifier.io
MIT License
8.62k stars 1.39k forks source link

Is it possible to format only some specific range of lines? #610

Open actionless opened 9 years ago

actionless commented 9 years ago

It's possible to cut those lines before passing them into the js-beautify, but in this case formatting will be out of context. So it will be nice to pass whole file contents to js-beautify but apply formatting only to some specific range of lines.

bitwiseman commented 9 years ago

We don't have a function for that. It sounds like an api change, but probably wouldn't be too hard.

You are welcome to submit a PR, but if nothing else could you give a more detailed description of what you think this feature would look like?

actionless commented 9 years ago

Unfortunately, i don't think i'm ready to hack it right now, so i will try to describe it.

So the idea is to format some piece of code, but leave the rest as is. And indentation of formatted piece of code should match unformatted part.

For example, in autopep8, that option described like that:

  --range line line     only fix errors found within this inclusive range of
                        line numbers (e.g. 1 99); line numbers are indexed at
                        1
bitwiseman commented 9 years ago

You said:

It's possible to cut those lines before passing them into the js-beautify, but in this case formatting will be out of context.

Could you give some examples of that?

actionless commented 9 years ago

input file:

var i, a, b, c, max;
max = 1000000000;
var d = Date.now();

for (i = 0; i < max; i++) {
a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i;
}

when passing to formatter only line №6 and concatenating whole text back we will have:

var i, a, b, c, max;
max = 1000000000;
var d = Date.now();

for (i = 0; i < max; i++) {
a = 1234 + 5678 + i;
b = 1234 * 5678 + i;
c = 1234 / 2 + i;
}

while it's expected to have:

var i, a, b, c, max;
max = 1000000000;
var d = Date.now();

for (i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
    c = 1234 / 2 + i;
}

ie, as i said above it's more about indentation, but also it can be some cases with parenthesis, when range includes only left or only right one