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

feat: Context trait #66

Merged
merged 10 commits into from
Mar 26, 2024
Merged

feat: Context trait #66

merged 10 commits into from
Mar 26, 2024

Conversation

arendjr
Copy link
Contributor

@arendjr arendjr commented Mar 24, 2024

This PR renames the Context struct to MarzanoContext and introduces a new Context trait to take its place. While this PR doesn't accomplish much on its own, the end goal is to be able to extract the patterns and matching engine from marzano-core and put them into a crate of their own (grit-core-patterns?). This crate could in turn be used by Biome for its plugin engine.

There is one change in this PR that might be slightly controversial: I've refactored builtin_functions.rs a bit (see the first commit) so that context is no longer passed to built-in functions. This creates no issues now, but could possibly create limitations with future functions. But the reason I did this is that otherwise the Builtins type would need to have a generic argument for the context, and because the context can return the built-ins, this created a recursive dependency. This got me into a fight with the borrow-checker I was not able to resolve otherwise.

If this PR is approved, I will continue decoupling stuff until I'm able to extract the grit-core-patterns crate without having dependencies on Tree-Sitter or other Marzano crates. Please let me know if there are concerns with this strategy.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced a new Context trait with comprehensive methods for enhanced pattern, predicate, and function handling.
    • Added new implementations and adjustments for MarzanoContext to align with the Context trait.
  • Refactor
    • Reorganized the module structure for better clarity and usability.
    • Promoted certain modules to public to increase their accessibility.
    • Shifted towards using trait objects and trait bounds for more flexible context handling across various pattern-related functionalities.
    • Improved parameter handling in suppress.rs and text_unparser.rs for more efficient string processing.

Copy link
Contributor

coderabbitai bot commented Mar 24, 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 17 minutes and 52 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 14083df and b08fa85.

Walkthrough

The project underwent a structural refinement, introducing a new context module and redefining the Context trait to centralize access to various components like patterns, predicates, and built-in functions. This overhaul simplifies the module hierarchy and enhances flexibility in context handling across the board. The adjustment of function signatures and parameter types, notably to trait objects and references, streamlines interactions and efficiency, particularly in string handling.

Changes

File(s) Summary
crates/core/src/lib.rs Introduced context module; reorganized module structure.
crates/core/src/context.rs Added Context trait with comprehensive access methods.
crates/core/src/pattern.rs Renamed Context to MarzanoContext; updated trait implementations; made modules public.
crates/core/src/pattern/... (multiple files) Standardized use of trait objects for Context; updated imports and method calls.
crates/core/src/pattern/test.rs Introduced testing utilities for Context.
crates/core/src/suppress.rs, crates/core/src/text_unparser.rs Changed current_name parameter type from Option<String> to Option<&str>.

🐇✨
In the realm of code, where logic intertwines,
A rabbit hopped through, leaving changes behind.
With a flick and a twist of its tiny paw,
Structured the chaos, without a flaw.
Now the code flows, like a river so bright,
Thanks to the rabbit, who danced through the night.
🌟📜🐾

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-tests 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 tests 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 tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 73e866f and b56b112.
Files selected for processing (64)
  • crates/core/src/context.rs (1 hunks)
  • crates/core/src/lib.rs (1 hunks)
  • crates/core/src/pattern.rs (7 hunks)
  • crates/core/src/pattern/accessor.rs (3 hunks)
  • crates/core/src/pattern/accumulate.rs (6 hunks)
  • crates/core/src/pattern/add.rs (4 hunks)
  • crates/core/src/pattern/after.rs (3 hunks)
  • crates/core/src/pattern/and.rs (3 hunks)
  • crates/core/src/pattern/any.rs (3 hunks)
  • crates/core/src/pattern/assignment.rs (3 hunks)
  • crates/core/src/pattern/ast_node.rs (4 hunks)
  • crates/core/src/pattern/before.rs (3 hunks)
  • crates/core/src/pattern/boolean_constant.rs (2 hunks)
  • crates/core/src/pattern/bubble.rs (2 hunks)
  • crates/core/src/pattern/built_in_functions.rs (17 hunks)
  • crates/core/src/pattern/call.rs (4 hunks)
  • crates/core/src/pattern/code_snippet.rs (2 hunks)
  • crates/core/src/pattern/contains.rs (3 hunks)
  • crates/core/src/pattern/divide.rs (4 hunks)
  • crates/core/src/pattern/dynamic_snippet.rs (4 hunks)
  • crates/core/src/pattern/equal.rs (2 hunks)
  • crates/core/src/pattern/every.rs (2 hunks)
  • crates/core/src/pattern/file_pattern.rs (2 hunks)
  • crates/core/src/pattern/files.rs (2 hunks)
  • crates/core/src/pattern/float_constant.rs (2 hunks)
  • crates/core/src/pattern/function_definition.rs (5 hunks)
  • crates/core/src/pattern/functions.rs (5 hunks)
  • crates/core/src/pattern/if.rs (3 hunks)
  • crates/core/src/pattern/includes.rs (2 hunks)
  • crates/core/src/pattern/int_constant.rs (2 hunks)
  • crates/core/src/pattern/like.rs (3 hunks)
  • crates/core/src/pattern/limit.rs (2 hunks)
  • crates/core/src/pattern/list.rs (3 hunks)
  • crates/core/src/pattern/list_index.rs (3 hunks)
  • crates/core/src/pattern/log.rs (4 hunks)
  • crates/core/src/pattern/map.rs (2 hunks)
  • crates/core/src/pattern/match.rs (2 hunks)
  • crates/core/src/pattern/maybe.rs (4 hunks)
  • crates/core/src/pattern/modulo.rs (4 hunks)
  • crates/core/src/pattern/multiply.rs (4 hunks)
  • crates/core/src/pattern/not.rs (3 hunks)
  • crates/core/src/pattern/or.rs (3 hunks)
  • crates/core/src/pattern/pattern_definition.rs (2 hunks)
  • crates/core/src/pattern/patterns.rs (5 hunks)
  • crates/core/src/pattern/predicate_definition.rs (2 hunks)
  • crates/core/src/pattern/predicate_return.rs (2 hunks)
  • crates/core/src/pattern/predicates.rs (2 hunks)
  • crates/core/src/pattern/range.rs (2 hunks)
  • crates/core/src/pattern/regex.rs (2 hunks)
  • crates/core/src/pattern/resolved_pattern.rs (8 hunks)
  • crates/core/src/pattern/rewrite.rs (4 hunks)
  • crates/core/src/pattern/sequential.rs (3 hunks)
  • crates/core/src/pattern/some.rs (2 hunks)
  • crates/core/src/pattern/state.rs (1 hunks)
  • crates/core/src/pattern/step.rs (6 hunks)
  • crates/core/src/pattern/string_constant.rs (3 hunks)
  • crates/core/src/pattern/subtract.rs (4 hunks)
  • crates/core/src/pattern/test.rs (1 hunks)
  • crates/core/src/pattern/undefined.rs (2 hunks)
  • crates/core/src/pattern/variable.rs (2 hunks)
  • crates/core/src/pattern/where.rs (2 hunks)
  • crates/core/src/pattern/within.rs (2 hunks)
  • crates/core/src/suppress.rs (3 hunks)
  • crates/core/src/text_unparser.rs (1 hunks)
