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: add language option. #69

Merged
merged 14 commits into from
Mar 27, 2024
Merged

Conversation

Shylock-Hg
Copy link
Contributor

@Shylock-Hg Shylock-Hg commented Mar 25, 2024

Close #58
/claim #58

Summary by CodeRabbit

  • New Features

    • Added support for specifying a language option when applying patterns through the CLI. This allows users to override the default language specified in the pattern.
  • Tests

    • Introduced new tests to ensure the correct application of patterns with the language option across various scenarios.
  • Refactor

    • Enhanced pattern language functionality and CLI argument parsing with new dependencies and implementations.

Copy link
Contributor

coderabbitai bot commented Mar 25, 2024

Walkthrough

The recent changes introduce a --language flag in the CLI, enhancing the user experience by allowing language specification directly in the command line for applying patterns. This update simplifies the process, especially for inline patterns, by removing the need to declare the language within the pattern itself. The implementation includes updates to argument parsing, tests to validate the new functionality, and modifications to support serialization and command-line argument handling for the language options.

Changes

Files Change Summary
cli/src/commands/apply_pattern.rs Added language option to ApplyPatternArgs, updated logic for language override.
cli_bin/tests/apply.rs Added tests for --language option scenarios.
language/src/target_language.rs Enhanced enum handling with clap and serde for CLI and serialization support.

Assessment against linked issues

