This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 228319 - "emulate" multi-line literals in java editors
Summary: "emulate" multi-line literals in java editors
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 7.4
Hardware: PC Mac OS X
: P3 normal with 1 vote (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-04-06 00:41 UTC by athompson
Modified: 2013-08-10 12:58 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description athompson 2013-04-06 00:41:29 UTC
It's a shame that java doesn't have scala's and groovy's "triple quote" operator, which allows for multi-line literals, and it doesn't appear likely to be added any time in the future. However, with the technology added to Netbeans for wrapping lines in the editor, it should be doable to "emulate" this feature. The actual code would still be a multi-line concatenation of strings, but Netbeans could present it in a much more readable, appealing, and editable format. This is the ideal time to implement this feature, because since java now has the String.format and MessageFormat.format functions, there is no longer a need to worry about mixing in variables.

[This article][1] has an interesting solution for the presentation issues involved with multi-line literals, and I borrowed from it for this proposal. my suggestion for multi-line literals would look like this (note the single quote followed immediately by a new line):

public void sayHello() {
    String name = ...
    String greeting = "
        Hello, %s!

            It's very nice to meet you! We promise you'll
        have lots of fun using NetBeans. NetBeans is the
        best IDE out there, and it's consistently the first
        to support the latest technologies.

        Sincerely,
        The NetBeans Team
    ";
    System.out.println(String.format(greeting, name));
}


For each new line, all leading whitespace (up until the normal indentation column) would be ignored. Since the editor is simply showing a prettier presentation of a multi-line literal, it would not be possible to navigate to prior columns; the letter "H" is effectively at column 1 (there should be a formatting setting allowing users to choose between the normal indentation level and no indentation for multi-line text). Whitespace after the normal indentation column is kept. The newline after the starting quote is not included, but any newline before the end quote *is* included. The leading whitespace on the line with the end quote is also ignored as usual.

In this case, the output would be:

---
Hello, Alvin!

    It's very nice to meet you! We promise you'll
have lots of fun using NetBeans. NetBeans is the
best IDE out there, and it's consistently the first
to support the latest technologies.

Sincerely,
The NetBeans Team
---

The actual text would be the same old concatenation of strings; Netbeans is simply presenting it differently:

public void sayHello() {
    String name = ...
    String greeting =
        "Hello, %s!\n" +
        "\n" +
        "    It's very nice to meet you! We promise you'll\n" +
        "have lots of fun using NetBeans. NetBeans is the\n" +
        "best IDE out there, and it's consistently the first\n" +
        "to support the latest technologies.\n" +
        "\n" +
        "Sincerely,\n" +
        "The NetBeans Team\n"
    ;
    System.out.println(String.format(greeting, name));
}

When editing, Netbeans would automatically escape the newlines (and tabs) and insert all the needed quotes and plus signs.


[1]: http://blog.joda.org/2008/01/java-7-multi-line-string-literals_594.html
Comment 1 athompson 2013-04-06 00:56:40 UTC
I should mention that there should be an easy and obvious way to switch between "presentation" mode and "literal" mode.
Comment 2 athompson 2013-04-08 16:23:24 UTC
I should mention the main driving factor for this is to make the editing of SQL/JPQL queries bearable, since they're used quite a bit these days and often get so long that you wind up putting in a great deal of effort simply editing them with all the quotes, extra spaces, and stuff you have to throw in. Think of how much more pleasant it would be to do just something like this:

Query q = em.createQuery("
    SELECT m
    FROM
        User u
        INNER JOIN u.group g
        INNER JOIN g.messages m
        INNER JOIN FETCH m.attachments
    WHERE
        u.userName = :userName
        AND m.subject = :subject
        AND u.status = :active
        AND g.status = :active
        AND m.status = :active
        AND m.messageStatus = :new
");

So obviously, JPQL syntax highlighting and CC working with this would be a plus.
Comment 3 markiewb 2013-08-10 12:58:42 UTC
Sounds like the feature "embedded languages" I've seen in IntelliJ Idea. 

IIRC NetBeans has also API to supported embedded languages. See the HTML support. HTML code is mixed up with CSS and JS. And every language has it's own syntax highlighting and code completion.