-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathposition.js
100 lines (81 loc) · 2.22 KB
/
position.js
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
'use babel';
/*
* Envy - Text editing supercharger
*
* Copyright (c) 2017 Philipp Emanuel Weidmann <pew@worldwidemann.com>
*
* Nemo vir est qui mundum non reddat meliorem.
*
* Released under the terms of the MIT License
* (https://opensource.org/licenses/MIT)
*/
import {Point} from 'atom';
import {EditorContext} from './base-classes';
import {getTextType} from './utilities';
// An editor-aware "Point" alternative
export default class Position extends EditorContext {
constructor(row, column) {
super();
this.row = row;
this.column = column;
}
static fromPoint(point) {
point = Point.fromObject(point);
return new Position(point.row, point.column);
}
toPoint() {
return new Point(this.row, this.column);
}
isEqual(position) {
return this.toPoint().isEqual(position);
}
copy(row = this.row, column = this.column) {
return new Position(row, column);
}
setClipped(row, column) {
let point = this.editor.clipBufferPosition([row, column]);
this.row = point.row;
this.column = point.column;
}
isBufferStart() {
return this.isEqual([0, 0]);
}
isBufferEnd() {
return this.isEqual(this.editor.getBuffer().getEndPosition());
}
isLineStart() {
return this.column === 0;
}
isLineEnd() {
return this.column === this.editor.getBuffer().lineLengthForRow(this.row);
}
getRelativePosition(offset) {
let position = this.copy();
for (let i = 0; i < Math.abs(offset); i++) {
if (offset > 0) {
if (position.isLineEnd()) {
position.setClipped(position.row + 1, 0);
} else {
position.column++;
}
} else {
if (position.isLineStart()) {
position.setClipped(position.row - 1, Infinity);
} else {
position.column--;
}
}
}
return position;
}
getRelativeText(offset) {
return this.editor.getTextInBufferRange([this, this.getRelativePosition(offset)]);
}
getRelativeTextType(offset) {
return getTextType(this.getRelativeText(offset));
}
canExpandTextType(textType, direction) {
return textType >= 0 && this.getRelativeTextType(direction) === textType &&
!((direction > 0) ? this.isLineEnd() : this.isLineStart());
}
}