Objective Addressed Explanation
--language flag on the CLI (#58)

Poem

🐇 CodeRabbit hopped along,
Adding flags where they belong.
--language here, a pattern there,
Commands now flow with extra flair.
🌟 No more clutter, sleek and neat,
With every line, a task complete.
Cheers to changes, big and small,
Together, we enhance it all. 🎉

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.

@Shylock-Hg Shylock-Hg changed the title Add language option. feat: add language option. Mar 25, 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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 61241d6 and 898fb25.
Files selected for processing (1)
  • crates/cli/src/commands/apply_pattern.rs (4 hunks)
Additional comments: 2
crates/cli/src/commands/apply_pattern.rs (2)
  • 112-113: The addition of the language field to the ApplyPatternArgs struct is well-implemented and aligns with the PR's objectives to enhance the CLI's usability.
  • 130-130: Initializing the language field with Default::default() in the Default implementation for ApplyPatternArgs is appropriate and ensures the correct default behavior.

crates/cli/src/commands/apply_pattern.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

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

Thanks for contributing! Please provide a few integration tests, both for inline patterns and named patterns.

Also, if the pattern has a defined language that differs from the specified language it should probably be an error. --language just changes the default.

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 898fb25 and 00c6c47.
Files selected for processing (1)
  • crates/cli/src/commands/apply_pattern.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli/src/commands/apply_pattern.rs

Shylock-Hg and others added 2 commits March 25, 2024 11:03
Co-authored-by: Morgante Pell <morgante@grit.io>
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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 00c6c47 and f62be24.
Files ignored due to path filters (3)
  • crates/cli_bin/tests/snapshots/apply__language_option_apply.snap is excluded by: !**/*.snap
  • crates/cli_bin/tests/snapshots/apply__language_option_conflict_apply.snap is excluded by: !**/*.snap
  • crates/cli_bin/tests/snapshots/apply__language_option_inline_pattern_apply.snap is excluded by: !**/*.snap
Files selected for processing (2)
  • crates/cli/src/commands/apply_pattern.rs (4 hunks)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli/src/commands/apply_pattern.rs

crates/cli_bin/tests/apply.rs Outdated Show resolved Hide resolved
crates/cli_bin/tests/apply.rs Outdated Show resolved Hide resolved
crates/cli_bin/tests/apply.rs Outdated Show resolved Hide resolved
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 f62be24 and 07bf859.
Files selected for processing (1)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli_bin/tests/apply.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 07bf859 and 94e35fa.
Files selected for processing (2)
  • crates/cli/src/commands/apply_pattern.rs (4 hunks)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • crates/cli/src/commands/apply_pattern.rs
  • crates/cli_bin/tests/apply.rs

@Shylock-Hg
Copy link
Contributor Author

Thanks for contributing! Please provide a few integration tests, both for inline patterns and named patterns.

Also, if the pattern has a defined language that differs from the specified language it should probably be an error. --language just changes the default.

I think it's ok now.

// from the tempdir as cwd, run marzano apply
let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
// apply_cmd.current_dir(basic_path);
Copy link
Contributor

Choose a reason for hiding this comment

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

no commented out code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

let mut apply_cmd = get_test_cmd()?;
apply_cmd.current_dir(dir.as_path());
// apply_cmd.current_dir(basic_path);
apply_cmd.arg("apply").arg("--force").arg("pattern.grit").arg("--language").arg("python");
Copy link
Contributor

Choose a reason for hiding this comment

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

we only want this feature for inline patterns. If the pattern is defined in a file we should log a warning that the option was unused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, it's done.


#[test]
fn language_option_conflict_apply() -> Result<()> {
let pattern = r"language php
Copy link
Contributor

Choose a reason for hiding this comment

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

lets use a currently supported language. I know were adding php soon, but it's not in yet

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My fault, it's ok now.

@ilevyor ilevyor mentioned this pull request Mar 25, 2024
@@ -109,6 +109,8 @@ pub struct ApplyPatternArgs {
/// Clear cache before running apply
#[clap(long = "refresh-cache", conflicts_with = "cache")]
pub refresh_cache: bool,
#[clap(long = "language", alias="lang")]
pub language: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Use an enum, so documentation generation and clap will work better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I can't get your point. Could you detail it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Only certain values are valid here, so you should use an enum.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I got it. Should we support flavor for this enum?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use this existing enum:

pub enum PatternLanguage {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

      --language <LANGUAGE>
          [possible values: js(js_do_not_use), js(typescript), js, html, css, json, java, csharp, python, markdown(block), markdown(inline), go, rust, ruby, solidity, hcl, yaml, sql, vue, toml, universal]

I think it is.

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 94e35fa and 9101821.
Files selected for processing (1)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli_bin/tests/apply.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9101821 and 8bfbbbe.
Files selected for processing (1)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli_bin/tests/apply.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8bfbbbe and 7cf1489.
Files ignored due to path filters (1)
  • crates/cli_bin/tests/snapshots/apply__language_option_file_pattern_apply.snap is excluded by: !**/*.snap
Files selected for processing (2)
  • crates/cli/src/commands/apply_pattern.rs (5 hunks)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • crates/cli/src/commands/apply_pattern.rs
  • crates/cli_bin/tests/apply.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7cf1489 and d45c525.
Files ignored due to path filters (1)
  • crates/cli_bin/tests/snapshots/apply__language_option_named_pattern_apply.snap is excluded by: !**/*.snap
Files selected for processing (1)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Additional comments: 4
crates/cli_bin/tests/apply.rs (4)
  • 2020-2049: The test language_option_file_pattern_apply correctly sets up a scenario to test applying a pattern with a specific language option. However, it asserts the command executed successfully without specifically checking if the language option was correctly applied. Consider adding a more detailed assertion to ensure the language option's effect is as expected.
  • 2051-2081: The test language_option_inline_pattern_apply demonstrates applying an inline pattern with a language option. Similar to the previous comment, it would be beneficial to include assertions that specifically verify the effect of the language option to ensure it's applied correctly.
  • 2083-2117: In language_option_named_pattern_apply, the pattern definition seems to be included directly in the test code. This approach might not accurately reflect how users define and apply named patterns. Consider moving the pattern definition to a separate file or using a predefined pattern to align the test more closely with real-world usage.
  • 2119-2149: The test language_option_conflict_apply aims to verify the behavior when there's a conflict between the language option and the pattern's defined language. However, the assertion checks for command failure, which might not be the most informative outcome. It would be more useful to check for a specific error message that clearly indicates the nature of the conflict to the user.

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 d45c525 and 233a51c.
Files ignored due to path filters (3)
  • Cargo.lock is excluded by: !**/*.lock
  • crates/cli_bin/tests/snapshots/apply__invalid_language_option_apply.snap is excluded by: !**/*.snap
  • crates/language/Cargo.toml is excluded by: !**/*.toml
Files selected for processing (3)
  • crates/cli/src/commands/apply_pattern.rs (5 hunks)
  • crates/cli_bin/tests/apply.rs (1 hunks)
  • crates/language/src/target_language.rs (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • crates/cli/src/commands/apply_pattern.rs
  • crates/cli_bin/tests/apply.rs
Additional comments: 3
crates/language/src/target_language.rs (3)
  • 27-29: The addition of clap and serde dependencies is appropriate for the new functionality. clap is used for parsing command-line arguments, which is essential for the --language option in the CLI. serde is used for serialization, likely for the PatternLanguage enum to facilitate easy conversion to and from string or JSON representations.
  • 48-48: The PatternLanguage enum now implements Serialize, which is crucial for converting enum instances to a format that can be easily stored or transmitted, such as JSON. This change supports the CLI's need to handle language specifications in a flexible and interoperable manner.
  • 377-429: The implementation of ValueEnum and PossibleValue for PatternLanguage is a significant enhancement. It allows PatternLanguage enum variants to be directly used with clap for command-line argument parsing, improving the usability and maintainability of the code. This change facilitates the new --language CLI option by enabling users to specify languages in a standardized and type-safe manner.

However, it's important to ensure that the representations used in to_possible_value (e.g., "js(js_do_not_use)", "js(typescript)") are clear and intuitive for users. Consider documenting these representations or providing help text in the CLI to guide users on how to specify languages correctly.

Consider adding documentation or help text in the CLI to explain the language representations used in to_possible_value to ensure clarity for users.

@@ -371,6 +374,61 @@ impl PatternLanguage {
}
}

impl ValueEnum for PatternLanguage {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could just derive this. See this example: clap-rs/clap#4327

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

crates/language/src/target_language.rs Outdated Show resolved Hide resolved
crates/language/src/target_language.rs Outdated Show resolved Hide resolved
crates/language/src/target_language.rs Outdated Show resolved Hide resolved
@@ -285,6 +293,22 @@ pub(crate) async fn run_apply_pattern(
}
}
};
if let Some(lang_option) = &arg.language {
if is_pattern_file(&pattern) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to special case pattern files - we should also not allow overriding the language in those cases if there's a conflict.

Similarly I should be able to grit apply a foo.grit that doesn't have a language specified.

Concretely, the purpose of the --language option is to change the default pattern language from JS to whatever is passed.

Copy link
Contributor Author

@Shylock-Hg Shylock-Hg Mar 27, 2024

Choose a reason for hiding this comment

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

@ilevyor Could you agree with this? You comment it before, #69 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the only remaining change. I'm the tech lead, you don't need @ilevyor to approve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, it's done.

crates/cli_bin/tests/apply.rs Outdated Show resolved Hide resolved
Comment on lines 2141 to 2146
// Read back the main.py file
let target_file = dir.join("main.py");
let content: String = std::fs::read_to_string(target_file)?;

// assert that it matches snapshot
assert_snapshot!(content);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can skip the snapshot here.

Suggested change
// Read back the main.py file
let target_file = dir.join("main.py");
let content: String = std::fs::read_to_string(target_file)?;
// assert that it matches snapshot
assert_snapshot!(content);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to guarante file is unmodified.

Copy link
Contributor

Choose a reason for hiding this comment

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

Understood. Instead of a snapshot you should just confirm the current contents match the original contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, it's done.

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 233a51c and 9f29639.
Files selected for processing (1)
  • crates/language/src/target_language.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/language/src/target_language.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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9f29639 and 3d78b77.
Files selected for processing (1)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli_bin/tests/apply.rs

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

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

Do not use snapshots incorrectly.

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 3d78b77 and fae87d5.
Files selected for processing (2)
  • crates/cli/src/commands/apply_pattern.rs (4 hunks)
  • crates/cli_bin/tests/apply.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/cli/src/commands/apply_pattern.rs
Additional comments: 5
crates/cli_bin/tests/apply.rs (5)
  • 2021-2048: The test language_option_file_pattern_apply correctly tests the scenario where a pattern file is applied with a --language option that does not match the file's language, expecting the command to fail. However, it's important to ensure that the pattern file (pattern.grit) explicitly specifies a language or is inherently tied to Python to validate the test's intent.
  • 2051-2080: The test language_option_inline_pattern_apply aims to apply an inline pattern with a specified language option. This test assumes the inline pattern is applicable to Python files. To strengthen the test, consider explicitly mentioning in a comment that the pattern is designed for Python syntax to clarify the test's context.
  • 2083-2116: The test language_option_named_pattern_apply introduces a named pattern with a language option. It's crucial to ensure that the named pattern (test_pattern) is defined elsewhere in the test suite or documentation, as it's not inherently clear from the test itself. Providing a reference or ensuring the pattern's existence elsewhere would enhance clarity.
  • 2119-2150: The test language_option_conflict_apply effectively tests the scenario where the --language option conflicts with the language specified within the pattern. This test is crucial for ensuring that the CLI tool correctly handles language conflicts. It's well-implemented and serves its purpose effectively.
  • 2153-2183: The test invalid_language_option_apply checks the behavior when an invalid language option is provided. This is an important test for validating error handling in the CLI tool. However, ensure that the error message or failure condition is explicitly checked in the test to confirm that the failure is due to the invalid language option and not some other reason.

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

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

Thanks for contributing!

@morgante morgante merged commit ce8e8f9 into getgrit:main Mar 27, 2024
4 checks passed
@Shylock-Hg Shylock-Hg deleted the feature/language-option branch March 28, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--language flag on the CLI
3 participants