Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
#68
Browse files Browse the repository at this point in the history
  • Loading branch information
74th committed Jan 13, 2018
1 parent 7e7f026 commit 904ff05
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/motion/textObjectSelection/Word.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as Utils from "../../Utils";
import { Position, Range } from "../../VimStyle";
import { AbstractTextObjectSelection } from "./AbstractTextObjectSelection";

const INNER = 0;
const OUTER = 1;

/**
* ciw ciW
* caw caW
*/
export class WordSelection extends AbstractTextObjectSelection {

public IsWORD: boolean;
public InnerOuter: number;

constructor() {
super();
}

public CalculateRange(editor: IEditor, vim: IVimStyle, start: IPosition): IRange {

let count = this.Count;
if (this.InnerOuter === OUTER) {
count++;
}
const range = new Range();
const forwardEdgeResult = this.SearchForwardEdge(editor, start, count);
range.start = this.SearchBackwordEdge(editor, start);

return range;
}

private SearchBackwordEdge(editor: IEditor, start: IPosition): IPosition {
const line = editor.ReadLine(start.Line);
const startClass = Utils.GetCharClass(line.charCodeAt(start.Char));
let char: number;
for (char = start.Char - 1; char > 0; char--) {
const charClass = Utils.GetCharClass(line.charCodeAt(char));
if (startClass === CharGroup.Spaces || !this.IsWORD) {
if (charClass !== startClass) {
break;
}
} else {
// WORD
if (charClass !== CharGroup.Spaces) {
break;
}
}
}
return new Position(start.Line, char + 1);
}

private SearchForwardEdge(editor: IEditor, start: IPosition, count: number): {pos: IPosition, hasSpace: boolean } {
const pos = start.Copy();
const line = editor.ReadLine(start.Line);
const startClass = Utils.GetCharClass(line.charCodeAt(start.Char));
let char: number;
for (char = start.Char - 1; char > 0; char--) {
const charClass = Utils.GetCharClass(line.charCodeAt(char));
if (startClass === CharGroup.Spaces || !this.IsWORD) {
if (charClass !== startClass) {
break;
}
} else {
// WORD
if (charClass !== CharGroup.Spaces) {
break;
}
}
}
// countの1つめは、そのスペースor単語を切り取る
// countの2つめは、

// aaa aaa a|aa$$ccc ddd
// daw-> aaa aaa|$$ccc ddd
// 前方にしかスペースが無い場合、それを切り取る
// aaa aaa a|aa ccc ddd
// 後方にスペースが有る場合、それを切り取る

return { pos, hasSpace: false };
};

}

// ciw
export function AddInnerWordSelection(num: number, action: IAction) {
let m = new WordSelection();
m.InnerOuter = INNER;
m.Count = num > 0 ? num : 1;
let a = <IRequireMotionAction>action;
a.Selection = m;
}

// caw
export function AddOuterApostropheSelection(num: number, action: IAction) {
let m = new WordSelection();
m.InnerOuter = OUTER;
m.Count = num > 0 ? num : 1;
let a = <IRequireMotionAction>action;
a.Selection = m;
}
35 changes: 35 additions & 0 deletions test/vim/TextObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ TextObjects["delete inner word"] = {
key: "d2iw",
out: ["aaa |ccc ddd"],
},
"d2iw 2": {
in: ["aaa b|bb$ccc ddd"],
key: "d2iw",
out: ["aaa |ccc ddd"],
},
"d2iw 3": {
in: ["aaa bbb ccc d|dd "],
key: "d2iw",
out: ["aaa bbb ccc |"],
},
"d3iw": {
in: ["aaa b|bb ccc ddd"],
key: "d3iw",
Expand Down Expand Up @@ -60,11 +70,36 @@ TextObjects["delete outer word"] = {
key: "daw",
out: ["aaa |ccc ddd"],
},
"daw 2": {
in: ["aaa b|bb$ccc ddd"],
key: "daw",
out: ["aaa|$ccc ddd"],
},
"daw 4": {
in: ["aaa bbb ccc d|dd"],
key: "daw",
out: ["aaa bbb ccc|"],
},
"daw 5": {
in: ["aaa bbb ccc d|dd "],
key: "daw",
out: ["aaa bbb ccc |"],
},
"daw 3": {
in: ["aaa$b|bb$ccc ddd"],
key: "daw",
out: ["aaa$|$ccc ddd"],
},
"d2aw": {
in: ["aaa b|bb ccc ddd"],
key: "d2aw",
out: ["aaa |ddd"],
},
"d2aw 2": {
in: ["aaa b|bb$ccc ddd"],
key: "d2aw",
out: ["aaa|ccc ddd"],
},
"d3aw": {
in: ["aaa b|bb ccc ddd"],
key: "d3aw",
Expand Down

0 comments on commit 904ff05

Please sign in to comment.