Skip to content

refactor: remove Matches/Diffs macro in cli#1820

Merged
HerringtonDarkholme merged 1 commit intomainfrom
refactor-cli
Feb 23, 2025
Merged

refactor: remove Matches/Diffs macro in cli#1820
HerringtonDarkholme merged 1 commit intomainfrom
refactor-cli

Conversation

@HerringtonDarkholme
Copy link
Copy Markdown
Member

@HerringtonDarkholme HerringtonDarkholme commented Feb 23, 2025

fix #1819

Summary by CodeRabbit

  • Refactor

    • Unified the output formatting across all display modes—including colored, interactive, and JSON views—for more consistent and reliable presentation of reports.
    • Simplified how results are processed before display, ensuring clear and dependable match and difference reports.
  • Tests

    • Updated testing routines to verify the improved output handling, contributing to a more stable and predictable user experience.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 23, 2025

Walkthrough

The pull request refactors various CLI print modules by removing macros and type aliases related to NodeMatch and Diff. Function signatures in the Printer trait and related methods are updated to accept concrete types (i.e., Vec<NodeMatch> and Vec<Diff>) instead of macro-generated iterators. This simplification eliminates unnecessary lifetime parameters and streamlines the collection of match results in both production and test code, while maintaining existing control flow.

Changes

File(s) Change Summary
crates/cli/.../cloud_print.rs
crates/cli/.../colored_print.rs
crates/cli/.../interactive_print.rs
crates/cli/.../json_print.rs
crates/cli/.../mod.rs
Removed macro definitions and type aliases for Matches/Diffs; updated Printer trait and related methods to use Vec<NodeMatch> and Vec<Diff>, eliminating unnecessary lifetime parameters and generic type parameters.
crates/cli/.../colored_print/test.rs Modified tests to collect match iterators into vectors using .collect().
crates/cli/.../run.rs
crates/cli/.../scan.rs
Updated file scanning functions to call .collect() on iterators and removed redundant variable assignments.

Possibly related PRs

Poem

I'm a bunny, hopping through code so light,
Clearing away macros, making type signatures bright.
No more twisting aliases or lifetimes in the fray,
Just neat vectors leading the clearer way.
With a hop and a skip, I celebrate this refactor delight! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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

