File: range.js

package info (click to toggle)
ktexteditor 5.78.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,404 kB
  • sloc: cpp: 81,731; javascript: 19,151; xml: 171; sh: 20; makefile: 11
file content (107 lines) | stat: -rw-r--r-- 3,091 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
    This file is part of the Kate project within KDE.
    SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>

    SPDX-License-Identifier: MIT
*/

// required katepart js libraries
require ("cursor.js");

/**
 * Prototype Range.
 */
function Range() {

  if (arguments.length === 0) {
    return new Range(0, 0, 0, 0);
  }

  if (arguments.length === 1 && typeof arguments[0] == "object") {
    // assume: range = new Range(otherRange);
    return arguments[0].clone();
  }

  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") {
    this.start = new Cursor(arguments[0], arguments[1]);
    this.end = new Cursor(arguments[2], arguments[3]);
  } else {
    throw "Wrong usage of Range constructor";
  }
}

Range.prototype.clone = function() {
  return new Range(this.start, this.end);
}

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);
  }

  // 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);
}

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 + ")";
}

Range.invalid = function() {
  return new Range(-1, -1, -1, -1);
}

// kate: indent-width 2; replace-tabs on;