CommentTracker: do not add comments at the beginning of reference qualifier: add it to the parent element instead

This commit is contained in:
Tagir Valeev
2018-02-04 16:34:23 +07:00
parent 71eea37e04
commit 5bfb15ec8c
4 changed files with 31 additions and 27 deletions
@@ -1,14 +1,18 @@
// "Add on demand static import for 'java.util.Arrays'" "true"
import java.util.*;
import static java.util.Arrays.*;
class Foo {
void test(String[] foos, String[] bars) {
System.out.println(/*foos1*//*foos2*/asList(foos)+":"+ /*bars1*//*bars2*/asList(bars));
System.out.println(/*foos1*//*foos2*/asList(foos)+":"+/*bars1*//*bars2*/asList(bars));
}
void test2(String[] foos, String[] bars) {
System.out.println(/*foos1*//*foos2*/asList(foos)+":"+ /*bars1*//*bars2*/asList(bars));
System.out.println(/*foos0*//*foos1*//*foos2*/asList(foos)+":"+/*bars0*//*bars1*//*bars2*/asList(bars));
}
void test3(String[] foos, String[] bars) {
System.out.println(//line comment
asList(foos)+":"+//line comment
asList(bars));
}
}
@@ -1,10 +1,8 @@
// "Add on demand static import for 'java.util.Arrays'" "true"
import java.util.Arrays;
import static java.util.Arrays.*;
public class MyFile {
void test() {
System.out.println(/*1*//*2*/asList(/*3*//*4*/asList("foo", /*5*//*6*/asList("bar", "baz")), /*7*//*8*/asList("qux")));
System.out.println(/*1*//*2*/asList(/*3*//*4*/asList("foo", /*5*/ /*6*/ asList("bar", "baz")), /*7*/ /*8*/ asList("qux")));
}
}
@@ -1,12 +1,16 @@
// "Add on demand static import for 'java.util.Arrays'" "true"
import java.util.*;
class Foo {
void test(String[] foos, String[] bars) {
System.out.println(<caret>Arrays/*foos1*/./*foos2*/asList(foos)+":"+Arrays/*bars1*/./*bars2*/asList(bars));
}
void test2(String[] foos, String[] bars) {
System.out.println(Arrays/*foos1*/./*foos2*/asList(foos)+":"+Arrays/*bars1*/./*bars2*/asList(bars));
System.out.println(/*foos0*/Arrays/*foos1*/./*foos2*/asList(foos)+":"+/*bars0*/Arrays/*bars1*/./*bars2*/asList(bars));
}
void test3(String[] foos, String[] bars) {
System.out.println(Arrays.//line comment
asList(foos)+":"+Arrays.//line comment
asList(bars));
}
}
@@ -1,6 +1,4 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.psiutils;
import com.intellij.lang.ASTFactory;
@@ -112,7 +110,18 @@ public class CommentTracker {
*/
public void deleteAndRestoreComments(@NotNull PsiElement element) {
grabCommentsOnDelete(element);
insertCommentsBefore(element instanceof PsiVariable ? element.getParent() : element);
PsiElement anchor = element;
if (element instanceof PsiVariable) {
anchor = element.getParent();
}
else if ((element.getParent() instanceof PsiJavaCodeReferenceElement &&
((PsiJavaCodeReferenceElement)element.getParent()).getQualifier() == element)) {
anchor = element.getParent();
if (anchor.getParent() instanceof PsiMethodCallExpression) {
anchor = anchor.getParent();
}
}
insertCommentsBefore(anchor);
element.delete();
}
@@ -262,21 +271,10 @@ public class CommentTracker {
}
private void grabCommentsOnDelete(PsiElement element) {
if (element instanceof PsiExpression && element.getParent() instanceof PsiExpressionStatement) {
if (element instanceof PsiExpression && element.getParent() instanceof PsiExpressionStatement ||
element.getParent() instanceof PsiJavaCodeReferenceElement) {
element = element.getParent();
}
if (element.getParent() instanceof PsiJavaCodeReferenceElement) {
PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement)element.getParent();
if (element == ref.getQualifier()) {
for (PsiElement child : ref.getChildren()) {
if(child.textMatches(".")) {
break;
}
grabComments(child);
}
return;
}
}
grabComments(element);
}