File indexing completed on 2024-05-19 04:00:11

0001 /*
0002     This file is part of the Kate project within KDE.
0003     SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 /**
0009  * Prototype Cursor.
0010  *
0011  * \section cursor_intro Introduction
0012  * The Cursor class provides the two properties \p line and \p column. Since a
0013  * lot of text operations are based on lines and columns such as inserting text
0014  * at a cursor position, the Cursor class plays a central role in KatePart
0015  * scripting. The entire scripting API is usually based on cursors whenever
0016  * possible.
0017  *
0018  * \section cursor_usage Using Cursors
0019  * There are several ways to construct a Cursor:
0020  * \code
0021  * var cursor1 = new Cursor(); // constructs a (valid) cursor at position (0, 0)
0022  * var cursor2 = new Cursor(2, 4); // constructs a cursor at position (2, 4)
0023  * var cursor3 = new Cursor(cursor2); // copies the cursor2
0024  * var cursor4 = Cursor.invalid(); // constructs invalid cursor at (-1, -1)
0025  * \endcode
0026  *
0027  * There are several convenience member functions that easy working with
0028  * Cursors. Use isValid() to check whether a Cursor is a valid text cursor.
0029  * To compare two cursors either use equals() or compareTo().
0030  *
0031  * \see Range
0032  */
0033 function Cursor() {
0034 
0035   if (arguments.length === 0) {
0036     return new Cursor(0, 0);
0037   }
0038 
0039   if (arguments.length === 1 && typeof arguments[0] == "object") {
0040     // assume: cursor = new Cursor(otherCursor);
0041     return arguments[0].clone();
0042   }
0043 
0044   if (arguments.length === 2 && typeof arguments[0] == "number"
0045                              && typeof arguments[1] == "number") {
0046     // assume: cursor = new Cursor(line, column);
0047     this.line = parseInt(arguments[0], 10);
0048     this.column = parseInt(arguments[1], 10);
0049   } else {
0050     throw "Wrong usage of Cursor constructor";
0051   }
0052 }
0053 
0054 Cursor.prototype.clone = function() {
0055   return new Cursor(this.line, this.column);
0056 }
0057 
0058 Cursor.prototype.setPosition = function(line, column) {
0059   this.line = line;
0060   this.column = column;
0061 }
0062 
0063 Cursor.prototype.isValid = function() {
0064   return (this.line >= 0) && (this.column >= 0);
0065 }
0066 
0067 Cursor.prototype.compareTo = function(other) {
0068   if (this.line > other.line || (this.line === other.line && this.column > other.column)) {
0069     return 1;
0070   }
0071   if (this.line < other.line || (this.line === other.line && this.column < other.column)) {
0072     return -1;
0073   }
0074   return 0;
0075 }
0076 
0077 Cursor.prototype.equals = function(other) {
0078   return (this.line === other.line && this.column === other.column);
0079 }
0080 
0081 Cursor.prototype.toString = function() {
0082   if (this.isValid()) {
0083     return "Cursor(" + this.line+ "," + this.column+ ")";
0084   } else {
0085     return "Cursor()";
0086   }
0087 }
0088 
0089 Cursor.invalid = function() {
0090   return new Cursor(-1, -1);
0091 }
0092 
0093 // kate: indent-width 2; replace-tabs on;