Comment saver is more clever about comma's

This commit is contained in:
Valentin Kipyatkov
2015-05-26 14:44:11 +03:00
parent d3403dee1a
commit d5d1158e44
4 changed files with 44 additions and 4 deletions
@@ -230,7 +230,7 @@ public class CommentSaver(originalElements: PsiChildRange) {
val anchor = chooseAnchor(anchorBefore, anchorAfter)
if (anchor != null) {
val anchorElement = findFinalAnchorElement(anchor)
val anchorElement = findFinalAnchorElement(anchor, comment)
val parent = anchorElement.getParent()
if (anchor.before) {
parent.addAfter(comment, anchorElement)
@@ -288,21 +288,29 @@ public class CommentSaver(originalElements: PsiChildRange) {
return anchorBefore //TODO: more analysis?
}
private fun findFinalAnchorElement(anchor: Anchor): PsiElement {
private fun findFinalAnchorElement(anchor: Anchor, comment: PsiComment): PsiElement {
val tokensBetween = anchor.treeElementsBetween.filterIsInstance<TokenTreeElement>()
var psiElement = anchor.element
if (tokensBetween.isEmpty()) return psiElement
fun PsiElement.next(): PsiElement? {
val filter = { element: PsiElement -> element !is PsiWhiteSpace && element.getTextLength() > 0 }
return if (anchor.before) nextLeaf(filter) else prevLeaf(filter)
}
var psiElement = anchor.element
for (token in tokensBetween.reverse()) {
val next = psiElement.next() ?: break
if (next.getNode().getElementType() != token.tokenType) break
psiElement = next
}
// don't put end of line comment right before comma
if (anchor.before && comment.getNode().getElementType() == JetTokens.EOL_COMMENT) {
val next = psiElement.next()
if (next != null && next.getNode().getElementType() == JetTokens.COMMA) {
psiElement = next
}
}
return psiElement
}
}
@@ -0,0 +1,13 @@
// "Replace with 'newFun(p1, -1, p2, p3, -2)'" "true"
@deprecated("", ReplaceWith("newFun(p1, -1, p2, p3, -2)"))
fun oldFun(p1: Int, p2: Int, p3: Int){}
fun newFun(p1: Int, a: Int, p2: Int, p3: Int, b: Int){}
fun foo() {
<caret>oldFun(0, // 0
1, // 1
2 // 2
)
@@ -0,0 +1,13 @@
// "Replace with 'newFun(p1, -1, p2, p3, -2)'" "true"
@deprecated("", ReplaceWith("newFun(p1, -1, p2, p3, -2)"))
fun oldFun(p1: Int, p2: Int, p3: Int){}
fun newFun(p1: Int, a: Int, p2: Int, p3: Int, b: Int){}
fun foo() {
newFun(0, // 0
-1, 1, // 1
2, // 2
-2)
@@ -3221,6 +3221,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class KeepComments extends AbstractQuickFixTest {
@TestMetadata("addArguments.kt")
public void testAddArguments() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/keepComments/addArguments.kt");
doTest(fileName);
}
public void testAllFilesPresentInKeepComments() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/deprecatedSymbolUsage/keepComments"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}