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

fix: presence of trailing comma (#416) #418

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

abhishek818
Copy link
Contributor

@abhishek818 abhishek818 commented Jul 13, 2024

/claim #416
/closes #416

Greptile Summary

This is an auto-generated summary

  • Added logic to identify and remove lines with only a trailing comma after replacements in crates/core/src/inline_snippets.rs
  • Implemented checks for content of lines affected by replacements
  • Collected ranges of lines containing only a comma for removal
  • Ensured proper handling of character boundaries and offsets

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of code snippets by removing lines with trailing commas within the specified range.
  • Tests

    • Updated tests to ensure trailing commas are correctly removed in specific dictionary declarations and added new expected outputs for verification.

Copy link
Contributor

coderabbitai bot commented Jul 13, 2024

Walkthrough

Walkthrough

This change addresses the issue of trailing commas in code snippets by introducing a logic to identify and remove lines with trailing commas. The update ensures that lines containing only commas are removed during the replacement process. Additionally, it modifies the trailing_comma_after_argument_removal() test function to reflect these changes and validate the correct behavior.

Changes

Files & Directories Change Summary
crates/core/src/inline_snippets.rs Introduced logic to identify and remove lines with trailing commas in code snippets.
crates/core/src/test.rs Updated the test function trailing_comma_after_argument_removal() to remove trailing commas and added new expected output.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant System
    participant TestFramework

    User->>System: Provides code snippet with trailing commas
    System->>System: Identifies and removes trailing commas
    System->>User: Returns updated code snippet

    TestFramework->>System: Runs `trailing_comma_after_argument_removal()` test
    System->>TestFramework: Provides updated code snippet
    TestFramework->>TestFramework: Verifies the output
    TestFramework->>User: Returns test results
Loading

Assessment against linked issues

Objective Addressed Explanation
Handling and removing hanging commas after removing kwargs in Python (#416)

Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f3e105b and 7118310.

Files selected for processing (2)
  • crates/core/src/inline_snippets.rs (1 hunks)
  • crates/core/src/test.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/core/src/inline_snippets.rs
Additional comments not posted (1)
crates/core/src/test.rs (1)

12019-12114: LGTM! Verify the correctness of the test pattern and expected output.

The changes to the test pattern and expected output look good and address the issue described in the PR objectives.

However, ensure that the test pattern correctly matches the intended input and that the expected output is accurate.

Verification successful

Verification Successful: Test Pattern and Expected Output

The test pattern and expected output for the function trailing_comma_after_argument_removal are correctly implemented and meet the intended changes specified in the PR objectives.

  • The test pattern correctly matches the intended input structure.
  • The expected output accurately reflects the desired transformation.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the test pattern and expected output.

# Test: Ensure that the test pattern correctly matches the intended input.
rg --type rust -A 10 $'fn trailing_comma_after_argument_removal()' crates/core/src/test.rs

# Test: Ensure that the expected output is accurate.
rg --type rust -A 10 $'expected: r#"' crates/core/src/test.rs

Length of output: 83899


Script:

#!/bin/bash
# Extract the complete implementation of the function `trailing_comma_after_argument_removal` and its expected output.
rg --type rust -A 50 'fn trailing_comma_after_argument_removal()' crates/core/src/test.rs

Length of output: 2495


Script:

#!/bin/bash
# Extract the complete implementation of the function `trailing_comma_after_argument_removal` and its expected output.
rg --type rust -A 100 'fn trailing_comma_after_argument_removal()' crates/core/src/test.rs

Length of output: 4397


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

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

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@abhishek818
Copy link
Contributor Author

@morgante Pls review. All tests are passing locally for me.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
crates/core/src/test.rs (1)

12017-12017: Add a newline before the comment.

Adding a newline before the comment improves readability.

+

// refer https://github.com/getgrit/gritql/issues/416

Signed-off-by: Abhishek Kumar Gupta <[email protected]>
@@ -234,12 +234,28 @@ pub(crate) fn inline_sorted_snippets_with_offset(
}
}

let mut trailing_comma_lines = vec![];
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have a delete_hanging_comma function that should be used - we don't want two different pieces of code dealing with orphaned commas.

Any solution should either be there or in a replacement.

Copy link
Contributor Author

@abhishek818 abhishek818 Jul 14, 2024

Choose a reason for hiding this comment

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

@morgante
Had tried that earlier. I mean, placing the code fix in delete_hanging_comma function or in sub functions called inside it. That resulted in failure of an existing edge test case. This happens since offsets are again calculated after calling delete_hanging_comma and then actual replacements happens.

So, I feel doing this fix during actual replacements is the best approach.

Pasting here the changes (or little variations of it, you will get the idea) I was trying with :
just manipulated the range indexes here.

fn delete_hanging_comma(
    code: &str,
    replacements: &mut [(EffectRange, String)],
    offset: usize,
) -> Result<(String, Vec<ReplacementInfo>)> {
    ....
    ....
    // Handle the case when after applying replacements, a single comma
    // gets left behind on a single line
    let mut temp_code = code.to_string();
    for (range, snippet) in replacements.iter_mut() {
        let mut adjusted_range = adjust_range(&range.effective_range(), offset, &temp_code)?;
        if adjusted_range.start > temp_code.len() || adjusted_range.end > temp_code.len() {
            bail!("Range {:?} is out of bounds for code:\n{}\n", adjusted_range, temp_code);
        }

        temp_code.replace_range(adjusted_range.clone(), snippet);

        let line_start = temp_code[..adjusted_range.start].rfind('\n').map_or(0, |pos| pos + 1);
        let line_end = temp_code[adjusted_range.start..].find('\n').map_or(temp_code.len(), |pos| adjusted_range.start + pos);
        let line_content = temp_code[line_start..line_end].trim();
        
        // Check for lines with a single comma after replacements
        if line_content == "," {
            // Ensure the next character is a newline
            if line_end < temp_code.len() && temp_code.chars().nth(line_end) == Some('\n') {
                adjusted_range.end = line_end + 1;
            }

            // Update the original range to replace the range with an empty string later.
            range.range.end += adjusted_range.end - line_start;     <--------------------------------------------
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to do it in one of those two locations. Please don't make our handling even more spaghetti.

Copy link
Contributor Author

@abhishek818 abhishek818 Jul 15, 2024

Choose a reason for hiding this comment

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

@morgante hey one doubt I have :

In below test case (existing), shouldnt we remove the empty newline?

#[test]
fn only_deduplicate_rewrites6() {
    run_test_expected({
        TestArgExpected {
            pattern: r#"
                engine marzano(0.1)
                language json
                pair($key, $value) => . where {
                    $key <: `"extends"`,
                }"#
            .to_owned(),
            source: r#"
            {
                "env": "bar",
                "extends": "foo",
                "baz": 1
            }"#
            .to_owned(),
            expected: r#"
            {
                "env": "bar",
                                            <----------------------------------
                "baz": 1
            }"#
            .to_owned(),
        }
    })
    .unwrap();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

No, it is not necessary for Grit to handle removing newlines. We rely on formatters to handle that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@morgante got it. This fix should be satisfactory, have a look..

@abhishek818 abhishek818 changed the title fix: removal of trailing comma lines (#416) fix: presence of trailing comma (#416) Jul 15, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
crates/core/src/test.rs (1)

12017-12017: Add a more descriptive comment.

The comment should provide more context about the issue being addressed.

- // refer https://github.com/getgrit/gritql/issues/416
+ // This test addresses issue #416 (https://github.com/getgrit/gritql/issues/416) which involves removing dangling commas after keyword arguments in Python.

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.

Please include at least 3 test cases that demonstrate you have fully thought through your implementation.

@@ -300,6 +300,27 @@ fn delete_hanging_comma(
replacements: &mut [(EffectRange, String)],
offset: usize,
) -> Result<(String, Vec<ReplacementInfo>)> {
// Handle the case when after applying replacements, a single comma
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 completely incorrect. You should not be assuming the comma is on a single line.

The original bug is also present even if arguments are on 1 line: https://app.grit.io/studio?key=-Pm14wW6mvH0S41J_hFh8

Please think logically, don't just try to brute force the single case.

Copy link
Contributor Author

@abhishek818 abhishek818 Jul 15, 2024

Choose a reason for hiding this comment

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

@morgante
you mean when it is like this? :

bibtex_citation="",
n_samples={_EVAL_SPLIT: 1820}, avg_character_length={_EVAL_SPLIT: 78},

then my current fix prints with a extra space:

bibtex_citation="",
 stats=GeneralDescriptiveStats(n_samples={_EVAL_SPLIT: 1820}, avg_character_length={_EVAL_SPLIT: 78}),

But this is due to the single space present between both the matching words (just after the comma of first argument)

Copy link
Contributor Author

@abhishek818 abhishek818 Jul 15, 2024

Choose a reason for hiding this comment

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

I don't find any other edge cases (except for arguments on multiple lines which works)
Since I am still new to gritql, can you provide a case where it fails.

Copy link
Contributor

Choose a reason for hiding this comment

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

Your fix completely fails in cases where the arguments are on 1 line. The original bug isn't related to line handling at all and introducing that is a fundamental mistake in your fix.


/// Same as above test, but ensures the behavior doesn't depend on line breaks
#[test]
fn trailing_comma_after_argument_removal_one_line() {
    run_test_expected({
        TestArgExpected {
            pattern: r#"
                language python
                `TaskMetadata($args)` where {
                        $args <: any {
                            contains `n_samples=$_` as $ns_kwarg where {
                                $ns_kwarg <: `n_samples = $ns_val` => .
                            },
                            contains `avg_character_length=$_` as $avg_kwarg where {
                                $avg_kwarg <: `avg_character_length = $avg_val` => `stats=GeneralDescriptiveStats(n_samples=$ns_val, avg_character_length=$avg_val)`
                            },
                        },
                    }
                "#.to_owned(),
            source: r#"
                TaskMetadata(description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).", main_score="f1", domains=["News"], text_creation="created", n_samples={_EVAL_SPLIT: 1820}, reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles")
            "#
                .to_owned(),
            expected: r#"
                TaskMetadata(description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).", main_score="f1", domains=["News"], text_creation="created",   reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles")
            "#
                .to_owned(),
        }
    })
    .unwrap();
}

Also has nothing to do with GritQL, you don't need to change the pattern to see how this is broken.

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.

Don't rely on line breaks.

@morgante
Copy link
Contributor

I think you're probably working in entirely the wrong place.

The bug is likely in the mini state machine / parser we have here: https://github.com/getgrit/rewriter/blob/main/vendor/gritql/crates/core/src/inline_snippets.rs#L535-L652

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.

Hanging commas after removing kwargs in Python
2 participants