Skip to content

Commit

Permalink
Fix bug in "Wrap selection in tag" command (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib authored Feb 1, 2024
1 parent 1281077 commit f491105
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/young-turkeys-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'playroom': patch
---

Fix bug in "Wrap selection in tag" command that caused the start cursor to occasionally be placed in the wrong postion.
15 changes: 15 additions & 0 deletions cypress/e2e/keymaps.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ describe('Keymaps', () => {
`);
});

it('should ignore surrounding whitespace when wrapping a single line selection', () => {
typeCode(' ');
typeCode('{leftArrow}');
selectToEndOfLine();

typeCode(`{shift+${modifierKey}+,}`);
typeCode('span');

assertCodePaneContains(dedent`
<span> <div>First line</div></span>
<div>Second line</div>
<div>Third line</div>
`);
});

it('should wrap a multi-line selection', () => {
typeCode('{shift+downArrow}');
selectToEndOfLine();
Expand Down
5 changes: 4 additions & 1 deletion src/Playroom/CodeEditor/keymaps/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export const wrapInTag = (cm: Editor) => {
existingIndent,
});

const startCursorCharacterPosition =
from.ch + 1 + (isMultiLineSelection ? existingIndent : 0);
const newStartCursor = new Pos(
from.line + linesAdded,
from.ch + existingIndent + 1
startCursorCharacterPosition
);

const newEndCursor = isMultiLineSelection
? new Pos(to.line + linesAdded + 2, from.ch + existingIndent + 2)
: new Pos(to.line + linesAdded, to.ch + 4);
Expand Down

0 comments on commit f491105

Please sign in to comment.