Additional comments: 126
crates/core/src/pattern/test.rs (2)
  • 1-3: The introduction of the Context trait is a significant architectural change. It abstracts context handling, allowing for more flexible implementations. Ensure that all components that previously relied on the concrete Context struct are updated to work with this trait.
  • 5-9: The BuiltInFunction struct and the Func type alias demonstrate how the Context trait can be utilized in function signatures. This is a good example of leveraging trait objects for greater flexibility. However, consider adding documentation comments to explain the purpose and usage of these constructs, especially since they play a crucial role in the new architecture.
crates/core/src/pattern/undefined.rs (1)
  • 15-15: The modification of the _context parameter to use &'a impl Context<'a> instead of a concrete type is in line with the PR's goal of abstracting context handling. This change enhances flexibility and modularity. Ensure that all usages of the Undefined struct's execute method are updated accordingly.
crates/core/src/lib.rs (1)
  • 4-4: The addition of the context module is a key part of the PR's architectural changes. It centralizes the definition and implementation of the new Context trait, facilitating easier maintenance and future enhancements. Ensure that the module is properly documented to aid in understanding its role within the system.
crates/core/src/pattern/int_constant.rs (1)
  • 39-39: Adjusting the _context parameter to use &'a impl Context<'a> in the Matcher trait implementation for IntConstant aligns with the PR's architectural changes. This enhances flexibility in context handling. Ensure consistency in this approach across all Matcher implementations.
crates/core/src/pattern/float_constant.rs (1)
  • 39-39: The adjustment of the _context parameter to use &'a impl Context<'a> in the Matcher trait implementation for FloatConstant is consistent with the PR's architectural changes. This approach enhances modularity and should be consistently applied across all Matcher implementations.
crates/core/src/pattern/files.rs (1)
  • 27-27: The use of &'a impl Context<'a> in the Matcher trait implementation for Files is a good example of the PR's efforts to abstract context handling. This change promotes flexibility and should be consistently applied across all relevant implementations.
crates/core/src/pattern/file_pattern.rs (1)
  • 27-27: The adjustment of the context parameter to use &'a impl Context<'a> in the Matcher trait implementation for FilePattern aligns with the PR's goal of abstracting context handling. This change enhances modularity and should be consistently applied across all Matcher implementations.
crates/core/src/pattern/predicate_return.rs (2)
  • 9-9: The import of the Context trait aligns with the PR's objectives of introducing a new Context trait for more flexible context handling.
  • 55-55: The update to the execute_func method signature, using a trait object for the context parameter, enhances flexibility and modularity in context handling. This change aligns with the PR's objectives.
crates/core/src/pattern/includes.rs (1)
  • 63-63: The modification of the context parameter type in the execute method to use a trait object (&'a impl Context<'a>) aligns with the PR's goal of abstracting context handling. This change promotes flexibility and modularity.
crates/core/src/text_unparser.rs (1)
  • 28-28: Changing the current_name parameter type to Option<&str> in the apply_effects function is a good practice, as it reduces unnecessary string cloning and memory allocations, potentially improving performance.
crates/core/src/pattern/equal.rs (1)
  • 79-79: The update to the execute_func method signature in the Equal struct, using a trait object for the context parameter, aligns with the PR's objectives of abstracting context handling. This enhances flexibility and modularity.
crates/core/src/pattern/functions.rs (1)
  • 22-22: The changes to use impl Trait for the context parameter in various trait method signatures, along with adjustments to method calls, align with the PR's objectives of enhancing flexibility and modularity in context handling.

Also applies to: 37-37, 52-52, 89-89

crates/core/src/pattern/add.rs (1)
  • 69-69: The updates to the context parameter type in the call and evaluate functions to use impl Context<'a> align with the PR's objectives of abstracting context handling. This enhances flexibility and modularity.

Also applies to: 79-79

crates/core/src/pattern/divide.rs (1)
  • 69-69: The updates to the context parameter type in the call and evaluate functions to use impl Context<'a> align with the PR's objectives of abstracting context handling. This enhances flexibility and modularity.

Also applies to: 79-79

crates/core/src/pattern/subtract.rs (1)
  • 68-68: The updates to the context parameter type in the call and evaluate functions to use impl Context<'a> align with the PR's objectives of abstracting context handling. This enhances flexibility and modularity.

Also applies to: 78-78

crates/core/src/pattern/multiply.rs (2)
  • 8-8: The import of Context as a trait is aligned with the PR's objective to introduce a Context trait for more flexible context handling. This change supports the architectural shift towards using trait objects for context, enhancing modularity and flexibility.
  • 68-68: The update to method signatures to accept a reference to an impl Context<'a> instead of a concrete Context struct is consistent with the PR's goal of decoupling and abstracting context handling. This change allows for more versatile implementations of the Context trait, facilitating easier modifications and extensions in the future.

Also applies to: 78-78, 99-99

crates/core/src/pattern/modulo.rs (2)
  • 8-8: The inclusion of the Context trait in the imports supports the PR's architectural changes towards a more flexible and abstracted context handling mechanism. This change is crucial for the broader goal of decoupling and modularizing the system.
  • 68-68: Updating the method signatures to use &'a impl Context<'a> instead of a concrete Context struct aligns with the PR's objectives of enhancing modularity and flexibility in context management. This approach allows for the Context trait to be implemented by various structs, supporting the system's evolving needs.

Also applies to: 78-78, 101-101

crates/core/src/pattern/like.rs (2)
  • 9-9: Adding the Context trait to the imports is in line with the PR's direction of abstracting context handling. This change facilitates the use of the Context trait across different parts of the system, promoting a more flexible and modular architecture.
  • 81-81: The modification of the Matcher trait implementation to use &'a impl Context<'a> enhances the system's flexibility in handling context. This change is consistent with the PR's objectives of abstracting and decoupling context management, allowing for a wider range of context implementations.

Also applies to: 99-99

