Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion packages/diffs/src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3737,7 +3737,14 @@ export class Editor<LAnnotation> implements DiffsEditor<LAnnotation> {
}

const { start, end } = range;
for (let line = start.line; line <= end.line; line++) {
let firstLine = start.line;
let endLine = end.line + 1;
if (this.#renderRange !== undefined) {
const { startingLine, totalLines } = this.#renderRange;
firstLine = Math.max(firstLine, startingLine);
endLine = Math.min(endLine, startingLine + totalLines);
}
for (let line = firstLine; line < endLine; line++) {
if (!this.#isLineVisible(line)) {
continue;
}
Expand Down
38 changes: 37 additions & 1 deletion packages/diffs/test/editorVirtualizedEdit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { afterAll, describe, expect, test } from 'bun:test';
import { afterAll, describe, expect, spyOn, test } from 'bun:test';

import { File } from '../src/components/File';
import { DEFAULT_THEMES } from '../src/constants';
import { Editor } from '../src/editor/editor';
import { DirectionForward } from '../src/editor/selection';
import { disposeHighlighter } from '../src/highlighter/shared_highlighter';
import type { FileContents, RenderRange } from '../src/types';
import { installDom, wait } from './domHarness';
Expand Down Expand Up @@ -304,3 +305,38 @@ describe('Editor edits at the bottom of a virtualized window', () => {
}
});
});

describe('Editor selections in a virtualized window', () => {
test('limits a document-spanning selection to the rendered lines', async () => {
const range = makeRange(4900, 7);
const { cleanup, content, editor, fileContainer } =
await createWindowedEditor(10_000, range);
editor.setOptions({ roundedSelection: false });

const querySelector = spyOn(content, 'querySelector');
try {
editor.setState({
selections: [
{
start: { line: 0, character: 0 },
end: { line: 9999, character: 10 },
direction: DirectionForward,
},
],
view: { scrollLeft: 0, scrollTop: 0 },
});

expect(
querySelector.mock.calls
.map(([selector]) => /^\[data-line="(\d+)"\]/.exec(selector)?.[1])
.filter((line) => line !== undefined)
).toEqual(['10000']);
expect(
fileContainer.shadowRoot?.querySelectorAll('[data-selection-range]')
).toHaveLength(range.totalLines);
} finally {
querySelector.mockRestore();
cleanup();
}
});
});