2025-11-14 01:21:48 +00:00
|
|
|
import 'server-only';
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
import { sew } from "@/actions";
|
2025-05-28 23:08:42 +00:00
|
|
|
import { searchResponseSchema } from "@/features/search/schemas";
|
|
|
|
|
import { search } from "@/features/search/searchApi";
|
|
|
|
|
import { ServiceError } from "@/lib/serviceError";
|
2025-11-14 01:21:48 +00:00
|
|
|
import { isServiceError } from "@/lib/utils";
|
|
|
|
|
import { withOptionalAuthV2 } from "@/withAuthV2";
|
2025-05-28 23:08:42 +00:00
|
|
|
import { SearchResponse } from "../search/types";
|
2025-11-14 01:21:48 +00:00
|
|
|
import { FindRelatedSymbolsRequest, FindRelatedSymbolsResponse } from "./types";
|
2025-05-28 23:08:42 +00:00
|
|
|
|
|
|
|
|
// The maximum number of matches to return from the search API.
|
|
|
|
|
const MAX_REFERENCE_COUNT = 1000;
|
|
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
export const findSearchBasedSymbolReferences = async (props: FindRelatedSymbolsRequest): Promise<FindRelatedSymbolsResponse | ServiceError> => sew(() =>
|
|
|
|
|
withOptionalAuthV2(async () => {
|
|
|
|
|
const {
|
|
|
|
|
symbolName,
|
|
|
|
|
language,
|
|
|
|
|
revisionName = "HEAD",
|
|
|
|
|
} = props;
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
const query = `\\b${symbolName}\\b rev:${revisionName} ${getExpandedLanguageFilter(language)} case:yes`;
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
const searchResult = await search({
|
|
|
|
|
query,
|
|
|
|
|
matches: MAX_REFERENCE_COUNT,
|
|
|
|
|
contextLines: 0,
|
|
|
|
|
});
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
if (isServiceError(searchResult)) {
|
|
|
|
|
return searchResult;
|
|
|
|
|
}
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
return parseRelatedSymbolsSearchResponse(searchResult);
|
|
|
|
|
}));
|
2025-05-28 23:08:42 +00:00
|
|
|
|
|
|
|
|
|
2025-11-14 01:21:48 +00:00
|
|
|
export const findSearchBasedSymbolDefinitions = async (props: FindRelatedSymbolsRequest): Promise<FindRelatedSymbolsResponse | ServiceError> => sew(() =>
|
|
|
|
|
withOptionalAuthV2(async () => {
|
2025-05-28 23:08:42 +00:00
|
|
|
const {
|
|
|
|
|
symbolName,
|
|
|
|
|
language,
|
|
|
|
|
revisionName = "HEAD",
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
const query = `sym:\\b${symbolName}\\b rev:${revisionName} ${getExpandedLanguageFilter(language)}`;
|
|
|
|
|
|
|
|
|
|
const searchResult = await search({
|
|
|
|
|
query,
|
|
|
|
|
matches: MAX_REFERENCE_COUNT,
|
|
|
|
|
contextLines: 0,
|
2025-09-20 23:51:14 +00:00
|
|
|
});
|
2025-05-28 23:08:42 +00:00
|
|
|
|
|
|
|
|
if (isServiceError(searchResult)) {
|
|
|
|
|
return searchResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseRelatedSymbolsSearchResponse(searchResult);
|
2025-11-14 01:21:48 +00:00
|
|
|
}));
|
2025-05-28 23:08:42 +00:00
|
|
|
|
|
|
|
|
const parseRelatedSymbolsSearchResponse = (searchResult: SearchResponse) => {
|
|
|
|
|
const parser = searchResponseSchema.transform(async ({ files }) => ({
|
|
|
|
|
stats: {
|
2025-10-08 06:55:36 +00:00
|
|
|
matchCount: searchResult.stats.actualMatchCount,
|
2025-05-28 23:08:42 +00:00
|
|
|
},
|
|
|
|
|
files: files.flatMap((file) => {
|
|
|
|
|
const chunks = file.chunks;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fileName: file.fileName.text,
|
|
|
|
|
repository: file.repository,
|
|
|
|
|
repositoryId: file.repositoryId,
|
|
|
|
|
webUrl: file.webUrl,
|
|
|
|
|
language: file.language,
|
|
|
|
|
matches: chunks.flatMap((chunk) => {
|
|
|
|
|
return chunk.matchRanges.map((range) => ({
|
|
|
|
|
lineContent: chunk.content,
|
|
|
|
|
range: range,
|
|
|
|
|
}))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}).filter((file) => file.matches.length > 0),
|
|
|
|
|
repositoryInfo: searchResult.repositoryInfo
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return parser.parseAsync(searchResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expands the language filter to include all variants of the language.
|
|
|
|
|
const getExpandedLanguageFilter = (language: string) => {
|
|
|
|
|
switch (language) {
|
|
|
|
|
case "TypeScript":
|
|
|
|
|
case "JavaScript":
|
|
|
|
|
case "JSX":
|
|
|
|
|
case "TSX":
|
|
|
|
|
return `(lang:TypeScript or lang:JavaScript or lang:JSX or lang:TSX)`
|
|
|
|
|
default:
|
|
|
|
|
return `lang:${language}`
|
|
|
|
|
}
|
|
|
|
|
}
|