From b6469eb90d8e7bc1a3da2b2c9f62787a0df5a0bb Mon Sep 17 00:00:00 2001 From: Robert Lin Date: Sat, 3 Dec 2022 18:49:49 -0800 Subject: [PATCH] search: add support for chunkMatches (#18) --- src/components/SearchCommand.tsx | 53 ++++++++++++++++++++----- src/hooks/search.tsx | 6 ++- src/sourcegraph/stream-search/index.ts | 2 +- src/sourcegraph/stream-search/stream.ts | 20 ++++++++-- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/components/SearchCommand.tsx b/src/components/SearchCommand.tsx index 80a0b5c..289b15d 100644 --- a/src/components/SearchCommand.tsx +++ b/src/components/SearchCommand.tsx @@ -319,9 +319,24 @@ function SearchResultItem({ case "content": icon.source = Icon.Snippets; - title = match.lineMatches.map((l) => l.line.trim()).join(" ... "); subtitle = match.path; - matchDetails.push(count(match.lineMatches.length, "line match", "line matches")); + + // Support both lineMatches and chunkMatches + if (match.chunkMatches) { + title = match.chunkMatches + .map((c) => + c.content + .split("\n") + .map((l) => l.trim()) + .join(" ... ") + ) + .join(" ... "); + matchDetails.push(count(match.chunkMatches?.length, "chunk match", "chunk matches")); + } else if (match.lineMatches) { + title = match.lineMatches.map((l) => l.line.trim()).join(" ... "); + matchDetails.push(count(match.lineMatches.length, "line match", "line matches")); + } + drilldownAction = makeDrilldownAction("Search File", setSearchText, { repo: match.repository, file: match.path, @@ -406,14 +421,32 @@ function MultiResultView({ searchResult }: { searchResult: { url: string; match: return ( - {match.lineMatches.map((l) => ( - {resultActions(urlWithLineNumber(searchResult.url, l.lineNumber))}} - /> - ))} + { + // support both chunkMatches and lineMatches + match.chunkMatches + ? match.chunkMatches.map((c) => ( + + {resultActions(urlWithLineNumber(searchResult.url, c.contentStart.line))} + + } + /> + )) + : match.lineMatches?.map((l) => ( + {resultActions(urlWithLineNumber(searchResult.url, l.lineNumber))} + } + /> + )) + } ); diff --git a/src/hooks/search.tsx b/src/hooks/search.tsx index adcd799..50d96fd 100644 --- a/src/hooks/search.tsx +++ b/src/hooks/search.tsx @@ -104,8 +104,10 @@ export function useSearch(src: Sourcegraph, maxResults: number) { }, onAlert: (alert) => { const toast = ExpandableToast(push, "Alert", alert.title, alert.description || ""); - if (alert.kind === "lucky-search-queries") { - toast.style = Toast.Style.Success; + switch (alert.kind) { + case "smart-search-additional-results": + case "smart-search-pure-results": + toast.style = Toast.Style.Success; } toast.show(); }, diff --git a/src/sourcegraph/stream-search/index.ts b/src/sourcegraph/stream-search/index.ts index be2dd48..b967d65 100644 --- a/src/sourcegraph/stream-search/index.ts +++ b/src/sourcegraph/stream-search/index.ts @@ -95,7 +95,7 @@ export async function performSearch( case "content": // Line number appears 0-indexed, for ease of use increment it so links // aren't off by 1. - match.lineMatches.forEach((l) => { + match.lineMatches?.forEach((l) => { l.lineNumber += 1; }); break; diff --git a/src/sourcegraph/stream-search/stream.ts b/src/sourcegraph/stream-search/stream.ts index 7222056..33540a1 100644 --- a/src/sourcegraph/stream-search/stream.ts +++ b/src/sourcegraph/stream-search/stream.ts @@ -39,18 +39,22 @@ export interface PathMatch { repoLastFetched?: string; branches?: string[]; commit?: string; + debug?: string; } export interface ContentMatch { type: "content"; path: string; + pathMatches?: Range[]; repository: string; repoStars?: number; repoLastFetched?: string; branches?: string[]; commit?: string; - lineMatches: LineMatch[]; + lineMatches?: LineMatch[]; + chunkMatches?: ChunkMatch[]; hunks?: DecoratedHunk[]; + debug?: string; } export interface DecoratedHunk { @@ -76,12 +80,18 @@ export interface Location { column: number; } -interface LineMatch { +export interface LineMatch { line: string; lineNumber: number; offsetAndLengths: number[][]; } +interface ChunkMatch { + content: string; + contentStart: Location; + ranges: Range[]; +} + export interface SymbolMatch { type: "symbol"; path: string; @@ -91,6 +101,7 @@ export interface SymbolMatch { branches?: string[]; commit?: string; symbols: MatchedSymbol[]; + debug?: string; } export interface MatchedSymbol { @@ -117,6 +128,8 @@ export interface CommitMatch { message: string; authorName: string; authorDate: string; + committerName: string; + committerDate: string; repoStars?: number; repoLastFetched?: string; @@ -128,6 +141,7 @@ export interface CommitMatch { export interface RepositoryMatch { type: "repo"; repository: string; + repositoryMatches?: Range[]; repoStars?: number; repoLastFetched?: string; description?: string; @@ -221,7 +235,7 @@ export interface Filter { kind: "file" | "repo" | "lang" | "utility"; } -export type AlertKind = "lucky-search-queries"; +export type AlertKind = "smart-search-additional-results" | "smart-search-pure-results"; interface Alert { title: string;