Skip to content

Recipe UseTextBlocks: Textblock not constructed AFTER it encounters a variable in a string. #261

Open

Description

@jbessels

Was testing with the UseTextBlocks recipe. Wanted to use it for our SQL statements. Noticed that it works partially. Our SQL statement consists out of + variable or constant + . It appears that after encountering a variable a textblock is no longer created. Also reran mvn rewrite:run again just to see if it would be picked up in a second run. It doesn't. During the test the ONLY active recipe was UseTextBlocks

Question: Is there a minimum number of string concats needed before a textblock is used??

Gonna check all the converted files, should I find more 'interesting things' I can enter it here in this thread.

Before

        int thisVariableStopsTextBlocks = 1; 

        String stringWithVariableInIt =
                "This " +
                "is  " +
                "text " +
                "BEFORE the variable " +
                thisVariableStopsTextBlocks +
                "This " +
                "is  " +
                "text " +
                "AFTER the variable. " +
                "As is visible a TextBlock is no longer constructed";

After org.openrewrite.java.migrate.lang.UseTextBlocks: {convertStringsWithoutNewlines=true}

        int thisVariableStopsTextBlocks = 1;

        String stringWithVariableInIt =
                """
                This \
                is  \
                text \
                BEFORE the variable \
                """ +
                thisVariableStopsTextBlocks +
                "This " +
                "is  " +
                "text " +
                "AFTER the variable. " +
                "As is visible a TextBlock is no longer constructed";

Activity

  1. added
    bugSomething isn't working
    questionFurther information is requested
    on Apr 2, 2023
  2. timtebeek commented on Apr 2, 2023

    @timtebeek
    Member

    Question: Is there a minimum number of string concats needed before a textblock is used??

    I'm not immediately seeing a minimum number of lines in UseTextBlocks, nor an explicit test to verify such behavior in UseTextBlocksTest. It appears to look for string concatenations on new lines. Does that answer your question?

  3. jbessels commented on Apr 2, 2023

    @jbessels
    Author

    Tim thanks for the effort, appreciated. I currently have an 'ExamplesForOpenRewrite' class for other tickets I created. Was to lazy for the first ticket of the day......Have played a bit with TextBlocks. Looks what you say is true. They must be on separate lines. I Can NOT force it with \n in a string.

    Before

    String noTextBlockCreated= "no" + "textblock\n" + "will" + "be" + "created despite newline \n IN the string";
    String textBlockCreated = "yes" +
                    "textblock";
    

    After

     String noTextBlockCreated= "no" + "textblock\n" + "will" + "be" + "created despite newline \n IN the string";
            String textBlockCreated = """
                    yes\
                    textblock\
                    """;
    
  4. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    questionFurther information is requested
    on Apr 3, 2023
  5. kunli2 commented on Apr 3, 2023

    @kunli2
    Contributor

    Hi @jbessels , Thanks for reporting this, yeah, for the question Question: Is there a minimum number of string concatenation needed before a text block is used?, @timtebeek answered it, there is no minimum number of string concatenation needed but new line/lines are required in string concatenation.

    This is a known unsupported string grouping now, it will be good to have an enhancement for this.

  6. jbessels commented on Apr 3, 2023

    @jbessels
    Author

    Tbh a wee bit bit sad that the ticket degraded from 'bug' to enhancement' but I fully understand. Bigger fish to fry first. I've some to offer ;-) It doesn't happen in that many places. I guess I can put the existing contcats part after the ' breaking variable' in a variable itself, rerun UseTextBlocks and have a new TextBlock. remove the variable and then concat it after the 'breaking variable.'

  7. kunli2 commented on Apr 3, 2023

    @kunli2
    Contributor

    TextBlock

    Yeah, that works, but it will be easier for you if we can support grouping, I think I can do this enhancement in two weeks (probably this week), so you don't need to change the code. :)

  8. jbessels commented on Apr 3, 2023

    @jbessels
    Author

    I think I can restrain myself for that long ;-) Thanks for the heads up, had almost started with the 'manual labor' task. Your very fast response is very much appreciated!!

  9. jbessels commented on Apr 11, 2023

    @jbessels
    Author

    Any news on the 'grouping' improvement??

  10. kunli2 commented on Apr 11, 2023

    @kunli2
    Contributor

    Hi @jbessels, our team just prioritized the tasks, and we are planning to work on some high-priority tasks at this moment.
    and of course, we always welcome a PR implementing from the community.

  11. jbessels commented on Apr 11, 2023

    @jbessels
    Author

    No problem and I fully understand. So far (including this issue) I've had very good support from Moderne and its all very much appreciated. Then I'm just going for my original solution, some manual labor. As said before: I guess I can put the existing contcats part after the ' breaking variable' in a variable itself, rerun UseTextBlocks and have a new TextBlock. remove the variable and then concat it after the 'breaking variable.'

  12. kunli2 commented on Apr 12, 2023

    @kunli2
    Contributor

    Yes, I think it works.

  13. moved this to Backlog in OpenRewriteon Jul 18, 2023
  14. timtebeek commented on Aug 16, 2023

    @timtebeek
    Member

    As of #273 we no longer create partial text blocks; we can track creating either multiple text blocks, or switching over to a single text block with for instance """.formatted here.

  15. jbessels commented on Aug 29, 2023

    @jbessels
    Author

    I tested the tmp fix (#273) and AFAICT it works.

    I REALLY hope a real fix like "switching over to a single text block with for instance """.formatted is going to be implemented as we have LOTS of constructions as mentioned in the OP. Now we currently end up with lots of classes where textblocks and the string concats reside in one file, which is suboptimal.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions