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

Commit b2df944

Browse files
Merge pull request #9743 from MarcelGerber/find-in-match-highlight
Smarter line clipping in Find in Files
2 parents c321002 + 343b3c6 commit b2df944

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/search/FindInFiles.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ define(function (require, exports, module) {
5050
*/
5151
var ZERO_FILES_TO_SEARCH = {};
5252

53+
/**
54+
* Maximum length of text displayed in search results panel
55+
* @const
56+
*/
57+
var MAX_DISPLAY_LENGTH = 200;
58+
5359
/**
5460
* The search query and results model.
5561
* @type {SearchModel}
@@ -91,7 +97,8 @@ define(function (require, exports, module) {
9197
return [];
9298
}
9399

94-
var match, lineNum, line, ch, totalMatchLength, matchedLines, numMatchedLines, lastLineLength,
100+
var match, lineNum, line, ch, totalMatchLength, matchedLines, numMatchedLines, lastLineLength, endCh,
101+
padding, leftPadding, rightPadding, highlightOffset, highlightEndCh,
95102
lines = StringUtils.getLines(contents),
96103
matches = [];
97104

@@ -103,13 +110,31 @@ define(function (require, exports, module) {
103110
numMatchedLines = matchedLines.length;
104111
totalMatchLength = match[0].length;
105112
lastLineLength = matchedLines[matchedLines.length - 1].length;
113+
endCh = (numMatchedLines === 1 ? ch + totalMatchLength : lastLineLength);
114+
highlightEndCh = (numMatchedLines === 1 ? endCh : line.length);
115+
highlightOffset = 0;
106116

107-
// Don't store more than 200 chars per line
108-
line = line.substr(0, Math.min(200, line.length));
117+
if (highlightEndCh <= MAX_DISPLAY_LENGTH) {
118+
// Don't store more than 200 chars per line
119+
line = line.substr(0, Math.min(MAX_DISPLAY_LENGTH, line.length));
120+
} else if (totalMatchLength > MAX_DISPLAY_LENGTH) {
121+
// impossible to display the whole match
122+
line = line.substr(ch, ch + MAX_DISPLAY_LENGTH);
123+
highlightOffset = ch;
124+
} else {
125+
// Try to have both beginning and end of match displayed
126+
padding = MAX_DISPLAY_LENGTH - totalMatchLength;
127+
rightPadding = Math.floor(Math.min(padding / 2, line.length - highlightEndCh));
128+
leftPadding = Math.ceil(padding - rightPadding);
129+
highlightOffset = ch - leftPadding;
130+
line = line.substring(highlightOffset, highlightEndCh + rightPadding);
131+
}
109132

110133
matches.push({
111134
start: {line: lineNum, ch: ch},
112-
end: {line: lineNum + numMatchedLines - 1, ch: (numMatchedLines === 1 ? ch + totalMatchLength : lastLineLength)},
135+
end: {line: lineNum + numMatchedLines - 1, ch: endCh},
136+
137+
highlightOffset: highlightOffset,
113138

114139
// Note that the following offsets from the beginning of the file are *not* updated if the search
115140
// results change. These are currently only used for multi-file replacement, and we always

src/search/SearchResultsView.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,9 @@ define(function (require, exports, module) {
433433
itemIndex: searchItems.length,
434434
matchIndex: i,
435435
line: match.start.line + 1,
436-
pre: match.line.substr(0, match.start.ch),
437-
highlight: match.line.substring(match.start.ch, multiLine ? undefined : match.end.ch),
438-
post: multiLine ? "\u2026" : match.line.substr(match.end.ch),
436+
pre: match.line.substr(0, match.start.ch - match.highlightOffset),
437+
highlight: match.line.substring(match.start.ch - match.highlightOffset, multiLine ? undefined : match.end.ch - match.highlightOffset),
438+
post: multiLine ? "\u2026" : match.line.substr(match.end.ch - match.highlightOffset),
439439
start: match.start,
440440
end: match.end,
441441
isChecked: match.isChecked,

0 commit comments

Comments
 (0)