Skip to content

Commit

Permalink
Merge pull request #131 from aioutecism/fix/delection-on-end-of-document
Browse files Browse the repository at this point in the history
Fix wrong behavior when modify last line of document
  • Loading branch information
aioutecism authored Sep 27, 2016
2 parents fbf19fa + 71f2a47 commit 4aba393
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 32 deletions.
11 changes: 3 additions & 8 deletions src/Actions/Delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ActionDelete {
});

if (args.motions.some(motion => motion.isLinewise)) {
ranges = ranges.map(range => document.validateRange(UtilRange.toLinewise(range)));
ranges = ranges.map(range => UtilRange.toLinewise(range, document));
}

ranges = UtilRange.unionOverlaps(ranges);
Expand Down Expand Up @@ -218,7 +218,7 @@ export class ActionDelete {
}

@PrototypeReflect.metadata(SymbolMetadata.Action.isChange, true)
static line(args: {
static byLines(args: {
shouldYank?: boolean
}): Thenable<boolean> {
args.shouldYank = args.shouldYank === undefined ? false : args.shouldYank;
Expand All @@ -231,12 +231,7 @@ export class ActionDelete {

const document = activeTextEditor.document;

let ranges = activeTextEditor.selections.map(selection => document.validateRange(
new Range(
selection.start.line, 0,
selection.end.line + 1, 0
)
));
let ranges = activeTextEditor.selections.map(selection => UtilRange.toLinewise(selection, document));

ranges = UtilRange.unionOverlaps(ranges);

Expand Down
8 changes: 4 additions & 4 deletions src/Actions/Register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ActionRegister {
});

if (args.motions.some(motion => motion.isLinewise)) {
ranges = ranges.map(range => document.validateRange(UtilRange.toLinewise(range)));
ranges = ranges.map(range => UtilRange.toLinewise(range, document));
}

ranges = UtilRange.unionOverlaps(ranges);
Expand Down Expand Up @@ -99,9 +99,9 @@ export class ActionRegister {
return Promise.resolve(false);
}

let ranges = activeTextEditor.selections.map(selection => {
return UtilRange.toLinewise(selection);
});
const document = activeTextEditor.document;

let ranges = activeTextEditor.selections.map(selection => UtilRange.toLinewise(selection, document));

ranges = UtilRange.unionOverlaps(ranges);

Expand Down
2 changes: 1 addition & 1 deletion src/Modes/Normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ModeNormal extends Mode {
], args: {
shouldYank: true
} },
{ keys: 'd d', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'd d', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'D', actions: [
ActionDelete.byMotions,
ActionSelection.validateSelections,
Expand Down
8 changes: 4 additions & 4 deletions src/Modes/Visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class ModeVisual extends Mode {
{ keys: 'backspace', actions: [ActionDelete.selectionsOrRight], args: {shouldYank: true} },
{ keys: 'delete', actions: [ActionDelete.selectionsOrRight], args: {shouldYank: true} },
{ keys: 'x', actions: [ActionDelete.selectionsOrRight], args: {shouldYank: true} },
{ keys: 'X', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'X', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'd', actions: [ActionDelete.selectionsOrRight], args: {shouldYank: true} },
{ keys: 'D', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'D', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'c', actions: [
ActionDelete.selectionsOrRight,
ActionMode.toInsert,
], args: {shouldYank: true} },
{ keys: 'C', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
Expand All @@ -57,7 +57,7 @@ export class ModeVisual extends Mode {
ActionMode.toInsert,
], args: {shouldYank: true} },
{ keys: 'S', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
Expand Down
20 changes: 10 additions & 10 deletions src/Modes/VisualLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ export class ModeVisualLine extends Mode {
ActionMode.toInsert
] },

{ keys: 'backspace', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'delete', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'x', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'X', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'd', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'D', actions: [ActionDelete.line], args: {shouldYank: true} },
{ keys: 'backspace', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'delete', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'x', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'X', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'd', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'D', actions: [ActionDelete.byLines], args: {shouldYank: true} },
{ keys: 'c', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
{ keys: 'C', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
{ keys: 's', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
{ keys: 'S', actions: [
ActionDelete.line,
ActionDelete.byLines,
ActionInsert.newLineBefore,
ActionMode.toInsert,
], args: {shouldYank: true} },
Expand Down
20 changes: 16 additions & 4 deletions src/Utils/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ export class UtilRange {
return to;
}

static toLinewise(from: Range): Range {
return new Range(
from.start.line, 0,
static toLinewise(from: Range, document: TextDocument): Range {
let startLine: number;
let startCharacter: number;

if (from.start.line !== 0 && from.end.line === document.lineCount - 1) {
startLine = from.start.line - 1;
startCharacter = Infinity;
}
else {
startLine = from.start.line;
startCharacter = 0;
}

return document.validateRange(new Range(
startLine, startCharacter,
from.end.line + 1, 0
);
));
}

}
59 changes: 58 additions & 1 deletion test/Actions/Delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {window, Selection} from 'vscode';
import {Configuration} from '../../src/Configuration';
import {ActionDelete} from '../../src/Actions/Delete';
import {MotionWord} from '../../src/Motions/Word';
import {MotionCharacter} from '../../src/Motions/Character';

export function run() {

Expand All @@ -26,7 +27,21 @@ export function run() {
in: 'Foo end\nBar end',
selection: new Selection(1, 0, 1, 0),
out: 'Foo end\nBar end',
}
},
{
message: 'MotionCharacter.up at first line',
motions: [MotionCharacter.up()],
in: 'Foo end\nBar end',
selection: new Selection(0, 2, 0, 2),
out: 'Bar end',
},
{
message: 'MotionCharacter.down at last line',
motions: [MotionCharacter.down()],
in: 'Foo end\nBar end',
selection: new Selection(1, 0, 1, 0),
out: 'Foo end',
},
];

let promise = Promise.resolve();
Expand Down Expand Up @@ -55,4 +70,46 @@ export function run() {
});

});

test('ActionDelete.byLines', (done) => {

const testCases = [
{
message: 'Selection at first line',
in: 'Foo end\nBar end',
selection: new Selection(0, 2, 0, 2),
out: 'Bar end',
},
{
message: 'Selection at last line',
in: 'Foo end\nBar end',
selection: new Selection(1, 0, 1, 0),
out: 'Foo end',
},
];

let promise = Promise.resolve();

while (testCases.length > 0) {
const testCase = testCases.shift();
promise = promise.then(() => {

return TestUtil.createTempDocument(testCase.in).then(() => {
TestUtil.setSelection(testCase.selection);

return ActionDelete.byLines({}).then(() => {
assert.equal(TestUtil.getDocument().getText(), testCase.out, testCase.message);
});
});

});
}

promise.then(() => {
done();
}, (error) => {
done(error);
});

});
};

0 comments on commit 4aba393

Please sign in to comment.