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

Support Text Blocks (JEP 368) #382

Closed
sormuras opened this issue Jun 24, 2019 · 8 comments
Closed

Support Text Blocks (JEP 368) #382

sormuras opened this issue Jun 24, 2019 · 8 comments

Comments

@sormuras
Copy link
Contributor

sormuras commented Jun 24, 2019

A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired. This is a preview language feature in JDK 13.

Syntax and more details at https://openjdk.java.net/jeps/355 https://openjdk.java.net/jeps/368

Formatting a Java source file...
java -jar google-java-format-1.7-all-deps.jar TextBlock.java

...that contains Text Blocks...

public class TextBlock {
  public static void main(String... args) {
    var block = """

    java --enable-preview --s\\o/urce 14 TextBlock.java
    openjdk                   |              -|-
    14-ea                     /\\  o           /\\
    2020-03-17
    """.indent(6);

    System.out.print(block);
  }
}

...fails with:

TextBlock.java:3:20: error: unclosed string literal
TextBlock.java:5:13: error: ';' expected
TextBlock.java:5:19: error: not a statement
TextBlock.java:5:30: error: ';' expected
TextBlock.java:5:31: error: illegal character: '\'
TextBlock.java:5:32: error: illegal character: '\'
TextBlock.java:5:34: error: not a statement
TextBlock.java:5:39: error: ';' expected
TextBlock.java:6:13: error: ';' expected
TextBlock.java:7:33: error: illegal character: '\'
TextBlock.java:7:32: error: not a statement
TextBlock.java:7:34: error: illegal character: '\'
TextBlock.java:7:50: error: illegal character: '\'
TextBlock.java:7:49: error: not a statement
TextBlock.java:7:51: error: illegal character: '\'
TextBlock.java:9:8: error: unclosed string literal
@sormuras
Copy link
Contributor Author

Some errors are reported when calling java with --enable-preview. Seems like that option isn't passed to the internal javac instance, methinks.

@sormuras
Copy link
Contributor Author

Or is error-prone not there, yet?

<javac.version>9+181-r4173-1</javac.version>

@sormuras
Copy link
Contributor Author

How about omit Text Blocks entirely from formatting?

@talios
Copy link

talios commented Aug 16, 2019

@sormuras if existing formatting changes the indent position of the start of the block, GJF should reindent the text block to preserve the gray-space eliding by the compiler. Reformatting shouldn't break tests, which that may.

@sormuras
Copy link
Contributor Author

if existing formatting changes [...]

Lines contributing to a """-String literal shouldn't be touched at all - as an initial work-around.

nakamura-to added a commit to domaframework/doma-it that referenced this issue Sep 6, 2019
@sormuras
Copy link
Contributor Author

if existing formatting changes the indent position of the start of the block, [...]

IIRC, the position of the start """ does not influence the white-space trim. The terminating """ does.

See https://blog.codefx.org/java/text-blocks/ (as jdk.java.net doesn't work at the moment)

@sormuras
Copy link
Contributor Author

sormuras commented Nov 5, 2019

Second Preview of Text Blocks issued: https://openjdk.java.net/jeps/368

New escape sequences
To allow finer control of the processing of newlines and white space, text blocks support two additional escape sequences not available in ordinary string literals.
First, the \<line-terminator> escape sequence explicitly suppresses the insertion of a newline character.
...
Second, the \s escape sequence simply translates to a single space (\u0020).
...

Updated issue title accordingly.

@sormuras sormuras changed the title Support Text Blocks (JEP 355) Support Text Blocks (JEP 368) Nov 5, 2019
@cushon
Copy link
Collaborator

cushon commented Mar 25, 2020

#436

@cushon cushon closed this as completed Mar 25, 2020
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

No branches or pull requests

3 participants