decouple from CodeStyleSettingsManager; introduce JavaCodeStyleSettingsFacade
authorDmitry Jemerov <yole@jetbrains.com>
Thu, 22 Sep 2011 16:47:05 +0000 (18:47 +0200)
committerDmitry Jemerov <yole@jetbrains.com>
Thu, 22 Sep 2011 17:31:38 +0000 (19:31 +0200)
14 files changed:
java/java-impl/src/com/intellij/codeInsight/intention/impl/AddSingleMemberStaticImportAction.java
java/java-impl/src/com/intellij/codeInspection/actions/ReplaceImplementsWithStaticImportAction.java
java/java-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacadeImpl.java [new file with mode: 0644]
java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelper.java
java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelperImpl.java
java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java
java/java-impl/src/com/intellij/psi/impl/compiled/ClsElementImpl.java
java/java-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java
java/java-impl/src/com/intellij/psi/impl/source/javadoc/PsiDocCommentImpl.java
java/java-impl/src/com/intellij/psi/impl/source/tree/java/ClassElement.java
java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java
java/openapi/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacade.java [new file with mode: 0644]
plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/typedef/GrTypeDefinitionImpl.java
resources/src/META-INF/IdeaPlugin.xml

index b6e920e0bb3e12a028ff627d2d1ea5cd1f6f82bc..fe0a95174dbfd75589ee6def810573dd7ac622d7 100644 (file)
@@ -28,7 +28,6 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.Key;
 import com.intellij.psi.*;
-import com.intellij.psi.impl.JavaPsiImplementationHelper;
 import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
 import com.intellij.psi.util.InheritanceUtil;
 import com.intellij.psi.util.PsiTreeUtil;
