Skip to content

Commit

Permalink
support CRLF in BlockComment
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Nov 10, 2023
1 parent aa3df33 commit a924b79
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3814,24 +3814,10 @@ private Space toSpace(@Nullable PsiElement element) {

IElementType elementType = element.getNode().getElementType();
if (elementType == KtTokens.WHITE_SPACE) {
String whiteSpace = element.getText();

// replace `\n` to CRLF back if it's CRLF in the source
TextRange range = element.getTextRange();
int left = findFirstGreaterOrEqual(cRLFLocations, range.getStartOffset());
int right = left != -1 ? findFirstLessOrEqual(cRLFLocations, range.getEndOffset(), left) : -1;
boolean hasCRLF = left != -1 && left <= right;

if (hasCRLF) {
for (int i = right; i >= left; i--) {
whiteSpace = replaceNewLineWithCRLF(whiteSpace, cRLFLocations.get(i) - range.getStartOffset());
}
}

return Space.build(whiteSpace, emptyList());
return Space.build(maybeAdjustCRLF(element), emptyList());
} else if (elementType == KtTokens.EOL_COMMENT ||
elementType == KtTokens.BLOCK_COMMENT) {
String nodeText = element.getText();
String nodeText = maybeAdjustCRLF(element);
boolean isBlockComment = ((PsiComment) element).getTokenType() == KtTokens.BLOCK_COMMENT;
String comment = isBlockComment ? nodeText.substring(2, nodeText.length() - 2) : nodeText.substring(2);
List<Comment> comments = new ArrayList<>(1);
Expand All @@ -3844,6 +3830,25 @@ private Space toSpace(@Nullable PsiElement element) {
return Space.EMPTY;
}

// replace `\n` to CRLF back if it's CRLF in the source
private String maybeAdjustCRLF(PsiElement element) {
String text = element.getText();
if (!isSpace(element.getNode())) {
return text;
}

TextRange range = element.getTextRange();
int left = findFirstGreaterOrEqual(cRLFLocations, range.getStartOffset());
int right = left != -1 ? findFirstLessOrEqual(cRLFLocations, range.getEndOffset(), left) : -1;
boolean hasCRLF = left != -1 && left <= right;
if (hasCRLF) {
for (int i = right; i >= left; i--) {
text = replaceNewLineWithCRLF(text, cRLFLocations.get(i) - range.getStartOffset());
}
}
return text;
}

private Space kdocToSpace(KDoc kDoc) {
StringBuilder sb = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,6 @@ public fun method() {}
);
}

@ExpectedToFail("CRLF in BLOCK_COMMENT to be handled")
@SuppressWarnings("TextBlockMigration")
@Test
void blockCommentCRLF() {
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/org/openrewrite/kotlin/tree/CRLFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

import static org.openrewrite.kotlin.Assertions.kotlin;

@SuppressWarnings("All")
class CRLFTest implements RewriteTest {

@Test
void crlf() {
rewriteRun(
kotlin(
"package some.other.name\r\n" + "class A { }\r\n" + "class B { }"
"package some.other.name\r\n" +
"class A { }\r\n" +
"class B { }"
)
);
}
Expand All @@ -35,7 +38,9 @@ void crlf() {
void consecutiveCRLF() {
rewriteRun(
kotlin(
"package some.other.name\r\n\r\n\r\n" + "class A { }\r\n\r\n\r\n" + "class B { }"
"package some.other.name\r\n\r\n\r\n" +
"class A { }\r\n\r\n\r\n" +
"class B { }"
)
);
}
Expand Down Expand Up @@ -64,4 +69,17 @@ void crlfInKdoc() {
)
);
}

@Test
void crlfInBlockComment() {
rewriteRun(
kotlin(
"public class A {\r\n" +
"/*a\r\n" +
" b*/\r\n" +
"public fun method() {}\r\n" +
"}"
)
);
}
}

0 comments on commit a924b79

Please sign in to comment.