crates/core/src/pattern/map.rs (2)
  • 7-7: The import of the Context trait aligns with the PR's goal of abstracting context handling through traits. This change supports the architectural shift towards more flexible and modular context management.
  • 78-78: Updating the Matcher trait implementation for GritMap to use &'a impl Context<'a> is consistent with the PR's objectives. This change allows for more versatile context implementations, supporting the system's modularity and flexibility.
crates/core/src/pattern/limit.rs (2)
  • 8-8: Importing the Context trait is in line with the PR's direction towards a more abstracted and flexible context handling mechanism. This change is essential for the broader architectural adjustments being made across the system.
  • 78-78: The update to the Matcher trait implementation for Limit to use &'a impl Context<'a> and the introduction of context.ignore_limit_pattern() are significant. These changes not only align with the PR's objectives of abstracting context handling but also introduce a functional adjustment to how limit patterns are handled. This approach enhances the system's flexibility and modularity by abstracting the logic for checking limit pattern conditions.

Also applies to: 81-81

crates/core/src/pattern/sequential.rs (2)
  • 6-6: The import of the Context trait and the Step struct from crate::context and crate::pattern respectively, aligns with the PR's objectives of enhancing modularity and flexibility in context management. This change supports the architectural shift towards using trait objects for context.
  • 89-89: Updating the Matcher trait implementation for Sequential to use &'a impl Context<'a> is consistent with the PR's goal of abstracting and decoupling context handling. This change allows for more versatile implementations of the Context trait, facilitating easier modifications and extensions in the future.
crates/core/src/pattern/predicate_definition.rs (2)
  • 10-10: Importing the Context trait is in line with the PR's direction of abstracting context handling through traits. This change supports the architectural shift towards more flexible and modular context management.
  • 101-101: The update to the call method signature to use &'a impl Context<'a> instead of a concrete Context struct aligns with the PR's objectives of enhancing modularity and flexibility in context management. This approach allows for the Context trait to be implemented by various structs, supporting the system's evolving needs.
crates/core/src/pattern/assignment.rs (2)
  • 10-10: The import of the Context trait aligns with the PR's goal of abstracting context handling through traits. This change supports the architectural shift towards more flexible and modular context management.
  • 82-82: Updating the Matcher and Evaluator trait implementations for Assignment to use &'a impl Context<'a> is consistent with the PR's objectives. This change allows for more versatile context implementations, supporting the system's modularity and flexibility.

Also applies to: 95-95

crates/core/src/pattern/boolean_constant.rs (2)
  • 7-7: The import of the Context trait aligns with the PR's objective to introduce a more abstract and flexible context handling mechanism. Good adjustment.
  • 44-44: The update to use a generic context type (impl Context) in the Matcher trait implementation for BooleanConstant is a positive change towards more flexible and modular code. Well done.
crates/core/src/pattern/within.rs (1)
  • 63-63: Updating the Matcher trait implementation to use a trait object for the context parameter enhances flexibility and aligns with the PR's goal of abstracting context handling. Good job.
crates/core/src/pattern/maybe.rs (2)
  • 56-56: The use of a trait object for the context parameter in the Matcher trait implementation for Maybe is a solid move towards more abstract and flexible context handling.
  • 111-111: Similarly, using a trait object for the context parameter in the Evaluator trait implementation for PrMaybe aligns with the PR's goals and enhances code flexibility.
crates/core/src/pattern/pattern_definition.rs (1)
  • 102-102: Adopting a trait object for the context parameter in the PatternDefinition::call method is a commendable change that aligns with the PR's objectives of abstracting context handling.
crates/core/src/pattern/string_constant.rs (2)
  • 48-48: Using a trait object for the context parameter in the Matcher trait implementation for StringConstant is a positive step towards more flexible and abstract context handling.
  • 92-92: Similarly, the update in the Matcher trait implementation for AstLeafNode to use a trait object for the context parameter aligns with the PR's objectives and enhances code modularity.
crates/core/src/pattern/bubble.rs (1)
  • 120-120: The update to use a trait object for the context parameter in the Matcher trait implementation for Bubble is a commendable change that aligns with the PR's objectives of abstracting context handling.
crates/core/src/pattern/range.rs (1)
  • 92-92: Adopting a trait object for the context parameter in the Matcher trait implementation for Range is a positive step towards more flexible and abstract context handling.
crates/core/src/suppress.rs (2)
  • 14-14: Changing the parameter type for current_name from Option<String> to Option<&str> in is_binding_suppressed is a subtle optimization that avoids unnecessary allocations. Good improvement.
  • 50-50: Similarly, the adjustment in is_suppress_comment to use Option<&str> for current_name enhances performance by reducing allocations. Well done.
crates/core/src/pattern/and.rs (1)
  • 8-10: The import of Context has been updated from super to crate::context, and the function signatures for Matcher and Evaluator implementations now use a trait bound impl Context<'a> instead of a concrete type. This change aligns with the PR's objective to introduce a Context trait for more flexible context management. However, ensure that all instances where Context is used have been updated to accommodate this change.
crates/core/src/pattern/every.rs (1)
  • 6-9: The import statements have been reordered, and the function signatures now correctly use the Context trait. This modification supports the PR's goal of decoupling and abstracting context handling. It's important to verify that these changes are consistently applied across all relevant parts of the codebase to ensure compatibility and maintainability.
crates/core/src/pattern/after.rs (1)
  • 7-9: The changes in import statements and the update to function signatures to use the Context trait are consistent with the PR's objectives. These adjustments facilitate a more flexible and abstract approach to context management. It's crucial to ensure that the rest of the codebase is aligned with these changes to avoid any integration issues.
crates/core/src/pattern/before.rs (1)
  • 7-9: The adjustments to import statements and the update to function signatures to use the Context trait in before.rs align with the overarching goal of the PR to abstract context handling. These changes contribute to a more modular and maintainable codebase. Ensure that these modifications are consistently applied and tested across the entire project.
crates/core/src/pattern/not.rs (1)
  • 9-11: The update to use a trait bound impl Context<'a> in function signatures and the reorganization of imports in not.rs are in line with the PR's objectives to enhance flexibility and modularity in context management. These changes are crucial for resolving the recursive dependency issue mentioned in the PR description. It's important to ensure that these changes do not introduce any unintended side effects in the codebase.
crates/core/src/pattern/any.rs (1)
  • 8-10: The modifications in any.rs, including the reorganization of imports and the update to function signatures to use the Context trait, support the PR's goal of abstracting context handling. These changes contribute to the system's modularity and maintainability. It's essential to verify that these updates are thoroughly tested and consistent across the codebase.
