| 5 |
lars |
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
2 |
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
3 |
|
|
|
4 |
(function(mod) {
|
|
|
5 |
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
6 |
mod(require("../../lib/codemirror"));
|
|
|
7 |
else if (typeof define == "function" && define.amd) // AMD
|
|
|
8 |
define(["../../lib/codemirror"], mod);
|
|
|
9 |
else // Plain browser env
|
|
|
10 |
mod(CodeMirror);
|
|
|
11 |
})(function(CodeMirror) {
|
|
|
12 |
"use strict";
|
|
|
13 |
|
|
|
14 |
CodeMirror.defineMode("kotlin", function (config, parserConfig) {
|
|
|
15 |
function words(str) {
|
|
|
16 |
var obj = {}, words = str.split(" ");
|
|
|
17 |
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
|
|
|
18 |
return obj;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
var multiLineStrings = parserConfig.multiLineStrings;
|
|
|
22 |
|
|
|
23 |
var keywords = words(
|
|
|
24 |
"package continue return object while break class data trait interface throw super" +
|
|
|
25 |
" when type this else This try val var fun for is in if do as true false null get set");
|
|
|
26 |
var softKeywords = words("import" +
|
|
|
27 |
" where by get set abstract enum open annotation override private public internal" +
|
|
|
28 |
" protected catch out vararg inline finally final ref");
|
|
|
29 |
var blockKeywords = words("catch class do else finally for if where try while enum");
|
|
|
30 |
var atoms = words("null true false this");
|
|
|
31 |
|
|
|
32 |
var curPunc;
|
|
|
33 |
|
|
|
34 |
function tokenBase(stream, state) {
|
|
|
35 |
var ch = stream.next();
|
|
|
36 |
if (ch == '"' || ch == "'") {
|
|
|
37 |
return startString(ch, stream, state);
|
|
|
38 |
}
|
|
|
39 |
// Wildcard import w/o trailing semicolon (import smth.*)
|
|
|
40 |
if (ch == "." && stream.eat("*")) {
|
|
|
41 |
return "word";
|
|
|
42 |
}
|
|
|
43 |
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
|
|
|
44 |
curPunc = ch;
|
|
|
45 |
return null;
|
|
|
46 |
}
|
|
|
47 |
if (/\d/.test(ch)) {
|
|
|
48 |
if (stream.eat(/eE/)) {
|
|
|
49 |
stream.eat(/\+\-/);
|
|
|
50 |
stream.eatWhile(/\d/);
|
|
|
51 |
}
|
|
|
52 |
return "number";
|
|
|
53 |
}
|
|
|
54 |
if (ch == "/") {
|
|
|
55 |
if (stream.eat("*")) {
|
|
|
56 |
state.tokenize.push(tokenComment);
|
|
|
57 |
return tokenComment(stream, state);
|
|
|
58 |
}
|
|
|
59 |
if (stream.eat("/")) {
|
|
|
60 |
stream.skipToEnd();
|
|
|
61 |
return "comment";
|
|
|
62 |
}
|
|
|
63 |
if (expectExpression(state.lastToken)) {
|
|
|
64 |
return startString(ch, stream, state);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
// Commented
|
|
|
68 |
if (ch == "-" && stream.eat(">")) {
|
|
|
69 |
curPunc = "->";
|
|
|
70 |
return null;
|
|
|
71 |
}
|
|
|
72 |
if (/[\-+*&%=<>!?|\/~]/.test(ch)) {
|
|
|
73 |
stream.eatWhile(/[\-+*&%=<>|~]/);
|
|
|
74 |
return "operator";
|
|
|
75 |
}
|
|
|
76 |
stream.eatWhile(/[\w\$_]/);
|
|
|
77 |
|
|
|
78 |
var cur = stream.current();
|
|
|
79 |
if (atoms.propertyIsEnumerable(cur)) {
|
|
|
80 |
return "atom";
|
|
|
81 |
}
|
|
|
82 |
if (softKeywords.propertyIsEnumerable(cur)) {
|
|
|
83 |
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
|
|
|
84 |
return "softKeyword";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if (keywords.propertyIsEnumerable(cur)) {
|
|
|
88 |
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
|
|
|
89 |
return "keyword";
|
|
|
90 |
}
|
|
|
91 |
return "word";
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
tokenBase.isBase = true;
|
|
|
95 |
|
|
|
96 |
function startString(quote, stream, state) {
|
|
|
97 |
var tripleQuoted = false;
|
|
|
98 |
if (quote != "/" && stream.eat(quote)) {
|
|
|
99 |
if (stream.eat(quote)) tripleQuoted = true;
|
|
|
100 |
else return "string";
|
|
|
101 |
}
|
|
|
102 |
function t(stream, state) {
|
|
|
103 |
var escaped = false, next, end = !tripleQuoted;
|
|
|
104 |
|
|
|
105 |
while ((next = stream.next()) != null) {
|
|
|
106 |
if (next == quote && !escaped) {
|
|
|
107 |
if (!tripleQuoted) {
|
|
|
108 |
break;
|
|
|
109 |
}
|
|
|
110 |
if (stream.match(quote + quote)) {
|
|
|
111 |
end = true;
|
|
|
112 |
break;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {
|
|
|
117 |
state.tokenize.push(tokenBaseUntilBrace());
|
|
|
118 |
return "string";
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (next == "$" && !escaped && !stream.eat(" ")) {
|
|
|
122 |
state.tokenize.push(tokenBaseUntilSpace());
|
|
|
123 |
return "string";
|
|
|
124 |
}
|
|
|
125 |
escaped = !escaped && next == "\\";
|
|
|
126 |
}
|
|
|
127 |
if (multiLineStrings)
|
|
|
128 |
state.tokenize.push(t);
|
|
|
129 |
if (end) state.tokenize.pop();
|
|
|
130 |
return "string";
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
state.tokenize.push(t);
|
|
|
134 |
return t(stream, state);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
function tokenBaseUntilBrace() {
|
|
|
138 |
var depth = 1;
|
|
|
139 |
|
|
|
140 |
function t(stream, state) {
|
|
|
141 |
if (stream.peek() == "}") {
|
|
|
142 |
depth--;
|
|
|
143 |
if (depth == 0) {
|
|
|
144 |
state.tokenize.pop();
|
|
|
145 |
return state.tokenize[state.tokenize.length - 1](stream, state);
|
|
|
146 |
}
|
|
|
147 |
} else if (stream.peek() == "{") {
|
|
|
148 |
depth++;
|
|
|
149 |
}
|
|
|
150 |
return tokenBase(stream, state);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
t.isBase = true;
|
|
|
154 |
return t;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
function tokenBaseUntilSpace() {
|
|
|
158 |
function t(stream, state) {
|
|
|
159 |
if (stream.eat(/[\w]/)) {
|
|
|
160 |
var isWord = stream.eatWhile(/[\w]/);
|
|
|
161 |
if (isWord) {
|
|
|
162 |
state.tokenize.pop();
|
|
|
163 |
return "word";
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
state.tokenize.pop();
|
|
|
167 |
return "string";
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
t.isBase = true;
|
|
|
171 |
return t;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
function tokenComment(stream, state) {
|
|
|
175 |
var maybeEnd = false, ch;
|
|
|
176 |
while (ch = stream.next()) {
|
|
|
177 |
if (ch == "/" && maybeEnd) {
|
|
|
178 |
state.tokenize.pop();
|
|
|
179 |
break;
|
|
|
180 |
}
|
|
|
181 |
maybeEnd = (ch == "*");
|
|
|
182 |
}
|
|
|
183 |
return "comment";
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
function expectExpression(last) {
|
|
|
187 |
return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
|
|
|
188 |
last == "newstatement" || last == "keyword" || last == "proplabel";
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
function Context(indented, column, type, align, prev) {
|
|
|
192 |
this.indented = indented;
|
|
|
193 |
this.column = column;
|
|
|
194 |
this.type = type;
|
|
|
195 |
this.align = align;
|
|
|
196 |
this.prev = prev;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
function pushContext(state, col, type) {
|
|
|
200 |
return state.context = new Context(state.indented, col, type, null, state.context);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
function popContext(state) {
|
|
|
204 |
var t = state.context.type;
|
|
|
205 |
if (t == ")" || t == "]" || t == "}")
|
|
|
206 |
state.indented = state.context.indented;
|
|
|
207 |
return state.context = state.context.prev;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
// Interface
|
|
|
211 |
|
|
|
212 |
return {
|
|
|
213 |
startState: function (basecolumn) {
|
|
|
214 |
return {
|
|
|
215 |
tokenize: [tokenBase],
|
|
|
216 |
context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
|
|
|
217 |
indented: 0,
|
|
|
218 |
startOfLine: true,
|
|
|
219 |
lastToken: null
|
|
|
220 |
};
|
|
|
221 |
},
|
|
|
222 |
|
|
|
223 |
token: function (stream, state) {
|
|
|
224 |
var ctx = state.context;
|
|
|
225 |
if (stream.sol()) {
|
|
|
226 |
if (ctx.align == null) ctx.align = false;
|
|
|
227 |
state.indented = stream.indentation();
|
|
|
228 |
state.startOfLine = true;
|
|
|
229 |
// Automatic semicolon insertion
|
|
|
230 |
if (ctx.type == "statement" && !expectExpression(state.lastToken)) {
|
|
|
231 |
popContext(state);
|
|
|
232 |
ctx = state.context;
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
if (stream.eatSpace()) return null;
|
|
|
236 |
curPunc = null;
|
|
|
237 |
var style = state.tokenize[state.tokenize.length - 1](stream, state);
|
|
|
238 |
if (style == "comment") return style;
|
|
|
239 |
if (ctx.align == null) ctx.align = true;
|
|
|
240 |
if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
|
|
|
241 |
// Handle indentation for {x -> \n ... }
|
|
|
242 |
else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
|
|
|
243 |
popContext(state);
|
|
|
244 |
state.context.align = false;
|
|
|
245 |
}
|
|
|
246 |
else if (curPunc == "{") pushContext(state, stream.column(), "}");
|
|
|
247 |
else if (curPunc == "[") pushContext(state, stream.column(), "]");
|
|
|
248 |
else if (curPunc == "(") pushContext(state, stream.column(), ")");
|
|
|
249 |
else if (curPunc == "}") {
|
|
|
250 |
while (ctx.type == "statement") ctx = popContext(state);
|
|
|
251 |
if (ctx.type == "}") ctx = popContext(state);
|
|
|
252 |
while (ctx.type == "statement") ctx = popContext(state);
|
|
|
253 |
}
|
|
|
254 |
else if (curPunc == ctx.type) popContext(state);
|
|
|
255 |
else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
|
|
|
256 |
pushContext(state, stream.column(), "statement");
|
|
|
257 |
state.startOfLine = false;
|
|
|
258 |
state.lastToken = curPunc || style;
|
|
|
259 |
return style;
|
|
|
260 |
},
|
|
|
261 |
|
|
|
262 |
indent: function (state, textAfter) {
|
|
|
263 |
if (!state.tokenize[state.tokenize.length - 1].isBase) return 0;
|
|
|
264 |
var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
|
|
|
265 |
if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;
|
|
|
266 |
var closing = firstChar == ctx.type;
|
|
|
267 |
if (ctx.type == "statement") {
|
|
|
268 |
return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
|
|
|
269 |
}
|
|
|
270 |
else if (ctx.align) return ctx.column + (closing ? 0 : 1);
|
|
|
271 |
else return ctx.indented + (closing ? 0 : config.indentUnit);
|
|
|
272 |
},
|
|
|
273 |
|
|
|
274 |
closeBrackets: {triples: "'\""},
|
|
|
275 |
electricChars: "{}",
|
|
|
276 |
blockCommentStart: "/*",
|
|
|
277 |
blockCommentEnd: "*/",
|
|
|
278 |
lineComment: "//"
|
|
|
279 |
};
|
|
|
280 |
});
|
|
|
281 |
|
|
|
282 |
CodeMirror.defineMIME("text/x-kotlin", "kotlin");
|
|
|
283 |
|
|
|
284 |
});
|