-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-KDE4.js
298 lines (268 loc) · 10.9 KB
/
R-KDE4.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/** kate-script
* name: R
* license: LGPL
* author: Pierre de Villemerereuil <pierre.de.villemereuil@mailoo.org>
* revision: 1
* indent-languages: R, R Script
* kate-version: 3.10
*/
// Some functions from Python indentation file (credit to Paul Giannaros <paul@giannaros.org>, Gerald Senarclens de Grancy <oss@senarclens.eu>)
// NB: This script works best if "<-" is used for assignment rather than "="
// required katepart js libraries
require ("range.js");
require ("string.js");
openings = ['(', '[','{'];
closings = [')', ']','}']; // requires same order as in openings
operators = ['+', '-', '*', '/', '^',
'&', '|', '==', '>', '<', '<=', '>=', '!=',
'%%', '%*%', '%/%', '%in%',
'%>%', '%T>%', '%$%', '%<>%'];
equalOperatorSigns = ['=', '>', '<', '!'];
// Extension of endswith for an array of tests
function endsWithAny(suffixes, string) {
return suffixes.some(function (suffix) {
return string.endsWith(suffix);
});
}
// Return the given line without comments and leading or trailing whitespace.
// but keep strings (compared to getCode)
// Eg.
// getCode(x) -> "for i in range(3):"
// if document.line(x) == " for i in range(3):"
// getCode(x) -> "for i in range(3):"
// if document.line(x) == "for i in range(3): "
// getCode(x) -> "for i in range(3):"
// if document.line(x) == "for i in range(3): # grand"
function getCodeWithString(lineNr) {
var line = document.line(lineNr);
var code = '';
for (var position = 0; position < line.length; position++) {
if (document.isCode(lineNr, position) || document.isString(lineNr, position)) {
code += line[position];
}
}
return code.trim();
}
// Return the given line without comments and leading or trailing whitespace.
// Eg.
// getCode(x) -> "for i in range(3):"
// if document.line(x) == " for i in range(3):"
// getCode(x) -> "for i in range(3):"
// if document.line(x) == "for i in range(3): "
// getCode(x) -> "for i in range(3):"
// if document.line(x) == "for i in range(3): # grand"
function getCode(lineNr) {
var line = document.line(lineNr);
var code = '';
for (var position = 0; position < line.length; position++) {
if (document.isCode(lineNr, position)) {
code += line[position];
}
}
return code.trim();
}
// Returns the number of spaces after "pos""
// on the line number "lineNr"
function countSpaces(lineNr, pos) {
var line = document.line(lineNr);
var add = 0;
var pos = pos + 1;
while (line[pos] == " ") {
add++;
pos++;
}
return add;
}
// Returns the indent if finding a mismatch using brackets, commas and equal signs
// If there are no mismatch, -1 is returned.
// `lineNr`: number of the line for which the indent is calculated
function calcMismatchIndent(lineNr) {
// initialising some counters
var countClosing = new Array();
closings.forEach(function(elem) {
countClosing[elem] = 0;
});
var countComma = 0;
// starting looking for mismatches
for (var i = lineNr; i >= 0; --i) {
var lineString = document.line(i);
for (var j = lineString.length; j >= 0; --j) {
// Ignore comments and strings
if (document.isComment(i, j) || document.isString(i, j))
continue;
// Testing for brackets
// If a closing bracket, add 1 to counter
if (closings.indexOf(lineString[j]) > -1) {
countClosing[lineString[j]]++;
}
// If an opening bracket, add 1 to the corresponding closing counter
var index = openings.indexOf(lineString[j]);
if (index > -1) {
countClosing[closings[index]]--;
// If an open-but-not-closed bracket is found
// Return indent corresponding to its position
if (countClosing[closings[index]] == -1)
return {indent : j + 1 + countSpaces(i,j), line : i, type : "unclosed"};
}
// If the start of the line is reached and
// no comma was "opened"
if (j == document.firstVirtualColumn(i) && countComma == 0) {
// Test if all brackets are closed
var allclosed = true;
for (var key in countClosing) {
if (countClosing[key] != 0) {
allclosed = false;
}
} // If they are all closed, return the indent of this line
if (allclosed) {
// if we didn't move, return -1 (keep indent), else return the indent of line i
if (i == lineNr) {
return {indent : -1, line : i, type : "allclosed"};
} else {
return {indent : j, line : i, type : "allclosed"};
}
}
}
// Counting the commas if needed
if (lineString[j] == ',') {
// If not between comma-inducing brackets
if (countClosing[')'] == 0 && countClosing[']'] == 0)
countComma++;
}
// Handling equal signs
if (lineString[j] == "=" && equalOperatorSigns.indexOf(lineString[j - 1]) == -1 && lineString[j + 1] != "=") {
// If not between equal-inducing brackets
if (countClosing[')'] == 0 && countClosing[']'] == 0) {
// If no comma is "closing" the equal
if (countComma == 0) {
// Return the position of equal to create a new indent
return {indent : j + 1 + countSpaces(i,j), line : i, type : "equal"};
}
}
}
}
}
return {indent : -1, line : i};
}
// Returns the indent based on operators
// `lineNr`: number of the line for which the indent is calculated
// `indentWidth` : indent width
// `lineLastOp` : does the line on which returns was hit end with an operator
// (note that the line for lineLastOp is not necessarily lineNr)
function calcOperatorIndent(lineNr, indentWidth, lineLastOp) {
var currentIndent = document.firstVirtualColumn(lineNr);
// If we haven't indented yet and line ends up with an operator
// then indent
if (currentIndent == 0 && lineLastOp) {
return indentWidth;
}
// If the current line ends up with an operator
if (lineLastOp) {
var previousLine = getCode(lineNr - 1);
while (previousLine == '' && lineNr >= 0) {
lineNr = lineNr - 1;
previousLine = getCode(lineNr - 1);
}
// If the line before the indent line doesn't ends up with an operator
if (!endsWithAny(operators, previousLine)) {
// then indent
return currentIndent + indentWidth;
} else {
// else don't
return currentIndent;
}
} else {
var previousLine = getCode(lineNr - 1);
while (previousLine == '' && lineNr >= 0) {
lineNr = lineNr - 1;
previousLine = getCode(lineNr - 1);
}
// If the previous line ends with an operator
if (endsWithAny(operators, previousLine)) {
// Looking for the start of the operator indenting
for (i = lineNr - 1; i>=0; --i) {
// If we indented in the past
if (document.firstVirtualColumn(i) < currentIndent) {
currentIndent = document.firstVirtualColumn(i);
var previousLine = getCode(i - 1);
// and doesn't end up with an operator
if (!endsWithAny(operators, previousLine)) {
//return this line indent otherwise
return currentIndent;
}
}
}
} else {
return currentIndent;
}
// If the current line doesn't ends up with an operator, we might need to unindent
// Let's look above
for (i = lineNr; i>=0; --i) {
// If a line has a lower indent
if (document.firstVirtualColumn(i) <= currentIndent) {
currentIndent = document.firstVirtualColumn(i);
var previousLine = getCode(i - 1);
// and doesn't end up with an operator
if (!endsWithAny(operators, previousLine)) {
//return this line indent
return currentIndent;
}
}
}
}
return -1;
}
// Return the amount of characters (in spaces) to be indented.
// Special indent() return values:
// -2 = no indent
// -1 = keep last indent
function indent(line, indentWidth, character) {
if (line == 0) // don't ever act on document's first line
return -2;
var lastLine = getCode(line - 1);
var lastChar = lastLine.substr(-1);
// if empty line, strictly keep indent
// (-1 seems to be not strict and "restore" latest indent with text)
if (!lastLine.length) {
return countSpaces(line - 1, -1);
}
// opening brackets and returns: simply indent
if (openings.indexOf(lastChar) > -1) {
return document.firstVirtualColumn(line - 1) + indentWidth;
}
// calculate indents based on mismatch of brackets, commas and equal signs
var mismatch = calcMismatchIndent(line - 1);
var indent = mismatch.indent;
// if indent is based on non-opened brackets, try indent because of operators
// Don't do it if the end is "<-" though (necessary because "-" is an operator...)
if (mismatch.type == "allclosed" && !lastLine.endsWith('<-')) {
// compute indent due to an operator
indent = calcOperatorIndent(mismatch.line, indentWidth, endsWithAny(operators, lastLine));
}
if (mismatch.type == "unclosed" && endsWithAny(operators, lastLine)) {
// If there is formula on this line,
// return its position as indent
var lineString = document.line(line - 1);
for (j = lineString.length; j>=0; --j) {
if (document.isComment(line - 1, j) || document.isString(line - 1, j))
continue;
if (lineString[j] == "~") {
return j + 1 + countSpaces(line - 1,j)
}
}
}
// At that point, we might have computed an indent equal to the current one,
// let's keep it simple
if (document.firstVirtualColumn(line - 1) == indent) {
indent = -1;
}
// Assignment is important and particular, so always indent when we do it
if (getCodeWithString(line - 1).endsWith('<-')) {
if (indent > -1)
indent += indentWidth;
else
indent = document.firstVirtualColumn(line - 1) + indentWidth;
}
return indent;
}
// kate: space-indent on; indent-width 4; replace-tabs on;