Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
josharian committed Aug 20, 2023
1 parent e0391b4 commit 45b18d6
Show file tree
Hide file tree
Showing 16 changed files with 1,664 additions and 47 deletions.
5 changes: 4 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ export * from "./getFakeCommandServerApi";
export * from "./types/TestCaseFixture";
export * from "./util/getEnvironmentVariableStrict";
export * from "./util/CompositeKeyDefaultMap";
export { InMemoryTextDocument, InMemoryTextEditor } from "./testUtil/hatText";
export {
InMemoryTextDocument,
InMemoryTextEditor,
} from "./testUtil/mockEditor";
32 changes: 32 additions & 0 deletions packages/common/src/testUtil/mockEditor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as assert from "assert";
import { InMemoryTextDocument, Range } from "..";

suite("mockEditor", () => {
test("basic", () => {
const s = "abc\n\n123\n";
const doc: InMemoryTextDocument = new InMemoryTextDocument("test.txt", s);

for (let i = 0; i < s.length; i++) {
const pos = doc.positionAt(i);
const offset = doc.offsetAt(pos);
assert.equal(offset, i);
}
const line0 = doc.lineAt(0);
assert.equal(line0.text, "abc");
assert.equal(line0.firstNonWhitespaceCharacterIndex, 0);
assert.equal(line0.isEmptyOrWhitespace, false);
assert.equal(line0.lineNumber, 0);
assert.ok(line0.range.isEqual(new Range(0, 0, 0, 2)));
assert.equal(line0.rangeIncludingLineBreak.start.character, 0);
assert.equal(line0.lastNonWhitespaceCharacterIndex, 2);

const line1 = doc.lineAt(1);
assert.equal(line1.text, "");
assert.equal(line1.firstNonWhitespaceCharacterIndex, 0);
assert.equal(line1.isEmptyOrWhitespace, true);
assert.equal(line1.lineNumber, 1);
assert.ok(line1.range.isEqual(new Range(1, 0, 1, 0)));
assert.equal(line1.rangeIncludingLineBreak.start.character, 0);
assert.equal(line1.lastNonWhitespaceCharacterIndex, 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export class InMemoryTextLine implements TextLine {
* The range this line covers without the line separator characters.
*/
get range(): Range {
return new Range(this.lineNumber, 0, this.lineNumber, this.text.length);
// TODO: is this right? seems weird...one part of this must be wrong.
if (this.text.length === 0) {
return new Range(this.lineNumber, 0, this.lineNumber, 0);
}
return new Range(this.lineNumber, 0, this.lineNumber, this.text.length - 1);
}

/**
Expand Down Expand Up @@ -76,8 +80,7 @@ export class InMemoryTextLine implements TextLine {
if (!matches) {
return 0;
}
// TODO: is the +1 correct? copilot added it.
return this.text.lastIndexOf(matches[matches.length - 1]) + 1;
return this.text.lastIndexOf(matches[matches.length - 1]);
}

/**
Expand Down Expand Up @@ -184,20 +187,16 @@ export class InMemoryTextDocument implements TextDocument {
}

export class InMemoryTextEditor implements TextEditor {
private primarySelection: Selection;
public primarySelection: Selection;

constructor(
document: TextDocument,
primarySelection: Selection,
active: boolean,
) {
constructor(document: TextDocument, active: boolean) {
this.id = document.uri.toString();
this.document = document;
this.primarySelection = primarySelection;
this.primarySelection = new Selection(0, 0, 0, 0);
// TODO: support visible ranges
// TODO: support multiple selections
// TODO: support options
this.options = new defaultTextEditorOptions();
this.options = new DefaultTextEditorOptions();
this.isActive = active;
}

Expand Down Expand Up @@ -247,7 +246,7 @@ export class InMemoryTextEditor implements TextEditor {
}
}

class defaultTextEditorOptions implements TextEditorOptions {
class DefaultTextEditorOptions implements TextEditorOptions {
get tabSize(): number | string {
return 4;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/common/src/types/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ export class Range {
return this.start.isEqual(this.end);
}

/**
* Check if this range is equal to `other`.
*
* @param other A range.
* @return `true` if the start and end of the given range are equal to
* the start and end of this range.
*/
public isEqual(other: Range): boolean {
return this.start.isEqual(other.start) && this.end.isEqual(other.end);
}

/**
* `true` if `start.line` and `end.line` are equal.
*/
Expand Down
Loading

0 comments on commit 45b18d6

Please sign in to comment.