1
ace.define('ace/mode/golang', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/golang_highlight_rules', 'ace/mode/matching_brace_outdent', 'ace/mode/behaviour/cstyle', 'ace/mode/folding/cstyle'], function(require, exports, module) {
3
var oop = require("../lib/oop");
4
var TextMode = require("./text").Mode;
5
var Tokenizer = require("../tokenizer").Tokenizer;
6
var GolangHighlightRules = require("./golang_highlight_rules").GolangHighlightRules;
7
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
8
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
9
var CStyleFoldMode = require("./folding/cstyle").FoldMode;
11
var Mode = function() {
12
var highlighter = new GolangHighlightRules();
14
this.$tokenizer = new Tokenizer(highlighter.getRules());
15
this.$keywordList = highlighter.$keywordList;
16
this.$outdent = new MatchingBraceOutdent();
17
this.foldingRules = new CStyleFoldMode();
19
oop.inherits(Mode, TextMode);
23
this.lineCommentStart = "//";
24
this.blockComment = {start: "/*", end: "*/"};
26
this.getNextLineIndent = function(state, line, tab) {
27
var indent = this.$getIndent(line);
29
var tokenizedLine = this.$tokenizer.getLineTokens(line, state);
30
var tokens = tokenizedLine.tokens;
31
var endState = tokenizedLine.state;
33
if (tokens.length && tokens[tokens.length-1].type == "comment") {
37
if (state == "start") {
38
var match = line.match(/^.*[\{\(\[]\s*$/);
45
};//end getNextLineIndent
47
this.checkOutdent = function(state, line, input) {
48
return this.$outdent.checkOutdent(line, input);
51
this.autoOutdent = function(state, doc, row) {
52
this.$outdent.autoOutdent(doc, row);
55
}).call(Mode.prototype);
59
ace.define('ace/mode/golang_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/doc_comment_highlight_rules', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
60
var oop = require("../lib/oop");
61
var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
62
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
64
var GolangHighlightRules = function() {
66
"else|break|case|return|goto|if|const|" +
67
"continue|struct|default|switch|for|" +
68
"func|import|package|chan|defer|fallthrough|go|interface|map|range" +
71
var buildinConstants = ("nil|true|false|iota");
73
var keywordMapper = this.createKeywordMapper({
75
"constant.language": buildinConstants
84
DocCommentHighlightRules.getStartRule("doc-start"),
86
token : "comment", // multi line comment
90
token : "string", // single line
91
regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
93
token : "string", // single line
94
regex : '[`](?:[^`]*)[`]'
96
token : "string", // multi line string start
98
regex : '[`](?:[^`]*)$',
101
token : "constant.numeric", // rune
102
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))[']"
104
token : "constant.numeric", // hex
105
regex : "0[xX][0-9a-fA-F]+\\b"
107
token : "constant.numeric", // float
108
regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
110
token : keywordMapper,
111
regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
113
token : "keyword.operator",
114
regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^="
116
token : "punctuation.operator",
117
regex : "\\?|\\:|\\,|\\;|\\."
119
token : "paren.lparen",
122
token : "paren.rparen",
134
token : "comment", // closing comment
138
token : "comment", // comment spanning whole line
145
regex : '(?:[^`]*)`',
154
this.embedRules(DocCommentHighlightRules, "doc-",
155
[ DocCommentHighlightRules.getEndRule("start") ]);
157
oop.inherits(GolangHighlightRules, TextHighlightRules);
159
exports.GolangHighlightRules = GolangHighlightRules;
162
ace.define('ace/mode/doc_comment_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
165
var oop = require("../lib/oop");
166
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
168
var DocCommentHighlightRules = function() {
172
token : "comment.doc.tag",
173
regex : "@[\\w\\d_]+" // TODO: fix email addresses
175
token : "comment.doc.tag",
178
defaultToken : "comment.doc"
183
oop.inherits(DocCommentHighlightRules, TextHighlightRules);
185
DocCommentHighlightRules.getStartRule = function(start) {
187
token : "comment.doc", // doc comment
188
regex : "\\/\\*(?=\\*)",
193
DocCommentHighlightRules.getEndRule = function (start) {
195
token : "comment.doc", // closing comment
202
exports.DocCommentHighlightRules = DocCommentHighlightRules;
206
ace.define('ace/mode/matching_brace_outdent', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
209
var Range = require("../range").Range;
211
var MatchingBraceOutdent = function() {};
215
this.checkOutdent = function(line, input) {
216
if (! /^\s+$/.test(line))
219
return /^\s*\}/.test(input);
222
this.autoOutdent = function(doc, row) {
223
var line = doc.getLine(row);
224
var match = line.match(/^(\s*\})/);
226
if (!match) return 0;
228
var column = match[1].length;
229
var openBracePos = doc.findMatchingBracket({row: row, column: column});
231
if (!openBracePos || openBracePos.row == row) return 0;
233
var indent = this.$getIndent(doc.getLine(openBracePos.row));
234
doc.replace(new Range(row, 0, row, column-1), indent);
237
this.$getIndent = function(line) {
238
return line.match(/^\s*/)[0];
241
}).call(MatchingBraceOutdent.prototype);
243
exports.MatchingBraceOutdent = MatchingBraceOutdent;
246
ace.define('ace/mode/behaviour/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/behaviour', 'ace/token_iterator', 'ace/lib/lang'], function(require, exports, module) {
249
var oop = require("../../lib/oop");
250
var Behaviour = require("../behaviour").Behaviour;
251
var TokenIterator = require("../../token_iterator").TokenIterator;
252
var lang = require("../../lib/lang");
254
var SAFE_INSERT_IN_TOKENS =
255
["text", "paren.rparen", "punctuation.operator"];
256
var SAFE_INSERT_BEFORE_TOKENS =
257
["text", "paren.rparen", "punctuation.operator", "comment"];
260
var autoInsertedBrackets = 0;
261
var autoInsertedRow = -1;
262
var autoInsertedLineEnd = "";
263
var maybeInsertedBrackets = 0;
264
var maybeInsertedRow = -1;
265
var maybeInsertedLineStart = "";
266
var maybeInsertedLineEnd = "";
268
var CstyleBehaviour = function () {
270
CstyleBehaviour.isSaneInsertion = function(editor, session) {
271
var cursor = editor.getCursorPosition();
272
var iterator = new TokenIterator(session, cursor.row, cursor.column);
273
if (!this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS)) {
274
var iterator2 = new TokenIterator(session, cursor.row, cursor.column + 1);
275
if (!this.$matchTokenType(iterator2.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS))
278
iterator.stepForward();
279
return iterator.getCurrentTokenRow() !== cursor.row ||
280
this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_BEFORE_TOKENS);
283
CstyleBehaviour.$matchTokenType = function(token, types) {
284
return types.indexOf(token.type || token) > -1;
287
CstyleBehaviour.recordAutoInsert = function(editor, session, bracket) {
288
var cursor = editor.getCursorPosition();
289
var line = session.doc.getLine(cursor.row);
290
if (!this.isAutoInsertedClosing(cursor, line, autoInsertedLineEnd[0]))
291
autoInsertedBrackets = 0;
292
autoInsertedRow = cursor.row;
293
autoInsertedLineEnd = bracket + line.substr(cursor.column);
294
autoInsertedBrackets++;
297
CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
298
var cursor = editor.getCursorPosition();
299
var line = session.doc.getLine(cursor.row);
300
if (!this.isMaybeInsertedClosing(cursor, line))
301
maybeInsertedBrackets = 0;
302
maybeInsertedRow = cursor.row;
303
maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
304
maybeInsertedLineEnd = line.substr(cursor.column);
305
maybeInsertedBrackets++;
308
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
309
return autoInsertedBrackets > 0 &&
310
cursor.row === autoInsertedRow &&
311
bracket === autoInsertedLineEnd[0] &&
312
line.substr(cursor.column) === autoInsertedLineEnd;
315
CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
316
return maybeInsertedBrackets > 0 &&
317
cursor.row === maybeInsertedRow &&
318
line.substr(cursor.column) === maybeInsertedLineEnd &&
319
line.substr(0, cursor.column) == maybeInsertedLineStart;
322
CstyleBehaviour.popAutoInsertedClosing = function() {
323
autoInsertedLineEnd = autoInsertedLineEnd.substr(1);
324
autoInsertedBrackets--;
327
CstyleBehaviour.clearMaybeInsertedClosing = function() {
328
maybeInsertedBrackets = 0;
329
maybeInsertedRow = -1;
332
this.add("braces", "insertion", function (state, action, editor, session, text) {
333
var cursor = editor.getCursorPosition();
334
var line = session.doc.getLine(cursor.row);
336
var selection = editor.getSelectionRange();
337
var selected = session.doc.getTextRange(selection);
338
if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
340
text: '{' + selected + '}',
343
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
344
if (/[\]\}\)]/.test(line[cursor.column])) {
345
CstyleBehaviour.recordAutoInsert(editor, session, "}");
351
CstyleBehaviour.recordMaybeInsert(editor, session, "{");
358
} else if (text == '}') {
359
var rightChar = line.substring(cursor.column, cursor.column + 1);
360
if (rightChar == '}') {
361
var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
362
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
363
CstyleBehaviour.popAutoInsertedClosing();
370
} else if (text == "\n" || text == "\r\n") {
372
if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
373
closing = lang.stringRepeat("}", maybeInsertedBrackets);
374
CstyleBehaviour.clearMaybeInsertedClosing();
376
var rightChar = line.substring(cursor.column, cursor.column + 1);
377
if (rightChar == '}' || closing !== "") {
378
var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column}, '}');
382
var indent = this.getNextLineIndent(state, line.substring(0, cursor.column), session.getTabString());
383
var next_indent = this.$getIndent(line);
386
text: '\n' + indent + '\n' + next_indent + closing,
387
selection: [1, indent.length, 1, indent.length]
393
this.add("braces", "deletion", function (state, action, editor, session, range) {
394
var selected = session.doc.getTextRange(range);
395
if (!range.isMultiLine() && selected == '{') {
396
var line = session.doc.getLine(range.start.row);
397
var rightChar = line.substring(range.end.column, range.end.column + 1);
398
if (rightChar == '}') {
402
maybeInsertedBrackets--;
407
this.add("parens", "insertion", function (state, action, editor, session, text) {
409
var selection = editor.getSelectionRange();
410
var selected = session.doc.getTextRange(selection);
411
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
413
text: '(' + selected + ')',
416
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
417
CstyleBehaviour.recordAutoInsert(editor, session, ")");
423
} else if (text == ')') {
424
var cursor = editor.getCursorPosition();
425
var line = session.doc.getLine(cursor.row);
426
var rightChar = line.substring(cursor.column, cursor.column + 1);
427
if (rightChar == ')') {
428
var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
429
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
430
CstyleBehaviour.popAutoInsertedClosing();
440
this.add("parens", "deletion", function (state, action, editor, session, range) {
441
var selected = session.doc.getTextRange(range);
442
if (!range.isMultiLine() && selected == '(') {
443
var line = session.doc.getLine(range.start.row);
444
var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
445
if (rightChar == ')') {
452
this.add("brackets", "insertion", function (state, action, editor, session, text) {
454
var selection = editor.getSelectionRange();
455
var selected = session.doc.getTextRange(selection);
456
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
458
text: '[' + selected + ']',
461
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
462
CstyleBehaviour.recordAutoInsert(editor, session, "]");
468
} else if (text == ']') {
469
var cursor = editor.getCursorPosition();
470
var line = session.doc.getLine(cursor.row);
471
var rightChar = line.substring(cursor.column, cursor.column + 1);
472
if (rightChar == ']') {
473
var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
474
if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
475
CstyleBehaviour.popAutoInsertedClosing();
485
this.add("brackets", "deletion", function (state, action, editor, session, range) {
486
var selected = session.doc.getTextRange(range);
487
if (!range.isMultiLine() && selected == '[') {
488
var line = session.doc.getLine(range.start.row);
489
var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
490
if (rightChar == ']') {
497
this.add("string_dquotes", "insertion", function (state, action, editor, session, text) {
498
if (text == '"' || text == "'") {
500
var selection = editor.getSelectionRange();
501
var selected = session.doc.getTextRange(selection);
502
if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
504
text: quote + selected + quote,
508
var cursor = editor.getCursorPosition();
509
var line = session.doc.getLine(cursor.row);
510
var leftChar = line.substring(cursor.column-1, cursor.column);
511
if (leftChar == '\\') {
514
var tokens = session.getTokens(selection.start.row);
516
var quotepos = -1; // Track whether we're inside an open quote.
518
for (var x = 0; x < tokens.length; x++) {
520
if (token.type == "string") {
522
} else if (quotepos < 0) {
523
quotepos = token.value.indexOf(quote);
525
if ((token.value.length + col) > selection.start.column) {
528
col += tokens[x].value.length;
530
if (!token || (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf(quote) === token.value.length-1)))) {
531
if (!CstyleBehaviour.isSaneInsertion(editor, session))
537
} else if (token && token.type === "string") {
538
var rightChar = line.substring(cursor.column, cursor.column + 1);
539
if (rightChar == quote) {
550
this.add("string_dquotes", "deletion", function (state, action, editor, session, range) {
551
var selected = session.doc.getTextRange(range);
552
if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
553
var line = session.doc.getLine(range.start.row);
554
var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
555
if (rightChar == selected) {
564
oop.inherits(CstyleBehaviour, Behaviour);
566
exports.CstyleBehaviour = CstyleBehaviour;
569
ace.define('ace/mode/folding/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/range', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
572
var oop = require("../../lib/oop");
573
var Range = require("../../range").Range;
574
var BaseFoldMode = require("./fold_mode").FoldMode;
576
var FoldMode = exports.FoldMode = function(commentRegex) {
578
this.foldingStartMarker = new RegExp(
579
this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
581
this.foldingStopMarker = new RegExp(
582
this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
586
oop.inherits(FoldMode, BaseFoldMode);
590
this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
591
this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
593
this.getFoldWidgetRange = function(session, foldStyle, row) {
594
var line = session.getLine(row);
595
var match = line.match(this.foldingStartMarker);
600
return this.openingBracketBlock(session, match[1], row, i);
602
return session.getCommentFoldRange(row, i + match[0].length, 1);
605
if (foldStyle !== "markbeginend")
608
var match = line.match(this.foldingStopMarker);
610
var i = match.index + match[0].length;
613
return this.closingBracketBlock(session, match[1], row, i);
615
return session.getCommentFoldRange(row, i, -1);
619
}).call(FoldMode.prototype);