-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-FK5.js
417 lines (378 loc) · 16.1 KB
/
R-FK5.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
var katescript = {
"name": "R",
"author": "Pierre de Villemerereuil <pierre.de.villemereuil@mailoo.org>",
"license": "MIT",
"revision": 1,
"kate-version": "5.0",
"indent-languages": ["R", "R Script", "Script R"]
}; // kate-script-header, must be at the start of the file without comments
// Some functions modified from Python indentation file
// (credit to Paul Giannaros <paul@giannaros.org>, Gerald Senarclens de Grancy <oss@senarclens.eu>)
// required katepart js libraries
require ("range.js");
require ("string.js");
require ("utils.js")
triggerCharacters = '})]';
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) || document.isOthers(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, add) {
// initialising some counters
var countClosing = new Array();
closings.forEach(function(elem) {
countClosing[elem] = 0;
});
if (closings.indexOf(add) > -1) {
countClosing[add]++;
}
var countComma = 0;
// starting looking for mismatches
for (var i = lineNr; i >= 0; --i) {
var lastOpen = document.line(i).length;
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, line : i, pos: lastOpen, type : "unclosed"};
// Otherwise just update "lastOpen" to j
lastOpen = j;
}
// 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, pos: lastOpen, type : "allclosed"};
} else {
return {indent : j, line : i, pos: lastOpen, 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, type : "unknown"};
}
// Find the position of an align operator with in the focus line
// accounting for opening and closing brackets
// `lineNr`: number of the line to search the align operator in
// Returns -1 if nothing was found
function findAlignOperator(lineNr, pos) {
var lineString = document.line(lineNr);
// initialising some counters
var countClosing = new Array();
closings.forEach(function(elem) {
countClosing[elem] = 0;
});
for (var j = pos - 1; j >= 0; --j) {
// Ignore comments and strings
if (document.isComment(lineNr, j) || document.isString(lineNr, 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 -1 because nothing was found
if (countClosing[closings[index]] == -1)
return -1
}
if (lineString.substr(j - 1, 2) == "<-" ||
(lineString[j] == "=" && equalOperatorSigns.indexOf(lineString[j - 1]) == -1 && lineString[j + 1] != "=") ||
lineString[j] == "~")
{
// 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) {
return j;
}
}
}
return -1
}
// 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, pos, lineLastOp) {
var currentIndent = document.firstVirtualColumn(lineNr);
var refLine = 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 = getCodeWithString(refLine - 1);
while (previousLine == '' && refLine >= 0) {
refLine = refLine - 1;
previousLine = getCodeWithString(refLine - 1);
}
// If the line before the indent line doesn't ends up with an operator
if (!endsWithAny(operators, previousLine)) {
// then look for align operator
locAlign = findAlignOperator(lineNr, pos);
if (locAlign != -1) {
return locAlign + 1 + countSpaces(lineNr, locAlign);
} else {
// if no align operator, simply indent
return currentIndent + indentWidth;
}
} else {
// else don't
return currentIndent;
}
} else {
var previousLine = getCodeWithString(refLine - 1);
while (previousLine == '' && refLine >= 0) {
refLine = refLine - 1;
previousLine = getCodeWithString(refLine - 1);
}
// If the previous line ends with an operator
if (endsWithAny(operators, previousLine)) {
// Looking for the start of the operator indenting
for (i = refLine - 1; i>=0; --i) {
// If commented line, skip
if (getCodeWithString(i) != '') {
// If we indented in the past
if (document.firstVirtualColumn(i) < currentIndent) {
currentIndent = document.firstVirtualColumn(i);
var previousLine = getCodeWithString(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 = refLine; i>=0; --i) {
// If a line has a lower indent
if (document.firstVirtualColumn(i) <= currentIndent) {
currentIndent = document.firstVirtualColumn(i);
var previousLine = getCodeWithString(i - 1);
// and doesn't end up with an operator
if (!endsWithAny(operators, previousLine)) {
//return this line indent
return currentIndent;
}
}
}
}
return -1;
}
// Align when a closing bracket was entered
// `lineNr`: number of the line for which the indent is calculated
// `c` : the bracket that was entered
function alignBrackets(lineNr, lastChar, newChar, indentWidth) {
var charsMatch = ( lastChar == '(' && newChar == ')' ) ||
( lastChar == '{' && newChar == '}' ) ||
( lastChar == '[' && newChar == ']' );
if (charsMatch) {
indentation = document.firstVirtualColumn(lineNr - 1);
document.insertText(lineNr, document.firstColumn(lineNr), "\n");
view.setCursorPosition(lineNr, document.line(lineNr).length);
document.indent(new Range(lineNr + 1, 0, lineNr + 1, 1), indentation / indentWidth);
}
return document.firstVirtualColumn(lineNr - 1) + indentWidth;
}
// 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, ch) {
if (line == 0) // don't ever act on document's first line
return -2;
var lastLine = getCodeWithString(line - 1);
var lastChar = lastLine.substr(-1);
var newChar = getCodeWithString(line).substr(0, 1);
// Align if a closing bracket was entered
// (note that the code above might also be triggered if a bracket is closed right after an opened one)
if (closings.indexOf(ch) > -1) {
//if the entered bracket doesn't start the line, do nothing
if (newChar != ch) {
return document.firstVirtualColumn(line);
}
var matchOpen = openings[closings.indexOf(ch)];
if (lastChar == matchOpen) {
return alignBrackets(line, lastChar, newChar, indentWidth)
} else {
mismatch = calcMismatchIndent(line - 1, ch);
return document.firstVirtualColumn(mismatch.line);
}
}
// if empty line, strictly keep indent
// (-1 seems to be not strict and "restore" latest indent with text)
if (!lastLine.length) {
// debug("Empty line")
return countSpaces(line - 1, -1);
}
// opening brackets and returns: indent (and unindent following bracket if needed)
if (openings.indexOf(lastChar) > -1) {
return alignBrackets(line, lastChar, newChar, indentWidth);
}
// calculate indents based on mismatch of brackets, commas and equal signs
var mismatch = calcMismatchIndent(line - 1, '');
var indent = mismatch.indent;
// debug("mismatch.line = " + mismatch.line)
// debug("mismatch.pos = " + mismatch.pos)
// debug("mismatch.indent = " + mismatch.indent)
// debug("mismatch.type = " + mismatch.type)
// 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, mismatch.pos, endsWithAny(operators, lastLine));
}
if (mismatch.type == "unclosed" && endsWithAny(operators, lastLine)) {
// look whether we should account for an align operator
locAlign = findAlignOperator(mismatch.line, mismatch.pos);
if (locAlign != -1) {
indent = locAlign + 1 + countSpaces(mismatch.line, locAlign);
}
}
// if unclosed bracket is found, check that the line didn't end with this bracket
// if it did, do not change anything to the indent
if (mismatch.type == "unclosed" && mismatch.indent == document.line(mismatch.line).length) {
indent = document.firstVirtualColumn(mismatch.line + 1);
}
// At that point, we might have computed an indent equal to the current one,
// let's keep it simple and set indent to -1 in that case
if (document.firstVirtualColumn(line - 1) == indent) {
indent = -1;
}
// If the next character is a closing bracket,
// let's see if we should indent back to the corresponding indent
if (closings.indexOf(newChar) > -1) {
mismatch = calcMismatchIndent(line - 1, newChar);
// change indent only if the closing bracket matches a "final" one
if (endsWithAny(openings, getCodeWithString(mismatch.line))) {
return document.firstVirtualColumn(mismatch.line);
}
}
// Assignment is important and particular, so always indent when we do it
if (getCodeWithString(line - 1).endsWith('<-') || 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;