Skip to content

Commit

Permalink
Skip whitespace lines for text object paragraphs.
Browse files Browse the repository at this point in the history
Also handle ip for "blank paragraphs" correctly.

Fixes #1994
  • Loading branch information
brandonbloom authored and Chillee committed Sep 3, 2017
1 parent 2a852dd commit 4bbf62c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/actions/textobject.ts
Expand Up @@ -395,11 +395,11 @@ export class SelectParagraph extends TextObjectMovement {
vimState.currentRegisterMode = RegisterMode.LineWise;

let start: Position;
const currentParagraphBegin = position.getCurrentParagraphBeginning();
const currentParagraphBegin = position.getCurrentParagraphBeginning(true);

if (position.isLineBeginning() && position.isLineEnd()) {
if (position.isLineWhite()) {
// The cursor is at an empty line, it can be both the start of next paragraph and the end of previous paragraph
start = position.getCurrentParagraphBeginning().getCurrentParagraphEnd();
start = position.getCurrentParagraphBeginning(true).getCurrentParagraphEnd(true);
} else {
if (currentParagraphBegin.isLineBeginning() && currentParagraphBegin.isLineEnd()) {
start = currentParagraphBegin.getRightThroughLineBreaks();
Expand All @@ -409,11 +409,8 @@ export class SelectParagraph extends TextObjectMovement {
}

// Include additional blank lines.
let stop = position.getCurrentParagraphEnd();
while (
TextEditor.getLineAt(stop.getDown(0)).text.trim() === '' &&
stop.line < TextEditor.getLineCount() - 1
) {
let stop = position.getCurrentParagraphEnd(true);
while (stop.line < TextEditor.getLineCount() - 1 && stop.getDown(0).isLineWhite()) {
stop = stop.getDown(0);
}

Expand All @@ -432,24 +429,31 @@ export class SelectInnerParagraph extends TextObjectMovement {
vimState.currentRegisterMode = RegisterMode.LineWise;

let start: Position;
let stop: Position = position.getCurrentParagraphEnd();

if (stop.isLineBeginning() && stop.isLineEnd()) {
stop = stop.getLeftThroughLineBreaks();
}

const currentParagraphBegin = position.getCurrentParagraphBeginning();
let stop: Position;

if (position.isLineBeginning() && position.isLineEnd()) {
// The cursor is at an empty line, it can be both the start of next paragraph and the end of previous paragraph
start = position.getCurrentParagraphBeginning().getCurrentParagraphEnd();
stop = position.getCurrentParagraphEnd().getCurrentParagraphBeginning();
if (position.isLineWhite()) {
// The cursor is at an empty line, so white lines are the paragraph.
start = position.getLineBegin();
stop = position.getLineEnd();
while (start.line > 0 && start.getUp(0).isLineWhite()) {
start = start.getUp(0);
}
while (stop.line < TextEditor.getLineCount() - 1 && stop.getDown(0).isLineWhite()) {
stop = stop.getDown(0);
}
} else {
if (currentParagraphBegin.isLineBeginning() && currentParagraphBegin.isLineEnd()) {
const currentParagraphBegin = position.getCurrentParagraphBeginning(true);
stop = position.getCurrentParagraphEnd(true);
if (currentParagraphBegin.isLineWhite()) {
start = currentParagraphBegin.getRightThroughLineBreaks();
} else {
start = currentParagraphBegin;
}

// Exclude additional blank lines.
while (stop.line > 0 && stop.isLineWhite()) {
stop = stop.getUp(0).getLineEnd();
}
}

return {
Expand Down
4 changes: 4 additions & 0 deletions src/common/motion/position.ts
Expand Up @@ -626,6 +626,10 @@ export class Position extends vscode.Position {
return (trimWhite ? text.trim() : text) === '';
}

public isLineWhite(): boolean {
return this.isLineBlank(true);
}

public getSentenceBegin(args: { forward: boolean }): Position {
if (args.forward) {
return this.getNextSentenceBeginWithRegex(this._sentenceEndRegex, false);
Expand Down

0 comments on commit 4bbf62c

Please sign in to comment.