Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace firstNonWhitespaceCharacterIndex with rangeTrimmed #2534

Merged
merged 16 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import type { TextLine } from "../../types/TextLine";
import { getLeadingWhitespace, getTrailingWhitespace } from "../../util/regex";

export class InMemoryTextLine implements TextLine {
readonly isEmptyOrWhitespace: boolean;
readonly range: Range;
readonly rangeIncludingLineBreak: Range;
readonly firstNonWhitespaceCharacterIndex: number;
readonly lastNonWhitespaceCharacterIndex: number;
readonly isEmptyOrWhitespace: boolean;
readonly rangeTrimmed: Range;
readonly lengthIncludingEol: number;

constructor(
Expand All @@ -23,10 +22,13 @@ export class InMemoryTextLine implements TextLine {
const end = new Position(lineNumber, text.length);
const endIncludingLineBreak =
eol != null ? new Position(lineNumber + 1, 0) : end;
this.firstNonWhitespaceCharacterIndex = getLeadingWhitespace(text).length;
this.lastNonWhitespaceCharacterIndex =
text.length - getTrailingWhitespace(text).length;
this.range = new Range(start, end);
this.rangeIncludingLineBreak = new Range(start, endIncludingLineBreak);
this.rangeTrimmed = this.isEmptyOrWhitespace
? this.range
: new Range(
start.translate(undefined, getLeadingWhitespace(text).length),
end.translate(undefined, -getTrailingWhitespace(text).length),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,23 @@ suite("InMemoryTextDocument.lineAt", () => {
assert.equal(line0.lineNumber, 0);
assert.equal(line0.text, "hello ");
assert.equal(line0.isEmptyOrWhitespace, false);
assert.equal(line0.firstNonWhitespaceCharacterIndex, 0);
assert.equal(line0.lastNonWhitespaceCharacterIndex, 5);
assert.equal(line0.range.toString(), "0:0-0:7");
assert.equal(line0.rangeIncludingLineBreak.toString(), "0:0-1:0");
assert.equal(line0.rangeTrimmed.toString(), "0:0-0:5");

assert.equal(line1.lineNumber, 1);
assert.equal(line1.text, " world");
assert.equal(line1.isEmptyOrWhitespace, false);
assert.equal(line1.firstNonWhitespaceCharacterIndex, 2);
assert.equal(line1.lastNonWhitespaceCharacterIndex, 7);
assert.equal(line1.range.toString(), "1:0-1:7");
assert.equal(line1.rangeIncludingLineBreak.toString(), "1:0-2:0");
assert.equal(line1.rangeTrimmed.toString(), "1:2-1:7");

assert.equal(line2.lineNumber, 2);
assert.equal(line2.text, " ");
assert.equal(line2.isEmptyOrWhitespace, true);
assert.equal(line2.firstNonWhitespaceCharacterIndex, 2);
assert.equal(line2.lastNonWhitespaceCharacterIndex, 0);
assert.equal(line2.range.toString(), "2:0-2:2");
assert.equal(line2.rangeIncludingLineBreak.toString(), "2:0-2:2");
assert.equal(line2.rangeTrimmed.toString(), "2:0-2:2");

assert.equal(lineUnderflow.lineNumber, 0);
assert.equal(lineOverflow.lineNumber, 2);
Expand Down
16 changes: 5 additions & 11 deletions packages/common/src/types/TextLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,14 @@ export interface TextLine {
readonly rangeIncludingLineBreak: Range;

/**
* The offset of the first character which is not a whitespace character as defined
* by `/\s/`. **Note** that if a line is all whitespace the length of the line is returned.
* The trimmed range which is not a whitespace character as defined by `/\s/`.
* **Note** that if a line is all whitespace the full a range of the line is
* used.
*/
readonly firstNonWhitespaceCharacterIndex: number;
readonly rangeTrimmed: Range;

/**
* The offset of the last character which is not a whitespace character as defined
* by `/\s/`. **Note** that if a line is all whitespace 0 is returned.
*/
readonly lastNonWhitespaceCharacterIndex: number;

/**
* Whether this line is whitespace only, shorthand
* for {@link TextLine.firstNonWhitespaceCharacterIndex} === {@link TextLine.text TextLine.text.length}.
* Whether this line is whitespace only
*/
readonly isEmptyOrWhitespace: boolean;
}
5 changes: 1 addition & 4 deletions packages/cursorless-engine/src/actions/BreakLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ function getEdits(editor: TextEditor, contentRanges: Range[]): Edit[] {
for (const range of contentRanges) {
const position = range.start;
const line = document.lineAt(position);
const indentation = line.text.slice(
0,
line.firstNonWhitespaceCharacterIndex,
);
const indentation = line.text.slice(0, line.rangeTrimmed.start.character);
const characterTrailingWhitespace = line.text
.slice(0, position.character)
.search(/\s+$/);
Expand Down
7 changes: 1 addition & 6 deletions packages/cursorless-engine/src/actions/JoinLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ function getEdits(editor: TextEditor, contentRanges: Range[]): Edit[] {
);
for (const [line1, line2] of pairwise(lineIter)) {
edits.push({
range: new Range(
line1.range.end.line,
line1.lastNonWhitespaceCharacterIndex,
line2.range.start.line,
line2.firstNonWhitespaceCharacterIndex,
),
range: new Range(line1.rangeTrimmed.end, line2.rangeTrimmed.start),
text: line2.isEmptyOrWhitespace ? "" : " ",
isReplace: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,5 @@ export function createLineTarget(
export function fitRangeToLineContent(editor: TextEditor, range: Range) {
const startLine = editor.document.lineAt(range.start);
const endLine = editor.document.lineAt(range.end);

return new Range(
startLine.lineNumber,
startLine.firstNonWhitespaceCharacterIndex,
endLine.lineNumber,
endLine.lastNonWhitespaceCharacterIndex,
);
return new Range(startLine.rangeTrimmed.start, endLine.rangeTrimmed.end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ export class DestinationImpl implements Destination {

if (this.isLineDelimiter) {
const line = this.editor.document.lineAt(insertionPosition);
const nonWhitespaceCharacterIndex = this.isBefore
? line.firstNonWhitespaceCharacterIndex
: line.lastNonWhitespaceCharacterIndex;
const trimmedPosition = this.isBefore
? line.rangeTrimmed.start
: line.rangeTrimmed.end;

// Use the full line with included indentation and trailing whitespaces
if (insertionPosition.character === nonWhitespaceCharacterIndex) {
// Use the full line width including indentation and trailing whitespaces
if (insertionPosition.isEqual(trimmedPosition)) {
return this.isBefore ? line.range.start : line.range.end;
}
}
Expand Down Expand Up @@ -197,8 +197,12 @@ function getIndentationString(editor: TextEditor, range: Range) {
continue;
}

if (line.firstNonWhitespaceCharacterIndex < length) {
length = line.firstNonWhitespaceCharacterIndex;
const trimmedPosition = line.isEmptyOrWhitespace
? line.rangeTrimmed.end
: line.rangeTrimmed.start;

if (trimmedPosition.character < length) {
length = trimmedPosition.character;
indentationString = line.text.slice(0, length);
}
}
Expand Down
18 changes: 11 additions & 7 deletions packages/cursorless-vscode/src/ide/vscode/VscodeTextLineImpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Range, TextLine } from "@cursorless/common";
import { Position, Range, TextLine } from "@cursorless/common";
import { fromVscodeRange } from "@cursorless/vscode-common";
import * as vscode from "vscode";

Expand All @@ -21,12 +21,16 @@ export default class VscodeTextLineImpl implements TextLine {
return fromVscodeRange(this.line.rangeIncludingLineBreak);
}

get firstNonWhitespaceCharacterIndex(): number {
return this.line.firstNonWhitespaceCharacterIndex;
}

get lastNonWhitespaceCharacterIndex(): number {
return this.line.text.trimEnd().length;
get rangeTrimmed(): Range {
return this.line.isEmptyOrWhitespace
? this.range
: new Range(
new Position(
this.lineNumber,
this.line.firstNonWhitespaceCharacterIndex,
),
new Position(this.lineNumber, this.line.text.trimEnd().length),
);
}

get isEmptyOrWhitespace(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,21 @@ import VscodeTextLineImpl from "./VscodeTextLineImpl";
* `[text, firstNonWhitespaceCharacterIndex, lastNonWhitespaceCharacterIndex]`
*/
const whiteSpaceTests: [string, number, number][] = [
[" ", 3, 0],
[" ", 0, 3],
["foo", 0, 3],
[" foo ", 1, 4],
];

suite("TextLine", function () {
this.timeout("100s");
this.retries(5);
whiteSpaceTests.forEach(
([
text,
firstNonWhitespaceCharacterIndex,
lastNonWhitespaceCharacterIndex,
]) => {
test(`whitespace '${text}'`, async () => {
const editor = await openNewEditor(text);
const line = new VscodeTextLineImpl(editor.document.lineAt(0));
whiteSpaceTests.forEach(([text, trimmedStart, trimmedEnd]) => {
test(`whitespace '${text}'`, async () => {
const editor = await openNewEditor(text);
const line = new VscodeTextLineImpl(editor.document.lineAt(0));

assert.equal(
line.firstNonWhitespaceCharacterIndex,
firstNonWhitespaceCharacterIndex,
);
assert.equal(
line.lastNonWhitespaceCharacterIndex,
lastNonWhitespaceCharacterIndex,
);
});
},
);
assert.equal(line.rangeTrimmed.start.character, trimmedStart);
assert.equal(line.rangeTrimmed.end.character, trimmedEnd);
});
});
});
Loading