~galaxyabstractor/lenasys/ace

« back to all changes in this revision

Viewing changes to js/ace/mode-css.js

  • Committer: galaxyAbstractor
  • Date: 2013-04-04 16:19:07 UTC
  • Revision ID: galaxyabstractor@gmail.com-20130404161907-eo1wnb7ac2rnizg8
Started implementation of a new codeviewer using Ace

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Distributed under the BSD license:
 
3
 *
 
4
 * Copyright (c) 2010, Ajax.org B.V.
 
5
 * All rights reserved.
 
6
 * 
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 *     * Redistributions of source code must retain the above copyright
 
10
 *       notice, this list of conditions and the following disclaimer.
 
11
 *     * Redistributions in binary form must reproduce the above copyright
 
12
 *       notice, this list of conditions and the following disclaimer in the
 
13
 *       documentation and/or other materials provided with the distribution.
 
14
 *     * Neither the name of Ajax.org B.V. nor the
 
15
 *       names of its contributors may be used to endorse or promote products
 
16
 *       derived from this software without specific prior written permission.
 
17
 * 
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
 
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 *
 
29
 * ***** END LICENSE BLOCK ***** */
 
30
 
 
31
define('ace/mode/css', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/css_highlight_rules', 'ace/mode/matching_brace_outdent', 'ace/worker/worker_client', 'ace/mode/behaviour/css', 'ace/mode/folding/cstyle'], function(require, exports, module) {
 
32
 
 
33
 
 
34
var oop = require("../lib/oop");
 
35
var TextMode = require("./text").Mode;
 
36
var Tokenizer = require("../tokenizer").Tokenizer;
 
37
var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules;
 
38
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
 
39
var WorkerClient = require("../worker/worker_client").WorkerClient;
 
40
var CssBehaviour = require("./behaviour/css").CssBehaviour;
 
41
var CStyleFoldMode = require("./folding/cstyle").FoldMode;
 
42
 
 
43
var Mode = function() {
 
44
    this.$tokenizer = new Tokenizer(new CssHighlightRules().getRules());
 
45
    this.$outdent = new MatchingBraceOutdent();
 
46
    this.$behaviour = new CssBehaviour();
 
47
    this.foldingRules = new CStyleFoldMode();
 
48
};
 
49
oop.inherits(Mode, TextMode);
 
50
 
 
51
(function() {
 
52
 
 
53
    this.foldingRules = "cStyle";
 
54
    this.blockComment = {start: "/*", end: "*/"};
 
55
 
 
56
    this.getNextLineIndent = function(state, line, tab) {
 
57
        var indent = this.$getIndent(line);
 
58
        var tokens = this.$tokenizer.getLineTokens(line, state).tokens;
 
59
        if (tokens.length && tokens[tokens.length-1].type == "comment") {
 
60
            return indent;
 
61
        }
 
62
 
 
63
        var match = line.match(/^.*\{\s*$/);
 
64
        if (match) {
 
65
            indent += tab;
 
66
        }
 
67
 
 
68
        return indent;
 
69
    };
 
70
 
 
71
    this.checkOutdent = function(state, line, input) {
 
72
        return this.$outdent.checkOutdent(line, input);
 
73
    };
 
74
 
 
75
    this.autoOutdent = function(state, doc, row) {
 
76
        this.$outdent.autoOutdent(doc, row);
 
77
    };
 
78
 
 
79
    this.createWorker = function(session) {
 
80
        var worker = new WorkerClient(["ace"], "ace/mode/css_worker", "Worker");
 
81
        worker.attachToDocument(session.getDocument());
 
82
 
 
83
        worker.on("csslint", function(e) {
 
84
            session.setAnnotations(e.data);
 
85
        });
 
86
 
 
87
        worker.on("terminate", function() {
 
88
            session.clearAnnotations();
 
89
        });
 
90
 
 
91
        return worker;
 
92
    };
 
93
 
 
94
}).call(Mode.prototype);
 
95
 
 
96
exports.Mode = Mode;
 
97
 
 
98
});
 
