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 // required katepart js libraries
0009 require ("cursor.js");
0010 
0011 /**
0012  * Prototype Range.
0013  */
0014 function Range() {
0015 
0016   if (arguments.length === 0) {
0017     return new Range(0, 0, 0, 0);
0018   }
0019 
0020   if (arguments.length === 1 && typeof arguments[0] == "object") {
0021     // assume: range = new Range(otherRange);
0022     return arguments[0].clone();
0023   }
0024 
0025   if (arguments.length === 2 && typeof arguments[0] == "object"
0026                              && typeof arguments[1] == "object") {
0027     // assume: range = new Range(startCursor, endCursor);
0028     this.start = arguments[0].clone();
0029     this.end = arguments[1].clone();
0030   } else if (arguments.length === 4 && typeof arguments[0] == "number"
0031                                     && typeof arguments[1] == "number"
0032                                     && typeof arguments[2] == "number"
0033                                     && typeof arguments[3] == "number") {
0034     this.start = new Cursor(arguments[0], arguments[1]);
0035     this.end = new Cursor(arguments[2], arguments[3]);
0036   } else {
0037     throw "Wrong usage of Range constructor";
0038   }
0039 }
0040 
0041 Range.prototype.clone = function() {
0042   return new Range(this.start, this.end);
0043 }
0044 
0045 Range.prototype.isValid = function() {
0046   return this.start.isValid() && this.end.isValid();
0047 }
0048 
0049 Range.prototype.isEmpty = function() {
0050   return this.start.equals(this.end);
0051 }
0052 
0053 Range.prototype.contains = function(cursorOrRange) {
0054   if (cursorOrRange.start && cursorOrRange.end) {
0055     // assume a range
0056     return (cursorOrRange.start.compareTo(this.start) >= 0 &&
0057             cursorOrRange.end.compareTo(this.end) <= 0);
0058   }
0059 
0060   // else: assume a cursor
0061   return (cursorOrRange.compareTo(this.start) >= 0 &&
0062           cursorOrRange.compareTo(this.end) < 0);
0063 }
0064 
0065 Range.prototype.containsColumn = function(column) {
0066   return (column >= this.start.column) && (column < this.end.column);
0067 }
0068 
0069 Range.prototype.containsLine = function(line) {
0070   return ((line > this.start.line) || ((line === this.start.line) && (this.start.column === 0))) && line < this.end.line;
0071 }
0072 
0073 Range.prototype.overlaps = function(range) {
0074   if (range.start.compareTo(this.start) <= 0) {
0075     return range.end.compareTo(this.start) > 0;
0076   }
0077   if (range.end.compareTo(this.end) >= 0) {
0078     return range.start.compareTo(this.end) < 0;
0079   }
0080   return this.contains(range);
0081 }
0082 
0083 Range.prototype.overlapsLine = function(line) {
0084   return (line >= this.start.line && line <= this.end.line);
0085 }
0086 
0087 Range.prototype.overlapsColumn = function(column) {
0088   return column >= this.start.column && column <= this.end.column;
0089 }
0090 
0091 Range.prototype.onSingleLine = function() {
0092   return (this.start.line == this.end.line);
0093 }
0094 
0095 Range.prototype.equals = function(other) {
0096   return (this.start.equals(other.start) && this.end.equals(other.end));
0097 }
0098 
0099 Range.prototype.toString = function() {
0100   return "Range(" + this.start + ", " + this.end + ")";
0101 }
0102 
0103 Range.invalid = function() {
0104   return new Range(-1, -1, -1, -1);
0105 }
0106 
0107 // kate: indent-width 2; replace-tabs on;