Skip to content

Commit 898c909

Browse files
migrate blocking search over to grpc. Centralize everything in searchApi
1 parent afcc30d commit 898c909

File tree

10 files changed

+587
-1104
lines changed

10 files changed

+587
-1104
lines changed

packages/web/src/app/api/(server)/stream_search/route.ts

Lines changed: 18 additions & 528 deletions
Large diffs are not rendered by default.

packages/web/src/features/chat/tools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ Multiple expressions can be or'd together with or, negated with -, or grouped wi
180180
const response = await search({
181181
query,
182182
matches: limit ?? 100,
183-
// @todo: we can make this configurable.
184183
contextLines: 3,
185184
whole: false,
186-
// @todo(mt): handle multi-tenancy.
185+
isCaseSensitivityEnabled: true,
186+
isRegexEnabled: true,
187187
});
188188

189189
if (isServiceError(response)) {

packages/web/src/features/codeNav/api.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ export const findSearchBasedSymbolReferences = async (props: FindRelatedSymbolsR
1919
revisionName = "HEAD",
2020
} = props;
2121

22-
const query = `\\b${symbolName}\\b rev:${revisionName} ${getExpandedLanguageFilter(language)} case:yes`;
22+
const query = `\\b${symbolName}\\b rev:${revisionName} ${getExpandedLanguageFilter(language)}`;
2323

2424
const searchResult = await search({
2525
query,
2626
matches: MAX_REFERENCE_COUNT,
2727
contextLines: 0,
28+
isCaseSensitivityEnabled: true,
29+
isRegexEnabled: true,
2830
});
2931

3032
if (isServiceError(searchResult)) {
@@ -49,6 +51,8 @@ export const findSearchBasedSymbolDefinitions = async (props: FindRelatedSymbols
4951
query,
5052
matches: MAX_REFERENCE_COUNT,
5153
contextLines: 0,
54+
isCaseSensitivityEnabled: true,
55+
isRegexEnabled: true,
5256
});
5357

5458
if (isServiceError(searchResult)) {

packages/web/src/features/search/fileSourceApi.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'server-only';
2-
import escapeStringRegexp from "escape-string-regexp";
32
import { fileNotFound, ServiceError, unexpectedError } from "../../lib/serviceError";
43
import { FileSourceRequest, FileSourceResponse } from "./types";
54
import { isServiceError } from "../../lib/utils";
@@ -12,18 +11,17 @@ import { withOptionalAuthV2 } from "@/withAuthV2";
1211

1312
export const getFileSource = async ({ fileName, repository, branch }: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => sew(() =>
1413
withOptionalAuthV2(async () => {
15-
const escapedFileName = escapeStringRegexp(fileName);
16-
const escapedRepository = escapeStringRegexp(repository);
17-
18-
let query = `file:${escapedFileName} repo:^${escapedRepository}$`;
14+
let query = `file:${fileName} repo:^${repository}$`;
1915
if (branch) {
20-
query = query.concat(` branch:${branch}`);
16+
query = query.concat(` rev:${branch}`);
2117
}
2218

2319
const searchResponse = await search({
2420
query,
2521
matches: 1,
2622
whole: true,
23+
isCaseSensitivityEnabled: true,
24+
isRegexEnabled: true,
2725
});
2826

2927
if (isServiceError(searchResponse)) {

packages/web/src/app/api/(server)/stream_search/transformer.ts renamed to packages/web/src/features/search/query.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Q } from '@/proto/zoekt/webserver/v1/Q';
1+
import { Q as ZoektGrpcQuery } from '@/proto/zoekt/webserver/v1/Q';
22
import {
33
AndExpr,
44
ArchivedExpr,
@@ -21,6 +21,12 @@ import {
2121
Tree,
2222
VisibilityExpr,
2323
} from '@sourcebot/query-language';
24+
import { parser as _lezerQueryParser } from '@sourcebot/query-language';
25+
26+
const lezerQueryParser = _lezerQueryParser.configure({
27+
strict: true,
28+
});
29+
2430

2531
type ArchivedValue = 'yes' | 'no' | 'only';
2632
type VisibilityValue = 'public' | 'private' | 'any';
@@ -38,10 +44,11 @@ const isForkValue = (value: string): value is ForkValue => {
3844
return value === 'yes' || value === 'no' || value === 'only';
3945
}
4046

41-
/**
42-
* Transform a Lezer parse tree into a Zoekt gRPC query
43-
*/
44-
export const transformToZoektQuery = ({
47+
export const parseQueryIntoLezerTree = (query: string): Tree => {
48+
return lezerQueryParser.parse(query);
49+
}
50+
51+
export const transformLezerTreeToZoektGrpcQuery = async ({
4552
tree,
4653
input,
4754
isCaseSensitivityEnabled,
@@ -53,9 +60,9 @@ export const transformToZoektQuery = ({
5360
isCaseSensitivityEnabled: boolean;
5461
isRegexEnabled: boolean;
5562
onExpandSearchContext: (contextName: string) => Promise<string[]>;
56-
}): Promise<Q> => {
63+
}): Promise<ZoektGrpcQuery> => {
5764

58-
const transformNode = async (node: SyntaxNode): Promise<Q> => {
65+
const transformNode = async (node: SyntaxNode): Promise<ZoektGrpcQuery> => {
5966
switch (node.type.id) {
6067
case Program: {
6168
// Program wraps the actual query - transform its child
@@ -134,7 +141,7 @@ export const transformToZoektQuery = ({
134141
}
135142
}
136143

137-
const transformPrefixExpr = async (node: SyntaxNode): Promise<Q> => {
144+
const transformPrefixExpr = async (node: SyntaxNode): Promise<ZoektGrpcQuery> => {
138145
// Find which specific prefix type this is
139146
const prefixNode = node.firstChild;
140147
if (!prefixNode) {
@@ -207,13 +214,13 @@ export const transformToZoektQuery = ({
207214
return {
208215
symbol: {
209216
expr: {
210-
substring: {
211-
pattern: value,
217+
regexp: {
218+
regexp: value,
212219
case_sensitive: isCaseSensitivityEnabled,
213220
file_name: false,
214221
content: true
215222
},
216-
query: "substring"
223+
query: "regexp"
217224
}
218225
},
219226
query: "symbol"

0 commit comments

Comments
 (0)