99
 
 
100
define('ace/mode/css_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/lang', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
 
101
 
 
102
 
 
103
var oop = require("../lib/oop");
 
104
var lang = require("../lib/lang");
 
105
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
 
106
var supportType = exports.supportType = "animation-fill-mode|alignment-adjust|alignment-baseline|animation-delay|animation-direction|animation-duration|animation-iteration-count|animation-name|animation-play-state|animation-timing-function|animation|appearance|azimuth|backface-visibility|background-attachment|background-break|background-clip|background-color|background-image|background-origin|background-position|background-repeat|background-size|background|baseline-shift|binding|bleed|bookmark-label|bookmark-level|bookmark-state|bookmark-target|border-bottom|border-bottom-color|border-bottom-left-radius|border-bottom-right-radius|border-bottom-style|border-bottom-width|border-collapse|border-color|border-image|border-image-outset|border-image-repeat|border-image-slice|border-image-source|border-image-width|border-left|border-left-color|border-left-style|border-left-width|border-radius|border-right|border-right-color|border-right-style|border-right-width|border-spacing|border-style|border-top|border-top-color|border-top-left-radius|border-top-right-radius|border-top-style|border-top-width|border-width|border|bottom|box-align|box-decoration-break|box-direction|box-flex-group|box-flex|box-lines|box-ordinal-group|box-orient|box-pack|box-shadow|box-sizing|break-after|break-before|break-inside|caption-side|clear|clip|color-profile|color|column-count|column-fill|column-gap|column-rule|column-rule-color|column-rule-style|column-rule-width|column-span|column-width|columns|content|counter-increment|counter-reset|crop|cue-after|cue-before|cue|cursor|direction|display|dominant-baseline|drop-initial-after-adjust|drop-initial-after-align|drop-initial-before-adjust|drop-initial-before-align|drop-initial-size|drop-initial-value|elevation|empty-cells|fit|fit-position|float-offset|float|font-family|font-size|font-size-adjust|font-stretch|font-style|font-variant|font-weight|font|grid-columns|grid-rows|hanging-punctuation|height|hyphenate-after|hyphenate-before|hyphenate-character|hyphenate-lines|hyphenate-resource|hyphens|icon|image-orientation|image-rendering|image-resolution|inline-box-align|left|letter-spacing|line-height|line-stacking-ruby|line-stacking-shift|line-stacking-strategy|line-stacking|list-style-image|list-style-position|list-style-type|list-style|margin-bottom|margin-left|margin-right|margin-top|margin|mark-after|mark-before|mark|marks|marquee-direction|marquee-play-count|marquee-speed|marquee-style|max-height|max-width|min-height|min-width|move-to|nav-down|nav-index|nav-left|nav-right|nav-up|opacity|orphans|outline-color|outline-offset|outline-style|outline-width|outline|overflow-style|overflow-x|overflow-y|overflow|padding-bottom|padding-left|padding-right|padding-top|padding|page-break-after|page-break-before|page-break-inside|page-policy|page|pause-after|pause-before|pause|perspective-origin|perspective|phonemes|pitch-range|pitch|play-during|position|presentation-level|punctuation-trim|quotes|rendering-intent|resize|rest-after|rest-before|rest|richness|right|rotation-point|rotation|ruby-align|ruby-overhang|ruby-position|ruby-span|size|speak-header|speak-numeral|speak-punctuation|speak|speech-rate|stress|string-set|table-layout|target-name|target-new|target-position|target|text-align-last|text-align|text-decoration|text-emphasis|text-height|text-indent|text-justify|text-outline|text-shadow|text-transform|text-wrap|top|transform-origin|transform-style|transform|transition-delay|transition-duration|transition-property|transition-timing-function|transition|unicode-bidi|vertical-align|visibility|voice-balance|voice-duration|voice-family|voice-pitch-range|voice-pitch|voice-rate|voice-stress|voice-volume|volume|white-space-collapse|white-space|widows|width|word-break|word-spacing|word-wrap|z-index";
 
107
var supportFunction = exports.supportFunction = "rgb|rgba|url|attr|counter|counters";
 
108
var supportConstant = exports.supportConstant = "absolute|after-edge|after|all-scroll|all|alphabetic|always|antialiased|armenian|auto|avoid-column|avoid-page|avoid|balance|baseline|before-edge|before|below|bidi-override|block-line-height|block|bold|bolder|border-box|both|bottom|box|break-all|break-word|capitalize|caps-height|caption|center|central|char|circle|cjk-ideographic|clone|close-quote|col-resize|collapse|column|consider-shifts|contain|content-box|cover|crosshair|cubic-bezier|dashed|decimal-leading-zero|decimal|default|disabled|disc|disregard-shifts|distribute-all-lines|distribute-letter|distribute-space|distribute|dotted|double|e-resize|ease-in|ease-in-out|ease-out|ease|ellipsis|end|exclude-ruby|fill|fixed|georgian|glyphs|grid-height|groove|hand|hanging|hebrew|help|hidden|hiragana-iroha|hiragana|horizontal|icon|ideograph-alpha|ideograph-numeric|ideograph-parenthesis|ideograph-space|ideographic|inactive|include-ruby|inherit|initial|inline-block|inline-box|inline-line-height|inline-table|inline|inset|inside|inter-ideograph|inter-word|invert|italic|justify|katakana-iroha|katakana|keep-all|last|left|lighter|line-edge|line-through|line|linear|list-item|local|loose|lower-alpha|lower-greek|lower-latin|lower-roman|lowercase|lr-tb|ltr|mathematical|max-height|max-size|medium|menu|message-box|middle|move|n-resize|ne-resize|newspaper|no-change|no-close-quote|no-drop|no-open-quote|no-repeat|none|normal|not-allowed|nowrap|nw-resize|oblique|open-quote|outset|outside|overline|padding-box|page|pointer|pre-line|pre-wrap|pre|preserve-3d|progress|relative|repeat-x|repeat-y|repeat|replaced|reset-size|ridge|right|round|row-resize|rtl|s-resize|scroll|se-resize|separate|slice|small-caps|small-caption|solid|space|square|start|static|status-bar|step-end|step-start|steps|stretch|strict|sub|super|sw-resize|table-caption|table-cell|table-column-group|table-column|table-footer-group|table-header-group|table-row-group|table-row|table|tb-rl|text-after-edge|text-before-edge|text-bottom|text-size|text-top|text|thick|thin|transparent|underline|upper-alpha|upper-latin|upper-roman|uppercase|use-script|vertical-ideographic|vertical-text|visible|w-resize|wait|whitespace|z-index|zero";
 
109
var supportConstantColor = exports.supportConstantColor = "aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow";
 
110
var supportConstantFonts = exports.supportConstantFonts = "arial|century|comic|courier|garamond|georgia|helvetica|impact|lucida|symbol|system|tahoma|times|trebuchet|utopia|verdana|webdings|sans-serif|serif|monospace";
 
111
 
 
112
var numRe = exports.numRe = "\\-?(?:(?:[0-9]+)|(?:[0-9]*\\.[0-9]+))";
 
113
var pseudoElements = exports.pseudoElements = "(\\:+)\\b(after|before|first-letter|first-line|moz-selection|selection)\\b";
 
114
var pseudoClasses  = exports.pseudoClasses =  "(:)\\b(active|checked|disabled|empty|enabled|first-child|first-of-type|focus|hover|indeterminate|invalid|last-child|last-of-type|link|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|only-child|only-of-type|required|root|target|valid|visited)\\b";
 
115
 
 
116
var CssHighlightRules = function() {
 
117
 
 
118
    var keywordMapper = this.createKeywordMapper({
 
119
        "support.function": supportFunction,
 
120
        "support.constant": supportConstant,
 
121
        "support.type": supportType,
 
122
        "support.constant.color": supportConstantColor,
 
123
        "support.constant.fonts": supportConstantFonts
 
124
    }, "text", true);
 
125
 
 
126
    var base_ruleset = [
 
127
        {
 
128
            token : "comment", // multi line comment
 
129
            regex : "\\/\\*",
 
130
            next : "ruleset_comment"
 
131
        }, {
 
132
            token : "string", // single line
 
133
            regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
 
134
        }, {
 
135
            token : "string", // single line
 
136
            regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
 
137
        }, {
 
138
            token : ["constant.numeric", "keyword"],
 
139
            regex : "(" + numRe + ")(ch|cm|deg|em|ex|fr|gd|grad|Hz|in|kHz|mm|ms|pc|pt|px|rad|rem|s|turn|vh|vm|vw|%)"
 
140
        }, {
 
141
            token : "constant.numeric",
 
142
            regex : numRe
 
143
        }, {
 
144
            token : "constant.numeric",  // hex6 color
 
145
            regex : "#[a-f0-9]{6}"
 
146
        }, {
 
147
            token : "constant.numeric", // hex3 color
 
148
            regex : "#[a-f0-9]{3}"
 
149
        }, {
 
150
            token : ["punctuation", "entity.other.attribute-name.pseudo-element.css"],
 
151
            regex : pseudoElements
 
152
        }, {
 
153
            token : ["punctuation", "entity.other.attribute-name.pseudo-class.css"],
 
154
            regex : pseudoClasses
 
155
        }, {
 
156
            token : ["support.function", "string", "support.function"],
 
157
            regex : "(url\\()(.*)(\\))"
 
158
        }, {
 
159
            token : keywordMapper,
 
160
            regex : "\\-?[a-zA-Z_][a-zA-Z0-9_\\-]*"
 
161
        }, {
 
162
            caseInsensitive: true
 
163
        }
 
164
      ];
 
165
 
 
166
    var ruleset = lang.copyArray(base_ruleset);
 
167
    ruleset.unshift({
 
168
        token : "paren.rparen",
 
169
        regex : "\\}",
 
170
        next:   "start"
 
171
    });
 
172
 
 
173
    var media_ruleset = lang.copyArray( base_ruleset );
 
174
    media_ruleset.unshift({
 
175
        token : "paren.rparen",
 
176
        regex : "\\}",
 
177
        next:   "media"
 
178
    });
 
179
 
 
180
    var base_comment = [{
 
181
          token : "comment", // comment spanning whole line
 
182
          regex : ".+"
 
183
    }];
 
184
 
 
185
    var comment = lang.copyArray(base_comment);
 
186
    comment.unshift({
 
187
          token : "comment", // closing comment
 
188
          regex : ".*?\\*\\/",
 
189
          next : "start"
 
190
    });
 
191
 
 
192
    var media_comment = lang.copyArray(base_comment);
 
193
    media_comment.unshift({
 
194
          token : "comment", // closing comment
 
195
          regex : ".*?\\*\\/",
 
196
          next : "media"
 
197
    });
 
198
 
 
199
    var ruleset_comment = lang.copyArray(base_comment);
 
200
    ruleset_comment.unshift({
 
201
          token : "comment", // closing comment
 
202
          regex : ".*?\\*\\/",
 
203
          next : "ruleset"
 
204
    });
 
205
 
 
206
    this.$rules = {
 
207
        "start" : [{
 
208
            token : "comment", // multi line comment
 
209
            regex : "\\/\\*",
 
210
            next : "comment"
 
211
        }, {
 
212
            token: "paren.lparen",
 
213
            regex: "\\{",
 
214
            next:  "ruleset"
 
215
        }, {
 
216
            token: "string",
 
217
            regex: "@.*?{",
 
218
            next:  "media"
 
219
        },{
 
220
            token: "keyword",
 
221
            regex: "#[a-z0-9-_]+"
 
222
        },{
 
223
            token: "variable",
 
224
            regex: "\\.[a-z0-9-_]+"
 
225
        },{
 
226
            token: "string",
 
227
            regex: ":[a-z0-9-_]+"
 
228
        },{
 
229
            token: "constant",
 
230
            regex: "[a-z0-9-_]+"
 
231
        },{
 
232
            caseInsensitive: true
 
233
        }],
 
234
 
 
235
        "media" : [ {
 
236
            token : "comment", // multi line comment
 
237
            regex : "\\/\\*",
 
238
            next : "media_comment"
 
239
        }, {
 
240
            token: "paren.lparen",
 
241
            regex: "\\{",
 
242
            next:  "media_ruleset"
 
243
        },{
 
244
            token: "string",
 
245
            regex: "\\}",
 
246
            next:  "start"
 
247
        },{
 
248
            token: "keyword",
 
249
            regex: "#[a-z0-9-_]+"
 
250
        },{
 
251
            token: "variable",
 
252
            regex: "\\.[a-z0-9-_]+"
 
253
        },{
 
254
            token: "string",
 
255
            regex: ":[a-z0-9-_]+"
 
256
        },{
 
257
            token: "constant",
 
258
            regex: "[a-z0-9-_]+"
 
259
        },{
 
260
            caseInsensitive: true
 
261
        }],
 
262
 
 
263
        "comment" : comment,
 
264
 
 
265
        "ruleset" : ruleset,
 
266
        "ruleset_comment" : ruleset_comment,
 
267
 
 
268
        "media_ruleset" : media_ruleset,
 
269
        "media_comment" : media_comment
 
270
    };
 
271
};
 
272
 
 
273
oop.inherits(CssHighlightRules, TextHighlightRules);
 
274
 
 
275
exports.CssHighlightRules = CssHighlightRules;
 
276
 
 
277
});
 
