Skip to content

fix: index single-line Kotlin objects - #1610

Open
mjq2020 wants to merge 1 commit into
CodeGraphContext:mainfrom
mjq2020:agent/index-single-line-kotlin-object
Open

fix: index single-line Kotlin objects#1610
mjq2020 wants to merge 1 commit into
CodeGraphContext:mainfrom
mjq2020:agent/index-single-line-kotlin-object

Conversation

@mjq2020

@mjq2020 mjq2020 commented Aug 13, 2026

Copy link
Copy Markdown

Summary

  • recognize the exact infix_expression shape produced for a single-line named Kotlin object
  • preserve the object's name and class context for functions inside that object
  • cover both the reported named-object case and an anonymous-object non-regression case

Root cause

The Kotlin tree-sitter grammar parses object A { fun x() = 1 } differently from the equivalent multiline declaration: it emits an infix_expression containing object_literal, simple_identifier, and lambda_literal, so the existing object_declaration query never sees the object. The parser still sees the nested function, but without its owning object context.

Tests

  • TMPDIR=/private/tmp pytest -q tests/unit/parsers/test_kotlin_parser.py — 90 passed
  • TMPDIR=/private/tmp ./tests/run_tests.sh fast — 1190 passed, 19 skipped before stopping slow unrelated golden cases; five existing failures were unrelated CLI inventory drift and bundle_export receiving a Typer OptionInfo
  • python -m compileall -q src/codegraphcontext/tools/languages/kotlin.py tests/unit/parsers/test_kotlin_parser.py
  • git diff --check

Fixes #1600

@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

@mjq2020 is attempting to deploy a commit to the shashankss1205's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Shashankss1205 Shashankss1205 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — verified, though it took me a detour to do it correctly, and the detour is worth recording.

My first attempt said this PR was a no-op, because on my local venv main already handled single-line objects. That was wrong: my venv had tree-sitter-language-pack 0.13.0, while pyproject.toml pins >=1.6,<2.0. The behaviour here is entirely grammar-version dependent.

On the version the project actually pins (1.14.3), the reported misparse reproduces exactly:

object A { fun x() = 1 }   -> top-level node = infix_expression,  object_declaration captures = []
object A { }               -> top-level node = object_declaration, captures = ['A']
object A {\n  fun x()\n}   -> top-level node = object_declaration, captures = ['A']

So only the one-line-with-body form breaks, which is precisely what your infix_expression → (object_literal, simple_identifier, lambda_literal) shape match targets.

Re-running the comparison on grammar 1.14.3:

main:     objects=[Empty, MultiLine]            functions=[a→MultiLine, b→None,     c→Plain]
#1610:    objects=[Empty, OneLine, MultiLine]   functions=[a→MultiLine, b→OneLine,  c→Plain]

So it recovers both the missing Object node and the orphaned function's class_contextb was previously landing with no owner at all, which is the part that would quietly corrupt containment queries.

Guarding on the exact three-child shape and checking the first child's text is literally object is appropriately conservative — it won't misfire on genuine infix expressions.

tests/unit/parsers/test_kotlin_parser.py: 90 passed under grammar 1.14.3. Full unit suite: 1206 passed.

Thanks @mjq2020 — good fix, and it closes #1600.

@Shashankss1205

Copy link
Copy Markdown
Collaborator

Approved above — the only thing blocking merge is that this is still marked as a draft. Mark it ready for review and I'll merge it straight away. (I'd rather you flip it than do it for you, in case you were still intending changes.)

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

Labels

None yet

Projects

Status: Backlog tasks

Development

Successfully merging this pull request may close these issues.

bug(parsers/kotlin): object A { fun x() = 1 } on one line is never indexed

2 participants