Skip to content

Commit

Permalink
Merge pull request #206 from wmaurer/feature/n-x_and_n-X
Browse files Browse the repository at this point in the history
Feature/n x and n X
  • Loading branch information
aioutecism authored May 31, 2018
2 parents 187c9d6 + 6fc6c1f commit 3600949
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 74 deletions.
89 changes: 15 additions & 74 deletions src/Actions/Delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export class ActionDelete {

@StaticReflect.metadata(SymbolMetadata.Action.isChange, true)
static selectionsOrLeft(args: {
isMultiLine?: boolean,
n?: number,
shouldYank?: boolean
} = {}): Thenable<boolean> {
args.isMultiLine = args.isMultiLine === undefined ? false : args.isMultiLine;
args.n = args.n === undefined ? 1 : args.n;
args.shouldYank = args.shouldYank === undefined ? false : args.shouldYank;

const activeTextEditor = window.activeTextEditor;
Expand All @@ -117,41 +117,12 @@ export class ActionDelete {
return Promise.resolve(false);
}

let document = activeTextEditor.document;
let ranges: Range[];

if (args.isMultiLine) {
ranges = activeTextEditor.selections.map(selection => {
if (! selection.isEmpty) {
return selection;
}

let position = selection.active;

if (position.character === 0) {
if (position.line === 0) {
return selection;
}
else {
let lineLength = document.lineAt(position.line - 1).text.length;
return new Range(
position.translate(-1, lineLength),
position
);
}
}
else {
return new Range(selection.active, selection.active.translate(0, -1));
}
});
}
else {
ranges = activeTextEditor.selections.map(selection => {
return selection.isEmpty && selection.active.character !== 0
? new Range(selection.active, selection.active.translate(0, -1))
: selection;
});
}
let ranges = activeTextEditor.selections.map(selection => {
let n = Math.min(selection.active.character, args.n!);
return selection.isEmpty && selection.active.character !== 0
? new Range(selection.active, selection.active.translate(0, -n))
: selection;
});

ranges = UtilRange.unionOverlaps(ranges);

Expand All @@ -170,10 +141,10 @@ export class ActionDelete {

@StaticReflect.metadata(SymbolMetadata.Action.isChange, true)
static selectionsOrRight(args: {
isMultiLine?: boolean,
n?: number,
shouldYank?: boolean
} = {}): Thenable<boolean> {
args.isMultiLine = args.isMultiLine === undefined ? false : args.isMultiLine;
args.n = args.n === undefined ? 1 : args.n;
args.shouldYank = args.shouldYank === undefined ? false : args.shouldYank;

const activeTextEditor = window.activeTextEditor;
Expand All @@ -182,41 +153,11 @@ export class ActionDelete {
return Promise.resolve(false);
}

let document = activeTextEditor.document;
let ranges: Range[];

if (args.isMultiLine) {
ranges = activeTextEditor.selections.map(selection => {
if (! selection.isEmpty) {
return selection;
}

let position = selection.active;
let lineLength = document.lineAt(position.line).text.length;

if (position.character === lineLength) {
if (position.line === document.lineCount - 1) {
return selection;
}
else {
return new Range(
position.line, position.character,
position.line + 1, 0
);
}
}
else {
return new Range(selection.active, selection.active.translate(0, +1));
}
});
}
else {
ranges = activeTextEditor.selections.map(selection => {
return selection.isEmpty
? new Range(selection.active, selection.active.translate(0, +1))
: selection;
});
}
let ranges = activeTextEditor.selections.map(selection => {
return selection.isEmpty
? new Range(selection.active, selection.active.translate(0, +args.n!))
: selection;
});

ranges = UtilRange.unionOverlaps(ranges);

Expand Down
7 changes: 7 additions & 0 deletions src/Modes/Normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@ export class ModeNormal extends Mode {
} },

{ keys: 'X', actions: [ActionDelete.selectionsOrLeft], args: {shouldYank: true} },
{ keys: '{N} X', actions: [ ActionDelete.selectionsOrLeft, ], args: { shouldYank: true } },
{ keys: 'x', actions: [
ActionDelete.selectionsOrRight,
ActionSelection.validateSelections,
], args: {
shouldYank: true
} },
{ keys: '{N} x', actions: [
ActionDelete.selectionsOrRight,
ActionSelection.validateSelections,
], args: {
shouldYank: true
} },
{ keys: 'delete', actions: [
ActionDelete.selectionsOrRight,
ActionSelection.validateSelections,
Expand Down
49 changes: 49 additions & 0 deletions test/ModeNormal/{N} x.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as BlackBox from '../Framework/BlackBox';

suite('Normal: {N} x', () => {
const testCases: BlackBox.TestCase[] = [
{
from: 'Fo[]o end\nBar end\nDoo end',
inputs: '1 x',
to: 'Fo[] end\nBar end\nDoo end',
},
{
from: 'Fo[]o end\nBar end\nDoo end',
inputs: '2 x',
to: 'Fo[]end\nBar end\nDoo end',
},
{
from: 'Fo[]o end\nBar end\nDoo end',
inputs: '10 x',
to: 'F[]o\nBar end\nDoo end',
},
];

for (let i = 0; i < testCases.length; i++) {
BlackBox.run(testCases[i]);
}
});

suite('Normal: {N} X', () => {
const testCases: BlackBox.TestCase[] = [
{
from: 'Fo[]o end\nBar end\nDoo end',
inputs: '1 X',
to: 'F[]o end\nBar end\nDoo end',
},
{
from: 'Fo[]o end\nBar end\nDoo end',
inputs: '2 X',
to: '[]o end\nBar end\nDoo end',
},
{
from: 'Foo en[]d\nBar end\nDoo end',
inputs: '10 X',
to: '[]d\nBar end\nDoo end',
},
];

for (let i = 0; i < testCases.length; i++) {
BlackBox.run(testCases[i]);
}
});

0 comments on commit 3600949

Please sign in to comment.