Skip to content

fix(opencode): expand single-line selection to enclosing LSP symbol - #42888

Closed
icyaaaww wants to merge 2 commits into
anomalyco:devfrom
icyaaaww:fix/lsp-symbol-expand-off-by-one
Closed

icyaaaww wants to merge 2 commits into
anomalyco:devfrom
icyaaaww:fix/lsp-symbol-expand-off-by-one

Conversation

@icyaaaww

@icyaaaww icyaaaww commented Aug 16, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #42887

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Single-line file selection (startLine === endLine) was never expanded to its enclosing symbol. The check compared
.start.line (LSP 0-based) against start (1-based, parsed from the ?start= query param), so it never matched. The
?.start?.line && guard also skipped symbols on LSP line 0.

Convert the LSP line to 1-based before comparing and assigning, keeping start/end 1-based for the Read tool offset/limit math.

How did you verify your code works?

Traced the index conventions: start feeds the Read tool offset, which is 1-based ( ool/read.ts does opts.offset - 1), while LSP documentSymbol ranges are 0-based (lsp/diagnostic.ts does
ange.start.line + 1 for display). The old comparison mixed the two and could never match; the fix converts to 1-based before comparing.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 16, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@Enough1122

Copy link
Copy Markdown

AI code review — automated review for reference; please use your judgment.

The functional fix in packages/opencode/src/session/prompt.ts:844 is correct and well-targeted: matching r.start.line + 1 === start fixes both the off-by-one (LSP 0-based vs URL 1-based) and the truthiness bug that silently skipped symbols starting at line 0, and converting both start/end consistently makes offset/limit land exactly on the symbol span. The new end-to-end test (mocked documentSymbol, ?start=2&end=2 → {"offset":2,"limit":4}) pins this nicely — it fails under the old comparison.

  • packages/opencode/test/session/prompt.test.ts:1 -- The entire ~2400-line test file was rewritten with CRLF line endings -- why: the working diff balloons to ~175KB while only ~80 lines are functional changes; this buries the real edit, breaks git blame for every untouched line, and introduces endings inconsistent with the rest of the repo (the base blob is LF) -- suggestion: restore LF (editor/prettier endOfLine: "lf" or a .gitattributes *.ts text eol=lf rule), re-push, and keep this PR limited to the actual change so it's reviewable.

  • packages/opencode/test/session/prompt.test.ts:2245 -- The line-0 regression half of the fix has no dedicated coverage -- why: the added test exercises a symbol starting at 0-based line 1; the other repaired bug (falsy r?.start?.line skipping symbols at line 0) can silently regress again without a pinning test -- suggestion: add a second mock case returning a symbol with start: { line: 0 } and assert ?start=1&end=1 expands to the full symbol.

  • packages/opencode/src/session/prompt.ts:847 -- First matching symbol wins via break -- why: when nested symbols share a start line (e.g. class declaration and its first member formatted together), which symbol wins depends on server ordering of the flattened documentSymbol result, making expansion nondeterministic across LSP implementations -- suggestion: prefer the longest span among symbols whose start matches (or pick the outermost by kind ordering) so the expanded range is stable.

  • Reviewed-by: ox-alpha (automated reviewer-f)

@kaeldrin-gh

Copy link
Copy Markdown

Hi! I noticed both of these PRs fix the same root cause from #42887 (1-based URL line vs 0-based LSP range in session/prompt.ts). I opened #47503 as well — it keeps the same approach but adds the line-0 regression test your review suggested (a symbol starting at LSP line 0 with ?start=1&end=1) and keeps the test diff small (focused additions instead of the whole file rewrite). Happy to defer to yours if you'd like to incorporate the line-0 test — just wanted to flag the overlap so it doesn't get lost. 🙏

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Single-line file selection doesn't expand to enclosing symbol (off-by-one)

3 participants