278
 
 
279
define('ace/mode/matching_brace_outdent', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
 
280
 
 
281
 
 
282
var Range = require("../range").Range;
 
283
 
 
284
var MatchingBraceOutdent = function() {};
 
285
 
 
286
(function() {
 
287
 
 
288
    this.checkOutdent = function(line, input) {
 
289
        if (! /^\s+$/.test(line))
 
290
            return false;
 
291
 
 
292
        return /^\s*\}/.test(input);
 
293
    };
 
294
 
 
295
    this.autoOutdent = function(doc, row) {
 
296
        var line = doc.getLine(row);
 
297
        var match = line.match(/^(\s*\})/);
 
298
 
 
299
        if (!match) return 0;
 
300
 
 
301
        var column = match[1].length;
 
302
        var openBracePos = doc.findMatchingBracket({row: row, column: column});
 
303
 
 
304
        if (!openBracePos || openBracePos.row == row) return 0;
 
305
 
 
306
        var indent = this.$getIndent(doc.getLine(openBracePos.row));
 
307
        doc.replace(new Range(row, 0, row, column-1), indent);
 
308
    };
 
309
 
 
310
    this.$getIndent = function(line) {
 
311
        return line.match(/^\s*/)[0];
 
312
    };
 
313
 
 
314
}).call(MatchingBraceOutdent.prototype);
 
