eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Formatter: New options for new lines within array initializer braces

Open
#2,511 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

Right now, for new lines _within_ array initializers, there's only the two toggles: "After opening brace of array initializer" and "Before closing brace of array initializer".
What I would like specifically is to have - in addition to the two static options `insert` and `do not insert` - some dynamic options that you would otherwise find for parentheses like "insert if wrapped" and "insert if not empty".

Here's examples of what's currently possible (correct me if I'm wrong) and what I would like:

**Currently possible:**
```java
// either this:
// ----------

int[] myArray = {1, 2};
int[] myArray = {1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20};

method(new String[] {1, 2});

@Annotation(
attribute = {@NestedAnnotation(attr1 = "somevalue", attr2 = "somevalue"),
@NestedAnnotation(attr1 = "someothervalue", attr2 = "someothervalue")}
)

// or that:
// ----------

int[] myArray = {
1, 2
};
int[] myArray = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20
};

method(new String[] {
1, 2
});

@Annotation(attribute = {
@NestedAnnotation(attr1 = "somevalue", attr2 = "somevalue"),
@NestedAnnotation(attr1 = "someothervalue", attr2 = "someothervalue")
})
```

**What I would like:**
```java
// keep short array initializers on one line:
int[] myArray = {1, 2};
// especially as they might be used inline:
method(new String[] {1, 2});

// wrap longer array initializers like this:
int[] myArray = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20
};

@Annotation(attribute = {
@NestedAnnotation(attr1 = "somevalue", attr2 = "somevalue"),
@NestedAnnotation(attr1 = "someothervalue", attr2 = "someothervalue")
})

// bonus points for this intermediary state:
int[] myArray =
{1, 2, 3, 4, 5, 6, 7, 8, 9};
```

Contributor guide

Open the contributing guide

Research direction

Start by locating the formatter settings and logic for array-initializer braces, then compare them with the existing parenthesis options for wrapped and non-empty constructs. The work is done when array initializers support the requested dynamic newline behavior and the short, wrapped, nested-annotation, and bonus examples format as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.