Skip to content

feat: return char offset in pyo3 API#1178

Merged
HerringtonDarkholme merged 3 commits intomainfrom
napi
Jun 1, 2024
Merged

feat: return char offset in pyo3 API#1178
HerringtonDarkholme merged 3 commits intomainfrom
napi

Conversation

@HerringtonDarkholme
Copy link
Copy Markdown
Member

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jun 1, 2024

Walkthrough

The recent changes to the pyo3 crate introduce a new unicode_position module and enhance existing structures to handle Unicode character positions more effectively. Key updates include the addition of UnicodePosition to SgRoot, modifications to the range and replace methods in SgNode, and adjustments to the Range struct to use character offsets. These changes improve the accuracy of text range and edit operations in Unicode strings.

Changes

Files Change Summary
crates/pyo3/src/lib.rs Introduced unicode_position module and added UnicodePosition to SgRoot struct.
crates/pyo3/src/py_node.rs Modified range and replace methods in SgNode to use UnicodePosition for character offsets. Updated Edit struct to use character positions instead of byte positions.
crates/pyo3/src/range.rs Updated Range::from method to accept UnicodePosition and adjusted position calculations to use character offsets.
crates/pyo3/src/unicode_position.rs Introduced Offset and UnicodePosition structs with methods for mapping between character and byte offsets in Unicode strings.
crates/pyo3/tests/test_range.py Updated test_unicode() function to correct the index comparison for the start of a range.
crates/pyo3/ast_grep_py/ast_grep_py.pyi Changed deleted_length attribute to end_position in Edit class.
crates/pyo3/tests/test_fix.py Added assertions related to edit positions in test_one_fix() and introduced a new test case test_change_unicode() for replacing identifiers with numeric values.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant SgNode
    participant UnicodePosition
    participant Edit
    User->>SgNode: Call range()
    SgNode->>UnicodePosition: Convert byte to char position
    UnicodePosition-->>SgNode: Return char positions
    SgNode-->>User: Return Range with char positions

    User->>SgNode: Call replace()
    SgNode->>UnicodePosition: Convert byte to char position
    UnicodePosition-->>SgNode: Return char positions
    SgNode->>Edit: Create Edit with char positions
    SgNode-->>User: Return Edit
Loading

Poem

In the land of code where bytes do dance,
Unicode joins, enhancing our stance.
Characters now in perfect array,
Edits precise, come what may.
With SgRoot and UnicodePosition in hand,
Our text manipulations are simply grand. 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f349273 and 066cb9b.

Files selected for processing (5)
  • crates/pyo3/src/lib.rs (2 hunks)
  • crates/pyo3/src/py_node.rs (2 hunks)
  • crates/pyo3/src/range.rs (3 hunks)
  • crates/pyo3/src/unicode_position.rs (1 hunks)
  • crates/pyo3/tests/test_range.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • crates/pyo3/tests/test_range.py
Additional comments not posted (9)
crates/pyo3/src/lib.rs (4)

5-5: Import of the new unicode_position module.

This import is necessary for the new functionality related to Unicode position handling.


13-13: Import of UnicodePosition from the unicode_position module.

This import is essential for using UnicodePosition in the SgRoot struct.


29-29: Addition of position field to SgRoot.

This field is crucial for storing Unicode position information, aligning with the PR's objectives to enhance Unicode handling.


36-42: Modification of SgRoot::new to initialize the position field.

The initialization of position using UnicodePosition::new(src) is correctly implemented and ensures that the Unicode position is set up when an SgRoot instance is created.

crates/pyo3/src/unicode_position.rs (1)

1-62: Introduction of Offset and UnicodePosition structs with methods for handling Unicode positions.

The implementation of these structs and their methods (char_to_byte and byte_to_char) is well-aligned with the PR's objectives to handle Unicode character positions accurately.

crates/pyo3/src/range.rs (2)

15-15: Update of the index field comment in Pos to indicate it represents a character offset.

This change clarifies the meaning of the index field, aligning it with the new functionality for accurate character offset handling.


101-109: Enhancements to Range::from to use UnicodePosition for calculating character offsets.

The modifications ensure that the Range calculations are now based on character offsets, which is crucial for accurate text processing in Unicode.

