Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce Binding variant usages #132

Merged
merged 3 commits into from
Apr 2, 2024

Conversation

arendjr
Copy link
Contributor

@arendjr arendjr commented Apr 2, 2024

This PR further reduces the amount of usages of Binding::* variants, mostly by using helper functions.

Usages of Binding::* variants internal to the struct have been renamed to Self::*, so I can quickly filter them out to see which ones remain.

It looks like after this, the only remaining cases are construction of Binding::List and Binding::Empty. These cannot be abstracted, but that should not be a problem once we have an abstract Binding trait, since instances will still be created of the concrete type (probably MarzanoBinding then).

Summary by CodeRabbit

  • Refactor
    • Enhanced pattern matching and binding creation logic for improved efficiency and readability.
    • Optimized pattern resolution processes to enhance performance.
    • Streamlined text unparser for more accurate output generation.

Copy link
Contributor

coderabbitai bot commented Apr 2, 2024

Warning

Rate Limit Exceeded

@arendjr has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 16 minutes and 43 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 8321813 and dce29c7.

Walkthrough

The update refines the core architecture, focusing on the Binding enum and pattern matching functionalities. It introduces new creation methods for Binding, enhances pattern resolution, and streamlines the usage of ResolvedPattern across various modules. This overhaul simplifies code structure, improves readability, and optimizes pattern matching operations, ensuring a more intuitive and efficient codebase management.

Changes

Files Changes
.../src/binding.rs Updated Binding enum variants to use Self, added creation functions, and adjusted methods for better usability.
.../src/pattern/{after, before, ast_node, list_index, or, regex, resolved_pattern}.rs Enhanced pattern resolution logic, simplified ResolvedPattern usage, and refactored imports and method calls.
.../src/text_unparser.rs Updated apply_effects function to utilize as_filename() method on Binding.

🐰✨
In the realm of code, where patterns intertwine,
A rabbit hopped through, making everything fine.
With a flick and a hop, and a magical bind,
It refined all the patterns, leaving none behind.
Now the code flows like verse, oh so refined.
🌟📜🐇

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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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/coderabbit-overrides.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
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 Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dbce137 and a011cd3.
Files selected for processing (9)
  • crates/core/src/binding.rs (8 hunks)
  • crates/core/src/pattern/after.rs (2 hunks)
  • crates/core/src/pattern/ast_node.rs (3 hunks)
  • crates/core/src/pattern/before.rs (2 hunks)
  • crates/core/src/pattern/list_index.rs (2 hunks)
  • crates/core/src/pattern/or.rs (1 hunks)
  • crates/core/src/pattern/regex.rs (2 hunks)
  • crates/core/src/pattern/resolved_pattern.rs (8 hunks)
  • crates/core/src/text_unparser.rs (2 hunks)
Additional comments not posted (25)
crates/core/src/text_unparser.rs (1)

50-51: The change to use as_filename() method instead of direct pattern matching on Binding::FileName aligns well with the PR's objective to minimize direct usage of Binding::* variants. This approach enhances code maintainability by encapsulating the filename extraction logic within the Binding enum itself. Good job on this improvement.

crates/core/src/pattern/after.rs (1)

63-63: The refactoring in the After struct's resolve method to directly return ResolvedPattern from the next_non_trivia_node simplifies the logic and aligns with the PR's objective to reduce direct usage of Binding::* variants. This change enhances code readability and maintainability.

crates/core/src/pattern/before.rs (1)

63-63: The refactoring in the Before implementation to directly use ResolvedPattern::from_node for creating ResolvedPattern instances simplifies the logic and aligns with the PR's objective to reduce direct usage of Binding::* variants. This change enhances code readability and maintainability.

crates/core/src/pattern/or.rs (1)

80-80: The adjustment in the comparison logic for node types within the Or module's Matcher implementation is a positive change. It simplifies the logic and potentially improves performance by ensuring that only nodes with matching types are considered for pattern execution. This aligns well with the PR's objectives.

crates/core/src/pattern/ast_node.rs (2)

166-166: The refactoring in the Matcher implementation within ast_node.rs to directly use ResolvedPattern instances simplifies the logic and aligns with the PR's objective to reduce direct usage of Binding::* variants. This change enhances code readability and maintainability.


181-181: The use of ResolvedPattern::from_range for handling comment content is a clean and efficient way to create ResolvedPattern instances, further aligning with the PR's objectives. This approach simplifies the code and enhances maintainability.

crates/core/src/pattern/regex.rs (1)

203-203: The refactoring in the RegexPattern implementation to directly use ResolvedPattern::from_range for creating ResolvedPattern instances simplifies the logic and aligns with the PR's objective to reduce direct usage of Binding::* variants. This change enhances code readability and maintainability.

crates/core/src/pattern/list_index.rs (1)

143-145: The refactoring in ListIndex to use NodeWithSource::new() for creating ResolvedPattern instances directly from nodes simplifies the logic and aligns with the PR's objective to reduce direct usage of Binding::* variants. This change enhances code readability and maintainability.

