# Alignment algorithm (was: Wrapping up the first two courses)

Brian Goetz brian.goetz at oracle.com
Mon Apr 22 16:04:35 UTC 2019

On 4/22/2019 11:26 AM, Jim Laskey wrote:
> Current "strip incidentals" algorithm as captured in String::align
> (string-tapas branch)
>
>     public String align(int n) {
>         if (isEmpty()) {
> return "";
>         }
>         long count = lines().count();
>         if (count == 1) {
> return strip();
>         }
>         int outdent = lines().skip(1)
>              .filter(not(String::isBlank))
>              .mapToInt(String::indexOfNonWhitespace)
>              .min()
>              .orElse(0);
>         String last = lines().skip(count - 1).findFirst().orElse("");
>         boolean lastIsBlank = last.isBlank();
>         if (lastIsBlank) {
> outdent = Integer.min(outdent, last.length());
>         }
>         return indentStream(lines(1, 1), n - outdent).map(s ->
> s.stripTrailing())
>  .collect(Collectors.joining("\n", "", lastIsBlank ? "\n" : ""));
>     }
>
>
> 2. long count = lines().count();
>    if (count == 1) {
>        return strip();
>    }
>
> Single line strings (no line terminators) are simply stripped.
>
>     """  single line  """ ==> "single line"

I think we should reconsider this one.  The interpretation we settled on
is: we're willing to treat a multi-line string as being a sequence of
lines, not just of characters, and we're willing to strip incidental
whitespace that arises from accidents of how the string is embedded in
the program.  But a single-line string doesn't have any of that; I think
it should be left alone, regardless of quotes.

>
> 3.  int outdent = lines().skip(1) ...
>
> Ignoring first line, determine least number of leading whitespaces for all
> non-blank lines.
>
>     String s = """
> ................line 1..
> ....................line 2.
> """;  ==> 16

I think we should reconsider whether a non-blank first line means that
we should consider any indentation on the first line too.  This has the
likely-beneficial side-effect that having a non-blank character
immediately following the """ effectively means "no stripping."

Considering the indentation of the _last_ blank line gives the user more
control while not requiring the user to distort indentation for common
cases.  So +1 to "CDI".

>
> Options;
>
> 2. a) Single line strings would be just stripLeading, but should
> beconsistent
> with multi-line and stripTrailing.
>
>     """  single line  """ ==> "single line  "
>     b) We could do nothing for single line.
>
>     """  single line  """ ==> "  single line  "

I vote (b).

> 5. a) If we omit close delimiter influence, only the content
> influences the
> indentation.  Loss of control by the user.

I think CDI is fine.

> 6. a) Could strip all leading/trailing blank lines, but awkward to
> recover the
> LOI. Not recommending.

Agreed.
> 9. a) Always add a last \n. Loss of control by the user.

The current behavior pairs nicely with CDI.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20190422/827ee949/attachment.html>