Skip to content

Commit

Permalink
search: add support for chunkMatches (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Dec 4, 2022
1 parent c8f78ac commit b6469eb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
53 changes: 43 additions & 10 deletions src/components/SearchCommand.tsx
Expand Up @@ -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,
Expand Down Expand Up @@ -406,14 +421,32 @@ function MultiResultView({ searchResult }: { searchResult: { url: string; match:
return (
<List navigationTitle={navigationTitle} searchBarPlaceholder="Filter matches">
<List.Section title={match.path} subtitle={matchTitle}>
{match.lineMatches.map((l) => (
<List.Item
key={nanoid()}
title={l.line}
accessories={[{ text: `L${l.lineNumber}` }]}
actions={<ActionPanel>{resultActions(urlWithLineNumber(searchResult.url, l.lineNumber))}</ActionPanel>}
/>
))}
{
// support both chunkMatches and lineMatches
match.chunkMatches
? match.chunkMatches.map((c) => (
<List.Item
key={nanoid()}
title={c.content}
accessories={[{ text: `L${c.contentStart.line}` }]}
actions={
<ActionPanel>
{resultActions(urlWithLineNumber(searchResult.url, c.contentStart.line))}
</ActionPanel>
}
/>
))
: match.lineMatches?.map((l) => (
<List.Item
key={nanoid()}
title={l.line}
accessories={[{ text: `L${l.lineNumber}` }]}
actions={
<ActionPanel>{resultActions(urlWithLineNumber(searchResult.url, l.lineNumber))}</ActionPanel>
}
/>
))
}
</List.Section>
</List>
);
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/search.tsx
Expand Up @@ -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();
},
Expand Down
2 changes: 1 addition & 1 deletion src/sourcegraph/stream-search/index.ts
Expand Up @@ -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;
Expand Down
20 changes: 17 additions & 3 deletions src/sourcegraph/stream-search/stream.ts
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -91,6 +101,7 @@ export interface SymbolMatch {
branches?: string[];
commit?: string;
symbols: MatchedSymbol[];
debug?: string;
}

export interface MatchedSymbol {
Expand All @@ -117,6 +128,8 @@ export interface CommitMatch {
message: string;
authorName: string;
authorDate: string;
committerName: string;
committerDate: string;
repoStars?: number;
repoLastFetched?: string;

Expand All @@ -128,6 +141,7 @@ export interface CommitMatch {
export interface RepositoryMatch {
type: "repo";
repository: string;
repositoryMatches?: Range[];
repoStars?: number;
repoLastFetched?: string;
description?: string;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b6469eb

Please sign in to comment.