crates/core/src/pattern/some.rs (1)
  • 4-11: The update to use a trait bound impl Context<'a> in the Matcher implementation within some.rs aligns with the PR's objectives to introduce a more flexible and abstract context management system. This change, along with the reorganization of imports, contributes to the decoupling efforts and enhances the codebase's modularity. Ensure that these changes are consistently applied and tested throughout the project.
crates/core/src/pattern/or.rs (1)
  • 8-10: The changes in or.rs, including the reorganization of imports and the update to function signatures to use the Context trait, are consistent with the PR's goal of enhancing context management flexibility. These modifications contribute to the system's overall modularity and maintainability. It's crucial to ensure that these changes are well-integrated and tested across the entire codebase.
crates/core/src/pattern/log.rs (4)
  • 9-9: The import statement has been reordered and now includes Context as a trait object. This change aligns with the PR's objective to introduce a Context trait for better abstraction and flexibility.
  • 35-35: The function signature for add_log now accepts a reference to an impl Context trait object instead of a concrete Context type. This modification is consistent with the PR's goal to utilize trait objects for more versatile context handling.
  • 129-129: Similarly, the execute method in the Matcher implementation for Log has been updated to accept a trait object reference for Context. This change supports the PR's architectural shift towards using trait objects for context management.
  • 140-140: The execute_func method in the Evaluator implementation for Log also reflects the PR's direction by accepting a trait object reference for Context. This adjustment is part of the broader effort to decouple and abstract context handling.
crates/core/src/pattern/dynamic_snippet.rs (3)
  • 10-10: The import statement has been modified to include Context directly, which simplifies the usage of the Context trait within this file. This change is part of the PR's effort to streamline context handling.
  • 55-55: The text method's signature now uses impl Context for the context parameter, aligning with the PR's objective to leverage trait objects for more flexible context management.
  • 74-74: The execute method in the Matcher implementation for DynamicPattern has been updated to accept a trait object reference for Context, consistent with the PR's architectural changes.
crates/core/src/pattern/if.rs (3)
  • 10-10: The import statement for Context has been adjusted, which is part of the PR's broader initiative to refactor context handling by introducing a Context trait.
  • 96-96: The execute method in the Matcher implementation for If now takes a trait reference for Context instead of a struct reference, aligning with the PR's goal to abstract context handling through trait objects.
  • 185-185: Similarly, the execute_func method in the Evaluator implementation for PrIf has been updated to accept a trait object reference for Context, consistent with the PR's architectural changes towards more flexible context management.
crates/core/src/pattern/match.rs (2)
  • 10-10: The import statement has been reordered and updated to include Context, which is part of the PR's efforts to refactor context handling by leveraging a Context trait.
  • 74-74: The execute_func method in the Evaluator implementation for Match now uses a trait object reference for Context, aligning with the PR's objective to abstract context handling through trait objects.
crates/core/src/pattern/where.rs (2)
  • 13-13: The import statement for Context has been adjusted, reflecting the PR's broader initiative to refactor context handling by introducing a Context trait for better abstraction.
  • 158-158: The execute method in the Matcher implementation for Where now accepts a trait object reference for Context, consistent with the PR's goal to utilize trait objects for more versatile context handling.
crates/core/src/pattern/accessor.rs (2)
  • 3-3: The import statement now includes Context, aligning with the PR's objective to refactor context handling by leveraging a Context trait.
  • 176-176: The execute method in the Matcher implementation for Accessor has been updated to accept a trait object reference for Context, consistent with the PR's architectural changes towards more flexible context management.
crates/core/src/pattern/list.rs (3)
  • 9-9: The import statement has been reorganized to include Context, which is part of the PR's efforts to refactor context handling by introducing a Context trait.
  • 119-119: The execute method in the Matcher implementation for List now uses a trait object reference for Context, aligning with the PR's objective to abstract context handling through trait objects.
  • 158-158: The execute_assoc function has been updated to accept a trait object reference for Context, consistent with the PR's architectural changes towards more flexible context management.
crates/core/src/pattern/ast_node.rs (2)
  • 1-1: The import statement now includes Context, reflecting the PR's broader initiative to refactor context handling by leveraging a Context trait for better abstraction.
  • 155-155: The execute method in the Matcher implementation for ASTNode has been updated to use a trait object reference for Context, aligning with the PR's objective to abstract context handling through trait objects.
crates/core/src/pattern/regex.rs (4)
  • 7-7: The import of State is added. Ensure that State is utilized within the file, otherwise, it might be an unnecessary import.
  • 9-9: The addition of imports from crate::{binding::Binding, context::Context}; is noted. Verify that these imports are necessary and correctly used within the file.
  • 13-13: The addition of marzano_language::{language::Language, target_language::TargetLanguage}; imports suggests an expansion in language processing capabilities. Ensure these are appropriately integrated into the regex pattern handling.
  • 150-150: The signature change to accept a trait object context: &'a impl Context<'a>, in the execute method of Matcher for RegexPattern is a significant improvement. It enhances flexibility by allowing any type that implements the Context trait to be passed in, adhering to the Dependency Inversion Principle.
crates/core/src/pattern/predicates.rs (2)
  • 22-22: The addition of use crate::context::Context; is appropriate given the changes in the file to accommodate the new Context trait. This ensures that the file has access to the necessary context-related functionality.
  • 231-231: Changing the context parameter type in the execute_func method from &Context<'a> to &'a impl Context<'a> is a good practice. It allows for more flexible context implementations to be used, aligning with the Dependency Inversion Principle.
crates/core/src/pattern/function_definition.rs (5)
  • 1-1: The addition of use crate::{binding::Constant, context::Context}; is necessary for the changes in this file, particularly the use of the Context trait. This ensures that the necessary types are available for use in the function definitions.
  • 25-25: The change in the call method signature to use context: &'a impl Context<'a>, instead of a concrete Context type is a positive change. It increases the flexibility and reusability of the FunctionDefinition trait by allowing any type that implements Context to be used.
  • 115-115: The same change in the call method signature for GritFunctionDefinition as noted earlier. This consistency across different implementations of the FunctionDefinition trait is good for maintainability and flexibility.
  • 205-205: The conditional compilation directive #[cfg(not(feature = "external_functions_common"))] is used to provide a fallback implementation of the call method when external functions are not enabled. This is a good practice for feature toggling and ensuring the codebase can adapt to different build configurations.
  • 215-215: The implementation of the call method under the #[cfg(feature = "external_functions_common")] directive demonstrates a thoughtful approach to handling external function calls, including error handling and type conversions. This detailed implementation ensures robustness and flexibility in function execution.
