Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e30f66

Browse files
Remove claim, untrue since over 5 years ago, that cursorOffset is incompatible with rangeStart/rangeEnd (#15750)
1 parent 39e4e7b commit 1e30f66

File tree

15 files changed

+18
-51
lines changed

15 files changed

+18
-51
lines changed

changelog_unreleased/misc/15750.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### Fix false claim in docs that cursorOffset is incompatible with rangeStart/rangeEnd (#15750 by @ExplodingCabbage)
2+
3+
The cursorOffset option has in fact been compatible with rangeStart/rangeEnd for over 5 years, thanks to work by @ds300. However, Prettier's documentation (including the CLI `--help` text) continued to claim otherwise, falsely. The documentation is now fixed.

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ await prettier.format("foo ( );", { semi: false, parser: "babel" });
2626

2727
`formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code. This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
2828

29-
The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`.
29+
The `cursorOffset` option should be provided, to specify where the cursor is.
3030

3131
```js
3232
await prettier.formatWithCursor(" 1", { cursorOffset: 2, parser: "babel" });

docs/options.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ These two options can be used to format code starting and ending at a given char
254254
- Backwards to the start of the first line containing the selected statement.
255255
- Forwards to the end of the selected statement.
256256

257-
These options cannot be used with `cursorOffset`.
258-
259257
| Default | CLI Override | API Override |
260258
| ---------- | --------------------- | ------------------- |
261259
| `0` | `--range-start <int>` | `rangeStart: <int>` |

src/index.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,6 @@ export interface CursorOptions extends Options {
548548
* Specify where the cursor is.
549549
*/
550550
cursorOffset: number;
551-
rangeStart?: never;
552-
rangeEnd?: never;
553551
}
554552

555553
export interface CursorResult {
@@ -572,7 +570,7 @@ export function check(source: string, options?: Options): Promise<boolean>;
572570
* `formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code.
573571
* This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
574572
*
575-
* The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`.
573+
* The `cursorOffset` option should be provided, to specify where the cursor is.
576574
*/
577575
export function formatWithCursor(
578576
source: string,

src/main/core-options.evaluate.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ const options = {
4949
type: "int",
5050
default: -1,
5151
range: { start: -1, end: Number.POSITIVE_INFINITY, step: 1 },
52-
description: outdent`
53-
Print (to stderr) where a cursor at the given position would move to after formatting.
54-
This option cannot be used with --range-start and --range-end.
55-
`,
52+
description:
53+
"Print (to stderr) where a cursor at the given position would move to after formatting.",
5654
cliCategory: CATEGORY_EDITOR,
5755
},
5856
endOfLine: {
@@ -160,7 +158,6 @@ const options = {
160158
description: outdent`
161159
Format code ending at a given character offset (exclusive).
162160
The range will extend forwards to the end of the selected statement.
163-
This option cannot be used with --cursor-offset.
164161
`,
165162
cliCategory: CATEGORY_EDITOR,
166163
},
@@ -172,7 +169,6 @@ const options = {
172169
description: outdent`
173170
Format code starting at a given character offset.
174171
The range will extend backwards to the start of the first line containing the selected statement.
175-
This option cannot be used with --cursor-offset.
176172
`,
177173
cliCategory: CATEGORY_EDITOR,
178174
},

src/standalone.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CursorOptions, CursorResult, Options, SupportInfo } from "./index.js";
44
* formatWithCursor both formats the code, and translates a cursor position from unformatted code to formatted code.
55
* This is useful for editor integrations, to prevent the cursor from moving when code is formatted
66
*
7-
* The cursorOffset option should be provided, to specify where the cursor is. This option cannot be used with rangeStart and rangeEnd.
7+
* The cursorOffset option should be provided, to specify where the cursor is.
88
*
99
* ```js
1010
* await prettier.formatWithCursor(" 1", { cursorOffset: 2, parser: "babel" });

tests/dts/unit/cases/standalone.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ prettierStandalone.formatWithCursor(" 1", { cursorOffset: 2, parser: "babel" });
44
prettierStandalone.formatWithCursor(" 1", {
55
cursorOffset: 2,
66
parser: "babel",
7-
// @ts-expect-error
87
rangeStart: 2,
98
});
109
prettierStandalone.formatWithCursor(" 1", {
1110
cursorOffset: 2,
1211
parser: "babel",
13-
// @ts-expect-error
1412
rangeEnd: 2,
1513
});
1614
prettierStandalone.format(" 1", { parser: "babel" });

tests/format/markdown/markdown/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ This is similar to the \`--list-different\` parameter in the CLI and is useful f
465465
\`formatWithCursor\` both formats the code, and translates a cursor position from unformatted code to formatted code.
466466
This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
467467
468-
The \`cursorOffset\` option should be provided, to specify where the cursor is. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
468+
The \`cursorOffset\` option should be provided, to specify where the cursor is.
469469
470470
\`\`\`js
471471
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
@@ -646,8 +646,6 @@ These two options can be used to format code starting and ending at a given char
646646
* Backwards to the start of the first line containing the selected statement.
647647
* Forwards to the end of the selected statement.
648648
649-
These options cannot be used with \`cursorOffset\`.
650-
651649
Default | CLI Override | API Override
652650
--------|--------------|-------------
653651
\`0\` | \`--range-start <int>\`| \`rangeStart: <int>\`
@@ -1454,7 +1452,6 @@ unformatted code to formatted code. This is useful for editor integrations, to
14541452
prevent the cursor from moving when code is formatted.
14551453
14561454
The \`cursorOffset\` option should be provided, to specify where the cursor is.
1457-
This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
14581455
14591456
\`\`\`js
14601457
prettier.formatWithCursor(' 1', { cursorOffset: 2 });
@@ -1670,8 +1667,6 @@ character offset (inclusive and exclusive, respectively). The range will extend:
16701667
- Backwards to the start of the first line containing the selected statement.
16711668
- Forwards to the end of the selected statement.
16721669
1673-
These options cannot be used with \`cursorOffset\`.
1674-
16751670
| Default | CLI Override | API Override |
16761671
| ---------- | --------------------- | ------------------- |
16771672
| \`0\` | \`--range-start <int>\` | \`rangeStart: <int>\` |
@@ -2442,7 +2437,7 @@ This is similar to the \`--list-different\` parameter in the CLI and is useful f
24422437
\`formatWithCursor\` both formats the code, and translates a cursor position from unformatted code to formatted code.
24432438
This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
24442439
2445-
The \`cursorOffset\` option should be provided, to specify where the cursor is. This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
2440+
The \`cursorOffset\` option should be provided, to specify where the cursor is.
24462441
24472442
\`\`\`js
24482443
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
@@ -2623,8 +2618,6 @@ These two options can be used to format code starting and ending at a given char
26232618
* Backwards to the start of the first line containing the selected statement.
26242619
* Forwards to the end of the selected statement.
26252620
2626-
These options cannot be used with \`cursorOffset\`.
2627-
26282621
Default | CLI Override | API Override
26292622
--------|--------------|-------------
26302623
\`0\` | \`--range-start <int>\`| \`rangeStart: <int>\`
@@ -3431,7 +3424,6 @@ unformatted code to formatted code. This is useful for editor integrations, to
34313424
prevent the cursor from moving when code is formatted.
34323425
34333426
The \`cursorOffset\` option should be provided, to specify where the cursor is.
3434-
This option cannot be used with \`rangeStart\` and \`rangeEnd\`.
34353427
34363428
\`\`\`js
34373429
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
@@ -3647,8 +3639,6 @@ character offset (inclusive and exclusive, respectively). The range will extend:
36473639
- Backwards to the start of the first line containing the selected statement.
36483640
- Forwards to the end of the selected statement.
36493641
3650-
These options cannot be used with \`cursorOffset\`.
3651-
36523642
| Default | CLI Override | API Override |
36533643
| ---------- | --------------------- | ------------------- |
36543644
| \`0\` | \`--range-start <int>\` | \`rangeStart: <int>\` |

tests/format/markdown/markdown/real-world-case.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ This is similar to the `--list-different` parameter in the CLI and is useful for
455455
`formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code.
456456
This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
457457

458-
The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`.
458+
The `cursorOffset` option should be provided, to specify where the cursor is.
459459

460460
```js
461461
prettier.formatWithCursor(" 1", { cursorOffset: 2 });
@@ -636,8 +636,6 @@ These two options can be used to format code starting and ending at a given char
636636
* Backwards to the start of the first line containing the selected statement.
637637
* Forwards to the end of the selected statement.
638638

639-
These options cannot be used with `cursorOffset`.
640-
641639
Default | CLI Override | API Override
642640
--------|--------------|-------------
643641
`0` | `--range-start <int>`| `rangeStart: <int>`

tests/integration/__tests__/__snapshots__/early-exit.js.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,12 @@ Config options:
107107
Editor options:
108108
109109
--cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.
110-
This option cannot be used with --range-start and --range-end.
111110
Defaults to -1.
112111
--range-end <int> Format code ending at a given character offset (exclusive).
113112
The range will extend forwards to the end of the selected statement.
114-
This option cannot be used with --cursor-offset.
115113
Defaults to Infinity.
116114
--range-start <int> Format code starting at a given character offset.
117115
The range will extend backwards to the start of the first line containing the selected statement.
118-
This option cannot be used with --cursor-offset.
119116
Defaults to 0.
120117
121118
Other options:
@@ -274,15 +271,12 @@ Config options:
274271
Editor options:
275272
276273
--cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.
277-
This option cannot be used with --range-start and --range-end.
278274
Defaults to -1.
279275
--range-end <int> Format code ending at a given character offset (exclusive).
280276
The range will extend forwards to the end of the selected statement.
281-
This option cannot be used with --cursor-offset.
282277
Defaults to Infinity.
283278
--range-start <int> Format code starting at a given character offset.
284279
The range will extend backwards to the start of the first line containing the selected statement.
285-
This option cannot be used with --cursor-offset.
286280
Defaults to 0.
287281
288282
Other options:

0 commit comments

Comments
 (0)