From 438339167ff176f0401eab0f953f6d7f6f1bc0ce Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 11 Feb 2011 16:21:03 +0000 Subject: [PATCH] Fixed gotoLine to consider wrapped lines when calculating where to scroll to I have created a new method VirtualRenderer#scrollToLine to do for lines what scrollToRow does for absolute rows in the editor. Previously gotoLine called scrollToRow which would jump to the nth row, but with line wraps this may not be the nth line. scrollToLine also takes an additional parameter to tell it whether to scroll the line to the top or center of the editor. --- lib/ace/editor.js | 5 ++++- lib/ace/virtual_renderer.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 685ee10a..3e07a226 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -790,6 +790,9 @@ var Editor =function(renderer, session) { this.renderer.scrollToRow(row); }; + this.scrollToLine = function(line, center) { + this.renderer.scrollToLine(line, center); + }; this.getCursorPosition = function() { return this.selection.getCursor(); @@ -820,7 +823,7 @@ var Editor =function(renderer, session) { this.$blockScrolling = false; if (!this.isRowVisible(this.getCursorPosition().row)) { - this.scrollToRow(lineNumber - 1 - Math.floor(this.getVisibleRowCount() / 2)); + this.scrollToLine(lineNumber, true); } }, diff --git a/lib/ace/virtual_renderer.js b/lib/ace/virtual_renderer.js index c7fa8d63..f1d8c999 100644 --- a/lib/ace/virtual_renderer.js +++ b/lib/ace/virtual_renderer.js @@ -592,6 +592,19 @@ var VirtualRenderer = function(container, theme) { this.scrollToY(row * this.lineHeight); }; + this.scrollToLine = function(line, center) { + var lineHeight = { lineHeight: this.lineHeight }; + var offset = 0; + for (var l = 1; l < line; l++) { + offset += this.session.getRowHeight(lineHeight, l-1); + } + + if (center) { + offset -= this.$size.scrollerHeight / 2; + } + this.scrollToY(offset); + }; + this.scrollToY = function(scrollTop) { var maxHeight = this.session.getScreenLength() * this.lineHeight - this.$size.scrollerHeight; var scrollTop = Math.max(0, Math.min(maxHeight, scrollTop));