Skip to content

Conversation

aksh1618
Copy link
Contributor

I recently happened upon this amazing project, kudos to all involved, I absolutely love the idea and implementation! I decided to use it for one of my projects, and it worked very well except for some minor cases, for which I decided to submit fixes:

  • Backslashes are not escaped properly, for example in case of a double escaped quote
  • Triple quotes are not escaped properly
  • Added a test for the special case of ending quote as well for completion

Thanks again for making this wonderful project, let me know if any further changes required here!

@timtebeek timtebeek added the enhancement New feature or request label Apr 26, 2023
@knutwannheden knutwannheden requested a review from kunli2 April 28, 2023 06:31
// escape backslashes
content = content.replace("\\", "\\\\");
// escape triple quotes
content = content.replace("\"\"\"", "\"\"\\\"");
Copy link
Contributor

@kunli2 kunli2 May 2, 2023

Choose a reason for hiding this comment

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

do you know why the triple quotes are transformed to \"\"\\\" but not \\\"\"\", the difference is the 1st or 3rd quote is escaped.
I found for an 8 quotes case:

        String s1 = "\"\"\"\"\"\"\"\"";
        String s2 = """
                                ""\"""\"""\
                                """;
        String s3 = """
                                \"""\"""\""\
                                """;

s1 and s2 are equivalent, however,

        String s3 = """
                                \"""\"""""\
                                """;

doesn't compile unless change it to

        String s3 = """
                                \"""\"""\""\
                                """;

which makes me think to escape the 3rd quote instead of 1st is like magic.
Do you have any insight of this?

Copy link
Contributor Author

@aksh1618 aksh1618 May 2, 2023

Choose a reason for hiding this comment

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

@kunli2 I went for escaping the third quote instead of the first to optimize for the number of escapes needed in case of n number of continuous quotes (For example: 2 escapes instead of 3 in case of 8 quotes). Another thing I noticed was that IDEA by default escapes the third quote as well, for instance if you copy-paste a series of quotes inside a text block.

In your examples, I can't see any difference between s2 & s3 (both), did GitHub formatting probably make them look the same? Could you share it in some other form such as a gist so that I can try to understand it better, maybe then I could answer it better?.

Copy link
Contributor

Choose a reason for hiding this comment

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

hi @aksh1618 , yes, I also found IDE by default escapes the 3rd quote.
what I noticed is below, the first statement in the code doesn't compile (\"""\""""" doesn't compile but \"""\"""\""\ does). I guess that is the reason why IDEA escapes the 3rd quote instead of 1st.

String s3 = """
                                \"""\"""""\
                                """;
String s3 = """
                                \"""\"""\""\
                                """;

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry for the confusion in my 1st comment, I have revised the code in the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kunli2 thanks for clarifying, it's a very good example! So to summarize, in this case if we're replacing programmatically successively, escaping the first one would not work as it leads to code that doesn't compile due to 4 quotes left together. So now it makes sense why they went with the third one as well instead of having to write a more complex replacement algorithm for handling leftover quotes 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

@aksh1618 yeah, it makes sense! thanks for fixing and contribution again!

@kunli2 kunli2 merged commit 248045c into openrewrite:main May 3, 2023
@aksh1618 aksh1618 deleted the aksh1618/fixUseTextBlocksEscaping branch May 4, 2023 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants