Skip to content

Ask AI: name a text selection's source lines in the question - #1732

Merged
backnotprop merged 2 commits into
mainfrom
feat/ask-ai-selection-line
Oct 6, 2026
Merged

backnotprop merged 2 commits into
mainfrom
feat/ask-ai-selection-line

Conversation

@backnotprop

@backnotprop backnotprop commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1731

When you select text in a markdown document and ask a question with Ask AI, the question now says which lines the selection is on:

Re: Selected text
Source: /path/to/doc.md, line 41

A selection inside a block that spans several lines, or across several blocks, gives a range: lines 41–44. Before this change the question carried only the path, so the agent could not tell which occurrence of a repeated phrase you meant.

What the line is

It is the source line span of the block that holds the selection. For a selection within one block, this is the same span the exported annotation for that selection prints, (line 41) or (lines 41–44), so the question and the feedback name the same place. A selection over several blocks gives a range from the first block's start line to the last block's end line; the export names only the first block in that case. Frontmatter is counted, so the numbers are file lines. Lines inside a block are not resolved further, because the rendered text does not map back to source lines reliably once inline markdown is involved.

Where it applies

  • Plan review and annotate (markdown), including linked documents and folder sessions.
  • The side chat, Ask this session, and the annotate agent terminal. All three send buildDefaultPrompt's output, and the agent terminal and side chat now share one scope mapping (packages/editor/askScope.ts).
  • The code-block composer also sends the fence's line span.

Not changed:

  • HTML and live-app pinpoints have no source lines. They already send the element's identity.
  • Diagram comments already send their line in detail (feat(diagrams): Ask AI from a diagram comment #1695).
  • A converted HTML or URL source (--markdown, URLs) sends no lines. Those lines belong to the converted markdown, not to the file the agent would read.
  • Code review Ask AI has its own prompt builder with file and line context. It is not touched.

For @plannotator/ui hosts

Everything is optional and additive: CommentAskAIContext.lineStart?/lineEnd?, AIQuestion.scope.lineStart?/lineEnd?, CommentPopoverState.blockIds?, and a new pure helper selectionSourceLines(blocks, blockIds) in utils/parser. A scope without lines produces the same prompt bytes as before. This is documented in packages/ui/HANDOFF.md.

Tests

  • packages/ui/components/Viewer.askAISelectionLine.test.tsx (DOM, added to the CI DOM step): renders the real Viewer and web-highlighter on a document that has the same phrase on line 3 and on line 9. Selecting each occurrence and clicking Ask AI gives the right line, and the prompt names line 9, not line 3. Both tests fail without the Viewer change.
  • packages/ui/utils/parser.selectionLines.test.ts: frontmatter offset, wrapped paragraphs, code fences, multi-block ranges, unknown and diff ids, and parity with the export's (line N) heading for single-block selections (after frontmatter, and a wrapped paragraph).
  • packages/editor/askScope.test.ts: the App mapping. A lone lineStart becomes a one-line range, a converted source drops the lines, and a context without lines gets no line keys.
  • packages/ui/utils/aiPrompt.test.ts: the Source: line formatting, and identical output when no lines (or invalid ones) are given.

bun test, bun run typecheck, the related DOM tests, the review/hook/opencode builds, and the guides.show check:manifest all pass.

A text-selection Ask AI question (side chat, Ask this session, agent
terminal) now says which lines the selection is on, e.g.
'Source: /docs/plan.md, line 41', so a phrase that occurs more than
once in the document is no longer ambiguous.

Fixes #1731
Extract App's Ask AI scope mapping to packages/editor/askScope.ts and
unit-test it (lineEnd defaults to lineStart; converted sources drop the
lines). Assert a single-block selection's question names the same span
as the exported annotation's heading. Qualify the docs: that parity
holds for a selection within one block.
@backnotprop
backnotprop merged commit 6d22b82 into main Oct 6, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ask AI: include the line number of a text selection in annotate mode

1 participant