crates/pyo3/src/py_node.rs (2)

32-36: Modification of SgNode::range to use UnicodePosition.

This change integrates the UnicodePosition functionality into the range method, ensuring that ranges are calculated based on character offsets, which is essential for correct Unicode handling.


356-356: Documentation update for the Edit struct to specify that position represents the byte position.

The updated documentation clarifies the nature of the position field, aligning it with the internal changes and enhancing the understanding of the code.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 73.62%. Comparing base (f349273) to head (02a6a31).

Files Patch % Lines
crates/pyo3/src/range.rs 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1178      +/-   ##
==========================================
- Coverage   73.65%   73.62%   -0.03%     
==========================================
  Files          76       76              
  Lines        5223     5225       +2     
==========================================
  Hits         3847     3847              
- Misses       1376     1378       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
crates/pyo3/tests/test_fix.py (1)

Line range hint 1-1: Remove unused imports.

- from ast_grep_py import SgRoot, Range, Pos
+ from ast_grep_py import SgRoot
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 066cb9b and 6a5079c.

Files selected for processing (3)
  • crates/pyo3/ast_grep_py/ast_grep_py.pyi (1 hunks)
  • crates/pyo3/src/py_node.rs (3 hunks)
  • crates/pyo3/tests/test_fix.py (1 hunks)
Additional context used
Ruff
crates/pyo3/tests/test_fix.py

1-1: ast_grep_py.Range imported but unused


1-1: ast_grep_py.Pos imported but unused

Additional comments not posted (7)
crates/pyo3/tests/test_fix.py (2)

18-20: The assertions correctly validate the new behavior of the replace method.


18-20: The test effectively checks the application of multiple edits and their correct ordering.

crates/pyo3/ast_grep_py/ast_grep_py.pyi (1)

16-16: The addition of end_position aligns with the changes in the Rust implementation, ensuring consistency across the bindings.

crates/pyo3/src/py_node.rs (4)

32-36: The updated range method now correctly uses the UnicodePosition for more accurate range calculations.


246-256: The replace method has been correctly updated to use character positions instead of byte positions, aligning with the new Unicode handling.


263-275: The conversion of character positions to byte positions in commit_edits ensures that edits are applied correctly in the final string.


375-378: The updated Edit struct now correctly reflects the changes to use character positions, ensuring consistency with the Python bindings.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
crates/pyo3/tests/test_fix.py (1)

Line range hint 1-1: Remove unused imports to clean up the code.

- from ast_grep_py import SgRoot, Range, Pos
+ from ast_grep_py import SgRoot
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6a5079c and 96e39a7.

Files selected for processing (1)
  • crates/pyo3/tests/test_fix.py (2 hunks)
Additional context used
Ruff
crates/pyo3/tests/test_fix.py

1-1: ast_grep_py.Range imported but unused


1-1: ast_grep_py.Pos imported but unused

Additional comments not posted (3)
crates/pyo3/tests/test_fix.py (3)

18-20: The assertions correctly test the new position and end_position fields in the Edit struct.


33-34: The test correctly handles multiple edits on Unicode strings and verifies the expected output.


35-41: The test effectively verifies the replacement of identifiers with numeric values in Unicode strings.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 96e39a7 and 02a6a31.

Files selected for processing (1)
  • crates/pyo3/tests/test_fix.py (2 hunks)
Additional context used
Ruff
crates/pyo3/tests/test_fix.py

1-1: ast_grep_py.Range imported but unused


1-1: ast_grep_py.Pos imported but unused

Additional comments not posted (3)
crates/pyo3/tests/test_fix.py (3)

18-20: LGTM! The assertions correctly validate the new character position handling.


33-33: LGTM! The test ensures edits are applied correctly in a scenario involving Unicode characters.


41-41: LGTM! The test effectively checks the replacement functionality in strings with Unicode identifiers.

@HerringtonDarkholme HerringtonDarkholme added this pull request to the merge queue Jun 1, 2024
Merged via the queue into main with commit f53627a Jun 1, 2024
@HerringtonDarkholme HerringtonDarkholme deleted the napi branch June 1, 2024 07:10
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.

1 participant