315
 
 
316
exports.MatchingBraceOutdent = MatchingBraceOutdent;
 
317
});
 
318
 
 
319
define('ace/mode/behaviour/css', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/behaviour', 'ace/mode/behaviour/cstyle', 'ace/token_iterator'], function(require, exports, module) {
 
320
 
 
321
 
 
322
var oop = require("../../lib/oop");
 
323
var Behaviour = require("../behaviour").Behaviour;
 
324
var CstyleBehaviour = require("./cstyle").CstyleBehaviour;
 
325
var TokenIterator = require("../../token_iterator").TokenIterator;
 
326
 
 
327
var CssBehaviour = function () {
 
328
 
 
329
    this.inherit(CstyleBehaviour);
 
330
 
 
331
    this.add("colon", "insertion", function (state, action, editor, session, text) {
 
332
        if (text === ':') {
 
333
            var cursor = editor.getCursorPosition();
 
334
            var iterator = new TokenIterator(session, cursor.row, cursor.column);
 
335
            var token = iterator.getCurrentToken();
 
336
            if (token && token.value.match(/\s+/)) {
 
337
                token = iterator.stepBackward();
 
338
            }
 
339
            if (token && token.type === 'support.type') {
 
340
                var line = session.doc.getLine(cursor.row);
 
341
                var rightChar = line.substring(cursor.column, cursor.column + 1);
 
342
                if (rightChar === ':') {
 
343
                    return {
 
344
                       text: '',
 
345
                       selection: [1, 1]
 
346
                    }
 
347
                }
 
348
                if (!line.substring(cursor.column).match(/^\s*;/)) {
 
349
                    return {
 
350
                       text: ':;',
 
351
                       selection: [1, 1]
 
352
                    }
 
353
                }
 
354
            }
 
355
        }
 
356
    });
 
357
 
 
358
    this.add("colon", "deletion", function (state, action, editor, session, range) {
 
359
        var selected = session.doc.getTextRange(range);
 
360
        if (!range.isMultiLine() && selected === ':') {
 
361
            var cursor = editor.getCursorPosition();
 
362
            var iterator = new TokenIterator(session, cursor.row, cursor.column);
 
363
            var token = iterator.getCurrentToken();
 
364
            if (token && token.value.match(/\s+/)) {
 
365
                token = iterator.stepBackward();
 
366
            }
 
367
            if (token && token.type === 'support.type') {
 
368
                var line = session.doc.getLine(range.start.row);
 
369
                var rightChar = line.substring(range.end.column, range.end.column + 1);
 
370
                if (rightChar === ';') {
 
371
                    range.end.column ++;
 
372
                    return range;
 
373
                }
 
374
            }
 
375
        }
 
376
    });
 
377
 
 
378
    this.add("semicolon", "insertion", function (state, action, editor, session, text) {
 
379
        if (text === ';') {
 
380
            var cursor = editor.getCursorPosition();
 
381
            var line = session.doc.getLine(cursor.row);
 
382
            var rightChar = line.substring(cursor.column, cursor.column + 1);
 
383
            if (rightChar === ';') {
 
384
                return {
 
385
                   text: '',
 
386
                   selection: [1, 1]
 
387
                }
 
388
            }
 
389
        }
 
390
    });
 
391
 
 
392
}
 
393
oop.inherits(CssBehaviour, CstyleBehaviour);
 
394
 
 
395
exports.CssBehaviour = CssBehaviour;
 
396
});
 
