Skip to content

Commit

Permalink
Merge pull request #307 from VSCodeVim/fix-visual-line-up
Browse files Browse the repository at this point in the history
Fix visual line selection from bottom to top.
  • Loading branch information
johnfn authored Jun 17, 2016
2 parents 1d614f0 + 8859d7d commit d68fb52
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ typings
*.sw?
.vscode-test
.DS_Store
*.vsix
8 changes: 6 additions & 2 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ export class DeleteOperator extends BaseOperator {
end = new Position(end.line, end.character + 1);
}

const isOnLastLine = end.line === TextEditor.getLineCount() - 1;

// Vim does this weird thing where it allows you to select and delete
// the newline character, which it places 1 past the last character
// in the line. Here we interpret a character position 1 past the end
Expand All @@ -490,13 +492,15 @@ export class DeleteOperator extends BaseOperator {
end = end.getDown(0);
}

// If we do dd on the final line of the document, we expect the line
// If we delete linewise to the final line of the document, we expect the line
// to be removed. This is actually a special case because the newline
// character we've selected to delete is the newline on the end of the document,
// but we actually delete the newline on the second to last line.

// Just writing about this is making me more confused. -_-
if (start.line === end.line && start.line !== 0 && vimState.currentRegisterMode === RegisterMode.LineWise) {
if (isOnLastLine &&
start.line !== 0 &&
vimState.effectiveRegisterMode() === RegisterMode.LineWise) {
start = start.getPreviousLineBegin().getLineEnd();
}

Expand Down
22 changes: 20 additions & 2 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ export class VimState {

public currentRegisterMode = RegisterMode.FigureItOutFromCurrentMode;

public effectiveRegisterMode(): RegisterMode {
if (this.currentRegisterMode === RegisterMode.FigureItOutFromCurrentMode) {
if (this.currentMode === ModeName.VisualLine) {
return RegisterMode.LineWise;
} else {
return RegisterMode.CharacterWise;
}
} else {
return this.currentRegisterMode;
}
}

public registerName = '"';

/**
Expand Down Expand Up @@ -537,8 +549,14 @@ export class ModeHandler implements vscode.Disposable {
}

if (this.currentModeName === ModeName.VisualLine) {
start = Position.EarlierOf(start, stop).getLineBegin();
stop = Position.LaterOf(start, stop).getLineEnd();
if (Position.EarlierOf(start, stop) === stop) {
[start, stop] = [stop, start];
}

start = start.getLineBegin();
stop = stop.getLineEnd();

vimState.currentRegisterMode = RegisterMode.LineWise;
}

return await recordedState.operator.run(vimState, start, stop);
Expand Down

0 comments on commit d68fb52

Please sign in to comment.