Skip to content

Commit 3d1447c

Browse files
Added range concise one based function (#2742)
The current concise function is 0-based, but sometimes when doing debug prints it's easier to have the positions be 1-based to match vscode ui. ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet
1 parent 611968d commit 3d1447c

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

packages/common/src/types/Position.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,21 @@ export class Position {
141141
}
142142

143143
/**
144-
* Return a concise string representation of the position.
144+
* Return a concise string representation of the position. 0-based.
145145
* @returns concise representation
146146
**/
147147
public concise(): string {
148148
return `${this.line}:${this.character}`;
149149
}
150150

151+
/**
152+
* Return a concise string representation of the position. 1-based.
153+
* @returns concise representation
154+
**/
155+
public conciseOneBased(): string {
156+
return `${this.line + 1}:${this.character + 1}`;
157+
}
158+
151159
public toString(): string {
152160
return this.concise();
153161
}

packages/common/src/types/Range.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,22 @@ export class Range {
144144
}
145145

146146
/**
147-
* Return a concise string representation of the range
147+
* Return a concise string representation of the range. 0-based.
148148
* @returns concise representation
149149
**/
150150
public concise(): string {
151151
return `${this.start.concise()}-${this.end.concise()}`;
152152
}
153153

154+
/**
155+
* Return a concise string representation of the range. 1-based.
156+
* @returns concise representation
157+
**/
158+
public conciseOneBased(): string {
159+
return `${this.start.conciseOneBased()}-${this.end.conciseOneBased()}`;
160+
}
161+
154162
public toString(): string {
155-
return this.concise();
163+
return `${this.start.concise()}-${this.end.concise()}`;
156164
}
157165
}

packages/common/src/types/Selection.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ export class Selection extends Range {
7474
}
7575

7676
/**
77-
* Return a concise string representation of the selection
77+
* Return a concise string representation of the selection. 0-based.
7878
* @returns concise representation
7979
**/
8080
public concise(): string {
8181
return `${this.anchor.concise()}->${this.active.concise()}`;
8282
}
8383

84+
/**
85+
* Return a concise string representation of the selection. 1-based.
86+
* @returns concise representation
87+
**/
88+
public conciseOneBased(): string {
89+
return `${this.start.conciseOneBased()}->${this.end.conciseOneBased()}`;
90+
}
91+
8492
public toString(): string {
8593
return this.concise();
8694
}

packages/common/src/types/position.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ suite("Position", () => {
6868
new Position(1, 1).translate(undefined, 5).isEqual(new Position(1, 6)),
6969
);
7070
});
71+
72+
test("concise", () => {
73+
assert.equal(new Position(1, 2).concise(), "1:2");
74+
assert.equal(new Position(1, 2).conciseOneBased(), "2:3");
75+
});
7176
});

packages/common/src/types/range.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,9 @@ suite("Range", () => {
113113
assert.ok(new Range(1, 2, 3, 4).toSelection(true).isReversed);
114114
assert.ok(!new Range(1, 2, 3, 4).toSelection(false).isReversed);
115115
});
116+
117+
test("concise", () => {
118+
assert.equal(new Range(1, 2, 3, 4).concise(), "1:2-3:4");
119+
assert.equal(new Range(1, 2, 3, 4).conciseOneBased(), "2:3-4:5");
120+
});
116121
});

packages/common/src/types/selection.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ suite("Selection", () => {
4949
assert.ok(new Selection(1, 2, 3, 4).toSelection(true).isReversed);
5050
assert.ok(!new Selection(1, 2, 3, 4).toSelection(false).isReversed);
5151
});
52+
53+
test("concise", () => {
54+
assert.equal(new Selection(1, 2, 3, 4).concise(), "1:2->3:4");
55+
assert.equal(new Selection(1, 2, 3, 4).conciseOneBased(), "2:3->4:5");
56+
});
5257
});

0 commit comments

Comments
 (0)