forked from acejump/AceJump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundaries.kt
44 lines (39 loc) · 1.77 KB
/
Boundaries.kt
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
package org.acejump.boundaries
import com.intellij.openapi.editor.Editor
import kotlin.math.max
import kotlin.math.min
/**
* Defines a (possibly) disjoint set of editor offsets that partitions
* the whole editor into two groups - offsets inside the range, and
* offsets outside the range.
*/
interface Boundaries {
/**
* Returns a range of editor offsets, starting at the first offset in the
* boundary, and ending at the last offset in the boundary. May include
* offsets outside the boundary, for ex. when the boundary is rectangular
* and the file has long lines which are only partially visible.
*/
fun getOffsetRange(editor: Editor, cache: EditorOffsetCache = EditorOffsetCache.Uncached): IntRange =
StandardBoundaries.VISIBLE_ON_SCREEN.getOffsetRange(editor, cache)
/**
* Returns whether the editor offset is included within the boundary.
*/
fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache = EditorOffsetCache.Uncached): Boolean =
StandardBoundaries.VISIBLE_ON_SCREEN.isOffsetInside(editor, offset, cache)
/**
* Creates a boundary so that an offset/range is within the boundary
* iff it is within both original boundaries.
*/
fun intersection(other: Boundaries): Boundaries =
if (this === other) this
else object: Boundaries {
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
val b1 = this@Boundaries.getOffsetRange(editor, cache)
val b2 = other.getOffsetRange(editor, cache)
return max(b1.first, b2.first)..min(b1.last, b2.last)
}
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean =
this@Boundaries.isOffsetInside(editor, offset, cache) && other.isOffsetInside(editor, offset, cache)
}
}