crates/core/src/pattern/accumulate.rs (4)
  • 15-15: The addition of State import is appropriate given the usage of State in the file. This ensures that the necessary functionality related to state management is accessible.
  • 18-18: The addition of use crate::context::Context; is necessary for the changes in this file to accommodate the new Context trait. This ensures that the file has access to the necessary context-related functionality.
  • 147-147: Changing the context parameter type in the execute method from &Context<'a> to &'a impl Context<'a> is a good practice. It allows for more flexible context implementations to be used, aligning with the Dependency Inversion Principle.
  • 225-225: The same change in the execute_func method signature to use context: &'a impl Context<'a>, instead of a concrete Context type is a positive change. It increases the flexibility and reusability of the Evaluator trait by allowing any type that implements Context to be used.
crates/core/src/pattern/state.rs (1)
  • 240-240: The change in the bindings_history_to_ranges function to accept current_name as an Option<&str> instead of &Option<String> is a subtle but meaningful improvement. It simplifies the handling of optional strings and potentially reduces unnecessary allocations.
crates/core/src/pattern/list_index.rs (2)
  • 11-13: The reordering and addition of imports, including use crate::context::Context;, are appropriate for the changes in this file. This ensures that the necessary types and functions are available for use in the list index handling.
  • 237-237: Changing the context parameter type in the execute method from &Context<'a> to &'a impl Context<'a> is a good practice. It allows for more flexible context implementations to be used, aligning with the Dependency Inversion Principle.
crates/core/src/pattern/rewrite.rs (4)
  • 12-12: The import statement for Context has been updated to reflect the introduction of the Context trait. This change aligns with the PR objectives of enhancing modularity and flexibility in context management.
  • 183-183: The function signature of execute_generalized has been modified to accept a reference to an implementation of the Context trait instead of a struct. This change is crucial for achieving the desired abstraction and flexibility in handling context. However, it's important to ensure that all implementations of the Context trait provide the necessary functionality expected by this method.
  • 260-260: The function signature of execute in the Matcher implementation for Rewrite has been updated to use the Context trait. This change is consistent with the PR's goal of decoupling and abstracting context handling. Ensure that the execute method's logic is compatible with all potential implementations of the Context trait.
  • 271-271: The function signature of execute_func in the Evaluator implementation for Rewrite has been updated to use the Context trait. This change supports the PR's objectives by abstracting context handling. It's essential to verify that the execute_func method functions correctly with all implementations of the Context trait.
crates/core/src/pattern/contains.rs (3)
  • 1-1: The import statement for Context has been updated to reflect the introduction of the Context trait. This change is in line with the PR's objectives of enhancing modularity and flexibility in context management.
  • 78-78: The function signature of execute_until has been modified to accept a reference to an implementation of the Context trait instead of a struct. This change supports the PR's goal of abstracting context handling. Ensure that the execute_until method's logic is compatible with all potential implementations of the Context trait.
  • 134-134: The function signature of execute in the Matcher implementation for Contains has been updated to use the Context trait. This change aligns with the PR's objectives by decoupling and abstracting context handling. It's important to verify that the execute method functions correctly with all implementations of the Context trait.
crates/core/src/pattern/step.rs (10)
  • 15-15: The import of the Context trait aligns with the PR's objective to introduce a new Context trait for more flexible context handling. This change is foundational and supports the subsequent modifications in the file.
  • 193-193: Changing the type of the context parameter in the execute method to &'a impl Context<'a> is a significant architectural change. It replaces the concrete type with a trait, allowing for more flexible implementations of the context. This change is crucial for achieving the PR's goal of enhancing modularity and flexibility in context management.
  • 197-197: Updating the method call to context.language().get_ts_language() with parentheses correctly adapts to the new Context trait's method signature. This change ensures that the code remains functional after the introduction of the trait.
  • 224-224: The method call state.bindings_history_to_ranges(context.language(), context.name()) has been updated to use the new Context trait methods. This change is consistent with the PR's objectives and demonstrates the application of the trait's methods in practice.
  • 261-262: The method calls context.language() and context.name() have been correctly updated to match the new Context trait's method signatures. This ensures that the code remains functional and aligns with the PR's architectural changes.
  • 267-267: The method call get_orphaned_ranges(&tree, &new_src, context.language()) correctly uses the Context trait's language method. This change is consistent with the PR's objectives and demonstrates the flexibility provided by the Context trait.
  • 283-283: The method call context.language() within the FileOwner::new constructor correctly adapts to the new Context trait's method signature. This ensures that the code remains functional after the introduction of the trait.
  • 286-286: The method call context.files().push(owned_file) demonstrates the use of the Context trait's files method. This change aligns with the PR's goal of enhancing modularity and flexibility in context management.
  • 289-289: The method call context.files().last().unwrap() within the state.files.push_revision call correctly uses the Context trait's files method. This ensures that the code remains functional and aligns with the PR's architectural changes.
  • 302-304: The method calls context.language(), context.files().push(owned_file), and state.files.push_new_file(context.files().last().unwrap()) correctly adapt to the new Context trait's method signatures. These changes are consistent with the PR's objectives and demonstrate the flexibility provided by the Context trait.
crates/core/src/pattern/built_in_functions.rs (7)
  • 3-3: The import of context::Context aligns with the PR's objective to introduce a Context trait, facilitating more flexible context management.
  • 62-62: Changing the function signature to accept &'a impl Context<'a> instead of a concrete Context type is a positive change. It increases flexibility and decouples the function from a specific context implementation, aligning with the PR's objectives.
  • 79-79: The modification of the F type to accept a simplified argument list is a good practice. It simplifies the function signatures and makes the code more readable and maintainable.
  • 92-92: Updating function signatures to accept Vec<Option<ResolvedPattern<'a>>> instead of &'a [Option<Pattern>] is a significant change. This adjustment is necessary due to the introduction of the Context trait and the refactoring of built-in functions. It simplifies the handling of patterns and enhances the flexibility of function arguments.
  • 119-119: The use of &'a impl Context<'a> in the call method of BuiltIns struct is consistent with the changes made throughout the file. It demonstrates the application of the Context trait, promoting a more abstract and flexible approach to context management.
  • 181-181: The changes in argument types for built-in functions, such as resolve_path_fn, to accept Vec<Option<ResolvedPattern<'a>>> instead of the previous types, are in line with the PR's objectives. These changes are necessary for the new context handling mechanism and improve the code's consistency.
  • 204-204: The simplification of function implementations, as seen in capitalize_fn, by directly working with the provided arguments and removing redundant handling, enhances code readability and maintainability. This change is part of the broader refactoring effort and aligns with best practices.
crates/core/src/pattern.rs (3)
  • 28-28: Promoting modules such as function_definition to be public (pub) is a significant change. This adjustment likely supports the broader architectural changes by making these modules accessible from other parts of the codebase. Ensure that this change aligns with the intended design and does not inadvertently expose internal details that should remain encapsulated.
