diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java index a8471ab2e96..5bc1c75db9b 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassHelper.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; @@ -13,24 +14,33 @@ import java.util.List; * @author svtk */ public class ImportClassHelper { - public static void perform(@NotNull JetType type, @NotNull PsiElement element, @NotNull PsiElement newElement) { - PsiElement parent = element; + public static void perform(@NotNull JetType type, @NotNull PsiElement elementToReplace, @NotNull PsiElement newElement) { + if (JetPluginUtil.checkTypeIsStandard(type, elementToReplace.getProject()) || ErrorUtils.isError(type.getMemberScope().getContainingDeclaration())) { + elementToReplace.replace(newElement); + return; + } + perform(JetPluginUtil.computeTypeFullName(type), elementToReplace, newElement); + } + + public static void perform(@NotNull String typeFullName, @NotNull JetNamespace namespace) { + perform(typeFullName, namespace, null, null); + } + + public static void perform(@NotNull String typeFullName, @NotNull PsiElement elementToReplace, @NotNull PsiElement newElement) { + PsiElement parent = elementToReplace; while (!(parent instanceof JetNamespace)) { parent = parent.getParent(); assert parent != null; } - JetNamespace namespace = (JetNamespace) parent; + perform(typeFullName, (JetNamespace) parent, elementToReplace, newElement); + } + + public static void perform(@NotNull String typeFullName, @NotNull JetNamespace namespace, @Nullable PsiElement elementToReplace, @Nullable PsiElement newElement) { List importDirectives = namespace.getImportDirectives(); - if (JetPluginUtil.checkTypeIsStandard(type, element.getProject()) || ErrorUtils.isError(type.getMemberScope().getContainingDeclaration())) { - element.replace(newElement); - return; - } - - String typeFullName = JetPluginUtil.computeTypeFullName(type); - - JetImportDirective newDirective = JetPsiFactory.createImportDirective(element.getProject(), typeFullName); + JetImportDirective newDirective = JetPsiFactory.createImportDirective(namespace.getProject(), typeFullName); + String lineSeparator = System.getProperty("line.separator"); if (!importDirectives.isEmpty()) { boolean isPresent = false; for (JetImportDirective directive : importDirectives) { @@ -42,7 +52,7 @@ public class ImportClassHelper { if (!isPresent) { JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1); lastDirective.getParent().addAfter(newDirective, lastDirective); - lastDirective.getParent().addAfter(JetPsiFactory.createWhiteSpace(element.getProject(), "\n"), lastDirective); + lastDirective.getParent().addAfter(JetPsiFactory.createWhiteSpace(namespace.getProject(), lineSeparator), lastDirective); } } else { @@ -50,9 +60,10 @@ public class ImportClassHelper { assert !declarations.isEmpty(); JetDeclaration firstDeclaration = declarations.iterator().next(); firstDeclaration.getParent().addBefore(newDirective, firstDeclaration); - firstDeclaration.getParent().addBefore(JetPsiFactory.createWhiteSpace(element.getProject(), "\n\n"), firstDeclaration); + firstDeclaration.getParent().addBefore(JetPsiFactory.createWhiteSpace(namespace.getProject(), lineSeparator + lineSeparator), firstDeclaration); + } + if (elementToReplace != null && newElement != null && elementToReplace != newElement) { + elementToReplace.replace(newElement); } - element.replace(newElement); - parent.replace(namespace); } }