PluginProbe
WPIDE – File Manager & Code Editor / 2.2
WPIDE – File Manager & Code Editor v2.2
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / ace-0.2.0 / src / mode-python.js

mode-python.js in WPIDE – File Manager & Code Editor 2.2, at ace-0.2.0/src/mode-python.js

295 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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/python', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/python_highlight_rules', 'ace/mode/folding/pythonic', 'ace/range'], 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 PythonHighlightRules = require("./python_highlight_rules").PythonHighlightRules;
38 var PythonFoldMode = require("./folding/pythonic").FoldMode;
39 var Range = require("../range").Range;
40
41 var Mode = function() {
42 this.$tokenizer = new Tokenizer(new PythonHighlightRules().getRules());
43 this.foldingRules = new PythonFoldMode("\\:");
44 };
45 oop.inherits(Mode, TextMode);
46
47 (function() {
48
49 this.toggleCommentLines = function(state, doc, startRow, endRow) {
50 var outdent = true;
51 var re = /^(\s*)#/;
52
53 for (var i=startRow; i<= endRow; i++) {
54 if (!re.test(doc.getLine(i))) {
55 outdent = false;
56 break;
57 }
58 }
59
60 if (outdent) {
61 var deleteRange = new Range(0, 0, 0, 0);
62 for (var i=startRow; i<= endRow; i++)
63 {
64 var line = doc.getLine(i);
65 var m = line.match(re);
66 deleteRange.start.row = i;
67 deleteRange.end.row = i;
68 deleteRange.end.column = m[0].length;
69 doc.replace(deleteRange, m[1]);
70 }
71 }
72 else {
73 doc.indentRows(startRow, endRow, "#");
74 }
75 };
76
77 this.getNextLineIndent = function(state, line, tab) {
78 var indent = this.$getIndent(line);
79
80 var tokenizedLine = this.$tokenizer.getLineTokens(line, state);
81 var tokens = tokenizedLine.tokens;
82
83 if (tokens.length && tokens[tokens.length-1].type == "comment") {
84 return indent;
85 }
86
87 if (state == "start") {
88 var match = line.match(/^.*[\{\(\[\:]\s*$/);
89 if (match) {
90 indent += tab;
91 }
92 }
93
94 return indent;
95 };
96
97 var outdents = {
98 "pass": 1,
99 "return": 1,
100 "raise": 1,
101 "break": 1,
102 "continue": 1
103 };
104
105 this.checkOutdent = function(state, line, input) {
106 if (input !== "\r\n" && input !== "\r" && input !== "\n")
107 return false;
108
109 var tokens = this.$tokenizer.getLineTokens(line.trim(), state).tokens;
110
111 if (!tokens)
112 return false;
113 do {
114 var last = tokens.pop();
115 } while (last && (last.type == "comment" || (last.type == "text" && last.value.match(/^\s+$/))));
116
117 if (!last)
118 return false;
119
120 return (last.type == "keyword" && outdents[last.value]);
121 };
122
123 this.autoOutdent = function(state, doc, row) {
124
125 row += 1;
126 var indent = this.$getIndent(doc.getLine(row));
127 var tab = doc.getTabString();
128 if (indent.slice(-tab.length) == tab)
129 doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
130 };
131
132 }).call(Mode.prototype);
133
134 exports.Mode = Mode;
135 });
136
137 define('ace/mode/python_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
138
139
140 var oop = require("../lib/oop");
141 var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
142
143 var PythonHighlightRules = function() {
144
145 var keywords = (
146 "and|as|assert|break|class|continue|def|del|elif|else|except|exec|" +
147 "finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|" +
148 "raise|return|try|while|with|yield"
149 );
150
151 var builtinConstants = (
152 "True|False|None|NotImplemented|Ellipsis|__debug__"
153 );
154
155 var builtinFunctions = (
156 "abs|divmod|input|open|staticmethod|all|enumerate|int|ord|str|any|" +
157 "eval|isinstance|pow|sum|basestring|execfile|issubclass|print|super|" +
158 "binfile|iter|property|tuple|bool|filter|len|range|type|bytearray|" +
159 "float|list|raw_input|unichr|callable|format|locals|reduce|unicode|" +
160 "chr|frozenset|long|reload|vars|classmethod|getattr|map|repr|xrange|" +
161 "cmp|globals|max|reversed|zip|compile|hasattr|memoryview|round|" +
162 "__import__|complex|hash|min|set|apply|delattr|help|next|setattr|" +
163 "buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern"
164 );
165 var keywordMapper = this.createKeywordMapper({
166 "invalid.deprecated": "debugger",
167 "support.function": builtinFunctions,
168 "constant.language": builtinConstants,
169 "keyword": keywords
170 }, "identifier");
171
172 var strPre = "(?:r|u|ur|R|U|UR|Ur|uR)?";
173
174 var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
175 var octInteger = "(?:0[oO]?[0-7]+)";
176 var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
177 var binInteger = "(?:0[bB][01]+)";
178 var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
179
180 var exponent = "(?:[eE][+-]?\\d+)";
181 var fraction = "(?:\\.\\d+)";
182 var intPart = "(?:\\d+)";
183 var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
184 var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
185 var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
186
187 this.$rules = {
188 "start" : [ {
189 token : "comment",
190 regex : "#.*$"
191 }, {
192 token : "string", // """ string
193 regex : strPre + '"{3}(?:[^\\\\]|\\\\.)*?"{3}'
194 }, {
195 token : "string", // multi line """ string start
196 merge : true,
197 regex : strPre + '"{3}.*$',
198 next : "qqstring"
199 }, {
200 token : "string", // " string
201 regex : strPre + '"(?:[^\\\\]|\\\\.)*?"'
202 }, {
203 token : "string", // ''' string
204 regex : strPre + "'{3}(?:[^\\\\]|\\\\.)*?'{3}"
205 }, {
206 token : "string", // multi line ''' string start
207 merge : true,
208 regex : strPre + "'{3}.*$",
209 next : "qstring"
210 }, {
211 token : "string", // ' string
212 regex : strPre + "'(?:[^\\\\]|\\\\.)*?'"
213 }, {
214 token : "constant.numeric", // imaginary
215 regex : "(?:" + floatNumber + "|\\d+)[jJ]\\b"
216 }, {
217 token : "constant.numeric", // float
218 regex : floatNumber
219 }, {
220 token : "constant.numeric", // long integer
221 regex : integer + "[lL]\\b"
222 }, {
223 token : "constant.numeric", // integer
224 regex : integer + "\\b"
225 }, {
226 token : keywordMapper,
227 regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
228 }, {
229 token : "keyword.operator",
230 regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
231 }, {
232 token : "paren.lparen",
233 regex : "[\\[\\(\\{]"
234 }, {
235 token : "paren.rparen",
236 regex : "[\\]\\)\\}]"
237 }, {
238 token : "text",
239 regex : "\\s+"
240 } ],
241 "qqstring" : [ {
242 token : "string", // multi line """ string end
243 regex : '(?:[^\\\\]|\\\\.)*?"{3}',
244 next : "start"
245 }, {
246 token : "string",
247 merge : true,
248 regex : '.+'
249 } ],
250 "qstring" : [ {
251 token : "string", // multi line ''' string end
252 regex : "(?:[^\\\\]|\\\\.)*?'{3}",
253 next : "start"
254 }, {
255 token : "string",
256 merge : true,
257 regex : '.+'
258 } ]
259 };
260 };
261
262 oop.inherits(PythonHighlightRules, TextHighlightRules);
263
264 exports.PythonHighlightRules = PythonHighlightRules;
265 });
266
267 define('ace/mode/folding/pythonic', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
268
269
270 var oop = require("../../lib/oop");
271 var BaseFoldMode = require("./fold_mode").FoldMode;
272
273 var FoldMode = exports.FoldMode = function(markers) {
274 this.foldingStartMarker = new RegExp("([\\[{])(?:\\s*)$|(" + markers + ")(?:\\s*)(?:#.*)?$");
275 };
276 oop.inherits(FoldMode, BaseFoldMode);
277
278 (function() {
279
280 this.getFoldWidgetRange = function(session, foldStyle, row) {
281 var line = session.getLine(row);
282 var match = line.match(this.foldingStartMarker);
283 if (match) {
284 if (match[1])
285 return this.openingBracketBlock(session, match[1], row, match.index);
286 if (match[2])
287 return this.indentationBlock(session, row, match.index + match[2].length);
288 return this.indentationBlock(session, row);
289 }
290 }
291
292 }).call(FoldMode.prototype);
293
294 });
295