Verification successful

The search results indicate extensive references to function_definition across various parts of the codebase, including within crates/core/src/ where it is used in multiple files such as context.rs, test.rs, functions.rs, function_definition.rs, call.rs, and compiler.rs. These references are not only in the context of module imports but also in the usage of types, functions, and structures related to function_definition. This demonstrates that the module function_definition being made public is indeed utilized throughout the codebase, aligning with the intended design and justifying its exposure for broader architectural integration.

Given the evidence from the search results, it can be concluded that the change to make the function_definition module public is justified and aligns with its usage across the codebase. This supports the initial review comment regarding the significance of such changes and their alignment with broader architectural goals.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the newly public modules are used appropriately in other parts of the codebase.
fd 'pattern.rs' --exec grep -Hn 'pub mod function_definition;'

Length of output: 123



Script:

#!/bin/bash
# Search for references to `function_definition` module across the codebase, excluding its declaration in `pattern.rs`.
rg --glob '!crates/core/src/pattern.rs' 'function_definition'

Length of output: 46968

* 775-775: Renaming `Context` to `MarzanoContext` is a foundational change that aligns with the PR's objectives. This renaming sets the stage for further modifications and clarifies the role of this struct within the new architectural design. Ensure that all references to the old `Context` struct have been updated accordingly.
Verification successful

The references to Context<'a> found in the script output are expected and align with the new architectural design, where Context is used as a trait. The renaming of Context to MarzanoContext and the updates to references have been correctly handled, indicating that the renaming and subsequent modifications are consistent with the PR's objectives.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining references to the old `Context` struct to ensure they've been updated.
rg --type rust 'Context<' || echo "No references to the old Context struct found."

Length of output: 8459

* 814-852: The implementation of the `Context` trait for `MarzanoContext` is a crucial part of the PR's objectives. It encapsulates a wide range of functionalities, including access to patterns, predicates, function definitions, and more. This implementation abstracts and generalizes context interactions across the system, promoting flexibility and modularity. Ensure that all required methods are implemented correctly and that the trait's contract is fully satisfied.
crates/core/src/pattern/resolved_pattern.rs (3)
  • 1-24: The reorganization of imports improves code readability and aligns with the architectural changes introduced in this PR. Good job on keeping the imports clean and organized.
  • 549-549: Adjusting function signatures to use the Context trait is consistent with the PR's objectives of abstracting context handling. Ensure that the dynamic nature of trait objects does not introduce performance overhead or type safety issues in critical paths.
  • 580-586: > 📝 NOTE

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

The minor code restructuring changes, including adjustments to how patterns and snippets are resolved, align well with the PR's objectives and improve code organization and flexibility. These changes appear to be carefully considered and should not introduce any issues.

crates/core/src/pattern/patterns.rs (4)
  • 61-61: The change from &Context<'a> to impl Context<'a> in the Matcher trait's execute method signature is a significant architectural adjustment. This change introduces more flexibility in how context can be implemented and passed around, aligning with the PR's objectives to enhance modularity and flexibility in context management. However, ensure that all implementors of this trait and callers of the execute method are updated accordingly to accommodate this change.
  • 587-587: Similar to the Matcher trait, the Pattern implementation's text method now uses impl Context<'a> instead of &Context<'a>. This change is consistent with the PR's goal of abstracting context handling and should offer improved flexibility. It's crucial to verify that all usages of this method are updated to reflect this new signature.
  • 598-598: The float method in the Pattern implementation also adopts the impl Context<'a> signature. This consistency across method signatures is beneficial for the architectural changes aimed at in the PR. As with other changes, ensure that all relevant method calls are reviewed and updated as necessary.
  • 1053-1053: The execute method in the Pattern implementation now uses impl Context<'a>, aligning with the changes observed in the Matcher trait and other methods within Pattern. This change supports the PR's objectives towards a more flexible and modular architecture. Confirm that the implementation and usage of this method across the codebase are compatible with this change.

Comment on lines 10 to 30
pub trait Context<'a> {
fn pattern_definitions(&self) -> &[PatternDefinition];

fn predicate_definitions(&self) -> &[PredicateDefinition];

fn function_definitions(&self) -> &[GritFunctionDefinition];

fn foreign_function_definitions(&self) -> &[ForeignFunctionDefinition];

fn ignore_limit_pattern(&self) -> bool;

// FIXME: Don't depend on Grit's file handling in Context.
fn files(&self) -> &FileOwners;

fn built_ins(&self) -> &BuiltIns;

// FIXME: This introduces a dependency on TreeSitter.
fn language(&self) -> &TargetLanguage;

fn name(&self) -> Option<&str>;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The Context trait is well-defined, covering a comprehensive set of methods for accessing various components. This abstraction is crucial for the PR's goal of enhancing modularity and flexibility. However, the FIXME comments on lines 21 and 26 highlight dependencies that may need to be addressed. Consider creating separate issues to track these dependencies and explore ways to decouple them from the Context trait.

Would you like me to open GitHub issues to track the resolution of the FIXME comments regarding dependencies on Grit's file handling and TreeSitter?

@ilevyor
Copy link
Contributor

ilevyor commented Mar 25, 2024

There is one change in this PR that might be slightly controversial: I've refactored builtin_functions.rs a bit (see the first commit) so that context is no longer passed to built-in functions.

Unfortunately this is somewhat of an issue. The context struct is only ever used (or at the very least introduced) for built in functions. The functions they are used in are in a closed source ai_builtins crate.

@arendjr
Copy link
Contributor Author

arendjr commented Mar 25, 2024

Unfortunately this is somewhat of an issue. The context struct is only ever used (or at the very least introduced) for built in functions. The functions they are used in are in a closed source ai_builtins crate.

I’ll try if I can come up with a different solution to this then!

@arendjr
Copy link
Contributor Author

arendjr commented Mar 25, 2024

