Skip to content

Commit

Permalink
Improve cursor tracking (#14812)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 6, 2023
1 parent d0538e2 commit e2f4ce3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions changelog_unreleased/javascript/14812.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#### Fix cursor positioning for a special case (#14812 by @fisker)

<!-- prettier-ignore -->
```js
// <|> is the cursor position

/* Input */
// All messages are represented in JSON.
// So, the prettier.py controls a subprocess which spawns "node {this_file}".
import {<|> } from "fs"

/* Prettier stable */
// All messages are represented in JSON.
// So, the prettier.py <|>controls a subprocess which spawns "node {this_file}".
import {} from "fs"

/* Prettier main */
// All messages are represented in JSON.
// So, the prettier.py controls a subprocess which spawns "node {this_file}".
import {<|>} from "fs"
```
8 changes: 4 additions & 4 deletions src/main/ast-to-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ function callPluginPrintFunction(path, options, printPath, args, embeds) {
doc = printer.print(path, options, printPath, args);
}

if (node === options.cursorNode) {
doc = inheritLabel(doc, (doc) => [cursor, doc, cursor]);
}

// We let JSXElement print its comments itself because it adds () around
// UnionTypeAnnotation has to align the child without the comments
if (
Expand All @@ -118,10 +122,6 @@ function callPluginPrintFunction(path, options, printPath, args, embeds) {
doc = printComments(path, doc, options);
}

if (node === options.cursorNode) {
doc = inheritLabel(doc, (doc) => [cursor, doc, cursor]);
}

return doc;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/format/js/cursor/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ haha()<|>;
================================================================================
`;

exports[`file-start-with-comment-3.js format 1`] = `
====================================options=====================================
cursorOffset: 127
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
=====================================input======================================
// All messages are represented in JSON.
// So, the prettier.py controls a subprocess which spawns "node {this_file}".
import {<|> } from "fs"
=====================================output=====================================
// All messages are represented in JSON.
// So, the prettier.py controls a subprocess which spawns "node {this_file}".
import {<|>} from "fs";
================================================================================
`;

exports[`range-0.js format 1`] = `
====================================options=====================================
cursorOffset: 56
Expand Down
3 changes: 3 additions & 0 deletions tests/format/js/cursor/file-start-with-comment-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// All messages are represented in JSON.
// So, the prettier.py controls a subprocess which spawns "node {this_file}".
import {<|> } from "fs"

1 comment on commit e2f4ce3

@xhon1980
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need help for beep32 is anyone to help me please??

Please sign in to comment.