Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 8aa5fb3

Browse files
committed
Merge pull request #4339 from adobe/jeff/fix-find-result-centering-rebased
Fix find first result centering
2 parents 30be376 + 6e28b8b commit 8aa5fb3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/editor/Editor.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,21 @@ define(function (require, exports, module) {
892892
return this._codeMirror.lineCount();
893893
};
894894

895+
/**
896+
* Deterines if line is fully visible.
897+
* @param {number} zero-based index of the line to test
898+
* @return {boolean} true if the line is fully visible, false otherwise
899+
*/
900+
Editor.prototype.isLineVisible = function (line) {
901+
var coords = this._codeMirror.charCoords({line: line, ch: 0}, "local"),
902+
scrollInfo = this._codeMirror.getScrollInfo(),
903+
top = scrollInfo.top,
904+
bottom = scrollInfo.top + scrollInfo.clientHeight;
905+
906+
// Check top and bottom and return false for partially visible lines.
907+
return (coords.top >= top && coords.bottom <= bottom);
908+
};
909+
895910
/**
896911
* Gets the number of the first visible line in the editor.
897912
* @returns {number} The 0-based index of the first visible line.

src/search/FindReplace.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ define(function (require, exports, module) {
105105
}
106106
}
107107

108-
var centerOptions = (isFindFirst) ? Editor.BOUNDARY_IGNORE_TOP : Editor.BOUNDARY_CHECK_NORMAL;
108+
var resultVisible = editor.isLineVisible(cursor.from().line),
109+
centerOptions = Editor.BOUNDARY_CHECK_NORMAL;
110+
111+
if (isFindFirst && resultVisible) {
112+
// no need to scroll if the line with the match is in view
113+
centerOptions = Editor.BOUNDARY_IGNORE_TOP;
114+
}
109115
editor.setSelection(cursor.from(), cursor.to(), true, centerOptions);
110116
state.posFrom = cursor.from();
111117
state.posTo = cursor.to();

0 commit comments

Comments
 (0)