Skip to content

Commit

Permalink
more tunning
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Nov 8, 2023
1 parent 26f87d7 commit 61f2836
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ public J visitClassBody(KtClassBody classBody, ExecutionContext data) {
continue;
}
Statement statement = convertToStatement(d.accept(this, data));
list.add(maybeFollowingSemicolon(statement, d));
list.add(maybeSemicolon(statement, d));
}

return new J.Block(
Expand Down Expand Up @@ -3337,7 +3337,7 @@ private J.Block convertToBlock(KtExpression ktExpression, ExecutionContext data)
}

private <J2 extends J> JRightPadded<J2> maybeSemicolon(J2 j, KtElement element) {
PsiElement maybeSemicolon = element.getLastChild();
PsiElement maybeSemicolon = findLastNotSpaceChild(element);
boolean hasSemicolon = maybeSemicolon instanceof LeafPsiElement && ((LeafPsiElement) maybeSemicolon).getElementType() == KtTokens.SEMICOLON;

if (hasSemicolon) {
Expand All @@ -3346,7 +3346,7 @@ private <J2 extends J> JRightPadded<J2> maybeSemicolon(J2 j, KtElement element)

maybeSemicolon = findFirstNonSpaceNextSibling(element);
if (maybeSemicolon instanceof LeafPsiElement && ((LeafPsiElement) maybeSemicolon).getElementType() == KtTokens.SEMICOLON) {
return new JRightPadded<>(j, prefix(maybeSemicolon), Markers.EMPTY.add(new Semicolon(randomId())));
return new JRightPadded<>(j, deepPrefix(maybeSemicolon), Markers.EMPTY.add(new Semicolon(randomId())));
}

return padRight(j, Space.EMPTY);
Expand Down Expand Up @@ -3502,7 +3502,7 @@ private Space endFix(@Nullable PsiElement element, @Nullable Set<PsiElement> con
return Space.EMPTY;
}

Space end = endFix(element.getLastChild());
Space end = endFix(findLastNotSpaceChild(element));
PsiElement lastSpace = findLastSpaceChild(element);
if (consumedSpaces != null && consumedSpaces.contains(element)) {
return end;
Expand Down Expand Up @@ -3898,13 +3898,26 @@ private PsiElement findFirstNotSpaceChild(PsiElement parent) {
}

@Nullable
private PsiElement findLastSpaceChild(@Nullable PsiElement parent) {
PsiElement ret = null;
private PsiElement findLastNotSpaceChild(@Nullable PsiElement parent) {
if (parent == null) {
return null;
}

for (PsiElement child = parent.getLastChild(); child != null; child = child.getPrevSibling()) {
if (!isSpace(child.getNode())) {
return child;
}
}
return null;
}

@Nullable
private PsiElement findLastSpaceChild(@Nullable PsiElement parent) {
if (parent == null) {
return null;
}

PsiElement ret = null;
for (PsiElement child = parent.getLastChild(); child != null; child = child.getPrevSibling()) {
if (isSpace(child.getNode())) {
ret = child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ void packageAndComments() {
rewriteRun(
kotlin(
"""
/* Comment */
/* C0 */
package a
import java.util.List
class A
// comment
class A /*C1*/
// C2
""",
SourceSpec::noTrim
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ void trailingSemicolon() {
kotlin(
"""
class Test {
var t = 1;
var t = 1 /*C1*/ ; /*C2*/
fun method() {}
}
"""
Expand Down

0 comments on commit 61f2836

Please sign in to comment.