crates/core/src/binding.rs (7)

83-92: The implementation of PartialEq for Binding has been updated to use Self instead of directly referencing the Binding enum. This change aligns with the PR's objective of minimizing direct enum variant usage and is a good practice for referring to enum variants within the enum itself. It enhances readability and maintainability.


310-324: The addition of from_constant, from_path, and from_range functions is a significant improvement. These functions encapsulate the creation of Binding instances, reducing direct variant usage and making the code more maintainable and readable. It's a good practice to provide such factory methods for enum variants, especially in cases where the enum is used extensively across the codebase.


351-358: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [354-368]

The update to the get_sexp method to use Self instead of Binding is consistent with the PR's goal. This change simplifies the code and makes it more consistent. However, it's important to ensure that all calls to get_sexp and related logic are updated accordingly to handle the new return types and logic.

Verification successful

The usage of get_sexp in pattern/log.rs correctly handles the updated return types and logic, as it properly matches against the method's Option<String> return type. This confirms that the changes to get_sexp are correctly accommodated in the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all calls to `get_sexp` handle the updated return types and logic correctly.
rg --type rust 'get_sexp\('

Length of output: 195



Script:

#!/bin/bash
# View the usage of `get_sexp` in pattern/log.rs to verify it handles the updated return types and logic correctly.
fd pattern/log.rs --exec cat {}

Length of output: 35



Script:

#!/bin/bash
# Directly display the usage of `get_sexp` in pattern/log.rs to ensure accurate verification.
rg --type rust 'get_sexp\(' crates/core/src/pattern/log.rs

Length of output: 121


365-381: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [375-421]

The position method's update to use Self instead of Binding is another example of aligning with the PR's objectives. This change, along with handling specific cases like Empty, Node, String, List, FileName, and ConstantRef, enhances the method's readability and maintainability. It's crucial to verify that all usages of position are compatible with these changes.

Verification successful

Given the broad set of results from the initial search, it's necessary to refine our approach to focus specifically on the usage of the position method within contexts that are likely to involve Binding objects. This will help verify compatibility with the changes made to the method. Let's proceed by examining calls to the position method within the core/src directory, particularly focusing on files that are likely to interact with Binding objects or their derivatives.


The examination of calls to the position method within the core/src directory suggests that the changes made to this method are compatible with its current usages. The method's update to enhance readability and maintainability by handling specific cases like Empty, Node, String, List, FileName, and ConstantRef aligns well with its usage across various contexts, including pattern matching and serialization. There's no indication that these changes adversely affect the existing calls to the position method, supporting the conclusion that the modifications are indeed compatible with the method's usages within the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all usages of `position` are compatible with the changes.
rg --type rust 'position\('

Length of output: 5900



Script:

#!/bin/bash
# Focus on calls to the `position` method within the `core/src` directory to verify compatibility with the changes.
rg --type rust 'position\(' crates/core/src/

Length of output: 2873


449-459: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [435-474]

The updates to the linearized_text method, including the use of Self and handling different Binding variants, are well-implemented. These changes contribute to the PR's goal of reducing direct enum variant usage and encapsulating functionality within methods. It's essential to ensure that the changes integrate well with the rest of the codebase, especially in terms of error handling and the return types.

Verification successful

The shell script output indicates that the linearized_text method is widely used across the codebase, suggesting that the changes to the method have been integrated into various parts of the system. Without specific errors or integration issues highlighted, and considering the method's broad usage, it's reasonable to conclude that the changes to linearized_text have been successfully integrated with the rest of the codebase. However, it's important to note that without a detailed analysis of each callsite and the changes made to the method, this conclusion is based on the assumption that widespread usage equates to successful integration.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify integration of `linearized_text` changes with the rest of the codebase.
rg --type rust 'linearized_text\('

Length of output: 1989


481-494: The text method's update to use Self and handle different Binding variants is consistent with the PR's objectives. This method's changes improve code readability and maintainability by encapsulating variant-specific logic. It's important to verify that these changes do not affect the method's callers negatively.


500-504: The source method's update to use Self instead of Binding and handle different Binding variants is a positive change. This method's changes align with the PR's goal of reducing direct enum variant usage and improving code readability. It's crucial to ensure that these changes integrate well with the rest of the codebase.

Verification successful

Given the widespread use of the source() method across various files in the codebase, as indicated by the script output, it's important to further verify the integration of the method's changes in a few specific instances. This will help ensure that the changes have been correctly applied and integrated without issues. Let's inspect the usage of the source() method in a couple of key files to get a more detailed understanding of how the changes have been integrated.


The inspection of specific instances where the source() method is called across different parts of the codebase confirms that the changes to the method have been correctly integrated. The method's updated signature is being used appropriately, and its return values are being handled correctly in various contexts. This indicates that the integration of the changes is successful and aligns with the PR's goal of reducing direct enum variant usage and improving code readability. The changes to the source() method and their integration with the rest of the codebase are approved.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify integration of `source` method changes with the rest of the codebase.
rg --type rust 'source\('

