From 5ce12d66587cae72bcae525cb93b4b61fa11ce2f Mon Sep 17 00:00:00 2001 From: Denis Zhdanov Date: Tue, 10 Aug 2010 09:57:57 +0400 Subject: [PATCH] IDEA-57201 "Optimize imports" drops comments between import statements Took static imports into consideration --- .../impl/source/codeStyle/ImportHelper.java | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java index cf68a02fa6e4..64f3e0545008 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java @@ -67,9 +67,9 @@ public class ImportHelper{ // /* comment */ // import b; // We want to preserve those comments then. - List comments = new ArrayList(); + List nonImports = new ArrayList(); // Note: this array may contain ".*" for unresolved imports! - List> names = new ArrayList>(collectNamesToImport(file, comments)); + List> names = new ArrayList>(collectNamesToImport(file, nonImports)); Collections.sort(names, new Comparator>() { public int compare(Pair o1, Pair o2) { return o1.getFirst().compareTo(o2.getFirst()); @@ -140,8 +140,8 @@ public class ImportHelper{ try { StringBuilder text = buildImportListText(resultList, classesOrPackagesToImportOnDemand, classesToUseSingle); - for (String comment : comments) { - text.append("\n").append(comment); + for (PsiElement nonImport : nonImports) { + text.append("\n").append(nonImport); } String ext = StdFileTypes.JAVA.getDefaultExtension(); PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject()); @@ -153,15 +153,17 @@ public class ImportHelper{ PsiImportList result = (PsiImportList)newImportList.copy(); PsiImportList oldList = file.getImportList(); if (oldList.isReplaceEquivalent(result)) return null; - PsiElement firstPrevious = newImportList.getPrevSibling(); - while (firstPrevious != null && firstPrevious.getPrevSibling() != null) { - firstPrevious = firstPrevious.getPrevSibling(); - } - for (PsiElement element = firstPrevious; element != null && element != newImportList; element = element.getNextSibling()) { - result.add(element.copy()); - } - for (PsiElement element = newImportList.getNextSibling(); element != null; element = element.getNextSibling()) { - result.add(element.copy()); + if (!nonImports.isEmpty()) { + PsiElement firstPrevious = newImportList.getPrevSibling(); + while (firstPrevious != null && firstPrevious.getPrevSibling() != null) { + firstPrevious = firstPrevious.getPrevSibling(); + } + for (PsiElement element = firstPrevious; element != null && element != newImportList; element = element.getNextSibling()) { + result.add(element.copy()); + } + for (PsiElement element = newImportList.getNextSibling(); element != null; element = element.getNextSibling()) { + result.add(element.copy()); + } } return result; } @@ -626,7 +628,7 @@ public class ImportHelper{ @NotNull // returns list of (name, isImportStatic) pairs - private static Collection> collectNamesToImport(@NotNull PsiJavaFile file, List comments){ + private static Collection> collectNamesToImport(@NotNull PsiJavaFile file, List comments){ Set> names = new THashSet>(); final JspFile jspFile = JspPsiUtil.getJspFile(file); @@ -647,7 +649,7 @@ public class ImportHelper{ } private static void collectNamesToImport(@NotNull final Set> names, - @NotNull List comments, + @NotNull List comments, @NotNull final PsiJavaFile file, PsiFile context) { String packageName = file.getPackageName(); @@ -659,7 +661,7 @@ public class ImportHelper{ } private static void addNamesToImport(@NotNull Set> names, - @NotNull List comments, + @NotNull List comments, @NotNull PsiElement scope, @NotNull String thisPackageName, PsiFile context){ @@ -671,9 +673,18 @@ public class ImportHelper{ final PsiElement child = stack.removeFirst(); if (child instanceof PsiImportList) { for (PsiElement element : child.getChildren()) { - IElementType elementType = element.getNode().getElementType(); - if (!JavaElementType.IMPORT_STATEMENT.equals(elementType) && !ElementType.WHITE_SPACE_BIT_SET.contains(elementType)) { - comments.add(element.getText()); + if (element == null) { + continue; + } + ASTNode node = element.getNode(); + if (node == null) { + continue; + } + IElementType elementType = node.getElementType(); + if (elementType != null &&!ElementType.IMPORT_STATEMENT_BASE_BIT_SET.contains(elementType) + && !ElementType.WHITE_SPACE_BIT_SET.contains(elementType)) + { + comments.add(element); } } continue;