Skip to content

Commit

Permalink
Translate cursor relative to nearest node, not SourceElement (prettie…
Browse files Browse the repository at this point in the history
…r#1989)

* Add test demonstrating SourceElement-relative cursor translation

See prettier#1981 (comment)

* Translate cursor relative to nearest node, not SourceElement

This partially address prettier#1981

See prettier#1981 (comment)

* Add test demonstrating incorrect cursor translation

Since the `cursorOffset` option (introduced in prettier#1637) works by tracking
the cursor position relative to an AST node (rather than a CST token),
it can produce incorrect results.

See prettier#1981
  • Loading branch information
josephfrazier authored and vjeux committed Jun 6, 2017
1 parent 19bd2a3 commit c863cbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ function findSiblingAncestors(startNodeAndParents, endNodeAndParents) {
};
}

function findNodeAtOffset(node, offset, parentNodes) {
function findNodeAtOffset(node, offset, predicate, parentNodes) {
predicate = predicate || (() => true);
parentNodes = parentNodes || [];
const start = util.locStart(node);
const end = util.locEnd(node);
Expand All @@ -134,14 +135,15 @@ function findNodeAtOffset(node, offset, parentNodes) {
const childResult = findNodeAtOffset(
childNode,
offset,
predicate,
[node].concat(parentNodes)
);
if (childResult) {
return childResult;
}
}

if (isSourceElement(node)) {
if (predicate(node)) {
return {
node: node,
parentNodes: parentNodes
Expand Down Expand Up @@ -199,8 +201,16 @@ function calculateRange(text, opts, ast) {
}
}

const startNodeAndParents = findNodeAtOffset(ast, startNonWhitespace);
const endNodeAndParents = findNodeAtOffset(ast, endNonWhitespace);
const startNodeAndParents = findNodeAtOffset(
ast,
startNonWhitespace,
isSourceElement
);
const endNodeAndParents = findNodeAtOffset(
ast,
endNonWhitespace,
isSourceElement
);
const siblingAncestors = findSiblingAncestors(
startNodeAndParents,
endNodeAndParents
Expand Down
16 changes: 16 additions & 0 deletions tests/cursor/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ test("translates cursor correctly in basic case", () => {
cursorOffset: 1
});
});

test("positions cursor relative to closest node, not SourceElement", () => {
const code = "return 15";
expect(prettier.formatWithCursor(code, { cursorOffset: 15 })).toEqual({
formatted: "return 15;\n",
cursorOffset: 7
});
});

test("keeps cursor inside formatted node", () => {
const code = "return 15";
expect(prettier.formatWithCursor(code, { cursorOffset: 14 })).toEqual({
formatted: "return 15;\n",
cursorOffset: 14 // TODO fix this
});
});

0 comments on commit c863cbe

Please sign in to comment.