Skip to content

Commit

Permalink
Rewrite J; fix bugs.
Browse files Browse the repository at this point in the history
J is just a rats nest of edge cases. Biggest disparity of all the
commands so far between how easy it seems and how annoying it is to
implement.
  • Loading branch information
johnfn committed Jun 21, 2016
1 parent 79ac7f5 commit c784fb4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,29 +1463,32 @@ class ActionJoin extends BaseCommand {
return vimState; // TODO: bell
}

let lineOne = TextEditor.getLineAt(position).text;
let lineTwo = TextEditor.getLineAt(position.getNextLineBegin()).text;

lineTwo = lineTwo.substring(position.getNextLineBegin().getFirstLineNonBlankChar().character);

// TODO(whitespace): need a better way to check for whitespace
const char = TextEditor.getLineAt(position.getNextLineBegin()).text[0];
const nextChar = TextEditor.getLineAt(position.getNextLineBegin()).text[1];
const lastCharCurrentLine = TextEditor.getLineAt(position).text[TextEditor.getLineAt(position).text.length - 1];
const startsWithWhitespace =
" \t".indexOf(char) !== -1;
const dontAddSpace =
(" \t()".indexOf(char) !== -1) || (" \t".indexOf(lastCharCurrentLine) !== -1);

const positionToDeleteTo =
startsWithWhitespace && " \t".indexOf(nextChar) !== -1 ?
position.getNextLineBegin().getFirstLineNonBlankChar() :
position.getLineEnd();

if (!dontAddSpace) {
await TextEditor.insertAt(" ", position.getNextLineBegin());
}
let oneEndsWithWhitespace = lineOne.length > 0 && " \t".indexOf(lineOne[lineOne.length - 1]) > -1;
let isParenthesisPair = (lineOne[lineOne.length - 1] === '(' && lineTwo[0] === ')');

const addSpace = !oneEndsWithWhitespace && !isParenthesisPair;

return await new DeleteOperator().run(
let resultLine = lineOne + (addSpace ? " " : "") + lineTwo;

let newState = await new DeleteOperator().run(
vimState,
position.getLineEnd(),
positionToDeleteTo
position.getLineBegin(),
lineTwo.length > 0 ?
position.getNextLineBegin().getLineEnd().getLeft() :
position.getLineEnd()
);

await TextEditor.insert(resultLine, position);

newState.cursorPosition = new Position(position.line, lineOne.length + (addSpace ? 1 : 0) + (isParenthesisPair ? 1 : 0) - 1);

return newState;
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/mode/normalModeTests/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ suite("Mode Normal", () => {
end: ['one| two']
});

newTest({
title: "Can handle 'J' with TWO indented lines",
start: [' on|e', ' two'],
keysPressed: 'kJ',
end: [' one| two']
});

newTest({
title: "Can handle 'J' with ')' first character on next line",
start: ['one(', ')tw|o'],
Expand Down

0 comments on commit c784fb4

Please sign in to comment.