Changeset View
Changeset View
Standalone View
Standalone View
src/script/data/indentation/ruby.js
Show All 21 Lines | |||||
22 | * | 22 | * | ||
23 | * You should have received a copy of the GNU Library General Public License | 23 | * You should have received a copy of the GNU Library General Public License | ||
24 | * along with this library; see the file COPYING.LIB. If not, write to | 24 | * along with this library; see the file COPYING.LIB. If not, write to | ||
25 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 25 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
26 | * Boston, MA 02110-1301, USA. | 26 | * Boston, MA 02110-1301, USA. | ||
27 | */ | 27 | */ | ||
28 | 28 | | |||
29 | // required katepart js libraries | 29 | // required katepart js libraries | ||
30 | require ("cursor.js"); | ||||
30 | require ("range.js"); | 31 | require ("range.js"); | ||
31 | 32 | | |||
32 | //BEGIN USER CONFIGURATION | 33 | //BEGIN USER CONFIGURATION | ||
33 | //END USER CONFIGURATION | 34 | //END USER CONFIGURATION | ||
34 | 35 | | |||
35 | 36 | | |||
36 | // specifies the characters which should trigger indent, beside the default '\n' | 37 | // specifies the characters which should trigger indent, beside the default '\n' | ||
37 | var triggerCharacters = "cdefhilnrsuw}]"; | 38 | var triggerCharacters = "cdefhilnrsuw}]"; | ||
38 | 39 | | |||
39 | // Indent after lines that match this regexp | 40 | // Indent after lines that match this regexp | ||
40 | var rxIndent = /^\s*(def|if|unless|for|while|until|class|module|else|elsif|case|when|begin|rescue|ensure|catch)\b/; | 41 | var rxIndent = /^\s*(def|if|unless|for|while|until|class|module|else|elsif|case|when|begin|rescue|ensure|catch)\b/; | ||
41 | 42 | | |||
42 | // Unindent lines that match this regexp | 43 | // Unindent lines that match this regexp | ||
43 | var rxUnindent = /^\s*((end|when|else|elsif|rescue|ensure)\b|[\]\}])(.*)$/; | 44 | var rxUnindent = /^\s*((end|when|else|elsif|rescue|ensure)\b|[\]\}])(.*)$/; | ||
44 | 45 | | |||
46 | var debugMode = false; | ||||
47 | function dbg() { | ||||
48 | if (debugMode) { | ||||
49 | debug.apply(this, arguments); | ||||
50 | } | ||||
51 | } | ||||
52 | | ||||
45 | function assert(cond) | 53 | function assert(cond) | ||
46 | { | 54 | { | ||
47 | if (!cond) | 55 | if (!cond) | ||
48 | throw "assertion failure"; | 56 | throw "assertion failure"; | ||
49 | } | 57 | } | ||
50 | 58 | | |||
51 | // Return the closest non-empty line, ignoring comments | 59 | // Return the closest non-empty line, ignoring comments | ||
52 | // (result <= line). Return -1 if the document | 60 | // (result <= line). Return -1 if the document | ||
Show All 31 Lines | |||||
84 | // the length of the matching part is either the end of the | 92 | // the length of the matching part is either the end of the | ||
85 | // statement, or a comment. | 93 | // statement, or a comment. | ||
86 | // | 94 | // | ||
87 | // The regexp must be global, and the search is continued until | 95 | // The regexp must be global, and the search is continued until | ||
88 | // a match is found, or the end of the string is reached. | 96 | // a match is found, or the end of the string is reached. | ||
89 | function testAtEnd(stmt, rx) | 97 | function testAtEnd(stmt, rx) | ||
90 | { | 98 | { | ||
91 | assert(rx.global); | 99 | assert(rx.global); | ||
92 | | ||||
93 | var cnt = stmt.content(); | 100 | var cnt = stmt.content(); | ||
94 | var res; | 101 | var res; | ||
95 | while (res = rx.exec(cnt)) { | 102 | // Work-around QML bug: | ||
103 | rx.lastIndex = 0; | ||||
104 | while ((res = rx.exec(cnt)) !== null) { | ||||
96 | var start = res.index; | 105 | var start = res.index; | ||
97 | var end = rx.lastIndex; | 106 | var end = rx.lastIndex; | ||
98 | if (stmt.isCode(start)) { | 107 | if (stmt.isCode(start)) { | ||
99 | if (end == cnt.length) | 108 | if (end == cnt.length) | ||
100 | return true; | 109 | return true; | ||
101 | if (stmt.isComment(end)) | 110 | if (stmt.isComment(end)) | ||
102 | return true; | 111 | return true; | ||
103 | } | 112 | } | ||
Show All 40 Lines | 145 | { | |||
144 | 153 | | |||
145 | // Return an object having 'line' and 'column' set to the given offset in a statement | 154 | // Return an object having 'line' and 'column' set to the given offset in a statement | ||
146 | this.offsetToCursor = function(offset) { | 155 | this.offsetToCursor = function(offset) { | ||
147 | // TODO Provide helper function for this when API is converted to using cursors: | 156 | // TODO Provide helper function for this when API is converted to using cursors: | ||
148 | var line = this.start; | 157 | var line = this.start; | ||
149 | while (line < this.end && document.lineLength(line) < offset) { | 158 | while (line < this.end && document.lineLength(line) < offset) { | ||
150 | offset -= document.lineLength(line++) + 1; | 159 | offset -= document.lineLength(line++) + 1; | ||
151 | } | 160 | } | ||
152 | return {line: line, column: offset}; | 161 | return new Cursor(line, offset); | ||
153 | } | 162 | } | ||
154 | 163 | | |||
155 | // Return document.attribute at the given offset in a statement | 164 | // Return document.attribute at the given offset in a statement | ||
156 | this.attribute = function(offset) { | 165 | this.attribute = function(offset) { | ||
157 | var cur = this.offsetToCursor(offset); | 166 | var cur = this.offsetToCursor(offset); | ||
158 | return document.attribute(cur.line, cur.column); | 167 | return document.attribute(cur.line, cur.column); | ||
159 | } | 168 | } | ||
160 | 169 | | |||
161 | // Return document.isCode at the given offset in a statement | 170 | // Return document.isCode at the given offset in a statement | ||
162 | this.isCode = function(offset) { | 171 | this.isCode = function(offset) { | ||
163 | var cur = this.offsetToCursor(offset); | 172 | var cur = this.offsetToCursor(offset); | ||
164 | return document.isCode(cur.line, cur.column); | 173 | return document.isCode(cur.line, cur.column); | ||
165 | } | 174 | } | ||
166 | 175 | | |||
167 | // Return document.isComment at the given offset in a statement | 176 | // Return document.isComment at the given offset in a statement | ||
168 | this.isComment = function(offset) { | 177 | this.isComment = function(offset) { | ||
169 | var cur = this.offsetToCursor(offset); | 178 | var cur = this.offsetToCursor(offset); | ||
170 | return document.isComment(cur.line, cur.column); | 179 | return document.isComment(cur.line, cur.column); | ||
171 | } | 180 | } | ||
172 | 181 | | |||
173 | // Return the indent at the beginning of the statement | 182 | // Return the indent at the beginning of the statement | ||
174 | this.indent = function() { | 183 | this.indent = function() { | ||
175 | return document.firstVirtualColumn(this.start) | 184 | return document.firstVirtualColumn(this.start); | ||
176 | } | 185 | } | ||
177 | 186 | | |||
178 | // Return the content of the statement from the document | 187 | // Return the content of the statement from the document | ||
179 | this.content = function() { | 188 | this.content = function() { | ||
180 | var cnt = ""; | 189 | var cnt = ""; | ||
181 | for (var l = this.start; l <= this.end; l++) { | 190 | for (var l = this.start; l <= this.end; l++) { | ||
182 | cnt += document.line(l).replace(/\\$/, " "); | 191 | cnt += document.line(l).replace(/\\$/, " "); | ||
183 | if (l < this.end) | 192 | if (l < this.end) | ||
▲ Show 20 Lines • Show All 179 Lines • Show Last 20 Lines |