|
| 1 | +# Don't evaluate type annotations at runtime |
| 2 | +from __future__ import annotations |
| 3 | +from typing import Literal, Optional |
| 4 | +import platform |
| 5 | + |
| 6 | +Point = int |
| 7 | +DIP = float |
| 8 | + |
| 9 | +class Region: |
| 10 | + """ |
| 11 | + A singular selection region. This region has a order - ``b`` may be before |
| 12 | + or at ``a``. |
| 13 | +
|
| 14 | + Also commonly used to represent an area of the text buffer, where ordering |
| 15 | + and ``xpos`` are generally ignored. |
| 16 | + """ |
| 17 | + |
| 18 | + __slots__ = ['a', 'b', 'xpos'] |
| 19 | + |
| 20 | + def __init__(self, a: Point, b: Optional[Point] = None, xpos: DIP = -1): |
| 21 | + """ """ |
| 22 | + if b is None: |
| 23 | + b = a |
| 24 | + |
| 25 | + self.a: Point = a |
| 26 | + """ The first end of the region. """ |
| 27 | + self.b: Point = b |
| 28 | + """ |
| 29 | + The second end of the region. In a selection this is the location of the |
| 30 | + caret. May be less than ``a``. |
| 31 | + """ |
| 32 | + self.xpos: DIP = xpos |
| 33 | + """ |
| 34 | + In a selection this is the target horizontal position of the region. |
| 35 | + This affects behavior when pressing the up or down keys. Use ``-1`` if |
| 36 | + undefined. |
| 37 | + """ |
| 38 | + |
| 39 | + def __iter__(self): |
| 40 | + """ |
| 41 | + Iterate through all the points in the region. |
| 42 | +
|
| 43 | + .. since:: 4023 3.8 |
| 44 | + """ |
| 45 | + return iter((self.a, self.b)) |
| 46 | + |
| 47 | + def __str__(self) -> str: |
| 48 | + return "(" + str(self.a) + ", " + str(self.b) + ")" |
| 49 | + |
| 50 | + def __repr__(self) -> str: |
| 51 | + if self.xpos == -1: |
| 52 | + return f'Region({self.a}, {self.b})' |
| 53 | + return f'Region({self.a}, {self.b}, xpos={self.xpos})' |
| 54 | + |
| 55 | + def __len__(self) -> int: |
| 56 | + """ :returns: The size of the region. """ |
| 57 | + return self.size() |
| 58 | + |
| 59 | + def __eq__(self, rhs: object) -> bool: |
| 60 | + """ |
| 61 | + :returns: Whether the two regions are identical. Ignores ``xpos``. |
| 62 | + """ |
| 63 | + return isinstance(rhs, Region) and self.a == rhs.a and self.b == rhs.b |
| 64 | + |
| 65 | + def __lt__(self, rhs: Region) -> bool: |
| 66 | + """ |
| 67 | + :returns: Whether this region starts before the rhs. The end of the |
| 68 | + region is used to resolve ties. |
| 69 | + """ |
| 70 | + lhs_begin = self.begin() |
| 71 | + rhs_begin = rhs.begin() |
| 72 | + |
| 73 | + if lhs_begin == rhs_begin: |
| 74 | + return self.end() < rhs.end() |
| 75 | + else: |
| 76 | + return lhs_begin < rhs_begin |
| 77 | + |
| 78 | + def __contains__(self, v: Region | Point) -> bool: |
| 79 | + """ |
| 80 | + :returns: Whether the provided `Region` or `Point` is entirely contained |
| 81 | + within this region. |
| 82 | +
|
| 83 | + .. since:: 4023 3.8 |
| 84 | + """ |
| 85 | + if isinstance(v, Region): |
| 86 | + return v.a in self and v.b in self |
| 87 | + elif isinstance(v, int): |
| 88 | + return v >= self.begin() and v <= self.end() |
| 89 | + else: |
| 90 | + fq_name = "" |
| 91 | + if v.__class__.__module__ not in {'builtins', '__builtin__'}: |
| 92 | + fq_name = f"{v.__class__.__module__}." |
| 93 | + fq_name += v.__class__.__qualname__ |
| 94 | + raise TypeError( |
| 95 | + "in <Region> requires int or Region as left operand" |
| 96 | + f", not {fq_name}") |
| 97 | + |
| 98 | + def to_tuple(self) -> tuple[Point, Point]: |
| 99 | + """ |
| 100 | + .. since:: 4075 |
| 101 | +
|
| 102 | + :returns: This region as a tuple ``(a, b)``. |
| 103 | + """ |
| 104 | + return (self.a, self.b) |
| 105 | + |
| 106 | + def empty(self) -> bool: |
| 107 | + """ :returns: Whether the region is empty, ie. ``a == b``. """ |
| 108 | + return self.a == self.b |
| 109 | + |
| 110 | + def begin(self) -> Point: |
| 111 | + """ :returns: The smaller of ``a`` and ``b``. """ |
| 112 | + if self.a < self.b: |
| 113 | + return self.a |
| 114 | + else: |
| 115 | + return self.b |
| 116 | + |
| 117 | + def end(self) -> Point: |
| 118 | + """ :returns: The larger of ``a`` and ``b``. """ |
| 119 | + if self.a < self.b: |
| 120 | + return self.b |
| 121 | + else: |
| 122 | + return self.a |
| 123 | + |
| 124 | + def size(self) -> int: |
| 125 | + """ Equivalent to `__len__`. """ |
| 126 | + return abs(self.a - self.b) |
| 127 | + |
| 128 | + def contains(self, x: Point) -> bool: |
| 129 | + """ Equivalent to `__contains__`. """ |
| 130 | + return x in self |
| 131 | + |
| 132 | + def cover(self, region: Region) -> Region: |
| 133 | + """ :returns: A `Region` spanning both regions. """ |
| 134 | + a = min(self.begin(), region.begin()) |
| 135 | + b = max(self.end(), region.end()) |
| 136 | + |
| 137 | + if self.a < self.b: |
| 138 | + return Region(a, b) |
| 139 | + else: |
| 140 | + return Region(b, a) |
| 141 | + |
| 142 | + def intersection(self, region: Region) -> Region: |
| 143 | + """ :returns: A `Region` covered by both regions. """ |
| 144 | + if self.end() <= region.begin(): |
| 145 | + return Region(0) |
| 146 | + if self.begin() >= region.end(): |
| 147 | + return Region(0) |
| 148 | + |
| 149 | + return Region(max(self.begin(), region.begin()), min(self.end(), region.end())) |
| 150 | + |
| 151 | + def intersects(self, region: Region) -> bool: |
| 152 | + """ :returns: Whether the provided region intersects this region. """ |
| 153 | + lb = self.begin() |
| 154 | + le = self.end() |
| 155 | + rb = region.begin() |
| 156 | + re = region.end() |
| 157 | + |
| 158 | + return ( |
| 159 | + (lb == rb and le == re) or |
| 160 | + (rb > lb and rb < le) or (re > lb and re < le) or |
| 161 | + (lb > rb and lb < re) or (le > rb and le < re)) |
| 162 | + |
| 163 | +platform = {'Darwin': 'osx', 'Linux': 'linux', 'Windows': 'windows'}[platform.system()] |
| 164 | + |
| 165 | +def platform() -> Literal["osx", "linux", "windows"]: |
| 166 | + """ |
| 167 | + :returns: The platform which the plugin is being run on. |
| 168 | + """ |
| 169 | + return platform |
| 170 | + |
| 171 | +def error_message(msg: str): |
| 172 | + """ Display an error dialog. """ |
| 173 | + print('ERROR:', msg, file = sys.stderr, flush = True) |
0 commit comments