1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
// Maybe reuse file search's functions?
import getMatches from "./get-matches";
import type { SourceId, TextSourceContent } from "../../types";
export function findSourceMatches(
sourceId: SourceId,
content: TextSourceContent,
queryText: string
): Object[] {
if (queryText == "") {
return [];
}
const modifiers = {
caseSensitive: false,
regexMatch: false,
wholeWord: false,
};
const text = content.value;
const lines = text.split("\n");
return getMatches(queryText, text, modifiers).map(({ line, ch }) => {
const { value, matchIndex } = truncateLine(lines[line], ch);
return {
sourceId,
line: line + 1,
column: ch,
matchIndex,
match: queryText,
value,
};
});
}
// This is used to find start of a word, so that cropped string look nice
const startRegex = /([ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?])/g;
// Similarly, find
const endRegex = new RegExp(
[
"([ !@#$%^&*()_+-=[]{};':\"\\|,.<>/?])",
'[^ !@#$%^&*()_+-=[]{};\':"\\|,.<>/?]*$"/',
].join("")
);
function truncateLine(text: string, column: number) {
if (text.length < 100) {
return {
matchIndex: column,
value: text,
};
}
// Initially take 40 chars left to the match
const offset = Math.max(column - 40, 0);
// 400 characters should be enough to figure out the context of the match
const truncStr = text.slice(offset, column + 400);
let start = truncStr.search(startRegex);
let end = truncStr.search(endRegex);
if (start > column) {
// No word separator found before the match, so we take all characters
// before the match
start = -1;
}
if (end < column) {
end = truncStr.length;
}
const value = truncStr.slice(start + 1, end);
return {
matchIndex: column - start - offset - 1,
value,
};
}
|