-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add --range-start and --range-end options to format only parts of the input
#1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
62bf068
5c97525
564bf13
c09e19e
3af3f01
8c99117
44bfd9b
91372be
c1a61eb
1e60895
2ca9f18
bec69d0
070b941
e1c993e
77a8fb0
ef3b992
ee94a37
7823f75
990a18c
db0ec7a
149e18c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
NOTE: This doesn't pass its test yet. Note that since we're reading the indentation from the first line, it is expected not to change. However, a semicolon is added, and the lines outside the range are not changed. The new approach is roughly: * Require that the range exactly covers an integer number of lines of the input * Detect the indentation of the line the range starts on * Format the range's substring using `printAstToDoc` * Add enough `indent`s to the doc to restore the detected indentation * Format the doc to a string with `printDocToString` * Prepend/append the original input before/after the range See #1609 (comment) --- Given `tests/range/range.js`, run the following: prettier tests/range/range.js --range-start 165 --range-end 246 See the range's text with: dd if=tests/range/range.js ibs=1 skip=165 count=81 2>/dev/null
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,10 +64,7 @@ function genericPrint(path, options, printPath, args) { | |
| var node = path.getValue(); | ||
|
|
||
| // Escape hatch | ||
| if ( | ||
| hasPrettierIgnoreComment(node) || | ||
| util.isOutsideRange(node, options.rangeStart, options.rangeEnd) | ||
| ) { | ||
| if (hasPrettierIgnoreComment(node)) { | ||
| return options.originalText.slice(util.locStart(node), util.locEnd(node)); | ||
| } | ||
|
|
||
|
|
@@ -4251,7 +4248,7 @@ function removeLines(doc) { | |
| }); | ||
| } | ||
|
|
||
| function printAstToDoc(ast, options) { | ||
| function printAstToDoc(ast, options, addIndents = 0) { | ||
| function printGenerically(path, args) { | ||
| return comments.printComments( | ||
| path, | ||
|
|
@@ -4263,7 +4260,14 @@ function printAstToDoc(ast, options) { | |
|
|
||
| const doc = printGenerically(FastPath.from(ast)); | ||
| docUtils.propagateBreaks(doc); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should likely be done after you add hardline as hardline intro a break. This isn't going to do anything wrong here but it would be better to do the "right" thing as if we're adding some more code that relies on this, it's going to break in unexpected ways.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I hadn't considered that (I didn't really think about what |
||
| return doc; | ||
| return addIndentsToDoc(doc, addIndents); | ||
| } | ||
|
|
||
| function addIndentsToDoc(doc, numIndents) { | ||
| if (numIndents <= 0) { | ||
| return doc; | ||
| } | ||
| return addIndentsToDoc(indent(doc), numIndents - 1); | ||
| } | ||
|
|
||
| module.exports = { printAstToDoc }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,24 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`range.js 1`] = ` | ||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| } | ||
|
|
||
| // will be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
| \`multiline template string | ||
| with too much indentation\` | ||
| } | ||
|
|
||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| \`multiline template string | ||
| with too much indentation\` | ||
| // The [165, 246) range selects the above two lines, including the second newline | ||
| } | ||
| } | ||
| } | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| } | ||
|
|
||
| // will be formatted | ||
| function ugly(has, unPrettier, param, spacing) { | ||
| \`multiline template string | ||
| with too much indentation\`; | ||
| } | ||
|
|
||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| \`multiline template string | ||
| with too much indentation\`; | ||
| // The [165, 246) range selects the above two lines, including the second newline | ||
| } | ||
| } | ||
| } | ||
|
|
||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| run_spec(__dirname, { rangeStart: 103, rangeEnd: 159 }); | ||
| run_spec(__dirname, { rangeStart: 165, rangeEnd: 246 }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,9 @@ | ||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| } | ||
|
|
||
| // will be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
| `multiline template string | ||
| with too much indentation` | ||
| } | ||
|
|
||
| // will NOT be formatted | ||
| function ugly ( has , unPrettier , param,spacing) { | ||
|
|
||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| function ugly ( {a=1, b = 2 } ) { | ||
| `multiline template string | ||
| with too much indentation` | ||
| // The [165, 246) range selects the above two lines, including the second newline | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at template literal code, I have some piece of code that is much more precise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, that is quite a lot better! Fixed in e1c993e and 77a8fb0