I've updated the PR so that Builtins doesn't need a generic argument and remains Marzano-specific. It means Biome will have to make its own implementation, but I think that should be fine.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b56b112 and 2066aa0.
Files selected for processing (59)
  • crates/core/src/context.rs (1 hunks)
  • crates/core/src/pattern.rs (8 hunks)
  • crates/core/src/pattern/accessor.rs (3 hunks)
  • crates/core/src/pattern/accumulate.rs (6 hunks)
  • crates/core/src/pattern/add.rs (4 hunks)
  • crates/core/src/pattern/after.rs (3 hunks)
  • crates/core/src/pattern/and.rs (3 hunks)
  • crates/core/src/pattern/any.rs (3 hunks)
  • crates/core/src/pattern/assignment.rs (3 hunks)
  • crates/core/src/pattern/ast_node.rs (4 hunks)
  • crates/core/src/pattern/before.rs (3 hunks)
  • crates/core/src/pattern/boolean_constant.rs (2 hunks)
  • crates/core/src/pattern/bubble.rs (2 hunks)
  • crates/core/src/pattern/built_in_functions.rs (17 hunks)
  • crates/core/src/pattern/call.rs (4 hunks)
  • crates/core/src/pattern/code_snippet.rs (2 hunks)
  • crates/core/src/pattern/contains.rs (3 hunks)
  • crates/core/src/pattern/divide.rs (4 hunks)
  • crates/core/src/pattern/dynamic_snippet.rs (4 hunks)
  • crates/core/src/pattern/equal.rs (2 hunks)
  • crates/core/src/pattern/every.rs (2 hunks)
  • crates/core/src/pattern/file_pattern.rs (2 hunks)
  • crates/core/src/pattern/files.rs (2 hunks)
  • crates/core/src/pattern/float_constant.rs (2 hunks)
  • crates/core/src/pattern/function_definition.rs (5 hunks)
  • crates/core/src/pattern/functions.rs (5 hunks)
  • crates/core/src/pattern/if.rs (3 hunks)
  • crates/core/src/pattern/includes.rs (2 hunks)
  • crates/core/src/pattern/int_constant.rs (2 hunks)
  • crates/core/src/pattern/like.rs (3 hunks)
  • crates/core/src/pattern/limit.rs (2 hunks)
  • crates/core/src/pattern/list.rs (3 hunks)
  • crates/core/src/pattern/list_index.rs (3 hunks)
  • crates/core/src/pattern/log.rs (4 hunks)
  • crates/core/src/pattern/map.rs (2 hunks)
  • crates/core/src/pattern/match.rs (2 hunks)
  • crates/core/src/pattern/maybe.rs (4 hunks)
  • crates/core/src/pattern/modulo.rs (4 hunks)
  • crates/core/src/pattern/multiply.rs (4 hunks)
  • crates/core/src/pattern/not.rs (3 hunks)
  • crates/core/src/pattern/or.rs (3 hunks)
  • crates/core/src/pattern/pattern_definition.rs (2 hunks)
  • crates/core/src/pattern/patterns.rs (5 hunks)
  • crates/core/src/pattern/predicate_definition.rs (2 hunks)
  • crates/core/src/pattern/predicate_return.rs (2 hunks)
  • crates/core/src/pattern/predicates.rs (2 hunks)
  • crates/core/src/pattern/range.rs (2 hunks)
  • crates/core/src/pattern/regex.rs (2 hunks)
  • crates/core/src/pattern/resolved_pattern.rs (8 hunks)
  • crates/core/src/pattern/rewrite.rs (4 hunks)
  • crates/core/src/pattern/sequential.rs (3 hunks)
  • crates/core/src/pattern/some.rs (2 hunks)
  • crates/core/src/pattern/step.rs (6 hunks)
  • crates/core/src/pattern/string_constant.rs (3 hunks)
  • crates/core/src/pattern/subtract.rs (4 hunks)
  • crates/core/src/pattern/undefined.rs (2 hunks)
  • crates/core/src/pattern/variable.rs (2 hunks)
  • crates/core/src/pattern/where.rs (2 hunks)
  • crates/core/src/pattern/within.rs (2 hunks)
Files skipped from review as they are similar to previous changes (57)
  • crates/core/src/context.rs
  • crates/core/src/pattern.rs
  • crates/core/src/pattern/accessor.rs
  • crates/core/src/pattern/accumulate.rs
  • crates/core/src/pattern/add.rs
  • crates/core/src/pattern/after.rs
  • crates/core/src/pattern/and.rs
  • crates/core/src/pattern/any.rs
  • crates/core/src/pattern/assignment.rs
  • crates/core/src/pattern/ast_node.rs
  • crates/core/src/pattern/before.rs
  • crates/core/src/pattern/boolean_constant.rs
  • crates/core/src/pattern/bubble.rs
  • crates/core/src/pattern/built_in_functions.rs
  • crates/core/src/pattern/call.rs
  • crates/core/src/pattern/code_snippet.rs
  • crates/core/src/pattern/contains.rs
  • crates/core/src/pattern/divide.rs
  • crates/core/src/pattern/dynamic_snippet.rs
  • crates/core/src/pattern/equal.rs
  • crates/core/src/pattern/every.rs
  • crates/core/src/pattern/file_pattern.rs
  • crates/core/src/pattern/files.rs
  • crates/core/src/pattern/float_constant.rs
  • crates/core/src/pattern/function_definition.rs
  • crates/core/src/pattern/functions.rs
  • crates/core/src/pattern/if.rs
  • crates/core/src/pattern/includes.rs
  • crates/core/src/pattern/int_constant.rs
  • crates/core/src/pattern/like.rs
  • crates/core/src/pattern/limit.rs
  • crates/core/src/pattern/list.rs
  • crates/core/src/pattern/list_index.rs
  • crates/core/src/pattern/map.rs
  • crates/core/src/pattern/match.rs
  • crates/core/src/pattern/maybe.rs
  • crates/core/src/pattern/modulo.rs
  • crates/core/src/pattern/not.rs
  • crates/core/src/pattern/or.rs
  • crates/core/src/pattern/pattern_definition.rs
  • crates/core/src/pattern/patterns.rs
  • crates/core/src/pattern/predicate_definition.rs
  • crates/core/src/pattern/predicate_return.rs
  • crates/core/src/pattern/predicates.rs
  • crates/core/src/pattern/range.rs
  • crates/core/src/pattern/regex.rs
  • crates/core/src/pattern/resolved_pattern.rs
  • crates/core/src/pattern/rewrite.rs
  • crates/core/src/pattern/sequential.rs
  • crates/core/src/pattern/some.rs
  • crates/core/src/pattern/step.rs
  • crates/core/src/pattern/string_constant.rs
  • crates/core/src/pattern/subtract.rs
  • crates/core/src/pattern/undefined.rs
  • crates/core/src/pattern/variable.rs
  • crates/core/src/pattern/where.rs
  • crates/core/src/pattern/within.rs
Additional comments: 4
crates/core/src/pattern/multiply.rs (2)
  • 8-8: The import of Context aligns with the PR's objective to introduce a Context trait for flexible context management. This change is crucial for decoupling and enhancing modularity within the system.
  • 68-68: The function signatures for call, evaluate, and execute now accept a reference to an implementation of the Context trait, rather than a concrete Context struct. This is a significant architectural improvement, promoting flexibility and decoupling of components. However, ensure that all implementations of the Context trait provide the necessary functionality expected by these methods.

