feat: Add start and end line numbers to CodeChunker chunks#336
feat: Add start and end line numbers to CodeChunker chunks#336AlanPonnachan wants to merge 7 commits intochonkie-inc:mainfrom
Conversation
Summary of ChangesHello @AlanPonnachan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request successfully adds start_line and end_line numbers to the Chunk objects created by CodeChunker, which is a great enhancement for mapping chunks back to the source code. The changes in the Chunk dataclass and the CodeChunker logic to extract this information from tree-sitter nodes are well-implemented. The addition of a new test case to verify this functionality is also a good step.
My review focuses on two main points:
- A potential
UnboundLocalErrorin the refactoredchunkmethod insrc/chonkie/chunker/code.pydue to a variable not being initialized before atryblock. - Incorrect assertions in the new test
test_code_chunker_adds_line_numbersintests/chunkers/test_code_chunker.py, which seem to be based on a misunderstanding of the test fixture's content.
Addressing these points will make the implementation more robust and ensure the tests are correctly validating the new feature.
This pull request resolves issue #326 by adding line number metadata to the
Chunkobjects generated by theCodeChunker.Previously, chunks only had
start_indexandend_indexwhich correspond to character offsets in the source text. This made it difficult to map a chunk back to its specific location (i.e., line numbers) in the original source code file, which is a common requirement for static analysis tools, code intelligence features, and displaying contextual search results.This PR introduces the
start_lineandend_lineattributes to eachChunk, providing this crucial context to the user.Changes Implemented
Modified
ChunkDataclass (src/chonkie/types/base.py):start_line: Optional[int]andend_line: Optional[int], to theChunkdataclass.__repr__,from_dict, andto_dictmethods to be aware of these new fields, ensuring they are correctly displayed and serialized.Updated
CodeChunker(src/chonkie/chunker/code.py):_create_chunksmethod now extracts the line number information from thetree-sitternodes associated with each chunk.node.start_point[0]andnode.end_point[0]attributes to get the 0-indexed row numbers and converts them to 1-indexed line numbers for user-friendliness.chunkmethod was slightly refactored to ensure thenode_groupsdata is available when creating chunks, while carefully preserving all existing logging statements.Added New Test Case (
tests/chunkers/test_code_chunker.py):test_code_chunker_adds_line_numbers, has been added to verify the new functionality.start_lineandend_lineare correctly populated, are valid integers, and accurately reflect the line numbers of the source code.Fixes #326