Skip to content

Commit 37888ea

Browse files
authored
Derank test files and improve coloring (#6)
* Derank test files and improve token coloring * fix
1 parent d279998 commit 37888ea

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

server/api.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"regexp"
9+
"sort"
910
"strings"
1011
"time"
1112

@@ -184,6 +185,14 @@ func (s *server) doSearch(ctx context.Context, backend *Backend, q *pb.Query) (*
184185
})
185186
}
186187

188+
// Derank test file results by sorting them to the end
189+
sort.SliceStable(reply.Results, func(i, j int) bool {
190+
return !isTestFile(reply.Results[i].Path) && isTestFile(reply.Results[j].Path)
191+
})
192+
sort.SliceStable(reply.FileResults, func(i, j int) bool {
193+
return !isTestFile(reply.FileResults[i].Path) && isTestFile(reply.FileResults[j].Path)
194+
})
195+
187196
reply.Info = &api.Stats{
188197
RE2Time: search.Stats.Re2Time,
189198
GitTime: search.Stats.GitTime,
@@ -275,3 +284,19 @@ func (s *server) ServeAPISearch(ctx context.Context, w http.ResponseWriter, r *h
275284

276285
replyJSON(ctx, w, 200, reply)
277286
}
287+
288+
func isTestFile(path string) bool {
289+
return strings.HasPrefix(path, "test/") ||
290+
strings.HasPrefix(path, "tests/") ||
291+
strings.Contains(path, "/test/") ||
292+
strings.Contains(path, "/tests/") ||
293+
strings.Contains(path, "/__tests__/") ||
294+
strings.HasSuffix(path, "_test.go") ||
295+
strings.HasSuffix(path, ".test.ts") ||
296+
strings.HasSuffix(path, ".test.js") ||
297+
strings.HasSuffix(path, ".test.tsx") ||
298+
strings.HasSuffix(path, "_test.py") ||
299+
strings.HasSuffix(path, ".spec.ts") ||
300+
strings.HasSuffix(path, ".spec.js") ||
301+
strings.HasSuffix(path, ".spec.tsx")
302+
}

web/src/codesearch/highlight.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,20 @@ function flattenTokens(tokens) {
7474
for (var i = 0; i < tokens.length; i++) {
7575
var token = tokens[i];
7676
if (typeof token === 'string') {
77-
result.push({type: null, text: token});
77+
result.push({type: null, alias: null, text: token});
7878
} else if (token.content) {
79+
var alias = token.alias || null;
7980
if (typeof token.content === 'string') {
80-
result.push({type: token.type, text: token.content});
81+
result.push({type: token.type, alias: alias, text: token.content});
8182
} else {
82-
// Nested tokens - flatten and inherit type for untyped children
83+
// Nested tokens - flatten and inherit type/alias for untyped children
8384
var nested = flattenTokens(
8485
Array.isArray(token.content) ? token.content : [token.content]
8586
);
8687
for (var j = 0; j < nested.length; j++) {
8788
result.push({
8889
type: nested[j].type || token.type,
90+
alias: nested[j].alias || alias,
8991
text: nested[j].text
9092
});
9193
}
@@ -124,12 +126,12 @@ function splitAtBounds(segments, bounds) {
124126
var lastCut = 0;
125127
for (var c = 0; c < cuts.length; c++) {
126128
if (cuts[c] > lastCut) {
127-
result.push({type: seg.type, text: seg.text.substring(lastCut, cuts[c])});
129+
result.push({type: seg.type, alias: seg.alias, text: seg.text.substring(lastCut, cuts[c])});
128130
}
129131
lastCut = cuts[c];
130132
}
131133
if (lastCut < seg.text.length) {
132-
result.push({type: seg.type, text: seg.text.substring(lastCut)});
134+
result.push({type: seg.type, alias: seg.alias, text: seg.text.substring(lastCut)});
133135
}
134136
}
135137

@@ -149,14 +151,22 @@ function buildNodes(segments, matchStart, matchEnd) {
149151
var segEnd = pos + seg.text.length;
150152
var inMatch = (pos >= matchStart && segEnd <= matchEnd);
151153

152-
if (!seg.type && !inMatch) {
154+
var hasType = seg.type || seg.alias;
155+
if (!hasType && !inMatch) {
153156
nodes.push(document.createTextNode(seg.text));
154157
} else {
155158
var span = document.createElement('span');
156159
var classes = [];
157-
if (seg.type) {
160+
if (hasType) {
158161
classes.push('token');
159-
classes.push(seg.type);
162+
if (seg.type) classes.push(seg.type);
163+
if (seg.alias) {
164+
if (Array.isArray(seg.alias)) {
165+
for (var a = 0; a < seg.alias.length; a++) classes.push(seg.alias[a]);
166+
} else {
167+
classes.push(seg.alias);
168+
}
169+
}
160170
}
161171
if (inMatch) {
162172
classes.push('matchstr');

0 commit comments

Comments
 (0)