Skip to content

Commit

Permalink
Added range concise one based function (#2742)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AndreasArvidsson authored Jan 21, 2025
1 parent 611968d commit 3d1447c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
10 changes: 9 additions & 1 deletion packages/common/src/types/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,21 @@ export class Position {
}

/**
* Return a concise string representation of the position.
* Return a concise string representation of the position. 0-based.
* @returns concise representation
**/
public concise(): string {
return `${this.line}:${this.character}`;
}

/**
* Return a concise string representation of the position. 1-based.
* @returns concise representation
**/
public conciseOneBased(): string {
return `${this.line + 1}:${this.character + 1}`;
}

public toString(): string {
return this.concise();
}
Expand Down
12 changes: 10 additions & 2 deletions packages/common/src/types/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,22 @@ export class Range {
}

/**
* Return a concise string representation of the range
* Return a concise string representation of the range. 0-based.
* @returns concise representation
**/
public concise(): string {
return `${this.start.concise()}-${this.end.concise()}`;
}

/**
* Return a concise string representation of the range. 1-based.
* @returns concise representation
**/
public conciseOneBased(): string {
return `${this.start.conciseOneBased()}-${this.end.conciseOneBased()}`;
}

public toString(): string {
return this.concise();
return `${this.start.concise()}-${this.end.concise()}`;
}
}
10 changes: 9 additions & 1 deletion packages/common/src/types/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,21 @@ export class Selection extends Range {
}

/**
* Return a concise string representation of the selection
* Return a concise string representation of the selection. 0-based.
* @returns concise representation
**/
public concise(): string {
return `${this.anchor.concise()}->${this.active.concise()}`;
}

/**
* Return a concise string representation of the selection. 1-based.
* @returns concise representation
**/
public conciseOneBased(): string {
return `${this.start.conciseOneBased()}->${this.end.conciseOneBased()}`;
}

public toString(): string {
return this.concise();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/types/position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ suite("Position", () => {
new Position(1, 1).translate(undefined, 5).isEqual(new Position(1, 6)),
);
});

test("concise", () => {
assert.equal(new Position(1, 2).concise(), "1:2");
assert.equal(new Position(1, 2).conciseOneBased(), "2:3");
});
});
5 changes: 5 additions & 0 deletions packages/common/src/types/range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ suite("Range", () => {
assert.ok(new Range(1, 2, 3, 4).toSelection(true).isReversed);
assert.ok(!new Range(1, 2, 3, 4).toSelection(false).isReversed);
});

test("concise", () => {
assert.equal(new Range(1, 2, 3, 4).concise(), "1:2-3:4");
assert.equal(new Range(1, 2, 3, 4).conciseOneBased(), "2:3-4:5");
});
});
5 changes: 5 additions & 0 deletions packages/common/src/types/selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ suite("Selection", () => {
assert.ok(new Selection(1, 2, 3, 4).toSelection(true).isReversed);
assert.ok(!new Selection(1, 2, 3, 4).toSelection(false).isReversed);
});

test("concise", () => {
assert.equal(new Selection(1, 2, 3, 4).concise(), "1:2->3:4");
assert.equal(new Selection(1, 2, 3, 4).conciseOneBased(), "2:3->4:5");
});
});

0 comments on commit 3d1447c

Please sign in to comment.