Fix repositioning the cursor at the end of a range format (#10938)

* Adjust the upper bound on cursorOffset when range formatting

* fix implementation

* Add changelog entry

* Update changelog entry to use PR number
This commit is contained in:
Jed Fox authored and GitHub committed 2021-05-24 02:49:49 +09:00
1 parent 1f9cbe436d
commit 261bb0314f
7 files changed
+112 -2

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#### Track cursor position properly when it’s at the end of the range to format (#10938 by @j-f1)
Previously, if the cursor was at the end of the range to format, it would simply be placed back at the end of the updated range.
Now, it will be repositioned if Prettier decides to add additional code to the end of the range (such as a semicolon).
<!-- prettier-ignore -->
```jsx
// Input (<|> represents the cursor)
const someVariable = myOtherVariable<|>
// range to format: ^^^^^^^^^^^^^^^
// Prettier stable
const someVariable = myOtherVariable;<|>
// range to format: ^^^^^^^^^^^^^^^
// Prettier main
const someVariable = myOtherVariable<|>;
// range to format: ^^^^^^^^^^^^^^^
```
+2 -2
View File
@@ -168,7 +168,7 @@ function formatRange(originalText, opts) {
rangeEnd: Number.POSITIVE_INFINITY,
// Track the cursor offset only if it's within our range
cursorOffset:
opts.cursorOffset > rangeStart && opts.cursorOffset < rangeEnd
opts.cursorOffset > rangeStart && opts.cursorOffset <= rangeEnd
? opts.cursorOffset - rangeStart
: -1,
// Always use `lf` to format, we'll replace it later
@@ -182,7 +182,7 @@ function formatRange(originalText, opts) {
const rangeTrimmed = rangeResult.formatted.trimEnd();
let { cursorOffset } = opts;
if (cursorOffset >= rangeEnd) {
if (cursorOffset > rangeEnd) {
// handle the case where the cursor was past the end of the range
cursorOffset += rangeTrimmed.length - rangeString.length;
} else if (rangeResult.cursorOffset >= 0) {
@@ -445,3 +445,86 @@ thisWontBeFormatted ( 1 ,3)
================================================================================
`;
exports[`range-5.js format 1`] = `
====================================options=====================================
cursorOffset: 23
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 23
rangeStart: 14
| | printWidth
=====================================input======================================
> 1 | const myVar = aFunction<|>
| ^^^^^^^^^
2 |
=====================================output=====================================
const myVar = aFunction<|>;
================================================================================
`;
exports[`range-6.js format 1`] = `
====================================options=====================================
cursorOffset: 24
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 24
rangeStart: 14
| | printWidth
=====================================input======================================
> 1 | const myVar = aFunction;<|>
| ^^^^^^^^^^
2 |
=====================================output=====================================
const myVar = aFunction;<|>
================================================================================
`;
exports[`range-7.js format 1`] = `
====================================options=====================================
cursorOffset: 23
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 24
rangeStart: 14
| | printWidth
=====================================input======================================
> 1 | const myVar = aFunction<|>;
| ^^^^^^^^^^^^^
2 |
=====================================output=====================================
const myVar = aFunction<|>;
================================================================================
`;
exports[`range-8.js format 1`] = `
====================================options=====================================
cursorOffset: 69
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 72
rangeStart: 30
| | printWidth
=====================================input======================================
1 | thisWontBeFormatted ( 1 ,3)
> 2 |
| ^
> 3 | thisWillBeFormatted (2 ,3, )<|>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 4 |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 5 | thisWontBeFormatted (2, 90 ,)
| ^
6 |
=====================================output=====================================
thisWontBeFormatted ( 1 ,3)
thisWillBeFormatted(2, 3)<|>;
thisWontBeFormatted (2, 90 ,)
================================================================================
`;
+1
View File
@@ -0,0 +1 @@
const myVar = <<<PRETTIER_RANGE_START>>>aFunction<|><<<PRETTIER_RANGE_END>>>
+1
View File
@@ -0,0 +1 @@
const myVar = <<<PRETTIER_RANGE_START>>>aFunction;<|><<<PRETTIER_RANGE_END>>>
+1
View File
@@ -0,0 +1 @@
const myVar = <<<PRETTIER_RANGE_START>>>aFunction<|>;<<<PRETTIER_RANGE_END>>>
+5
View File
@@ -0,0 +1,5 @@
thisWontBeFormatted ( 1 ,3)
<<<PRETTIER_RANGE_START>>>
thisWillBeFormatted (2 ,3, )<|>
<<<PRETTIER_RANGE_END>>> thisWontBeFormatted (2, 90 ,)