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 12 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 | undefined;
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
? undefined
: 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 @@ -34,41 +34,46 @@ suite("InMemoryTextDocument.lineAt", () => {
}

test("basic", () => {
const document = createTestDocument("hello \n world\n ");
const document = createTestDocument("hello \n world\n \n");

assert.equal(document.lineCount, 3);
assert.equal(document.lineCount, 4);

const line0 = document.lineAt(0);
const line1 = document.lineAt(1);
const line2 = document.lineAt(2);
const line3 = document.lineAt(3);
const lineUnderflow = document.lineAt(-1);
const lineOverflow = document.lineAt(100);

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.rangeIncludingLineBreak.toString(), "2:0-3:0");
assert.equal(line2.rangeTrimmed, undefined);

assert.equal(line3.lineNumber, 3);
assert.equal(line3.text, "");
assert.equal(line3.isEmptyOrWhitespace, true);
assert.equal(line3.range.toString(), "3:0-3:0");
assert.equal(line3.rangeIncludingLineBreak.toString(), "3:0-3:0");
assert.equal(line3.rangeTrimmed, undefined);

assert.equal(lineUnderflow.lineNumber, 0);
assert.equal(lineOverflow.lineNumber, 2);
assert.equal(lineOverflow.lineNumber, 3);
});
});
15 changes: 4 additions & 11 deletions packages/common/src/types/TextLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ 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 excludes leading and trailing whitespace (`/\s/`).
* **Note** that if a line is all whitespace this is undefined so the trimmed range can never be empty.
*/
readonly firstNonWhitespaceCharacterIndex: number;
readonly rangeTrimmed: Range | undefined;

/**
* 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;
}
2 changes: 1 addition & 1 deletion packages/cursorless-engine/src/actions/BreakLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function getEdits(editor: TextEditor, contentRanges: Range[]): Edit[] {
const line = document.lineAt(position);
const indentation = line.text.slice(
0,
line.firstNonWhitespaceCharacterIndex,
line.rangeTrimmed?.start?.character ?? line.range.start.character,
);
const characterTrailingWhitespace = line.text
.slice(0, position.character)
Expand Down
6 changes: 2 additions & 4 deletions packages/cursorless-engine/src/actions/JoinLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ 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,
line1.rangeTrimmed?.end ?? line1.range.end,
line2.rangeTrimmed?.start ?? line2.range.start,
),
text: line2.isEmptyOrWhitespace ? "" : " ",
isReplace: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ 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,
startLine.rangeTrimmed?.start ?? startLine.range.start,
endLine.rangeTrimmed?.end ?? endLine.range.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.range.start
: line.rangeTrimmed?.end ?? line.range.end;

// Use the full line with included indentation and trailing whitespaces
if (insertionPosition.character === nonWhitespaceCharacterIndex) {
// Use the full line with 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,10 @@ function getIndentationString(editor: TextEditor, range: Range) {
continue;
}

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

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 | undefined {
return this.line.isEmptyOrWhitespace
? undefined
: new Range(
new Position(
this.lineNumber,
this.line.firstNonWhitespaceCharacterIndex,
),
new Position(this.lineNumber, this.line.text.trimEnd().length),
);
}

get isEmptyOrWhitespace(): boolean {
Expand Down
32 changes: 10 additions & 22 deletions packages/cursorless-vscode/src/ide/vscode/textLine.vscode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,22 @@ import VscodeTextLineImpl from "./VscodeTextLineImpl";
*
* `[text, firstNonWhitespaceCharacterIndex, lastNonWhitespaceCharacterIndex]`
*/
const whiteSpaceTests: [string, number, number][] = [
[" ", 3, 0],
const whiteSpaceTests: [string, number | undefined, number | undefined][] = [
[" ", undefined, undefined],
["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);
});
});
});
23 changes: 8 additions & 15 deletions packages/neovim-common/src/ide/neovim/NeovimTextLineImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,16 @@ export default class NeovimTextLineImpl implements TextLine {
return new Range(this._lineNumber, 0, this._lineNumber + 1, 0);
}

get firstNonWhitespaceCharacterIndex(): number {
const index = /^(\s*)/.exec(this._text)![1].length;
// console.debug(
// `NeovimTextLineImpl.firstNonWhitespaceCharacterIndex=${index}`,
// );
return index;
}

get lastNonWhitespaceCharacterIndex(): number {
const index = this.text.trimEnd().length;
// console.debug(
// `NeovimTextLineImpl.lastNonWhitespaceCharacterIndex index=${index}`,
// );
return index;
get rangeTrimmed(): Range | undefined {
const startIndex = /^(\s*)/.exec(this._text)![1].length;
if (startIndex === this._text.length) {
return undefined;
}
const endIndex = this.text.trimEnd().length;
return new Range(this._lineNumber, startIndex, this._lineNumber, endIndex);
}

get isEmptyOrWhitespace(): boolean {
return this.firstNonWhitespaceCharacterIndex === this._text.length;
return this.rangeTrimmed == null;
}
}