Merge remote branch 'origin/master'

This commit is contained in:
Alex Tkachman
2011-11-24 17:38:32 +02:00
@@ -2,6 +2,7 @@ package org.jetbrains.jet.plugin.quickfix;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
@@ -13,24 +14,33 @@ import java.util.List;
* @author svtk * @author svtk
*/ */
public class ImportClassHelper { public class ImportClassHelper {
public static void perform(@NotNull JetType type, @NotNull PsiElement element, @NotNull PsiElement newElement) { public static void perform(@NotNull JetType type, @NotNull PsiElement elementToReplace, @NotNull PsiElement newElement) {
PsiElement parent = element; 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)) { while (!(parent instanceof JetNamespace)) {
parent = parent.getParent(); parent = parent.getParent();
assert parent != null; 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<JetImportDirective> importDirectives = namespace.getImportDirectives(); List<JetImportDirective> importDirectives = namespace.getImportDirectives();
if (JetPluginUtil.checkTypeIsStandard(type, element.getProject()) || ErrorUtils.isError(type.getMemberScope().getContainingDeclaration())) { JetImportDirective newDirective = JetPsiFactory.createImportDirective(namespace.getProject(), typeFullName);
element.replace(newElement);
return;
}
String typeFullName = JetPluginUtil.computeTypeFullName(type);
JetImportDirective newDirective = JetPsiFactory.createImportDirective(element.getProject(), typeFullName);
String lineSeparator = System.getProperty("line.separator");
if (!importDirectives.isEmpty()) { if (!importDirectives.isEmpty()) {
boolean isPresent = false; boolean isPresent = false;
for (JetImportDirective directive : importDirectives) { for (JetImportDirective directive : importDirectives) {
@@ -42,7 +52,7 @@ public class ImportClassHelper {
if (!isPresent) { if (!isPresent) {
JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1); JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
lastDirective.getParent().addAfter(newDirective, lastDirective); lastDirective.getParent().addAfter(newDirective, lastDirective);
lastDirective.getParent().addAfter(JetPsiFactory.createWhiteSpace(element.getProject(), "\n"), lastDirective); lastDirective.getParent().addAfter(JetPsiFactory.createWhiteSpace(namespace.getProject(), lineSeparator), lastDirective);
} }
} }
else { else {
@@ -50,9 +60,10 @@ public class ImportClassHelper {
assert !declarations.isEmpty(); assert !declarations.isEmpty();
JetDeclaration firstDeclaration = declarations.iterator().next(); JetDeclaration firstDeclaration = declarations.iterator().next();
firstDeclaration.getParent().addBefore(newDirective, firstDeclaration); 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);
} }
} }