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

fix: scroll cursor line to top editor for long inline preview #5514

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/autocomplete/inline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,15 @@ module.exports = {
// of the container
editor.execCommand("insertstring", "\n".repeat(200));

var deltaY;
var deltaY, row;
var initialScrollBy = editor.renderer.scrollBy;
editor.renderer.scrollBy = function(varX, varY) {
var initialScrollToRow = editor.renderer.scrollToRow;
editor.renderer.scrollBy = function(_, varY) {
deltaY = varY;
};
editor.renderer.scrollToRow = function(varRow) {
row = varRow;
};

inline.show(editor, completions[6], "l");
editor.renderer.$loop._flush();
Expand All @@ -295,8 +299,9 @@ module.exports = {

setTimeout(() => {
// Should scroll as much as possbile while keeping the cursor on screen
assert.strictEqual(deltaY, 490);
assert.strictEqual(row, 202);
editor.renderer.scrollBy = initialScrollBy;
editor.renderer.scrollToRow = initialScrollToRow;
done();
}, 50);
}, 50);
Expand Down
8 changes: 4 additions & 4 deletions src/virtual_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,18 +1781,18 @@ class VirtualRenderer {
var el = this.container;
var height = el.getBoundingClientRect().height;
var ghostTextHeight = textLines.length * this.lineHeight;
var fitsY = ghostTextHeight < height - pixelPosition.top;
var fitsY = ghostTextHeight < (height - pixelPosition.top);

// If it fits, no action needed
if (fitsY) return;

// If it can fully fit in the screen, scroll down until it fits on the screen
// if it cannot fully fit, scroll so that the cursor is at the top of the screen
// to fit as much as possible.
// if it cannot fully fit, scroll so that the row with the cursor
// is at the top of the screen.
if (ghostTextHeight < height) {
this.scrollBy(0, (textLines.length - 1) * this.lineHeight);
} else {
this.scrollBy(0, pixelPosition.top);
this.scrollToRow(insertPosition.row);
}
}

Expand Down
Loading