397
 
 
398
define('ace/mode/behaviour/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/behaviour', 'ace/token_iterator', 'ace/lib/lang'], function(require, exports, module) {
 
399
 
 
400
 
 
401
var oop = require("../../lib/oop");
 
402
var Behaviour = require("../behaviour").Behaviour;
 
403
var TokenIterator = require("../../token_iterator").TokenIterator;
 
404
var lang = require("../../lib/lang");
 
405
 
 
406
var SAFE_INSERT_IN_TOKENS =
 
407
    ["text", "paren.rparen", "punctuation.operator"];
 
408
var SAFE_INSERT_BEFORE_TOKENS =
 
409
    ["text", "paren.rparen", "punctuation.operator", "comment"];
 
410
 
 
411
 
 
412
var autoInsertedBrackets = 0;
 
413
var autoInsertedRow = -1;
 
414
var autoInsertedLineEnd = "";
 
415
var maybeInsertedBrackets = 0;
 
416
var maybeInsertedRow = -1;
 
417
var maybeInsertedLineStart = "";
 
418
var maybeInsertedLineEnd = "";
 
419
 
 
420
var CstyleBehaviour = function () {
 
421
    
 
422
    CstyleBehaviour.isSaneInsertion = function(editor, session) {
 
423
        var cursor = editor.getCursorPosition();
 
424
        var iterator = new TokenIterator(session, cursor.row, cursor.column);
 
425
        if (!this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS)) {
 
426
            var iterator2 = new TokenIterator(session, cursor.row, cursor.column + 1);
 
427
            if (!this.$matchTokenType(iterator2.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS))
 
428
                return false;
 
429
        }
 
430
        iterator.stepForward();
 
431
        return iterator.getCurrentTokenRow() !== cursor.row ||
 
432
            this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_BEFORE_TOKENS);
 
433
    };
 
