search
uses tcmts.cli.pages.searchCode, which uses tcmts.cli.pages.searchableTexts, which uses node:fs
tcm search <text>: every line of the codebase's code and pages with text in it, by definition, as an
editor's find in files (tcm find searches names, docs and signatures). Lines are numbered as tcm view shows the
definition. The text is as written, or a regular expression written /…/ (as tcm view --grep takes it); regex
takes all of it as one, the old way. under keeps to a namespace; at most limit lines are shown.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { searchCode } from "#codebase/tcmts/cli/pages"; import { noLines } from "#codebase/tcmts/cli/pattern"; import { bold, dim } from "#codebase/tcmts/cli/style"; export function search( cb: Codebase, text: string, opts: { regex?: boolean; caseSensitive?: boolean; wholeWord?: boolean; under?: string; limit?: number; } = {}, ): boolean { const under = opts.under; const r = searchCode( cb, text, { regex: !!opts.regex, caseSensitive: !!opts.caseSensitive, wholeWord: !!opts.wholeWord, ...(under ? { under } : {}), }, () => "", ); if (r.error) { console.log(`search ${JSON.stringify(text)}: ${r.error}`); return false; } const hits = r.hits; if (!hits.length) { console.log(dim(noLines(text, { regex: !!opts.regex, ...(under ? { under } : {}) }))); return true; } const limit = opts.limit ?? 200; let shown = 0; let lines = 0; for (const h of hits) { lines += h.lines.length; if (shown >= limit) continue; console.log(bold(h.path)); for (const l of h.lines) { if (shown++ >= limit) break; console.log(` ${dim(String(l.line - h.first + 1).padStart(4))} ${l.text.trim()}`); } } const more = lines - Math.min(lines, limit); console.log( dim( `\n${lines} line(s) in ${hits.length} definition(s)${more ? `; the first ${limit} shown (--limit <n> shows more)` : ""}`, ), ); if (r.truncated) console.log( dim( `it stopped at 500 definitions or 5000 matches: the rest of ${under ?? "the codebase"} wasn't searched. Narrow it: more of the text, --word, or --under <namespace>`, ), ); return true; }
uses
- tcmts.cli.codebase.Codebase
- tcmts.cli.pages.searchCode
- tcmts.cli.pattern.noLines
- tcmts.cli.style.bold
- tcmts.cli.style.dim