This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rangefinder.js
82 lines (69 loc) · 2.72 KB
/
rangefinder.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
/*
rangefinder.js - module for managing code folding regions
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
function rangeFinder(cm, start) {
// find ranges that can be folded -- sections (settings,
// variables, etc), keyword definitions and test case
// definitions
var startLine = cm.getLine(start.line);
var state = cm.getStateAfter(start.line);
var pos;
var result;
if (cm.getTokenTypeAt({line: start.line, ch: 1}) == "header") {
// Found a heading? Everything up to the next heading or EOF
// is a foldable region
pos = findNextLineWithToken(cm, ["header"], start.line + 1);
result = {
from: {line: start.line, ch: startLine.length},
to: pos};
return result;
}
if ((state.table_name === "test_cases" || state.table_name === "keywords") &&
isBlockStart(cm, start.line)) {
pos = findNextLineWithToken(cm, ["keyword", "header"], start.line + 1);
result = {
from: {line: start.line, ch: startLine.length},
to: pos};
return result;
}
}
function isBlockStart(cm, linenumber) {
// Return True if the given line is the beginning of a block
// (read: keyword or test case). Note that "keyword" here
// represents a token type / css style, NOT necessarily a
// robot framework keyword
var tokens = cm.getLineTokens(linenumber);
for (var i = 0; i < tokens.length; i++) {
if (tokens[i].type === "keyword") {
return true;
}
}
return false;
}
function findNextLineWithToken(cm, token_list, start) {
// Find the next line with a token from the given list of
// token types (eg: find the next line with a token of type
// "heading" or "keyword")
var tokens;
var result = null;
cm.eachLine(start, cm.lineCount(), function(line) {
tokens = cm.getLineTokens(line.lineNo());
for (var i = 0; i < tokens.length; i++) {
if (token_list.indexOf(tokens[i].type) >= 0) {
result = {line: line.lineNo() - 1, ch: line.text.length};
return result;
}
}
});
if (!result) {
var lastLine = cm.getLine(cm.lastLine());
var result = {line: cm.lastLine(), ch: lastLine.length};
}
return result;
}
exports.rangeFinder = rangeFinder;
})