SortContentAction: preserve comments in corner case

This commit is contained in:
Roman.Ivanov
2018-03-19 12:58:11 +07:00
parent 0800dca8a8
commit 4e34325187
3 changed files with 59 additions and 4 deletions
@@ -503,9 +503,7 @@ public class SortContentAction extends PsiElementBaseIntentionAction {
void generate(StringBuilder sb, boolean isLastInRow, boolean isLast) {
sb.append(myExpression.getText());
for (PsiElement element : myBeforeSeparator) {
sb.append(element.getText());
}
handleElementsBeforeSeparator(sb, isLast);
if (!isLast) {
sb.append(",");
}
@@ -513,7 +511,7 @@ public class SortContentAction extends PsiElementBaseIntentionAction {
for (PsiComment comment : myAfterSeparator) {
sb.append(" ")
.append(comment.getText());
if (comment.getText().contains("//")) {
if (comment.getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
sb.append("\n");
newLineSet = true;
}
@@ -525,5 +523,22 @@ public class SortContentAction extends PsiElementBaseIntentionAction {
sb.append("\n");
}
}
private void handleElementsBeforeSeparator(StringBuilder sb, boolean isLast) {
boolean newLineNeed = false;
for (PsiElement element : myBeforeSeparator) {
if (newLineNeed) {
sb.append('\n');
newLineNeed = false;
}
sb.append(element.getText());
if (element instanceof PsiComment && ((PsiComment)element).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
newLineNeed = true;
}
}
if (!isLast && newLineNeed) {
sb.append('\n');
}
}
}
}
@@ -0,0 +1,22 @@
// "Sort content" "true"
import java.util.*;
public class Main {
private static void foo(String a, String... vararg) {}
private void test() {
foo(/*6*/"bar", // 1
// 4
"bar",
"baz"// 7
// 8
,
"foo"/*3*/ // 2
// 5
);
}
}
@@ -0,0 +1,18 @@
// "Sort content" "true"
import java.util.*;
public class Main {
private static void foo(String a, String... vararg) {}
private void test() {
foo(/*6*/"bar", // 1
// 4
"foo"/*3*/, // 2
// 5
<caret>"bar",
"baz"// 7
// 8
);
}
}