434
    
 
435
    CstyleBehaviour.$matchTokenType = function(token, types) {
 
436
        return types.indexOf(token.type || token) > -1;
 
437
    };
 
438
    
 
439
    CstyleBehaviour.recordAutoInsert = function(editor, session, bracket) {
 
440
        var cursor = editor.getCursorPosition();
 
441
        var line = session.doc.getLine(cursor.row);
 
442
        if (!this.isAutoInsertedClosing(cursor, line, autoInsertedLineEnd[0]))
 
443
            autoInsertedBrackets = 0;
 
444
        autoInsertedRow = cursor.row;
 
445
        autoInsertedLineEnd = bracket + line.substr(cursor.column);
 
446
        autoInsertedBrackets++;
 
447
    };
 
448
    
 
449
    CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
 
450
        var cursor = editor.getCursorPosition();
 
451
        var line = session.doc.getLine(cursor.row);
 
452
        if (!this.isMaybeInsertedClosing(cursor, line))
 
453
            maybeInsertedBrackets = 0;
 
454
        maybeInsertedRow = cursor.row;
 
455
        maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
 
456
        maybeInsertedLineEnd = line.substr(cursor.column);
 
457
        maybeInsertedBrackets++;
 
458
    };
 
459
    
 
460
    CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
 
461
        return autoInsertedBrackets > 0 &&
 
462
            cursor.row === autoInsertedRow &&
 
463
            bracket === autoInsertedLineEnd[0] &&
 
464
            line.substr(cursor.column) === autoInsertedLineEnd;
 
465
    };
 
466
    
 
467
    CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
 
468
        return maybeInsertedBrackets > 0 &&
 
469
            cursor.row === maybeInsertedRow &&
 
470
            line.substr(cursor.column) === maybeInsertedLineEnd &&
 
471
            line.substr(0, cursor.column) == maybeInsertedLineStart;
 
472
    };
 
473
    
 
474
    CstyleBehaviour.popAutoInsertedClosing = function() {
 
475
        autoInsertedLineEnd = autoInsertedLineEnd.substr(1);
 
476
        autoInsertedBrackets--;
 
477
    };
 
478
    
 
479
    CstyleBehaviour.clearMaybeInsertedClosing = function() {
 
480
        maybeInsertedBrackets = 0;
 
481
        maybeInsertedRow = -1;
 
482
    };
 
483
 
 
484
    this.add("braces", "insertion", function (state, action, editor, session, text) {
 
485
        var cursor = editor.getCursorPosition();
 
486
        var line = session.doc.getLine(cursor.row);
 
487
        if (text == '{') {
 
488
            var selection = editor.getSelectionRange();
 
489
            var selected = session.doc.getTextRange(selection);
 
490
            if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
 
491
                return {
 
492
                    text: '{' + selected + '}',
 
493
                    selection: false
 
494
                };
 
495
            } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
 
496
                if (/[\]\}\)]/.test(line[cursor.column])) {
 
497
                    CstyleBehaviour.recordAutoInsert(editor, session, "}");
 
498
                    return {
 
499
                        text: '{}',
 
500
                        selection: [1, 1]
 
501
                    };
 
502
                } else {
 
503
                    CstyleBehaviour.recordMaybeInsert(editor, session, "{");
 
504
                    return {
 
505
                        text: '{',
 
506
                        selection: [1, 1]
 
507
                    };
 
508
                }
 
509
            }
 
510
        } else if (text == '}') {
 
511
            var rightChar = line.substring(cursor.column, cursor.column + 1);
 
512
            if (rightChar == '}') {
 
513
                var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
 
514
                if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
 
515
                    CstyleBehaviour.popAutoInsertedClosing();
 
516
                    return {
 
517
                        text: '',
 
518
                        selection: [1, 1]
 
519
                    };
 
520
                }
 
521
            }
 
