Skip to content

Include line in --json search output #505

@danmackinlay

Description

@danmackinlay

Include line in --json search output

Problem

The CLI formatter includes line numbers in search results (e.g. qmd://blog/notebook/database_vector.qmd:2), but the --json formatter omits them. The line number is computed from chunkPos via extractSnippet in the CLI path (cli/qmd.js:1562) but isn't passed through to searchResultsToJson in formatter.js.

This makes it harder to build editor integrations. For example, VS Code's code --goto file:line works great with the CLI output, but parsing coloured terminal output is fragile. A --json workflow like:

qmd query "vector database" --json | jq -r '.[0] | "\(.file):\(.line)"' | xargs code --goto

would be much more robust, but currently .line is absent from the JSON.

Suggested fix

In cli/formatter.js, searchResultsToJson (around line 53), the result object is built as:

return {
    docid: `#${row.docid}`,
    score: Math.round(row.score * 100) / 100,
    file: row.displayPath,
    title: row.title,
    ...(row.context && { context: row.context }),
    ...(body && { body }),
    ...(snippet && { snippet }),
};

Adding the line number would be something like:

const { line, snippet: snip } = extractSnippet(bodyStr, query, 300, row.chunkPos, undefined, opts.intent);
return {
    docid: `#${row.docid}`,
    score: Math.round(row.score * 100) / 100,
    file: row.displayPath,
    line,
    title: row.title,
    ...(row.context && { context: row.context }),
    ...(body && { body }),
    ...(snip && !opts.full && { snippet: snip }),
};

The extractSnippet function is already imported in qmd.js; it may need to be made available to formatter.js as well, or the line could be pre-computed and passed through on the result object.

Use case

Building shell/editor integrations that open search results at the matching line — VS Code (code --goto), Vim (:e +line file), Emacs, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions