This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmode-upwriter.js
92 lines (79 loc) · 2.72 KB
/
mode-upwriter.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
define("ace/mode/upwriter_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"], function(require, exports, module) {
"use strict";
const oop = require("../lib/oop");
const TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
const UpwriterHighlightRules = function() {
// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used
this.$rules = {
"start": [
{
"token" : "suffix",
"regex" : "['’][a-zA-Z]+"
},
{
"token" : "allowed",
"regex" : "\\b(?:" + window.__WORDS + ")\\b",
"caseInsensitive": true
},
{
"token" : "disallowed",
"regex" : "[a-zA-Z]+"
}
]
}
};
oop.inherits(UpwriterHighlightRules, TextHighlightRules);
exports.UpwriterHighlightRules = UpwriterHighlightRules;
});
define("ace/mode/upwriter",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/upwriter_highlight_rules"], function(require, exports, module) {
"use strict";
const oop = require("../lib/oop");
const TextMode = require("./text").Mode;
const UpwriterHighlightRules = require("./upwriter_highlight_rules").UpwriterHighlightRules;
let Mode = function() {
this.HighlightRules = UpwriterHighlightRules;
};
oop.inherits(Mode, TextMode);
Mode.prototype.$id = "ace/mode/upwriter";
/*
// optimized, hence ugly for loops
Mode.prototype.getFirstDisallowed = function(editor) {
var session = editor.getSession();
var i, j, line, first, lines = session.doc.getAllLines();
outer: for (i = 0; i < lines.length; i++) {
line = session.getTokens(i);
for (j = 0; j < line.length; j++) {
if (line[j].type === "disallowed") {
first = line[j].value;
break outer;
}
}
}
return first;
};
*/
Mode.prototype.getDisallowed = function(editor) {
let session = editor.getSession();
return Object.keys(Array.apply(this, Array(session.doc.getAllLines().length)).reduce(function(m, _nil, i) {
session.getTokens(i).forEach(function(token) {
if (token.type === "disallowed") m[token.value] = true;
});
return m;
}, {}));
}
Mode.prototype.onRecalculateAllowed = function(editor, cb) {
let self = this;
editor.getSession().on('change', debounce(function() {
cb(self.getDisallowed(editor));
}, 500));
};
exports.Mode = Mode;
function debounce(fn, delay) {
return function() {
fn.args = arguments;
fn.timeout_id && clearTimeout(fn.timeout_id);
fn.timeout_id = setTimeout(function() { return fn.apply(fn, fn.args); }, delay);
};
}
});