522
        } else if (text == "\n" || text == "\r\n") {
 
523
            var closing = "";
 
524
            if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
 
525
                closing = lang.stringRepeat("}", maybeInsertedBrackets);
 
526
                CstyleBehaviour.clearMaybeInsertedClosing();
 
527
            }
 
528
            var rightChar = line.substring(cursor.column, cursor.column + 1);
 
529
            if (rightChar == '}' || closing !== "") {
 
530
                var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column}, '}');
 
531
                if (!openBracePos)
 
532
                     return null;
 
533
 
 
534
                var indent = this.getNextLineIndent(state, line.substring(0, cursor.column), session.getTabString());
 
535
                var next_indent = this.$getIndent(line);
 
536
 
 
537
                return {
 
538
                    text: '\n' + indent + '\n' + next_indent + closing,
 
539
                    selection: [1, indent.length, 1, indent.length]
 
540
                };
 
541
            }
 
542
        }
 
543
    });
 
544
 
 
545
    this.add("braces", "deletion", function (state, action, editor, session, range) {
 
546
        var selected = session.doc.getTextRange(range);
 
547
        if (!range.isMultiLine() && selected == '{') {
 
548
            var line = session.doc.getLine(range.start.row);
 
549
            var rightChar = line.substring(range.end.column, range.end.column + 1);
 
550
            if (rightChar == '}') {
 
551
                range.end.column++;
 
552
                return range;
 
553
            } else {
 
554
                maybeInsertedBrackets--;
 
555
            }
 
556
        }
 
557
    });
 
558
 
 
559
    this.add("parens", "insertion", function (state, action, editor, session, text) {
 
560
        if (text == '(') {
 
561
            var selection = editor.getSelectionRange();
 
562
            var selected = session.doc.getTextRange(selection);
 
563
            if (selected !== "" && editor.getWrapBehavioursEnabled()) {
 
564
                return {
 
565
                    text: '(' + selected + ')',
 
566
                    selection: false
 
567
                };
 
568
            } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
 
569
                CstyleBehaviour.recordAutoInsert(editor, session, ")");
 
570
                return {
 
571
                    text: '()',
 
572
                    selection: [1, 1]
 
573
                };
 
574
            }
 
575
        } else if (text == ')') {
 
576
            var cursor = editor.getCursorPosition();
 
577
            var line = session.doc.getLine(cursor.row);
 
578
            var rightChar = line.substring(cursor.column, cursor.column + 1);
 
579
            if (rightChar == ')') {
 
580
                var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
 
581
                if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
 
582
                    CstyleBehaviour.popAutoInsertedClosing();
 
583
                    return {
 
584
                        text: '',
 
585
                        selection: [1, 1]
 
586
                    };
 
587
                }
 
588
            }
 
589
        }
 
590
    });
 
591
 
 
592
    this.add("parens", "deletion", function (state, action, editor, session, range) {
 
593
        var selected = session.doc.getTextRange(range);
 
594
        if (!range.isMultiLine() && selected == '(') {
 
595
            var line = session.doc.getLine(range.start.row);
 
596
            var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
 
597
            if (rightChar == ')') {
 
598
                range.end.column++;
 
599
                return range;
 
600
            }
 
601
        }
 
602
    });
 
603
 
 
604
    this.add("brackets", "insertion", function (state, action, editor, session, text) {
 
605
        if (text == '[') {
 
606
            var selection = editor.getSelectionRange();
 
607
            var selected = session.doc.getTextRange(selection);
 
608
            if (selected !== "" && editor.getWrapBehavioursEnabled()) {
 
609
                return {
 
610
                    text: '[' + selected + ']',
 
611
                    selection: false
 
612
                };
 
613
            } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
 
614
                CstyleBehaviour.recordAutoInsert(editor, session, "]");
 
615
                return {
 
616
                    text: '[]',
 
617
                    selection: [1, 1]
 
618
                };
 
619
            }
 
620
        } else if (text == ']') {
 
621
            var cursor = editor.getCursorPosition();
 
622
            var line = session.doc.getLine(cursor.row);
 
623
            var rightChar = line.substring(cursor.column, cursor.column + 1);
 
624
            if (rightChar == ']') {
 
625
                var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
 
626
                if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
 
627
                    CstyleBehaviour.popAutoInsertedClosing();
 
628
                    return {
 
629
                        text: '',
 
630
                        selection: [1, 1]
 
631
                    };
 
632
                }
 
633
            }
 
634
        }
 
635
    });
 
636
 
 
637
    this.add("brackets", "deletion", function (state, action, editor, session, range) {
 
638
        var selected = session.doc.getTextRange(range);
 
639
        if (!range.isMultiLine() && selected == '[') {
 
640
            var line = session.doc.getLine(range.start.row);
 
641
            var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
 
642
            if (rightChar == ']') {
 
643
                range.end.column++;
 
644
                return range;
 
645
            }
 
646
        }
 
647
    });
 
