| 1 | import Link from "next/link"; |
| 2 | import { FileCode2 } |
| 3 | import { Fragment, type |
| 4 | import type { ThemedToken |
| 5 | |
| 6 | import { |
| 7 | Empty, |
| 8 | EmptyDescription, |
| 9 | EmptyHeader, |
| 10 | EmptyMedia, |
| 11 | EmptyTitle, |
| 12 | } from "@/components/ui/empty"; |
| 13 | import type { components |
| 14 | import { highlightLines } |
| 15 | |
| 16 | type CodeSearchFile |
| 17 | repository?: components |
| 18 | }; |
| 19 | |
| 20 | type Range |
| 21 | |
| 22 | export async function CodeSearchResults |
| 23 | files, |
| 24 | repository, |
| 25 | error, |
| 26 | }: { |
| 27 | files: CodeSearchFile |
| 28 | /** Required when the files carry no repository of their own: a search scoped to one repository. */ |
| 29 | repository?: { |
| 30 | error?: string |
| 31 | }) { |
| 32 | if (error || |
| 33 | return ( |
| 34 | <Empty className=" |
| 35 | <EmptyHeader> |
| 36 | <EmptyMedia variant=" |
| 37 | <FileCode2 /> |
| 38 | </EmptyMedia> |
| 39 | <EmptyTitle>{error |
| 40 | <EmptyDescription> |
| 41 | {error ?? |
| 42 | "Try different words, a /regex/, or a file: or lang: filter."} |
| 43 | </EmptyDescription> |
| 44 | </EmptyHeader> |
| 45 | </Empty> |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | // The matches are scattered lines, not a file, so each is highlighted on its own: a grammar state carried across a gap would colour the next line wrongly. |
| 50 | const tokens = |
| 51 | files.map((file |
| 52 | Promise.all( |
| 53 | file.lines.map |
| 54 | async ({ line |
| 55 | (await highlightLines(line |
| 56 | [], |
| 57 | ), |
| 58 | ), |
| 59 | ), |
| 60 | ); |
| 61 | |
| 62 | return ( |
| 63 | <div className="flex flex-col gap-4 |
| 64 | {files.map(( |
| 65 | const { owner, |
| 66 | // Line numbers refer to the commit the index was built from, so link to that commit rather than a branch that may have moved. |
| 67 | const blob = |
| 68 | |
| 69 | return ( |
| 70 | <article |
| 71 | key={`${owner |
| 72 | className="overflow-hidden rounded-lg border" |
| 73 | > |
| 74 | <header className="flex items-center gap-2 border-b bg-muted/40 px-4 py-2 text-sm |
| 75 | {file.repository && |
| 76 | <> |
| 77 | <Link |
| 78 | href={`/${owner |
| 79 | className="text-muted-foreground hover:text-foreground hover:underline" |
| 80 | > |
| 81 | {owner}/{file |
| 82 | </Link> |
| 83 | <span className="text-muted-foreground/60 |
| 84 | </> |
| 85 | )} |
| 86 | <Link |
| 87 | href={blob} |
| 88 | className="min-w-0 truncate font-medium hover:underline" |
| 89 | > |
| 90 | {file.path} |
| 91 | </Link> |
| 92 | </header> |
| 93 | |
| 94 | {file.lines.length |
| 95 | <div className="overflow-x-auto |
| 96 | <table className="w-full border-collapse font-mono text-xs |
| 97 | <tbody> |
| 98 | {file.lines.map |
| 99 | const previous = |
| 100 | |
| 101 | return ( |
| 102 | <Fragment key={ |
| 103 | {previous && |
| 104 | line.lineNumber > |
| 105 | <tr aria-hidden className |
| 106 | <td className="select-none border-y border-r py-0.5 pr-3 text-right text-muted-foreground |
| 107 | ⋯ |
| 108 | </td> |
| 109 | <td className="border-y |
| 110 | </tr> |
| 111 | )} |
| 112 | <tr className="hover:bg-muted/40 |
| 113 | <td className="w-12 select-none border-r py-0.5 pr-3 text-right align-top text-muted-foreground |
| 114 | <Link |
| 115 | href={`${blob |
| 116 | className="hover:text-foreground" |
| 117 | > |
| 118 | {line.lineNumber} |
| 119 | </Link> |
| 120 | </td> |
| 121 | <td className="py-0.5 pl-4 whitespace-pre |
| 122 | {markMatches(tokens[f |
| 123 | </td> |
| 124 | </tr> |
| 125 | </Fragment> |
| 126 | ); |
| 127 | })} |
| 128 | </tbody> |
| 129 | </table> |
| 130 | </div> |
| 131 | )} |
| 132 | </article> |
| 133 | ); |
| 134 | })} |
| 135 | </div> |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** Syntax tokens with the matched spans marked. A match can start or end mid-token, so tokens are cut at match boundaries and each piece keeps its colour; the pieces of one match share a single mark. */ |
| 140 | export function markMatches(tokens |
| 141 | const pieces: |
| 142 | |
| 143 | for (const token |
| 144 | const end = |
| 145 | let at = |
| 146 | while (at < |
| 147 | const range = |
| 148 | const to = |
| 149 | ? Math.min( |
| 150 | : Math.min( |
| 151 | pieces.push({ |
| 152 | range, |
| 153 | node: ( |
| 154 | <span key={at |
| 155 | {token.content.slice |
| 156 | </span> |
| 157 | ), |
| 158 | }); |
| 159 | at = to; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | const parts: |
| 164 | for (let i |
| 165 | const { range |
| 166 | if (!range) |
| 167 | parts.push(pieces |
| 168 | continue; |
| 169 | } |
| 170 | const nodes: |
| 171 | while (pieces[i |
| 172 | parts.push( |
| 173 | <mark key={` |
| 174 | {nodes} |
| 175 | </mark>, |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | return parts; |
| 180 | } |