@@ -135,7 +134,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
     });
 
     if (resolved != null) {
-      JavaPsiImplementationHelper.getInstance(element.getProject()).bindToElementViaStaticImport(
+      PsiReferenceExpressionImpl.bindToElementViaStaticImport(
         getResolvedClass(element, (PsiMember)resolved), ((PsiNamedElement)resolved).getName(), ((PsiJavaFile)file).getImportList()
       );
     }
index 994088aaf758ca66e7a5d1de5de7530ff447d210..3b163eddb9c231caf6f54b1c81a07620226cc6a9 100644 (file)
@@ -30,6 +30,7 @@ import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
 import com.intellij.psi.impl.JavaPsiImplementationHelper;
 import com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef;
+import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.searches.DirectClassInheritorsSearch;
 import com.intellij.psi.search.searches.ReferencesSearch;
@@ -266,8 +267,7 @@ public class ReplaceImplementsWithStaticImportAction extends PsiElementBaseInten
                                     PsiReference reference,
                                     Project project) {
     if (reference instanceof PsiReferenceExpression) {
-      final JavaPsiImplementationHelper helper = JavaPsiImplementationHelper.getInstance(psiFile.getProject());
-      helper.bindToElementViaStaticImport(containingClass, fieldName, ((PsiJavaFile)psiFile).getImportList());
+      PsiReferenceExpressionImpl.bindToElementViaStaticImport(containingClass, fieldName, ((PsiJavaFile)psiFile).getImportList());
       final PsiElement qualifier = ((PsiReferenceExpression)reference).getQualifier();
       if (qualifier != null) {
         final Boolean canRemoveQualifier = qualifier.getCopyableUserData(ChangeContextUtil.CAN_REMOVE_QUALIFIER_KEY);
diff --git a/java/java-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacadeImpl.java b/java/java-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacadeImpl.java
new file mode 100644 (file)
index 0000000..12abb23
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.psi.codeStyle;
+
+import com.intellij.ide.highlighter.JavaFileType;
+
+/**
+ * @author yole
+ */
+public class JavaCodeStyleSettingsFacadeImpl extends JavaCodeStyleSettingsFacade {
+  private final CodeStyleSettingsManager myManager;
+
+  public JavaCodeStyleSettingsFacadeImpl(CodeStyleSettingsManager manager) {
+    myManager = manager;
+  }
+
+  @Override
+  public int getNamesCountToUseImportOnDemand() {
+    return myManager.getCurrentSettings().NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND;
+  }
+
+  @Override
+  public boolean useFQClassNames() {
+    return myManager.getCurrentSettings().USE_FQ_CLASS_NAMES;
+  }
+
+  @Override
+  public boolean isJavaDocLeadingAsterisksEnabled() {
+    return myManager.getCurrentSettings().JD_LEADING_ASTERISKS_ARE_ENABLED;
+  }
+
+  @Override
+  public int getIndentSize() {
+    return myManager.getCurrentSettings().getIndentSize(JavaFileType.INSTANCE);
+  }
+
+  @Override
+  public boolean isGenerateFinalParameters() {
+    return myManager.getCurrentSettings().GENERATE_FINAL_PARAMETERS;
+  }
+
+  @Override
+  public boolean isGenerateFinalLocals() {
+    return myManager.getCurrentSettings().GENERATE_FINAL_LOCALS;
+  }
+}
index e6a67dc77e460d48ce0045715b6854f95de6b808..36fb8fb77df07960b6fe71bc41fff3d43f732d27 100644 (file)
@@ -34,5 +34,5 @@ public abstract class JavaPsiImplementationHelper {
 
   public abstract ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement);
 
-  public abstract void bindToElementViaStaticImport(PsiClass qualifierClass, String staticName, PsiImportList importList);
+  public abstract PsiElement getDefaultMemberAnchor(PsiClass psiClass, PsiMember firstPsi);
 }
index f3ff71a0b99231c4f6c0af259a07b8031f083fd5..ee72b3d7245ac36b9c273020c18dde5d9cf75baa 100644 (file)
@@ -22,7 +22,6 @@ import com.intellij.openapi.roots.OrderEntry;
 import com.intellij.openapi.roots.OrderRootType;
 import com.intellij.openapi.roots.ProjectFileIndex;
 import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.CodeStyleSettings;
@@ -30,10 +29,8 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
 import com.intellij.psi.impl.compiled.ClsClassImpl;
 import com.intellij.psi.impl.source.codeStyle.ImportHelper;
 import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.util.PsiTreeUtil;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -115,30 +112,67 @@ public class JavaPsiImplementationHelperImpl extends JavaPsiImplementationHelper
   }
 
   @Override
-  public void bindToElementViaStaticImport(PsiClass qualifierClass, String staticName, PsiImportList importList) {
-    final String qualifiedName  = qualifierClass.getQualifiedName();
-    final List<PsiJavaCodeReferenceElement> refs = getImportsFromClass(importList, qualifiedName);
-    if (refs.size() < CodeStyleSettingsManager.getSettings(qualifierClass.getProject()).NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND) {
-      importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, staticName));
-    } else {
-      for (PsiJavaCodeReferenceElement ref : refs) {
-        final PsiImportStaticStatement importStatement = PsiTreeUtil.getParentOfType(ref, PsiImportStaticStatement.class);
-        if (importStatement != null) {
-          importStatement.delete();
+  public PsiElement getDefaultMemberAnchor(PsiClass aClass, PsiMember member) {
+    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(aClass.getProject());
+
+    int order = getMemberOrderWeight(member, settings);
+    if (order < 0) return null;
+
+    PsiElement lastMember = null;
+    for (PsiElement child = aClass.getFirstChild(); child != null; child = child.getNextSibling()) {
+      int order1 = getMemberOrderWeight(child, settings);
+      if (order1 < 0) continue;
+      if (order1 > order) {
+        if (lastMember != null) {
+          PsiElement nextSibling = lastMember.getNextSibling();
+          while (nextSibling instanceof PsiJavaToken && (nextSibling.getText().equals(",") || nextSibling.getText().equals(";"))) {
+            nextSibling = nextSibling.getNextSibling();
+          }
+          return nextSibling == null ? aClass.getLBrace().getNextSibling() : nextSibling;
+        }
+        else {
+          // The main idea is to avoid to anchor to 'white space' element because that causes reformatting algorithm
+          // to perform incorrectly. The algorithm is encapsulated at PostprocessReformattingAspect.doPostponedFormattingInner().
+          final PsiElement lBrace = aClass.getLBrace();
+          if (lBrace != null) {
+            PsiElement result = lBrace.getNextSibling();
+            while (result instanceof PsiWhiteSpace) {
+              result = result.getNextSibling();
+            }
+            return result;
+          }
         }
       }
-      importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, "*"));
+      lastMember = child;
     }
+    return aClass.getRBrace();
   }
 
-  private static List<PsiJavaCodeReferenceElement> getImportsFromClass(@NotNull PsiImportList importList, String className){
-    final List<PsiJavaCodeReferenceElement> array = new ArrayList<PsiJavaCodeReferenceElement>();
-    for (PsiImportStaticStatement staticStatement : importList.getImportStaticStatements()) {
-      final PsiClass psiClass = staticStatement.resolveTargetClass();
-      if (psiClass != null && Comparing.strEqual(psiClass.getQualifiedName(), className)) {
-        array.add(staticStatement.getImportReference());
+  public static int getMemberOrderWeight(PsiElement member, CodeStyleSettings settings) {
+    if (member instanceof PsiField) {
+      if (member instanceof PsiEnumConstant) {
+        return 1;
+      }
+      else {
+        return ((PsiField)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_FIELDS_ORDER_WEIGHT + 1
+                                                                          : settings.FIELDS_ORDER_WEIGHT + 1;
       }
     }
-    return array;
+    else if (member instanceof PsiMethod) {
+      if (((PsiMethod)member).isConstructor()) {
+        return settings.CONSTRUCTORS_ORDER_WEIGHT + 1;
+      }
+      else {
+        return ((PsiMethod)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_METHODS_ORDER_WEIGHT + 1
+                                                                           : settings.METHODS_ORDER_WEIGHT + 1;
+      }
+    }
+    else if (member instanceof PsiClass) {
+      return ((PsiClass)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_INNER_CLASSES_ORDER_WEIGHT + 1
+                                                                        : settings.INNER_CLASSES_ORDER_WEIGHT + 1;
+    }
+    else {
+      return -1;
+    }
   }
 }
index 1482dbaa7beebc722456c809375c36303bac7046..d087edb5287f9c5a82257c5b7ef8ce0b14a23d2a 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.CodeStyleManager;
 import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
+import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
 import com.intellij.psi.impl.light.*;
 import com.intellij.psi.impl.source.*;
 import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
@@ -247,7 +248,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
     PsiParameter parameter = createParameterFromText(text, null);
     final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject());
     PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL,
-                                CodeStyleSettingsManager.getSettings(myManager.getProject()).GENERATE_FINAL_PARAMETERS);
+                                JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters());
     markGenerated(parameter);
     parameter = (PsiParameter)JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter);
     return (PsiParameter)codeStyleManager.reformat(parameter);
@@ -568,7 +569,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
     final PsiVariable variable = (PsiVariable)statement.getDeclaredElements()[0];
     replace(variable.getTypeElement(), createTypeElement(type), text);
     PsiUtil.setModifierProperty(variable, PsiModifier.FINAL,
-                                CodeStyleSettingsManager.getSettings(myManager.getProject()).GENERATE_FINAL_LOCALS);
+                                JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals());
     if (initializer != null) {
       replace(variable.getInitializer(), initializer, text);
     }
index aef59c496d88c1be440a69bac9a883dc387489d4..fca05e026097a5714e0c4d31db28a413e38fd674 100644 (file)
@@ -15,7 +15,6 @@
  */
 package com.intellij.psi.impl.compiled;
 
-import com.intellij.ide.highlighter.JavaFileType;
 import com.intellij.lang.ASTNode;
 import com.intellij.lang.Language;
 import com.intellij.lang.java.JavaLanguage;
@@ -23,7 +22,7 @@ import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
+import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
 import com.intellij.psi.impl.PsiElementBase;
 import com.intellij.psi.impl.source.tree.TreeElement;
 import com.intellij.psi.tree.IElementType;
@@ -113,7 +112,7 @@ public abstract class ClsElementImpl extends PsiElementBase implements PsiCompil
   }
 
   protected int getIndentSize() {
-    return CodeStyleSettingsManager.getSettings(getProject()).getIndentSize(JavaFileType.INSTANCE);
+    return JavaCodeStyleSettingsFacade.getInstance(getProject()).getIndentSize();
   }
 
   public abstract void setMirror(@NotNull TreeElement element);