648
 
 
649
    this.add("string_dquotes", "insertion", function (state, action, editor, session, text) {
 
650
        if (text == '"' || text == "'") {
 
651
            var quote = text;
 
652
            var selection = editor.getSelectionRange();
 
653
            var selected = session.doc.getTextRange(selection);
 
654
            if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
 
655
                return {
 
656
                    text: quote + selected + quote,
 
657
                    selection: false
 
658
                };
 
659
            } else {
 
660
                var cursor = editor.getCursorPosition();
 
661
                var line = session.doc.getLine(cursor.row);
 
662
                var leftChar = line.substring(cursor.column-1, cursor.column);
 
663
                if (leftChar == '\\') {
 
664
                    return null;
 
665
                }
 
666
                var tokens = session.getTokens(selection.start.row);
 
667
                var col = 0, token;
 
668
                var quotepos = -1; // Track whether we're inside an open quote.
 
669
 
 
670
                for (var x = 0; x < tokens.length; x++) {
 
671
                    token = tokens[x];
 
672
                    if (token.type == "string") {
 
673
                      quotepos = -1;
 
674
                    } else if (quotepos < 0) {
 
675
                      quotepos = token.value.indexOf(quote);
 
676
                    }
 
677
                    if ((token.value.length + col) > selection.start.column) {
 
678
                        break;
 
679
                    }
 
680
                    col += tokens[x].value.length;
 
681
                }
 
682
                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)))) {
 
683
                    if (!CstyleBehaviour.isSaneInsertion(editor, session))
 
684
                        return;
 
685
                    return {
 
686
                        text: quote + quote,
 
687
                        selection: [1,1]
 
688
                    };
 
689
                } else if (token && token.type === "string") {
 
690
                    var rightChar = line.substring(cursor.column, cursor.column + 1);
 
691
                    if (rightChar == quote) {
 
692
                        return {
 
693
                            text: '',
 
694
                            selection: [1, 1]
 
695
                        };
 
696
                    }
 
697
                }
 
698
            }
 
699
        }
 
700
    });
 
701
 
 
702
    this.add("string_dquotes", "deletion", function (state, action, editor, session, range) {
 
703
        var selected = session.doc.getTextRange(range);
 
704
        if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
 
705
            var line = session.doc.getLine(range.start.row);
 
706
            var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
 
707
            if (rightChar == selected) {
 
708
                range.end.column++;
 
709
                return range;
 
710
            }
 
711
        }
 
712
    });
 
713
 
 
714
};
 
715
 
 
716
oop.inherits(CstyleBehaviour, Behaviour);
 
717
 
 
718
exports.CstyleBehaviour = CstyleBehaviour;
 
719
});
 
720
 
 
721
define('ace/mode/folding/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/range', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
 
722
 
 
723
 
 
724
var oop = require("../../lib/oop");
 
725
var Range = require("../../range").Range;
 
726
var BaseFoldMode = require("./fold_mode").FoldMode;
 
727
 
 
728
var FoldMode = exports.FoldMode = function(commentRegex) {
 
729
    if (commentRegex) {
 
730
        this.foldingStartMarker = new RegExp(
 
731
            this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
 
732
        );
 
733
        this.foldingStopMarker = new RegExp(
 
734
            this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
 
735
        );
 
736
    }
 
737
};
 
738
oop.inherits(FoldMode, BaseFoldMode);
 
739
 
 
740
(function() {
 
741
 
 
742
    this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
 
743
    this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
 
744
 
 
745
    this.getFoldWidgetRange = function(session, foldStyle, row) {
 
746
        var line = session.getLine(row);
 
747
        var match = line.match(this.foldingStartMarker);
 
748
        if (match) {
 
749
            var i = match.index;
 
750
 
 
751
            if (match[1])
 
752
                return this.openingBracketBlock(session, match[1], row, i);
 
753
 
 
754
            return session.getCommentFoldRange(row, i + match[0].length, 1);
 
755
        }
 
756
 
 
757
        if (foldStyle !== "markbeginend")
 
758
            return;
 
759
 
 
760
        var match = line.match(this.foldingStopMarker);
 
761
        if (match) {
 
762
            var i = match.index + match[0].length;
 
763
 
 
764
            if (match[1])
 
765
                return this.closingBracketBlock(session, match[1], row, i);
 
766
 
 
767
            return session.getCommentFoldRange(row, i, -1);
 
768
        }
 
769
    };
 
770
 
 
771
}).call(FoldMode.prototype);
 
772
 
 
773
});