CodeRabbit Commands (Invoked using PR comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

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

Documentation and Community

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

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 23, 2025

Codecov Report

Attention: Patch coverage is 79.74684% with 16 lines in your changes missing coverage. Please review.

Project coverage is 87.47%. Comparing base (3b20585) to head (3fd891b).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/cli/src/print/interactive_print.rs 0.00% 11 Missing ⚠️
crates/cli/src/print/cloud_print.rs 62.50% 3 Missing ⚠️
crates/cli/src/print/json_print.rs 96.42% 1 Missing ⚠️
crates/cli/src/run.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1820      +/-   ##
==========================================
+ Coverage   87.45%   87.47%   +0.02%     
==========================================
  Files          96       96              
  Lines       15556    15565       +9     
==========================================
+ Hits        13604    13615      +11     
+ Misses       1952     1950       -2     

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
crates/cli/src/print/json_print.rs (1)

321-322:

Details

❓ Verification inconclusive

Consider the memory implications of collecting all matches.

While the changes to use concrete vector types improve code clarity and maintainability, collecting all matches into memory at once could potentially impact performance for large files with many matches. However, given that the results need to be serialized to JSON anyway, the memory trade-off is likely acceptable in this context.

Run this script to assess the typical number of matches in real-world usage:

Also applies to: 332-332, 341-341, 469-470, 529-530, 594-595


🏁 Script executed:

#!/bin/bash
# Description: Check the average and maximum number of matches in test files
# to understand memory implications.

rg -U 'let matches = .+find_all.+collect' -A 1 | \
  rg 'assert|expect' | \
  rg -o '[0-9]+' || true

Length of output: 93


Memory Considerations Require Manual Verification

The code now clearly uses concrete vector types for collecting matches, which enhances clarity and maintainability. However, even though the serialized JSON output favors this approach, the script we ran to assess the typical match count produced no output. This inconclusive result means that you should manually verify that collecting all matches in memory does not negatively impact performance on large files.

  • Please confirm through profiling or further testing that the number of matches in real-world datasets remains within acceptable limits.
  • If extremely large files are expected, consider whether a streaming approach might be more efficient in future refactoring.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b20585 and 3fd891b.

📒 Files selected for processing (8)
  • crates/cli/src/print/cloud_print.rs (4 hunks)
  • crates/cli/src/print/colored_print.rs (11 hunks)
  • crates/cli/src/print/colored_print/test.rs (7 hunks)
  • crates/cli/src/print/interactive_print.rs (5 hunks)
  • crates/cli/src/print/json_print.rs (14 hunks)
  • crates/cli/src/print/mod.rs (2 hunks)
  • crates/cli/src/run.rs (1 hunks)
  • crates/cli/src/scan.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • crates/cli/src/scan.rs
🧰 Additional context used
🧠 Learnings (1)
crates/cli/src/print/json_print.rs (1)
Learnt from: HerringtonDarkholme
PR: ast-grep/ast-grep#1500
File: crates/cli/src/config.rs:60-60
Timestamp: 2024-11-10T16:52:06.569Z
Learning: In the `ast-grep` codebase, it's acceptable for some callers of `find_rules` to intentionally ignore the `RuleStats` returned in its tuple `(RuleCollection<SgLang>, RuleStats)` when the statistics are not needed.
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: coverage
🔇 Additional comments (11)
crates/cli/src/print/mod.rs (3)

23-23: LGTM! Type alias simplification improves clarity.

The removal of the generic parameter L and standardization to SgLang reduces complexity while maintaining functionality.


26-31: LGTM! Method signatures simplified to use concrete types.

The transition from macro-based types to concrete Vec<NodeMatch> and Vec<Diff> improves readability and maintainability.

Also applies to: 32-33


54-54: LGTM! Field type updated for consistency.

The node_match field type update aligns with the simplified NodeMatch type alias.

crates/cli/src/print/cloud_print.rs (2)

38-39: LGTM! Method signatures simplified to use concrete types.

The transition from macro-based types to concrete Vec<NodeMatch> and Vec<Diff> improves readability and maintainability.

Also applies to: 46-46, 50-50


134-134: LGTM! Test function updated to collect matches.

The explicit collection of matches into a vector aligns with the new method signatures.

crates/cli/src/print/colored_print/test.rs (1)

51-51: LGTM! Test functions updated to collect matches.

All test functions consistently updated to collect matches into vectors, aligning with the new method signatures while preserving test behavior.

Also applies to: 71-71, 111-111, 161-163, 178-178, 203-203, 272-272

crates/cli/src/print/interactive_print.rs (3)

67-68: LGTM! Method signatures simplified to use concrete types.

The transition from macro-based types to concrete Vec<NodeMatch> and Vec<Diff> improves readability and maintainability.

Also applies to: 90-90, 94-94


96-97: LGTM! Collection handling updated.

The explicit collection of diffs into a vector aligns with the new method signatures.


186-189: LGTM! Helper function signature updated.

The print_matches_and_confirm_next function signature updated to use concrete types, maintaining consistency with other changes.

crates/cli/src/run.rs (1)

329-329: LGTM! The changes align with the refactoring objectives.

The modifications to collect iterator results into vectors before passing them to printer methods are consistent with the broader effort to simplify type handling across the codebase.

Also applies to: 331-331

crates/cli/src/print/colored_print.rs (1)

313-314: LGTM! The changes preserve the match merging functionality.

The modifications to use concrete vector types are well-integrated with the existing match merging logic that handles overlapping and adjacent matches. The functionality for merging matches that start or end on the same line is maintained while improving code clarity.

Also applies to: 372-373, 422-423

@HerringtonDarkholme HerringtonDarkholme added this pull request to the merge queue Feb 23, 2025
Merged via the queue into main with commit 74671ba Feb 23, 2025
5 of 6 checks passed
@HerringtonDarkholme HerringtonDarkholme deleted the refactor-cli branch February 23, 2025 02:47
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.

refactor: remove Matches/Diffs macro in cli

1 participant