This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
text_objects.js
140 lines (124 loc) · 4.26 KB
/
text_objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function () {
function findPairs(lines, start, end) {
var row, col, line, chr, pairs, startStack;
pairs = [];
startStack = [];
for (row = 0; row < lines.length; row++) {
line = lines[row];
for (col = 0; col < line.length; col++) {
chr = line.charAt(col);
if (chr === start) {
startStack.push({row: row, col: col});
} else if (chr === end && startStack.length > 0) {
pairs.push(new Vimulator.CharacterRange(
startStack.pop(),
{row: row, col: col},
{inclusive: true}
));
}
}
}
return pairs;
}
Vimulator.TextObject = function (options) {
this.startDelim = options.start;
this.endDelim = options.end;
this.name = options.name;
};
Vimulator.TextObject.prototype.insideRange = function (vim) {
var pairs, i, p;
pairs = findPairs(vim.lines, this.startDelim, this.endDelim);
for (i = 0; i < pairs.length; i++) {
p = pairs[i];
if (p.contains(vim.cursor)) {
p.start.col += 1;
p.end.col -= 1;
return p;
}
}
return null;
};
Vimulator.TextObject.prototype.aroundRange = function (vim) {
var pairs, i, p;
pairs = findPairs(vim.lines, this.startDelim, this.endDelim);
for (i = 0; i < pairs.length; i++) {
p = pairs[i];
if (p.contains(vim.cursor)) {
return p;
}
}
return null;
};
Vimulator.TextObject.Commands = (function () {
var C, U, textObjects;
C = Vimulator.Command;
U = Vimulator.Utils;
textObjects = {};
textObjects['b'] = new Vimulator.TextObject({
name: "parentheses",
start: '(',
end: ')'
});
textObjects['('] = textObjects['b'];
textObjects[')'] = textObjects['b'];
textObjects['['] = new Vimulator.TextObject({
name: "square brackets",
start: '[',
end: ']'
});
textObjects[']'] = textObjects['['];
textObjects['B'] = new Vimulator.TextObject({
name: "curly braces",
start: '{',
end: '}'
});
textObjects['{'] = textObjects['B'];
textObjects['}'] = textObjects['B'];
return {
'a': new C({
argument: "literal",
callback: function (vim, count, key) {
var textObject = textObjects[key];
if (textObject) {
return textObject.aroundRange(vim);
} else {
vim.mode.cancelOperation();
}
},
description: function (count, key) {
var desc, textObject;
desc = "around " + U.literalArgDescription(key);
textObject = textObjects[key];
if (textObject) {
desc += " " + textObject.name;
} else if (key) {
desc += " (unknown text object)";
}
return desc;
}
}),
'i': new C({
argument: "literal",
callback: function (vim, count, key) {
var textObject = textObjects[key];
if (textObject) {
return textObject.insideRange(vim);
} else {
vim.mode.cancelOperation();
}
},
description: function (count, key) {
var desc, textObject;
desc = "inside " + U.literalArgDescription(key);
textObject = textObjects[key];
if (textObject) {
desc += " " + textObject.name;
} else if (key) {
desc += " (unknown text object)";
}
return desc;
}
})
};
}());
}());