Comment saver is more clever about comma's
This commit is contained in:
@@ -230,7 +230,7 @@ public class CommentSaver(originalElements: PsiChildRange) {
|
|||||||
val anchor = chooseAnchor(anchorBefore, anchorAfter)
|
val anchor = chooseAnchor(anchorBefore, anchorAfter)
|
||||||
|
|
||||||
if (anchor != null) {
|
if (anchor != null) {
|
||||||
val anchorElement = findFinalAnchorElement(anchor)
|
val anchorElement = findFinalAnchorElement(anchor, comment)
|
||||||
val parent = anchorElement.getParent()
|
val parent = anchorElement.getParent()
|
||||||
if (anchor.before) {
|
if (anchor.before) {
|
||||||
parent.addAfter(comment, anchorElement)
|
parent.addAfter(comment, anchorElement)
|
||||||
@@ -288,21 +288,29 @@ public class CommentSaver(originalElements: PsiChildRange) {
|
|||||||
return anchorBefore //TODO: more analysis?
|
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>()
|
val tokensBetween = anchor.treeElementsBetween.filterIsInstance<TokenTreeElement>()
|
||||||
var psiElement = anchor.element
|
|
||||||
if (tokensBetween.isEmpty()) return psiElement
|
|
||||||
|
|
||||||
fun PsiElement.next(): PsiElement? {
|
fun PsiElement.next(): PsiElement? {
|
||||||
val filter = { element: PsiElement -> element !is PsiWhiteSpace && element.getTextLength() > 0 }
|
val filter = { element: PsiElement -> element !is PsiWhiteSpace && element.getTextLength() > 0 }
|
||||||
return if (anchor.before) nextLeaf(filter) else prevLeaf(filter)
|
return if (anchor.before) nextLeaf(filter) else prevLeaf(filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var psiElement = anchor.element
|
||||||
for (token in tokensBetween.reverse()) {
|
for (token in tokensBetween.reverse()) {
|
||||||
val next = psiElement.next() ?: break
|
val next = psiElement.next() ?: break
|
||||||
if (next.getNode().getElementType() != token.tokenType) break
|
if (next.getNode().getElementType() != token.tokenType) break
|
||||||
psiElement = next
|
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
|
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")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public static class KeepComments extends AbstractQuickFixTest {
|
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 {
|
public void testAllFilesPresentInKeepComments() throws Exception {
|
||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/deprecatedSymbolUsage/keepComments"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/deprecatedSymbolUsage/keepComments"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user