File indexing completed on 2024-05-12 16:41:02
0001 /** 0002 * Prototype Cursor 0003 * (taken from KatePart) 0004 */ 0005 0006 // This code is part of the Kate project within KDE. 0007 // (C) 2009 Dominik Haumann <dhaumann kde org> 0008 // License: LGPL v2 or v3 0009 0010 /** 0011 * Prototype Cursor. 0012 * 0013 * \section cursor_intro Introduction 0014 * The Cursor class provides the two properties \p line and \p column. Since a 0015 * lot of text operations are based on lines and columns such as inserting text 0016 * at a cursor position, the Cursor class plays a central role in KatePart 0017 * scripting. The entire scripting API is usually based on cursors whenever 0018 * possible. 0019 * 0020 * \section cursor_usage Using Cursors 0021 * There are several ways to construct a Cursor: 0022 * \code 0023 * var cursor1 = new Cursor(); // constructs a (valid) cursor at position (0, 0) 0024 * var cursor2 = new Cursor(2, 4); // constructs a cursor at position (2, 4) 0025 * var cursor3 = new Cursor(cursor2); // copies the cursor2 0026 * var cursor4 = Cursor.invalid(); // constructs invalid cursor at (-1, -1) 0027 * \endcode 0028 * 0029 * There are several convenience member functions that easy working with 0030 * Cursors. Use isValid() to check whether a Cursor is a valid text cursor. 0031 * To compare two cursors either use equals() or compareTo(). 0032 * 0033 * \see Range 0034 */ 0035 function Cursor() { 0036 0037 if (arguments.length === 0) { 0038 return new Cursor(0, 0); 0039 } 0040 0041 if (arguments.length === 1 && typeof arguments[0] == "object") { 0042 // assume: cursor = new Cursor(otherCursor); 0043 return arguments[0].clone(); 0044 } 0045 0046 if (arguments.length === 2 && typeof arguments[0] == "number" 0047 && typeof arguments[1] == "number") { 0048 // assume: cursor = new Cursor(line, column); 0049 this.line = parseInt(arguments[0], 10); 0050 this.column = parseInt(arguments[1], 10); 0051 } else { 0052 throw "Wrong usage of Cursor constructor"; 0053 } 0054 } 0055 0056 Cursor.prototype.clone = function() { 0057 return new Cursor(this.line, this.column); 0058 } 0059 0060 Cursor.prototype.setPosition = function(line, column) { 0061 this.line = line; 0062 this.column = column; 0063 } 0064 0065 Cursor.prototype.isValid = function() { 0066 return (this.line >= 0) && (this.column >= 0); 0067 } 0068 0069 Cursor.prototype.compareTo = function(other) { 0070 if (this.line > other.line || (this.line === other.line && this.column > other.column)) { 0071 return 1; 0072 } 0073 if (this.line < other.line || (this.line === other.line && this.column < other.column)) { 0074 return -1; 0075 } 0076 return 0; 0077 } 0078 0079 Cursor.prototype.equals = function(other) { 0080 return (this.line === other.line && this.column === other.column); 0081 } 0082 0083 Cursor.prototype.toString = function() { 0084 if (this.isValid()) { 0085 return "Cursor(" + this.line+ "," + this.column+ ")"; 0086 } else { 0087 return "Cursor()"; 0088 } 0089 } 0090 0091 Cursor.invalid = function() { 0092 return new Cursor(-1, -1); 0093 } 0094 0095 /** 0096 * Prototype Range. 0097 * (taken from KatePart) 0098 */ 0099 0100 // This code is part of the Kate project within KDE. 0101 // (C) 2009 Dominik Haumann <dhaumann kde org> 0102 // License: LGPL v2 or v3 0103 /** 0104 * Prototype Range. 0105 */ 0106 function Range() { 0107 0108 if (arguments.length === 0) { 0109 return new Range(0, 0, 0, 0); 0110 } 0111 0112 if (arguments.length === 1 && typeof arguments[0] == "object") { 0113 // assume: range = new Range(otherRange); 0114 return arguments[0].clone(); 0115 } 0116 0117 if (arguments.length === 2 && typeof arguments[0] == "object" 0118 && typeof arguments[1] == "object") { 0119 // assume: range = new Range(startCursor, endCursor); 0120 this.start = arguments[0].clone(); 0121 this.end = arguments[1].clone(); 0122 } else if (arguments.length === 4 && typeof arguments[0] == "number" 0123 && typeof arguments[1] == "number" 0124 && typeof arguments[2] == "number" 0125 && typeof arguments[3] == "number") { 0126 this.start = new Cursor(arguments[0], arguments[1]); 0127 this.end = new Cursor(arguments[2], arguments[3]); 0128 } else { 0129 throw "Wrong usage of Range constructor"; 0130 } 0131 } 0132 0133 Range.prototype.clone = function() { 0134 return new Range(this.start, this.end); 0135 } 0136 0137 Range.prototype.isValid = function() { 0138 return this.start.isValid() && this.end.isValid(); 0139 } 0140 0141 Range.prototype.isEmpty = function() { 0142 return this.start.equals(this.end); 0143 } 0144 0145 Range.prototype.contains = function(cursorOrRange) { 0146 if (cursorOrRange.start && cursorOrRange.end) { 0147 // assume a range 0148 return (cursorOrRange.start.compareTo(this.start) >= 0 && 0149 cursorOrRange.end.compareTo(this.end) <= 0); 0150 } 0151 0152 // else: assume a cursor 0153 return (cursorOrRange.compareTo(this.start) >= 0 && 0154 cursorOrRange.compareTo(this.end) < 0); 0155 } 0156 0157 Range.prototype.containsColumn = function(column) { 0158 return (column >= this.start.column) && (column < this.end.column); 0159 } 0160 0161 Range.prototype.containsLine = function(line) { 0162 return ((line > this.start.line) || ((line === this.start.line) && (this.start.column === 0))) && line < this.end.line; 0163 } 0164 0165 Range.prototype.overlaps = function(range) { 0166 if (range.start.compareTo(this.start) <= 0) { 0167 return range.end.compareTo(this.start) > 0; 0168 } 0169 if (range.end.compareTo(this.end) >= 0) { 0170 return range.start.compareTo(this.end) < 0; 0171 } 0172 return this.contains(range); 0173 } 0174 0175 Range.prototype.overlapsLine = function(line) { 0176 return (line >= this.start.line && line <= this.end.line); 0177 } 0178 0179 Range.prototype.overlapsColumn = function(column) { 0180 return column >= this.start.column && column <= this.end.column; 0181 } 0182 0183 Range.prototype.onSingleLine = function() { 0184 return (this.start.line == this.end.line); 0185 } 0186 0187 Range.prototype.equals = function(other) { 0188 return (this.start.equals(other.start) && this.end.equals(other.end)); 0189 } 0190 0191 Range.prototype.toString = function() { 0192 return "Range(" + this.start + ", " + this.end + ")"; 0193 } 0194 0195 Range.invalid = function() { 0196 return new Range(-1, -1, -1, -1); 0197 } 0198 0199 // kate: indent-width 2;