1 | export class Range {
|
2 | constructor(start, end) {
|
3 | this.containsPosition = (position) => {
|
4 | if (this.start.line === position.line) {
|
5 | return this.start.character <= position.character;
|
6 | }
|
7 | else if (this.end.line === position.line) {
|
8 | return this.end.character >= position.character;
|
9 | }
|
10 | else {
|
11 | return this.start.line <= position.line && this.end.line >= position.line;
|
12 | }
|
13 | };
|
14 | this.start = start;
|
15 | this.end = end;
|
16 | }
|
17 | setStart(line, character) {
|
18 | this.start = new Position(line, character);
|
19 | }
|
20 | setEnd(line, character) {
|
21 | this.end = new Position(line, character);
|
22 | }
|
23 | }
|
24 | export class Position {
|
25 | constructor(line, character) {
|
26 | this.lessThanOrEqualTo = (position) => this.line < position.line ||
|
27 | (this.line === position.line && this.character <= position.character);
|
28 | this.line = line;
|
29 | this.character = character;
|
30 | }
|
31 | setLine(line) {
|
32 | this.line = line;
|
33 | }
|
34 | setCharacter(character) {
|
35 | this.character = character;
|
36 | }
|
37 | }
|
38 | export function offsetToPosition(text, loc) {
|
39 | const EOL = '\n';
|
40 | const buf = text.slice(0, loc);
|
41 | const lines = buf.split(EOL).length - 1;
|
42 | const lastLineIndex = buf.lastIndexOf(EOL);
|
43 | return new Position(lines, loc - lastLineIndex - 1);
|
44 | }
|
45 | export function locToRange(text, loc) {
|
46 | const start = offsetToPosition(text, loc.start);
|
47 | const end = offsetToPosition(text, loc.end);
|
48 | return new Range(start, end);
|
49 | }
|
50 |
|
\ | No newline at end of file |