Also applies to: 78-78, 99-99

crates/core/src/pattern/log.rs (2)
  • 9-9: The import adjustments and the use of the Context trait in the log.rs file are in line with the PR's objectives to refine dependencies and parameter types for better abstraction and flexibility in handling context-related functionality. This change supports the broader goal of making the system more adaptable.
  • 35-35: The function signatures for add_log, execute, and execute_func now accept a reference to an implementation of the Context trait. This change enhances the flexibility and decoupling of the Log functionality from specific context implementations. It's important to verify that all necessary context interactions are supported by the Context trait to ensure these methods function as intended.

Also applies to: 129-129, 140-140

@arendjr arendjr requested a review from morgante March 26, 2024 17:31
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 2066aa0 and 26d7802.
Files selected for processing (3)
  • crates/core/src/pattern/includes.rs (2 hunks)
  • crates/core/src/pattern/patterns.rs (5 hunks)
  • crates/core/src/pattern/regex.rs (3 hunks)
Files skipped from review as they are similar to previous changes (3)
  • crates/core/src/pattern/includes.rs
  • crates/core/src/pattern/patterns.rs
  • crates/core/src/pattern/regex.rs

@morgante
Copy link
Contributor

@arendjr This is going to need more work:

  • Clippy isn't happy
  • Rust builds fail

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 26d7802 and 14083df.
Files selected for processing (7)
  • crates/core/src/context.rs (1 hunks)
  • crates/core/src/parse.rs (3 hunks)
  • crates/core/src/pattern.rs (8 hunks)
  • crates/core/src/pattern/function_definition.rs (6 hunks)
  • crates/core/src/suppress.rs (3 hunks)
  • crates/language/src/target_language.rs (1 hunks)
  • crates/util/src/runtime.rs (5 hunks)
Files skipped from review as they are similar to previous changes (4)
  • crates/core/src/context.rs
  • crates/core/src/pattern.rs
  • crates/core/src/pattern/function_definition.rs
  • crates/core/src/suppress.rs
Additional comments: 10
crates/core/src/parse.rs (5)
  • 1-4: Reorganizing imports and using anyhow::Result directly improves readability and reduces the need for explicit error type declarations. This change aligns with Rust's idiomatic practices for error handling.
  • 9-11: Adding specific imports within the function scope is a good practice when they are only used in a particular context. This keeps the global namespace cleaner and reduces the cognitive load when understanding the file's dependencies.
  • 28-30: The error message provided in the parse_input_file function for the not(feature = "grit-parser") case is clear and informative. This is a good practice for feature-flagged functionality, guiding the user on how to resolve the issue.
  • 38-39: Using anyhow::Context for error handling in make_grit_parser under the grit-parser feature flag is a good practice. It provides more context on the error, making debugging easier.
  • 49-50: Similar to the previous comment, the error handling for the not(feature = "grit-parser") case in make_grit_parser is clear and guides the user on enabling the necessary feature flag.
crates/util/src/runtime.rs (4)
  • 26-31: Defining types for functions FetchFn and ExecExternalFn under specific feature flags and target architecture is a good practice. It enhances code readability and ensures type safety for function pointers used in different configurations.
  • 104-104: The constructor new for ExecutionContext under the network_requests_external and external_functions_ffi feature flags is well-defined. It initializes the struct with provided function pointers, which allows for flexible and configurable runtime behavior.
  • 126-128: The error handling in send_request for the network_requests feature flag uses anyhow::anyhow! to provide a clear error message when the Tokio runtime handle is not available. This is a good practice for providing actionable error messages to the developer.
  • 3-3: Consider using environment variable management libraries like dotenv for handling environment variables. This can provide a more structured approach to managing environment configurations and improve code maintainability.
crates/language/src/target_language.rs (1)
  • 168-168: The change in the get_language_with_parser function signature to use underscore placeholders indicates that the parameters are no longer used. Ensure that this change aligns with the intended functionality and that any dependent code has been updated accordingly.

Comment on lines 1 to 15
use crate::pattern::api::InputFile;
use anyhow::Result;
use marzano_language::{language::Language, target_language::TargetLanguage};
use serde_json::to_string_pretty;
use marzano_language::target_language::TargetLanguage;
use std::path::Path;
use tree_sitter::Parser;

use crate::{pattern::api::InputFile, tree_sitter_serde::tree_sitter_node_to_json};

#[cfg(feature = "grit-parser")]
pub fn parse_input_file(lang: &TargetLanguage, input: &str, path: &Path) -> Result<InputFile> {
use crate::tree_sitter_serde::tree_sitter_node_to_json;
use marzano_language::language::Language;
use serde_json::to_string_pretty;

let mut parser = Parser::new().unwrap();
parser.set_language(lang.get_ts_language()).unwrap();
let tree = parser.parse(input.as_bytes(), None).unwrap().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [12-27]

The use of unwrap() in multiple places within parse_input_file function poses a risk of panicking at runtime. Consider handling these potential errors gracefully to improve the robustness of the code.

-    let mut parser = Parser::new().unwrap();
-    parser.set_language(lang.get_ts_language()).unwrap();
-    let tree = parser.parse(input.as_bytes(), None).unwrap().unwrap();
-    let input_file_debug_text = to_string_pretty(&tree_sitter_node_to_json(
-        &tree.root_node(),
-        input,
-        Some(lang),
-    ))
-    .unwrap();
+    let mut parser = Parser::new().context("Failed to create new parser")?;
+    parser.set_language(lang.get_ts_language()).context("Failed to set language for parser")?;
+    let tree = parser.parse(input.as_bytes(), None).context("Failed to parse input")?.context("Parsed tree is empty")?;
+    let input_file_debug_text = to_string_pretty(&tree_sitter_node_to_json(&tree.root_node(), input, Some(lang)))
+        .context("Failed to convert tree to pretty JSON string")?;

@arendjr
Copy link
Contributor Author

arendjr commented Mar 26, 2024

WASM builds should be fixed now.

The Clippies weren't related to my PR, but it looks like they simply only showed up with WASM builds. Fixed too.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 14083df and 7efd67e.
Files selected for processing (1)
  • crates/core/src/parse.rs (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/core/src/parse.rs

@morgante
Copy link
Contributor

@arendjr FYI you don't have to listen to the AI rabbit unless you want to. We're just trying it out - so far it seems to mostly add noise.

@morgante morgante merged commit b53ad42 into getgrit:main Mar 26, 2024
3 checks passed
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.

3 participants