2024-09-11 04:55:00 +00:00
|
|
|
import escapeStringRegexp from "escape-string-regexp";
|
2024-09-10 06:16:41 +00:00
|
|
|
import { SHARD_MAX_MATCH_COUNT, TOTAL_MAX_MATCH_COUNT } from "../environment";
|
Search suggestions (#85)
The motivation for building search suggestions is two-fold: (1) to make the zoekt query language more approachable by presenting all available options to the user, and (2) make it easier for power-users to craft complex queries.
The meat-n-potatoes of this change are concentrated in searchBar.tsx and searchSuggestionBox.tsx. The suggestions box works by maintaining a state-machine of "modes". By default, the box is in the refine mode, where suggestions for different prefixes (e.g., repo:, lang:, etc.) are suggested to the user. When one of these prefixes is matched, the state-machine transitions to the corresponding mode (e.g., repository, language, etc.) and surfaces suggestions for that mode (if any).
The query is split up into parts by spaces " " (e.g., 'test repo:hello' -> ['test', 'repo:hello']). See splitQuery. The part that has the cursor over it is considered the active part. We evaluate which mode the state machine is in based on the active part. When a suggestion is clicked, we only modify the active part of the query.
Three modes are currently missing suggestion data: file (file names), revision (branch / tag names), and symbol (symbol names). In future PRs, we will need to introduce endpoints into the backend to allow the frontend to fetch this data and surface it as suggestions..
2024-11-23 02:50:13 +00:00
|
|
|
import { listRepositoriesResponseSchema, zoektSearchResponseSchema } from "../schemas";
|
2024-09-26 03:12:20 +00:00
|
|
|
import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "../types";
|
2024-09-11 04:55:00 +00:00
|
|
|
import { fileNotFound, invalidZoektResponse, ServiceError, unexpectedError } from "../serviceError";
|
2024-09-10 06:16:41 +00:00
|
|
|
import { isServiceError } from "../utils";
|
|
|
|
|
import { zoektFetch } from "./zoektClient";
|
|
|
|
|
|
2024-11-07 02:28:10 +00:00
|
|
|
// List of supported query prefixes in zoekt.
|
|
|
|
|
// @see : https://github.com/sourcebot-dev/zoekt/blob/main/query/parse.go#L417
|
|
|
|
|
enum zoektPrefixes {
|
|
|
|
|
archived = "archived:",
|
|
|
|
|
branchShort = "b:",
|
|
|
|
|
branch = "branch:",
|
|
|
|
|
caseShort = "c:",
|
|
|
|
|
case = "case:",
|
|
|
|
|
content = "content:",
|
|
|
|
|
fileShort = "f:",
|
|
|
|
|
file = "file:",
|
|
|
|
|
fork = "fork:",
|
|
|
|
|
public = "public:",
|
|
|
|
|
repoShort = "r:",
|
|
|
|
|
repo = "repo:",
|
|
|
|
|
regex = "regex:",
|
|
|
|
|
lang = "lang:",
|
|
|
|
|
sym = "sym:",
|
|
|
|
|
typeShort = "t:",
|
|
|
|
|
type = "type:",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mapping of additional "alias" prefixes to zoekt prefixes.
|
|
|
|
|
const aliasPrefixMappings: Record<string, zoektPrefixes> = {
|
|
|
|
|
"rev:": zoektPrefixes.branch,
|
|
|
|
|
"revision:": zoektPrefixes.branch,
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 06:31:51 +00:00
|
|
|
export const search = async ({ query, maxMatchDisplayCount, whole }: SearchRequest): Promise<SearchResponse | ServiceError> => {
|
2024-11-07 02:28:10 +00:00
|
|
|
// Replace any alias prefixes with their corresponding zoekt prefixes.
|
|
|
|
|
for (const [prefix, zoektPrefix] of Object.entries(aliasPrefixMappings)) {
|
|
|
|
|
query = query.replaceAll(prefix, zoektPrefix);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 06:16:41 +00:00
|
|
|
const body = JSON.stringify({
|
|
|
|
|
q: query,
|
2024-09-29 21:39:17 +00:00
|
|
|
// @see: https://github.com/sourcebot-dev/zoekt/blob/main/api.go#L892
|
2024-09-10 06:16:41 +00:00
|
|
|
opts: {
|
|
|
|
|
NumContextLines: 2,
|
|
|
|
|
ChunkMatches: true,
|
2024-09-26 06:31:51 +00:00
|
|
|
MaxMatchDisplayCount: maxMatchDisplayCount,
|
2024-09-10 06:16:41 +00:00
|
|
|
Whole: !!whole,
|
|
|
|
|
ShardMaxMatchCount: SHARD_MAX_MATCH_COUNT,
|
|
|
|
|
TotalMaxMatchCount: TOTAL_MAX_MATCH_COUNT,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const searchResponse = await zoektFetch({
|
|
|
|
|
path: "/api/search",
|
|
|
|
|
body,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!searchResponse.ok) {
|
|
|
|
|
return invalidZoektResponse(searchResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const searchBody = await searchResponse.json();
|
2024-11-07 02:28:10 +00:00
|
|
|
const parsedSearchResponse = zoektSearchResponseSchema.safeParse(searchBody);
|
2024-09-10 19:24:12 +00:00
|
|
|
if (!parsedSearchResponse.success) {
|
|
|
|
|
console.error(`Failed to parse zoekt response. Error: ${parsedSearchResponse.error}`);
|
|
|
|
|
return unexpectedError(`Something went wrong while parsing the response from zoekt`);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 02:28:10 +00:00
|
|
|
const isBranchFilteringEnabled = (
|
|
|
|
|
query.includes(zoektPrefixes.branch) ||
|
|
|
|
|
query.includes(zoektPrefixes.branchShort)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...parsedSearchResponse.data,
|
|
|
|
|
isBranchFilteringEnabled,
|
|
|
|
|
}
|
2024-09-10 06:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-07 18:27:42 +00:00
|
|
|
// @todo (bkellam) : We should really be using `git show <hash>:<path>` to fetch file contents here.
|
|
|
|
|
// This will allow us to support permalinks to files at a specific revision that may not be indexed
|
|
|
|
|
// by zoekt.
|
2024-11-07 02:28:10 +00:00
|
|
|
export const getFileSource = async ({ fileName, repository, branch }: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => {
|
2024-09-10 19:24:12 +00:00
|
|
|
const escapedFileName = escapeStringRegexp(fileName);
|
|
|
|
|
const escapedRepository = escapeStringRegexp(repository);
|
2024-11-07 02:28:10 +00:00
|
|
|
|
|
|
|
|
let query = `file:${escapedFileName} repo:^${escapedRepository}$`;
|
|
|
|
|
if (branch) {
|
|
|
|
|
query = query.concat(` branch:${branch}`);
|
|
|
|
|
}
|
2024-09-10 19:24:12 +00:00
|
|
|
|
2024-09-10 06:16:41 +00:00
|
|
|
const searchResponse = await search({
|
2024-11-07 02:28:10 +00:00
|
|
|
query,
|
2024-09-26 06:31:51 +00:00
|
|
|
maxMatchDisplayCount: 1,
|
2024-09-10 06:16:41 +00:00
|
|
|
whole: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isServiceError(searchResponse)) {
|
|
|
|
|
return searchResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const files = searchResponse.Result.Files;
|
|
|
|
|
|
2024-09-10 19:24:12 +00:00
|
|
|
if (!files || files.length === 0) {
|
2024-09-10 06:16:41 +00:00
|
|
|
return fileNotFound(fileName, repository);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 18:27:42 +00:00
|
|
|
const file = files[0];
|
|
|
|
|
const source = file.Content ?? '';
|
|
|
|
|
const language = file.Language;
|
2024-09-10 06:16:41 +00:00
|
|
|
return {
|
2025-01-07 18:27:42 +00:00
|
|
|
source,
|
|
|
|
|
language,
|
2024-09-10 06:16:41 +00:00
|
|
|
}
|
2024-09-11 04:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const listRepositories = async (): Promise<ListRepositoriesResponse | ServiceError> => {
|
|
|
|
|
const body = JSON.stringify({
|
|
|
|
|
opts: {
|
|
|
|
|
Field: 0,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const listResponse = await zoektFetch({
|
|
|
|
|
path: "/api/list",
|
|
|
|
|
body,
|
2024-09-11 19:15:12 +00:00
|
|
|
method: "POST",
|
|
|
|
|
cache: "no-store",
|
2024-09-11 04:55:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!listResponse.ok) {
|
|
|
|
|
return invalidZoektResponse(listResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listBody = await listResponse.json();
|
|
|
|
|
const parsedListResponse = listRepositoriesResponseSchema.safeParse(listBody);
|
|
|
|
|
if (!parsedListResponse.success) {
|
|
|
|
|
console.error(`Failed to parse zoekt response. Error: ${parsedListResponse.error}`);
|
|
|
|
|
return unexpectedError(`Something went wrong while parsing the response from zoekt`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsedListResponse.data;
|
2024-09-10 06:16:41 +00:00
|
|
|
}
|