Length of output: 2455



Script:

#!/bin/bash
# Inspect specific instances of `source()` method usage in key files.
rg --type rust 'source\(' crates/core/src/binding.rs -A 3 -B 3
rg --type rust 'source\(' crates/core/src/pattern/step.rs -A 3 -B 3
rg --type rust 'source\(' crates/cli/src/lister.rs -A 3 -B 3

Length of output: 3701

crates/core/src/pattern/resolved_pattern.rs (10)

60-60: The usage of ResolvedPattern::from_path in the name method of the File enum simplifies the creation of ResolvedPattern instances from file paths. This change enhances code readability and maintainability.


71-73: The absolute_path method now utilizes ResolvedPattern::from_path for creating ResolvedPattern instances from absolute file paths. This is a positive change that reduces direct Binding::* variant usage and encapsulates the creation logic within helper functions.


83-83: In the body method, the introduction of ResolvedPattern::from_range for creating ResolvedPattern instances based on file ranges is a good practice. It simplifies the logic and improves code readability.


94-94: The binding method's use of ResolvedPattern::from_node for creating ResolvedPattern instances from NodeWithSource objects aligns with the PR's objectives to reduce direct Binding::* variant usage. This encapsulation within helper functions is commendable.


479-479: The introduction of the from_constant method in the ResolvedPattern enum is a positive change. It simplifies the creation of ResolvedPattern instances from constants, enhancing code readability and maintainability.


483-483: The from_node method simplifies the creation of ResolvedPattern instances from NodeWithSource objects. This change aligns with the PR's objectives and improves code readability.


487-487: The from_list method is a good addition, simplifying the creation of ResolvedPattern instances from list bindings. This encapsulation within a helper function reduces direct Binding::* variant usage.


491-492: The empty_field method provides a straightforward way to create ResolvedPattern instances for empty fields. This is a valuable addition that enhances code readability and maintainability.


494-496: The from_path method is a commendable addition, simplifying the creation of ResolvedPattern instances from file paths. This change aligns with the PR's objectives to reduce direct Binding::* variant usage.


498-499: The introduction of the from_range method for creating ResolvedPattern instances from ranges is a positive change. It encapsulates the creation logic within a helper function, improving code readability.

@arendjr arendjr changed the title Reduce Binding variant usages chore: reduce Binding variant usages Apr 2, 2024
Copy link
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 Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dcf6465 and 6edf1d2.
Files selected for processing (8)
  • crates/core/src/binding.rs (8 hunks)
  • crates/core/src/pattern/after.rs (2 hunks)
  • crates/core/src/pattern/ast_node.rs (3 hunks)
  • crates/core/src/pattern/before.rs (2 hunks)
  • crates/core/src/pattern/or.rs (1 hunks)
  • crates/core/src/pattern/regex.rs (2 hunks)
  • crates/core/src/pattern/resolved_pattern.rs (6 hunks)
  • crates/core/src/text_unparser.rs (2 hunks)
Files skipped from review as they are similar to previous changes (8)
  • crates/core/src/binding.rs
  • crates/core/src/pattern/after.rs
  • crates/core/src/pattern/ast_node.rs
  • crates/core/src/pattern/before.rs
  • crates/core/src/pattern/or.rs
  • crates/core/src/pattern/regex.rs
  • crates/core/src/pattern/resolved_pattern.rs
  • crates/core/src/text_unparser.rs

Copy link
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 Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6edf1d2 and 8321813.
Files selected for processing (1)
  • crates/core/src/pattern/resolved_pattern.rs (7 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/core/src/pattern/resolved_pattern.rs

if let (Binding::Node(_src, binding_node), Pattern::ASTNode(node_pattern)) =
(binding_vector.last().unwrap(), p)
if let (Some(binding_node), Pattern::ASTNode(node_pattern)) =
(binding_vector.last().and_then(Binding::as_node), p)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not documented but the binding vector in a resolved pattern should be wrapped in a non-empty-vec struct as the vector is always created with an initial element, and only ever appended to, so last shouldn't really be an optional here .

@ilevyor
Copy link
Contributor

ilevyor commented Apr 2, 2024

Can you just fix the clippy complaints?

@arendjr
Copy link
Contributor Author

arendjr commented Apr 2, 2024

Oh, that's odd. The fix for the Clippy seems pushed, and locally it's indeed green. Maybe the CI incorrectly cached the result?

@ilevyor ilevyor merged commit 3581187 into getgrit:main Apr 2, 2024
3 of 5 checks passed
ilevyor added a commit that referenced this pull request Apr 2, 2024
arendjr added a commit to arendjr/gritql that referenced this pull request Apr 2, 2024
This pull request was closed.
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.

2 participants