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

feat: show 2 context characters of a line when moving to it #4998

Merged
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
57 changes: 32 additions & 25 deletions src/virtual_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,40 +1227,47 @@ var VirtualRenderer = function(container, theme) {

var pos = this.$cursorLayer.getPixelPosition(cursor);

var left = pos.left;
var top = pos.top;
var newLeft = pos.left;
var newTop = pos.top;

var topMargin = $viewMargin && $viewMargin.top || 0;
var bottomMargin = $viewMargin && $viewMargin.bottom || 0;

if (this.$scrollAnimation) {
this.$stopAnimation = true;
}
var scrollTop = this.$scrollAnimation ? this.session.getScrollTop() : this.scrollTop;
if (scrollTop + topMargin > top) {
if (offset && scrollTop + topMargin > top + this.lineHeight)
top -= offset * this.$size.scrollerHeight;
if (top === 0)
top = -this.scrollMargin.top;
this.session.setScrollTop(top);
} else if (scrollTop + this.$size.scrollerHeight - bottomMargin < top + this.lineHeight) {
if (offset && scrollTop + this.$size.scrollerHeight - bottomMargin < top - this.lineHeight)
top += offset * this.$size.scrollerHeight;
this.session.setScrollTop(top + this.lineHeight + bottomMargin - this.$size.scrollerHeight);

var currentTop = this.$scrollAnimation ? this.session.getScrollTop() : this.scrollTop;

if (currentTop + topMargin > newTop) {
if (offset && currentTop + topMargin > newTop + this.lineHeight)
newTop -= offset * this.$size.scrollerHeight;
if (newTop === 0)
newTop = -this.scrollMargin.top;
this.session.setScrollTop(newTop);
} else if (currentTop + this.$size.scrollerHeight - bottomMargin < newTop + this.lineHeight) {
if (offset && currentTop + this.$size.scrollerHeight - bottomMargin < newTop - this.lineHeight)
newTop += offset * this.$size.scrollerHeight;
this.session.setScrollTop(newTop + this.lineHeight + bottomMargin - this.$size.scrollerHeight);
}

var scrollLeft = this.scrollLeft;
var currentLeft = this.scrollLeft;
// Show 2 context characters of the line when moving to it
var twoCharsWidth = 2 * this.layerConfig.characterWidth;

if (scrollLeft > left) {
if (left < this.$padding + 2 * this.layerConfig.characterWidth)
left = -this.scrollMargin.left;
this.session.setScrollLeft(left);
} else if (scrollLeft + this.$size.scrollerWidth < left + this.characterWidth) {
this.session.setScrollLeft(Math.round(left + this.characterWidth - this.$size.scrollerWidth));
} else if (scrollLeft <= this.$padding && left - scrollLeft < this.characterWidth) {
this.session.setScrollLeft(0);
if (newLeft - twoCharsWidth < currentLeft) {
newLeft -= twoCharsWidth;
if (newLeft < this.$padding + twoCharsWidth) {
newLeft = -this.scrollMargin.left;
}
this.session.setScrollLeft(newLeft);
} else {
newLeft += twoCharsWidth;
if (currentLeft + this.$size.scrollerWidth < newLeft + this.characterWidth) {
this.session.setScrollLeft(Math.round(newLeft + this.characterWidth - this.$size.scrollerWidth));
} else if (currentLeft <= this.$padding && newLeft - currentLeft < this.characterWidth) {
this.session.setScrollLeft(0);
}
}
};

Expand Down
27 changes: 27 additions & 0 deletions src/virtual_renderer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,33 @@ module.exports = {
}, 60);
}, 60);
}, 60);
},
"test: scroll cursor into view": function() {
function X(n) {
return "X".repeat(n);
}
editor.session.setValue(`${X(10)}\n${X(1000)}}`);

var initialContentLeft = editor.renderer.content.getBoundingClientRect().left;

// Scroll so far to the right that the first line is completely hidden
editor.session.selection.$setSelection(1, 1000, 1, 1000);
editor.renderer.scrollCursorIntoView();
editor.renderer.$loop._flush();

editor.session.selection.$setSelection(0, 10, 0, 10);
editor.renderer.scrollCursorIntoView();
editor.renderer.$loop._flush();

var contentLeft = editor.renderer.content.getBoundingClientRect().left;
var scrollDelta = initialContentLeft - contentLeft;

const leftBoundPixelPos = editor.renderer.$cursorLayer.getPixelPosition({row: 0, column: 8}).left;
const rightBoundPixelPos = editor.renderer.$cursorLayer.getPixelPosition({row: 0, column: 9}).left;
assert.ok(
scrollDelta >= leftBoundPixelPos && scrollDelta < rightBoundPixelPos,
"Expected content to have been scrolled two characters beyond the cursor"
);
}

// change tab size after setDocument (for text layer)
Expand Down