Git commit eb455e085f8199b97d81841006f43c4fcf21f290 by Dominik Haumann. Committed on 28/02/2013 at 21:24. Pushed by dhaumann into branch 'master'.
Cursor, Range: extend js prototype - use prototype inheritance, should be faster - add bool Range.isEmpty() @Kile devs: maybe you want to adapt your cursor-range.js file. CCMAIL: holger.danielsson at versanet.de CCMAIL: michel.ludwig at kdemail.net CCMAIL: kwrite-devel at kde.org M +15 -0 doc/kate/advanced.docbook M +1 -1 part/script/data/indentation/cppstyle.js M +3 -3 part/script/data/indentation/cstyle.js M +42 -34 part/script/data/libraries/cursor.js M +74 -65 part/script/data/libraries/range.js http://commits.kde.org/kate/eb455e085f8199b97d81841006f43c4fcf21f290 diff --git a/doc/kate/advanced.docbook b/doc/kate/advanced.docbook index 03e53a1..9e8484a 100644 --- a/doc/kate/advanced.docbook +++ b/doc/kate/advanced.docbook @@ -1447,6 +1447,21 @@ Returns a clone of the range. <varlistentry> <term><synopsis> +bool Range.isEmpty(); +</synopsis></term> +<listitem><para> +Returns <literal>true</literal>, if the start and end cursors are equal. +</para> +<para>Example: <function>var empty = range.isEmpty();</function> +</para> +<para> +Since: KDE 4.11 +</para></listitem> +</varlistentry> + + +<varlistentry> +<term><synopsis> bool Range.isValid(); </synopsis></term> <listitem><para> diff --git a/part/script/data/indentation/cppstyle.js b/part/script/data/indentation/cppstyle.js index 17fadc6..4941c5b 100644 --- a/part/script/data/indentation/cppstyle.js +++ b/part/script/data/indentation/cppstyle.js @@ -824,7 +824,7 @@ function tryCloseBracket(cursor, ch) // (i.e. it is 'dangling' brace)... if (document.firstChar(line) == ch && document.firstColumn(line) == (column - 1)) { - var braceCursor = new Cursor().invalid(); + var braceCursor = Cursor.invalid(); if (ch != '>') braceCursor = document.anchor(line, column - 1, gBraceMap[ch]); // TODO Otherwise, it seems we have a template parameters list... diff --git a/part/script/data/indentation/cstyle.js b/part/script/data/indentation/cstyle.js index 2f3c0cd..be22333 100644 --- a/part/script/data/indentation/cstyle.js +++ b/part/script/data/indentation/cstyle.js @@ -116,7 +116,7 @@ function tryParenthesisBeforeBrace(line, column) while (column > firstColumn && document.isSpace(line, --column)); if (document.charAt(line, column) == ')') return document.anchor(line, column, '('); - return new Cursor().invalid(); + return Cursor.invalid(); } /** @@ -415,7 +415,7 @@ function tryCKeywords(line, isBrace) // if line ends with ')', find the '(' and check this line then. var lastPos = document.lastColumn(currentLine); - var cursor = new Cursor().invalid(); + var cursor = Cursor.invalid(); if (document.charAt(currentLine, lastPos) == ')') cursor = document.anchor(currentLine, lastPos, '('); if (cursor.isValid()) @@ -533,7 +533,7 @@ function tryStatement(line) if (result != null && result.index == 0) { var alignOnAnchor = result[3].length == 0 && result[2] != ')'; // search for opening ", ' or ( - var cursor = new Cursor().invalid(); + var cursor = Cursor.invalid(); if (result[2] == '"' || (alignOnSingleQuote && result[2] == "'")) { while(true) { diff --git a/part/script/data/libraries/cursor.js b/part/script/data/libraries/cursor.js index c3fe081..d533df2 100644 --- a/part/script/data/libraries/cursor.js +++ b/part/script/data/libraries/cursor.js @@ -18,7 +18,7 @@ * var cursor1 = new Cursor(); // constructs a (valid) cursor at position (0, 0) * var cursor2 = new Cursor(2, 4); // constructs a cursor at position (2, 4) * var cursor3 = new Cursor(cursor2); // copies the cursor2 - * var cursor4 = new Cursor().invalid(); // constructs invalid cursor at (-1, -1) + * var cursor4 = Cursor.invalid(); // constructs invalid cursor at (-1, -1) * \endcode * * There are several convenience member functions that easy working with @@ -27,51 +27,59 @@ * * \see Range */ -function Cursor() -{ - if (arguments.length == 0) { +function Cursor() { + + if (arguments.length === 0) { return new Cursor(0, 0); - } else if (arguments.length == 1 && typeof arguments[0] == "object") { + } + + if (arguments.length === 1 && typeof arguments[0] == "object") { // assume: cursor = new Cursor(otherCursor); return arguments[0].clone(); - } else if (arguments.length == 2 && typeof arguments[0] == "number" - && typeof arguments[1] == "number") { + } + + if (arguments.length === 2 && typeof arguments[0] == "number" + && typeof arguments[1] == "number") { // assume: cursor = new Cursor(line, column); - this.line = parseInt(arguments[0]); - this.column = parseInt(arguments[1]); + this.line = parseInt(arguments[0], 10); + this.column = parseInt(arguments[1], 10); } else { throw "Wrong usage of Cursor constructor"; } +} - this.clone = function() { - return new Cursor(this.line, this.column); - }; - - this.isValid = function() { - return (this.line >= 0) && (this.column >= 0); - }; +Cursor.prototype.clone = function() { + return new Cursor(this.line, this.column); +} - this.invalid = function() { - return new Cursor(-1, -1); - }; +Cursor.prototype.isValid = function() { + return (this.line >= 0) && (this.column >= 0); +} - this.compareTo = function(other) { - if (this.line > other.line || (this.line == other.line && this.column > other.column)) { - return 1; - } else if (this.line < other.line || (this.line == other.line && this.column < other.column)) { - return -1; - } else { - return 0; - } - }; +Cursor.prototype.compareTo = function(other) { + if (this.line > other.line || (this.line === other.line && this.column > other.column)) { + return 1; + } + if (this.line < other.line || (this.line === other.line && this.column < other.column)) { + return -1; + } + return 0; +} - this.equals = function(other) { - return (this.line == other.line && this.column == other.column); - }; +Cursor.prototype.equals = function(other) { + return (this.line === other.line && this.column === other.column); +} - this.toString = function() { - return "Cursor(" + this.line+ ", " + this.column+ ")"; - }; +Cursor.prototype.toString = function() { + if (this.isValid()) { + return "Cursor(" + this.line+ "," + this.column+ ")"; + } else { + return "Cursor()"; + } } +Cursor.invalid = function() { + return new Cursor(-1, -1); +} +// kate: indent-width 2; replace-tabs on; diff --git a/part/script/data/libraries/range.js b/part/script/data/libraries/range.js index af6056b..2555fab 100644 --- a/part/script/data/libraries/range.js +++ b/part/script/data/libraries/range.js @@ -8,88 +8,97 @@ require ("cursor.js"); /** * Prototype Range. */ -function Range() -{ - if (arguments.length == 0) { +function Range() { + + if (arguments.length === 0) { return new Range(0, 0, 0, 0); - } else if (arguments.length == 1 && typeof arguments[0] == "object") { + } + + if (arguments.length === 1 && typeof arguments[0] == "object") { // assume: range = new Range(otherRange); return arguments[0].clone(); - } else if (arguments.length == 2 && typeof arguments[0] == "object" - && typeof arguments[1] == "object") { + } + + if (arguments.length === 2 && typeof arguments[0] == "object" + && typeof arguments[1] == "object") { // assume: range = new Range(startCursor, endCursor); this.start = arguments[0].clone(); this.end = arguments[1].clone(); - } else if (arguments.length == 4 && typeof arguments[0] == "number" - && typeof arguments[1] == "number" - && typeof arguments[2] == "number" - && typeof arguments[3] == "number") { + } else if (arguments.length === 4 && typeof arguments[0] == "number" + && typeof arguments[1] == "number" + && typeof arguments[2] == "number" + && typeof arguments[3] == "number") { this.start = new Cursor(arguments[0], arguments[1]); this.end = new Cursor(arguments[2], arguments[3]); } else { throw "Wrong usage of Range constructor"; } +} - this.clone = function() { - return new Range(this.start, this.end); - }; - - this.isValid = function() { - return this.start.isValid() && this.end.isValid(); - }; - - this.invalid = function() { - return new Range(-1, -1, -1, -1); - }; - - this.contains = function(cursorOrRange) { - if (cursorOrRange.start && cursorOrRange.end) { - // assume a range - return (cursorOrRange.start.compareTo(this.start) >= 0 && - cursorOrRange.end.compareTo(this.end) <= 0); - } else { - // assume a cursor - return (cursorOrRange.compareTo(this.start) >= 0 && - cursorOrRange.compareTo(this.end) < 0); - } - }; - - this.containsColumn = function(column) { - return (column >= this.start.column) && (column < this.end.column); - }; - - this.containsLine = function(line) { - return (line > this.start.line || (line == this.start.line && this.start.column == 0)) && line < this.end.line; - }; - - this.overlaps = function(range) { - if (range.start.compareTo(this.start) <= 0) { - return range.end.compareTo(this.start) > 0; - } else if (range.end.compareTo(this.end) >= 0) { - return range.start.compareTo(this.end) < 0; - } else { - return this.contains(range); - } - }; - - this.overlapsLine = function(line) { - return (line >= this.start.line && line <= this.end.line); - } +Range.prototype.clone = function() { + return new Range(this.start, this.end); +} - this.overlapsColumn = function(column) { - return column >= this.start.column && column <= this.end.column; +Range.prototype.isValid = function() { + return this.start.isValid() && this.end.isValid(); +} + +Range.prototype.isEmpty = function() { + return this.start.equals(this.end); +} + +Range.prototype.contains = function(cursorOrRange) { + if (cursorOrRange.start && cursorOrRange.end) { + // assume a range + return (cursorOrRange.start.compareTo(this.start) >= 0 && + cursorOrRange.end.compareTo(this.end) <= 0); } - this.onSingleLine = function() { - return (this.start.line == this.end.line); + // else: assume a cursor + return (cursorOrRange.compareTo(this.start) >= 0 && + cursorOrRange.compareTo(this.end) < 0); +} + +Range.prototype.containsColumn = function(column) { + return (column >= this.start.column) && (column < this.end.column); +} + +Range.prototype.containsLine = function(line) { + return ((line > this.start.line) || ((line === this.start.line) && (this.start.column === 0))) && line < this.end.line; +} + +Range.prototype.overlaps = function(range) { + if (range.start.compareTo(this.start) <= 0) { + return range.end.compareTo(this.start) > 0; + } + if (range.end.compareTo(this.end) >= 0) { + return range.start.compareTo(this.end) < 0; } + return this.contains(range); +} + +Range.prototype.overlapsLine = function(line) { + return (line >= this.start.line && line <= this.end.line); +} - this.equals = function(other) { - return (this.start.equals(other.start) && this.end.equals(other.end)); - }; +Range.prototype.overlapsColumn = function(column) { + return column >= this.start.column && column <= this.end.column; +} + +Range.prototype.onSingleLine = function() { + return (this.start.line == this.end.line); +} + +Range.prototype.equals = function(other) { + return (this.start.equals(other.start) && this.end.equals(other.end)); +} + +Range.prototype.toString = function() { + return "Range(" + this.start + ", " + this.end + ")"; +} - this.toString = function() { - return "Range(" + this.start + ", " + this.end + ")"; - }; +Range.invalid = function() { + return new Range(-1, -1, -1, -1); } +// kate: indent-width 2; replace-tabs on; \ No newline at end of file
