eclipse-xtext / eclipse-xtext/xtext
Single line comments after code removes newlines when formatted
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 831
- Forks
- 330
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 12
Description
Having a single line comment after some code removes newlines when the code is formatted.
For example formatting:
def method() {// test
val test = 0
}
Results in:
def method() {// test
val test = 0
}
Whilst formatting:
def method() {
val test = 0
}
Results in:
def method() {
val test = 0
}
I fixed this problem in my case by adding the following code after this line.
I am not certain that this is the right place to fix it though.
// The amount of newlines in this space, sometimes a newline gets caught in a single line comment before this hidden region
val actualNewlines = region.getLineCount() - 1
// Since the newline got caught, newLineCount is one too small
if(actualNewlines > 0 && trailingNewLinesOfPreviousRegion > 0) {
if (formatting.newLineMin !== null && actualNewlines + trailingNewLinesOfPreviousRegion < formatting.newLineMin) {
newLineCount = formatting.newLineMin - trailingNewLinesOfPreviousRegion
} else if (formatting.newLineMax !== null && actualNewlines + trailingNewLinesOfPreviousRegion > formatting.newLineMax) {
newLineCount = formatting.newLineMax - trailingNewLinesOfPreviousRegion
} else {
// We were within range, so just don't mess up our existing newlines
newLineCount = actualNewlines
}
}
My solution assumes that trailingNewLinesOfPreviousRegion is only different from zero in the case that a single line comment precedes the current hidden region.
When we arrive at the replacement, newLineCount will be off by one because one of the newlines resides in the previous single line comment.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in org.eclipse.xtext/src/org/eclipse/xtext/formatting2/internal/WhitespaceReplacer.java around the referenced lines 76 and 92, then reproduce the formatter behavior with the two examples in the issue. Trace how trailingNewLinesOfPreviousRegion and newLineCount are calculated when a single-line comment precedes a hidden region. Done means formatting preserves the demonstrated blank line while still respecting the configured newline bounds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100