index 9e441705a70ca286a206b91aa3cc6b88a78f74f6..3c2aa6e458af90bbb606b342011861f2451de7fb 100644 (file)
@@ -19,8 +19,8 @@ import com.intellij.lang.ASTNode;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
+import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
 import com.intellij.psi.filters.*;
 import com.intellij.psi.filters.classes.AnnotationTypeFilter;
 import com.intellij.psi.filters.element.ModifierFilter;
@@ -540,7 +540,7 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme
 
   private PsiElement bindToClass(final PsiClass aClass) throws IncorrectOperationException {
     String qName = aClass.getQualifiedName();
-    final boolean preserveQualification = CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES && isFullyQualified();
+    final boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(getProject()).useFQClassNames() && isFullyQualified();
     final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
     if (qName == null) {
       qName = aClass.getName();
index 3c4ca70467751d5b7db6f9ded164e2e06ae24b28..01ccadaf8f78862baa20995cd7e0b42a5aa4194b 100644 (file)
@@ -19,7 +19,7 @@ import com.intellij.lang.ASTNode;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
+import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
 import com.intellij.psi.impl.source.Constants;
 import com.intellij.psi.impl.source.PsiElementArrayConstructor;
 import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -156,7 +156,7 @@ public class PsiDocCommentImpl extends LazyParseablePsiElement implements PsiDoc
     tag.addChild(newLine, null);
 
     ASTNode leadingWhitespaceAnchor = null;
-    if (CodeStyleSettingsManager.getSettings(project).JD_LEADING_ASTERISKS_ARE_ENABLED) {
+    if (JavaCodeStyleSettingsFacade.getInstance(project).isJavaDocLeadingAsterisksEnabled()) {
       final TreeElement leadingAsterisk = Factory.createSingleLeafElement(DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, treeCharTab,
                                                                           SharedImplUtil.getManagerByTree(tag));
 
index fabe6c279da9ff06b225df058010d87e411ea500..6e95cebc2b2cd8cf862f8483a633776820a56287 100644 (file)
@@ -19,7 +19,7 @@ import com.intellij.lang.ASTNode;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.CodeStyleSettings;
-import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
+import com.intellij.psi.impl.JavaPsiImplementationHelper;
 import com.intellij.psi.impl.PsiImplUtil;
 import com.intellij.psi.impl.source.Constants;
 import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -60,7 +60,9 @@ public class ClassElement extends CompositeElement implements Constants {
               before = anchor != findChildByRole(ChildRole.LBRACE);
             }
             else {
-              PsiElement psiElement = firstPsi instanceof PsiMember ? getDefaultAnchor(psiClass, (PsiMember)firstPsi) : null;
+              PsiElement psiElement = firstPsi instanceof PsiMember
+                                      ? JavaPsiImplementationHelper.getInstance(psiClass.getProject()).getDefaultMemberAnchor(psiClass, (PsiMember)firstPsi)
+                                      : null;
               anchor = psiElement != null ? SourceTreeToPsiMap.psiElementToTree(psiElement) : null;
               before = Boolean.TRUE;
             }
@@ -393,70 +395,6 @@ public class ClassElement extends CompositeElement implements Constants {
     }
   }
 
-  public static PsiElement getDefaultAnchor(PsiClass aClass, PsiMember member) {
-    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(aClass.getProject());
-
-    int order = getMemberOrderWeight(member, settings);
-    if (order < 0) return null;
-
-    PsiElement lastMember = null;
-    for (PsiElement child = aClass.getFirstChild(); child != null; child = child.getNextSibling()) {
-      int order1 = getMemberOrderWeight(child, settings);
-      if (order1 < 0) continue;
-      if (order1 > order) {
-        if (lastMember != null) {
-          PsiElement nextSibling = lastMember.getNextSibling();
-          while (nextSibling instanceof PsiJavaToken && (nextSibling.getText().equals(",") || nextSibling.getText().equals(";"))) {
-            nextSibling = nextSibling.getNextSibling();
-          }
-          return nextSibling == null ? aClass.getLBrace().getNextSibling() : nextSibling;
-        }
-        else {
-          // The main idea is to avoid to anchor to 'white space' element because that causes reformatting algorithm
-          // to perform incorrectly. The algorithm is encapsulated at PostprocessReformattingAspect.doPostponedFormattingInner().
-          final PsiElement lBrace = aClass.getLBrace();
-          if (lBrace != null) {
-            PsiElement result = lBrace.getNextSibling();
-            while (result instanceof PsiWhiteSpace) {
-              result = result.getNextSibling();
-            }
-            return result;
-          }
-        }
-      }
-      lastMember = child;
-    }
-    return aClass.getRBrace();
-  }
-
-  public static int getMemberOrderWeight(PsiElement member, CodeStyleSettings settings) {
-    if (member instanceof PsiField) {
-      if (member instanceof PsiEnumConstant) {
-        return 1;
-      }
-      else {
-        return ((PsiField)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_FIELDS_ORDER_WEIGHT + 1
-                                                                          : settings.FIELDS_ORDER_WEIGHT + 1;
-      }
-    }
-    else if (member instanceof PsiMethod) {
-      if (((PsiMethod)member).isConstructor()) {
-        return settings.CONSTRUCTORS_ORDER_WEIGHT + 1;
-      }
-      else {
-        return ((PsiMethod)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_METHODS_ORDER_WEIGHT + 1
-                                                                           : settings.METHODS_ORDER_WEIGHT + 1;
-      }
-    }
-    else if (member instanceof PsiClass) {
-      return ((PsiClass)member).hasModifierProperty(PsiModifier.STATIC) ? settings.STATIC_INNER_CLASSES_ORDER_WEIGHT + 1
-                                                                        : settings.INNER_CLASSES_ORDER_WEIGHT + 1;
-    }
-    else {
-      return -1;
-    }
-  }
-
   @Override
   protected boolean isVisibilitySupported() {
     return true;
index d7895d5dd8cb5f390fdc393adfb3431744623b5a..73b85b79cc610cd2efe63d57eae532895f1a01b6 100644 (file)
@@ -22,14 +22,13 @@ import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.pom.java.LanguageLevel;
 import com.intellij.psi.*;
-import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
+import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
 import com.intellij.psi.filters.AndFilter;
 import com.intellij.psi.filters.ConstructorFilter;
 import com.intellij.psi.filters.NotFilter;
 import com.intellij.psi.filters.OrFilter;
 import com.intellij.psi.impl.CheckUtil;
-import com.intellij.psi.impl.JavaPsiImplementationHelper;
 import com.intellij.psi.impl.PsiImplUtil;
 import com.intellij.psi.impl.PsiManagerEx;
 import com.intellij.psi.impl.source.SourceJavaCodeReference;
@@ -49,6 +48,7 @@ import com.intellij.psi.scope.processor.MethodResolverProcessor;
 import com.intellij.psi.scope.util.PsiScopesUtil;
 import com.intellij.psi.tree.ChildRoleBase;
 import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.psi.util.PsiUtil;
 import com.intellij.psi.util.TypeConversionUtil;
 import com.intellij.util.*;
@@ -102,7 +102,7 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements
       doImportStatic = false;
     }
     if (doImportStatic) {
-      JavaPsiImplementationHelper.getInstance(getProject()).bindToElementViaStaticImport(qualifierClass, staticName, importList);
+      bindToElementViaStaticImport(qualifierClass, staticName, importList);
     }
     else {
       PsiManagerEx manager = getManager();
@@ -115,6 +115,34 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements
     return this;
   }
 
+  public static void bindToElementViaStaticImport(final PsiClass qualifierClass, final String staticName, final PsiImportList importList)
+    throws IncorrectOperationException {
+    final String qualifiedName  = qualifierClass.getQualifiedName();
+    final List<PsiJavaCodeReferenceElement> refs = getImportsFromClass(importList, qualifiedName);
+    if (refs.size() < JavaCodeStyleSettingsFacade.getInstance(qualifierClass.getProject()).getNamesCountToUseImportOnDemand()) {
+      importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, staticName));
+    } else {
+      for (PsiJavaCodeReferenceElement ref : refs) {
+        final PsiImportStaticStatement importStatement = PsiTreeUtil.getParentOfType(ref, PsiImportStaticStatement.class);
+        if (importStatement != null) {
+          importStatement.delete();
+        }
+      }
+      importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, "*"));
+    }
+  }
+
+  private static List<PsiJavaCodeReferenceElement> getImportsFromClass(@NotNull PsiImportList importList, String className){
+    final List<PsiJavaCodeReferenceElement> array = new ArrayList<PsiJavaCodeReferenceElement>();
+    for (PsiImportStaticStatement staticStatement : importList.getImportStaticStatements()) {
+      final PsiClass psiClass = staticStatement.resolveTargetClass();
+      if (psiClass != null && Comparing.strEqual(psiClass.getQualifiedName(), className)) {
+        array.add(staticStatement.getImportReference());
+      }
+    }
+    return array;
+  }
+
   public void setQualifierExpression(@Nullable PsiExpression newQualifier) throws IncorrectOperationException {
     final PsiExpression oldQualifier = getQualifierExpression();
     if (newQualifier == null) {
@@ -492,7 +520,7 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements
     final PsiManager manager = getManager();
     final PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(getProject()).getParserFacade();
     if (element instanceof PsiClass) {
-      final boolean preserveQualification = CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES && isFullyQualified(this);
+      final boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(getProject()).useFQClassNames() && isFullyQualified(this);
       String qName = ((PsiClass)element).getQualifiedName();
       if (qName == null) {
         qName = ((PsiClass)element).getName();
diff --git a/java/openapi/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacade.java b/java/openapi/src/com/intellij/psi/codeStyle/JavaCodeStyleSettingsFacade.java
new file mode 100644 (file)
index 0000000..e492b41
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.psi.codeStyle;
+
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.project.Project;
+
+/**
+ * @author yole
+ */
+public abstract class JavaCodeStyleSettingsFacade {
+  public abstract int getNamesCountToUseImportOnDemand();
+
+  public abstract boolean useFQClassNames();
+
+  public abstract boolean isJavaDocLeadingAsterisksEnabled();
+
+  public abstract int getIndentSize();
+
+  public abstract boolean isGenerateFinalParameters();
+
+  public abstract boolean isGenerateFinalLocals();
+
+
+  public static JavaCodeStyleSettingsFacade getInstance(Project project) {
+    return ServiceManager.getService(project, JavaCodeStyleSettingsFacade.class);
+  }
+}
index 9d5acf7b10f9cc97a9f096f3eeb95a07e2dc7b20..75afbff986bf3962690ddc9fae59481d48a00229 100644 (file)
@@ -25,7 +25,6 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
 import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
 import com.intellij.psi.impl.*;
 import com.intellij.psi.impl.source.tree.LeafPsiElement;
-import com.intellij.psi.impl.source.tree.java.ClassElement;
 import com.intellij.psi.scope.PsiScopeProcessor;
 import com.intellij.psi.stubs.IStubElementType;
 import com.intellij.psi.util.CachedValueProvider;
@@ -749,12 +748,12 @@ public abstract class GrTypeDefinitionImpl extends GrStubElementBase<GrTypeDefin
   private PsiElement getDefaultAnchor(GrTypeDefinitionBody body, PsiMember member) {
     CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
 
-    int order = ClassElement.getMemberOrderWeight(member, settings);
+    int order = JavaPsiImplementationHelperImpl.getMemberOrderWeight(member, settings);
     if (order < 0) return null;
 
     PsiElement lastMember = null;
     for (PsiElement child = body.getFirstChild(); child != null; child = child.getNextSibling()) {
-      int order1 = ClassElement.getMemberOrderWeight(getAnyMember(child), settings);
+      int order1 = JavaPsiImplementationHelperImpl.getMemberOrderWeight(getAnyMember(child), settings);
       if (order1 < 0) continue;
       if (order1 > order) {
         final PsiElement lBrace = body.getLBrace();
index 7321231518e6cbcd7989852ab4bcfe988e620c1a..37f390470683807eccd265c6a2f9509d8b0c5ab4 100644 (file)
                     serviceImplementation="com.intellij.psi.impl.source.javadoc.JavadocManagerImpl"/>
     <projectService serviceInterface="com.intellij.psi.impl.JavaPsiImplementationHelper"
                     serviceImplementation="com.intellij.psi.impl.JavaPsiImplementationHelperImpl"/>
+    <projectService serviceInterface="com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade"
+                    serviceImplementation="com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacadeImpl"/>
 
     <applicationService serviceInterface="com.intellij.psi.JavaDirectoryService"
                         serviceImplementation="com.intellij.psi.impl.file.JavaDirectoryServiceImpl"/>