ensure no whitespaces/comments left after dequalify with type annotations (IDEA-165140)

This commit is contained in:
Anna.Kozlova
2016-12-07 18:44:34 +01:00
parent 4290a37a08
commit b086783d8d
4 changed files with 47 additions and 28 deletions
@@ -208,9 +208,16 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme
assert dot != null : this;
deleteChildRange(child.getPsi(), dot.getPsi());
List<PsiAnnotation> annotations = PsiTreeUtil.getChildrenOfTypeAsList(this, PsiAnnotation.class);
setAnnotations(annotations);
PsiModifierList modifierList = PsiImplUtil.findNeighbourModifierList(this);
if (modifierList != null) {
ASTNode ref = findChildByRole(ChildRole.REFERENCE_NAME);
assert ref != null : this;
PsiElement lastChild = ref.getPsi().getPrevSibling();
if (lastChild != null) {
modifierList.addRange(getFirstChild(), lastChild);
deleteChildRange(getFirstChild(), lastChild);
}
}
return;
}
@@ -614,31 +621,6 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme
return annotations;
}
private void setAnnotations(List<PsiAnnotation> annotations) {
if (annotations.isEmpty()) return;
PsiElement newParent = this;
PsiElement anchor = SourceTreeToPsiMap.treeElementToPsi(findChildByType(JavaTokenType.DOT));
if (anchor == null) {
PsiModifierList modifierList = PsiImplUtil.findNeighbourModifierList(this);
if (modifierList != null) {
newParent = modifierList;
}
}
for (PsiAnnotation annotation : annotations) {
if (annotation.getParent() != newParent) {
if (anchor != null) {
newParent.addAfter(annotation, anchor);
}
else {
newParent.add(annotation);
}
annotation.delete();
}
}
}
private boolean isFullyQualified(@NotNull PsiFile containingFile) {
int kind = getKind(containingFile);
switch (kind) {
@@ -0,0 +1,11 @@
import java.lang.annotation.*;
@Target({ElementType.TYPE_USE})
@interface TA { }
class Test {
private void bar(java.util.@TA List<String> na<caret>me) {
}
}
@@ -0,0 +1,12 @@
import java.lang.annotation.*;
import java.util.List;
@Target({ElementType.TYPE_USE})
@interface TA { }
class Test {
private void bar(@TA List<String> name) {
}
}
@@ -81,6 +81,20 @@ public class ShortenClassReferencesTest extends LightCodeInsightFixtureTestCase
"class Outer1 {class Inner {} {boolean b = new Inner() instanceof Inner;}}");
}
public void testWhiteSpaceForMovedTypeAnnotations() throws Exception {
myFixture.configureByFile(getTestName(false) + ".java");
PsiElement elementAtCaret = myFixture.getElementAtCaret();
assertTrue(elementAtCaret instanceof PsiParameter);
WriteCommandAction.runWriteCommandAction(getProject(),
() -> {
PsiTypeElement typeElement = (PsiTypeElement)JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(((PsiParameter)elementAtCaret).getTypeElement());
assertTrue(typeElement != null && typeElement.isValid());
assertEquals("List<String>", typeElement.getText());
});
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
private void doTest() {
myFixture.configureByFile(getTestName(false) + ".java");
doShortenRefs();