feat: return char offset in pyo3 API#1178
Conversation
WalkthroughThe recent changes to the Changes
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
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
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 newunicode_positionmodule.This import is necessary for the new functionality related to Unicode position handling.
13-13: Import ofUnicodePositionfrom theunicode_positionmodule.This import is essential for using
UnicodePositionin theSgRootstruct.
29-29: Addition ofpositionfield toSgRoot.This field is crucial for storing Unicode position information, aligning with the PR's objectives to enhance Unicode handling.
36-42: Modification ofSgRoot::newto initialize thepositionfield.The initialization of
positionusingUnicodePosition::new(src)is correctly implemented and ensures that the Unicode position is set up when anSgRootinstance is created.crates/pyo3/src/unicode_position.rs (1)
1-62: Introduction ofOffsetandUnicodePositionstructs with methods for handling Unicode positions.The implementation of these structs and their methods (
char_to_byteandbyte_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 theindexfield comment inPosto indicate it represents a character offset.This change clarifies the meaning of the
indexfield, aligning it with the new functionality for accurate character offset handling.
101-109: Enhancements toRange::fromto useUnicodePositionfor calculating character offsets.The modifications ensure that the
Rangecalculations 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 ofSgNode::rangeto useUnicodePosition.This change integrates the
UnicodePositionfunctionality into therangemethod, ensuring that ranges are calculated based on character offsets, which is essential for correct Unicode handling.
356-356: Documentation update for theEditstruct to specify thatpositionrepresents the byte position.The updated documentation clarifies the nature of the
positionfield, aligning it with the internal changes and enhancing the understanding of the code.
Codecov ReportAttention: Patch coverage is
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. |
There was a problem hiding this comment.
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
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.Rangeimported but unused
1-1:
ast_grep_py.Posimported but unused
Additional comments not posted (7)
crates/pyo3/tests/test_fix.py (2)
18-20: The assertions correctly validate the new behavior of thereplacemethod.
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 ofend_positionaligns with the changes in the Rust implementation, ensuring consistency across the bindings.crates/pyo3/src/py_node.rs (4)
32-36: The updatedrangemethod now correctly uses theUnicodePositionfor more accurate range calculations.
246-256: Thereplacemethod 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 incommit_editsensures that edits are applied correctly in the final string.
375-378: The updatedEditstruct now correctly reflects the changes to use character positions, ensuring consistency with the Python bindings.
There was a problem hiding this comment.
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
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.Rangeimported but unused
1-1:
ast_grep_py.Posimported but unused
Additional comments not posted (3)
crates/pyo3/tests/test_fix.py (3)
18-20: The assertions correctly test the newpositionandend_positionfields in theEditstruct.
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.
There was a problem hiding this comment.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
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.Rangeimported but unused
1-1:
ast_grep_py.Posimported 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.
No description provided.