Java 类com.intellij.psi.impl.source.codeStyle.ImportHelper 实例源码

项目:intellij-ce-playground    文件:JavaClassNameInsertHandler.java   
private static boolean shouldInsertFqnInJavadoc(@NotNull JavaPsiClassReferenceElement item,
                                                @NotNull PsiFile file,
                                                @NotNull Project project) 
{
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
  JavaCodeStyleSettings javaSettings = settings.getCustomSettings(JavaCodeStyleSettings.class);

  switch (javaSettings.CLASS_NAMES_IN_JAVADOC) {
    case FULLY_QUALIFY_NAMES_ALWAYS:
      return true;
    case SHORTEN_NAMES_ALWAYS_AND_ADD_IMPORT:
      return false;
    case FULLY_QUALIFY_NAMES_IF_NOT_IMPORTED:
      if (file instanceof PsiJavaFile) {
        PsiJavaFile javaFile = ((PsiJavaFile)file);
        return item.getQualifiedName() != null && !ImportHelper.isAlreadyImported(javaFile, item.getQualifiedName());
      }
    default:
      return false;
  }
}
项目:intellij-ce-playground    文件:ImportHelperTest.java   
@WrapInCommand
public void testConflictingClassesFromCurrentPackage() throws Throwable {
  final PsiFile file = configureByText(StdFileTypes.JAVA, "package java.util; class X{ Date d;}");
  assertEmpty(highlightErrors());

  new WriteCommandAction.Simple(getProject()) {
    @Override
    protected void run() throws Throwable {
      CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
      ImportHelper importHelper = new ImportHelper(settings);

      PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass("java.sql.Date", GlobalSearchScope.allScope(getProject()));
      boolean b = importHelper.addImport((PsiJavaFile)file, psiClass);
      assertFalse(b); // must fail
    }
  }.execute().throwException();
}
项目:tools-idea    文件:ImportHelperTest.java   
@WrapInCommand
public void testConflictingClassesFromCurrentPackage() throws Throwable {
  final PsiFile file = configureByText(StdFileTypes.JAVA, "package java.util; class X{ Date d;}");
  assertEmpty(highlightErrors());

  new WriteCommandAction.Simple(getProject()) {
    @Override
    protected void run() throws Throwable {
      CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
      ImportHelper importHelper = new ImportHelper(settings);

      PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass("java.sql.Date", GlobalSearchScope.allScope(getProject()));
      boolean b = importHelper.addImport((PsiJavaFile)file, psiClass);
      assertFalse(b); // must fail
    }
  }.execute().throwException();
}
项目:consulo-java    文件:JavaClassNameInsertHandler.java   
private static boolean shouldInsertFqnInJavadoc(@NotNull JavaPsiClassReferenceElement item, @NotNull PsiFile file, @NotNull Project project)
{
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    JavaCodeStyleSettings javaSettings = settings.getCustomSettings(JavaCodeStyleSettings.class);

    switch(javaSettings.CLASS_NAMES_IN_JAVADOC)
    {
        case FULLY_QUALIFY_NAMES_ALWAYS:
            return true;
        case SHORTEN_NAMES_ALWAYS_AND_ADD_IMPORT:
            return false;
        case FULLY_QUALIFY_NAMES_IF_NOT_IMPORTED:
            if(file instanceof PsiJavaFile)
            {
                PsiJavaFile javaFile = ((PsiJavaFile) file);
                return item.getQualifiedName() != null && !ImportHelper.isAlreadyImported(javaFile, item.getQualifiedName());
            }
        default:
            return false;
    }
}
项目:consulo-java    文件:ImportHelperTest.java   
@WrapInCommand
public void testConflictingClassesFromCurrentPackage() throws Throwable
{
    final PsiFile file = configureByText(JavaFileType.INSTANCE, "package java.util; class X{ Date d;}");
    assertEmpty(highlightErrors());

    new WriteCommandAction.Simple(getProject())
    {
        @Override
        protected void run() throws Throwable
        {
            CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
            ImportHelper importHelper = new ImportHelper(settings);

            PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass("java.sql.Date", GlobalSearchScope.allScope(getProject()));
            boolean b = importHelper.addImport((PsiJavaFile) file, psiClass);
            assertFalse(b); // must fail
        }
    }.execute().throwException();
}
项目:intellij-ce-playground    文件:ImportHelperTest.java   
private void checkAddImport(PsiJavaFile file, String fqn, String... expectedOrder) {
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
  ImportHelper importHelper = new ImportHelper(settings);

  PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(fqn, GlobalSearchScope.allScope(getProject()));
  boolean b = importHelper.addImport(file, psiClass);
  assertTrue(b);

  assertOrder(file, expectedOrder);
}
项目:intellij-ce-playground    文件:CodeGenerator.java   
private static void addImports(Module module, ImportHelper importHelper, PsiJavaFile file, String[] classNames) {
  for (String className : classNames) {
    PsiClass psiClass = NavigationEditorUtils.getPsiClass(module, className);
    if (psiClass != null) {
      importHelper.addImport(file, psiClass);
    } else {
      LOG.warn("Class not found: " + className);
    }
  }
}
项目:intellij-ce-playground    文件:UnnecessaryFullyQualifiedNameInspection.java   
private static boolean acceptFqnInJavadoc(PsiJavaFile javaFile, String fullyQualifiedName, CodeStyleSettings styleSettings) {
  if ("package-info.java".equals(javaFile.getName())) {
    return true;
  }
  JavaCodeStyleSettings javaSettings = styleSettings.getCustomSettings(JavaCodeStyleSettings.class);
  if (javaSettings.CLASS_NAMES_IN_JAVADOC == JavaCodeStyleSettings.FULLY_QUALIFY_NAMES_IF_NOT_IMPORTED) {
    return !ImportHelper.isAlreadyImported(javaFile, fullyQualifiedName);
  }
  return javaSettings.useFqNamesInJavadocAlways();
}
项目:tools-idea    文件:ImportHelperTest.java   
private void checkAddImport(PsiJavaFile file, String fqn, String... expectedOrder) {
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
  ImportHelper importHelper = new ImportHelper(settings);

  PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(fqn, GlobalSearchScope.allScope(getProject()));
  boolean b = importHelper.addImport(file, psiClass);
  assertTrue(b);

  assertOrder(file, expectedOrder);
}
项目:consulo-java    文件:JavaPsiImplementationHelperImpl.java   
@Override
public ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement)
{
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(list.getProject());
    ImportHelper importHelper = new ImportHelper(settings);
    return importHelper.getDefaultAnchor(list, statement);
}
项目:consulo-java    文件:ImportHelperTest.java   
private void checkAddImport(PsiJavaFile file, String fqn, String... expectedOrder)
{
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
    ImportHelper importHelper = new ImportHelper(settings);

    PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(fqn, GlobalSearchScope.allScope(getProject()));
    boolean b = importHelper.addImport(file, psiClass);
    assertTrue(b);

    assertOrder(file, expectedOrder);
}
项目:intellij-ce-playground    文件:JavaPsiImplementationHelperImpl.java   
@Override
public ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement) {
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(list.getProject());
  ImportHelper importHelper = new ImportHelper(settings);
  return importHelper.getDefaultAnchor(list, statement);
}
项目:intellij-ce-playground    文件:CodeGenerator.java   
private static void addImportsAsNecessary(Module module, PsiClass psiClass, @NonNull String... classNames) {
  PsiJavaFile file = (PsiJavaFile)psiClass.getContainingFile();
  ImportHelper importHelper = new ImportHelper(CodeStyleSettingsManager.getSettings(module.getProject()));
  addImports(module, importHelper, file, FRAMEWORK_IMPORTS);
  addImports(module, importHelper, file, classNames);
}
项目:intellij-ce-playground    文件:JavaFxImportsOptimizer.java   
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
  VirtualFile vFile = file.getVirtualFile();
  if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate();
  final Project project = file.getProject();
  if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) {
    return EmptyRunnable.INSTANCE;
  }
  final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>();
  collectNamesToImport(names, (XmlFile)file);
  Collections.sort(names, new Comparator<Pair<String, Boolean>>() {
    @Override
    public int compare(Pair<String, Boolean> o1, Pair<String, Boolean> o2) {
      return StringUtil.compare(o1.first, o2.first, true);
    }
  });
  final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
  final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings);
  final HashSet<String> onDemand = new HashSet<String>();
  ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings);
  final Set<String> imported = new HashSet<String>();
  final List<String> imports = new ArrayList<String>();
  for (Pair<String, Boolean> pair : sortedNames) {
    final String qName = pair.first;
    final String packageName = StringUtil.getPackageName(qName);
    if (imported.contains(packageName) || imported.contains(qName)) {
      continue;
    }
    if (onDemand.contains(packageName)) {
      imported.add(packageName);
      imports.add("<?import " + packageName + ".*?>");
    } else {
      imported.add(qName);
      imports.add("<?import " + qName + "?>");
    }
  }
  final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());

  final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, StringUtil.join(imports, "\n"));
  final XmlDocument document = dummyFile.getDocument();
  final XmlProlog newImportList = document.getProlog();
  if (newImportList == null) return EmptyRunnable.getInstance();
  return new Runnable() {
    @Override
    public void run() {
      final XmlDocument xmlDocument = ((XmlFile)file).getDocument();
      final XmlProlog prolog = xmlDocument.getProlog();
      if (prolog != null) {
        final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class);
        for (final XmlProcessingInstruction instruction : instructions) {
          final ASTNode node = instruction.getNode();
          final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME);
          if (nameNode != null && nameNode.getText().equals("import")) {
            instruction.delete();
          }
        }
        prolog.add(newImportList);
      } else {
        document.addBefore(newImportList, document.getRootTag());
      }
    }
  };
}
项目:tools-idea    文件:JavaPsiImplementationHelperImpl.java   
@Override
public ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement) {
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(list.getProject());
  ImportHelper importHelper = new ImportHelper(settings);
  return importHelper.getDefaultAnchor(list, statement);
}
项目:tools-idea    文件:JavaFxImportsOptimizer.java   
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
  VirtualFile vFile = file.getVirtualFile();
  if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate();
  final Project project = file.getProject();
  if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) {
    return EmptyRunnable.INSTANCE;
  }
  final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>();
  collectNamesToImport(names, (XmlFile)file);
  Collections.sort(names, new Comparator<Pair<String, Boolean>>() {
    @Override
    public int compare(Pair<String, Boolean> o1, Pair<String, Boolean> o2) {
      return StringUtil.compare(o1.first, o2.first, true);
    }
  });
  final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
  final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings);
  final HashSet<String> onDemand = new HashSet<String>();
  ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings);
  final Set<String> imported = new HashSet<String>();
  final List<String> imports = new ArrayList<String>();
  for (Pair<String, Boolean> pair : sortedNames) {
    final String qName = pair.first;
    final String packageName = StringUtil.getPackageName(qName);
    if (imported.contains(packageName) || imported.contains(qName)) {
      continue;
    }
    if (onDemand.contains(packageName)) {
      imported.add(packageName);
      imports.add("<?import " + packageName + ".*?>");
    } else {
      imported.add(qName);
      imports.add("<?import " + qName + "?>");
    }
  }
  final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());

  final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, StringUtil.join(imports, "\n"));
  final XmlDocument document = dummyFile.getDocument();
  final XmlProlog newImportList = document.getProlog();
  if (newImportList == null) return EmptyRunnable.getInstance();
  return new Runnable() {
    @Override
    public void run() {
      final XmlDocument xmlDocument = ((XmlFile)file).getDocument();
      final XmlProlog prolog = xmlDocument.getProlog();
      if (prolog != null) {
        final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class);
        for (final XmlProcessingInstruction instruction : instructions) {
          final ASTNode node = instruction.getNode();
          final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME);
          if (nameNode != null && nameNode.getText().equals("import")) {
            instruction.delete();
          }
        }
        prolog.add(newImportList);
      } else {
        document.addBefore(newImportList, document.getRootTag());
      }
    }
  };
}
项目:consulo-javafx    文件:JavaFxImportsOptimizer.java   
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
  VirtualFile vFile = file.getVirtualFile();
  if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate();
  final Project project = file.getProject();
  if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) {
    return EmptyRunnable.INSTANCE;
  }
  final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>();
  collectNamesToImport(names, (XmlFile)file);
  Collections.sort(names, new Comparator<Pair<String, Boolean>>() {
    @Override
    public int compare(Pair<String, Boolean> o1, Pair<String, Boolean> o2) {
      return StringUtil.compare(o1.first, o2.first, true);
    }
  });
  final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
  final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings);
  final HashSet<String> onDemand = new HashSet<String>();
  ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings);
  final Set<String> imported = new HashSet<String>();
  final List<String> imports = new ArrayList<String>();
  for (Pair<String, Boolean> pair : sortedNames) {
    final String qName = pair.first;
    final String packageName = StringUtil.getPackageName(qName);
    if (imported.contains(packageName) || imported.contains(qName)) {
      continue;
    }
    if (onDemand.contains(packageName)) {
      imported.add(packageName);
      imports.add("<?import " + packageName + ".*?>");
    } else {
      imported.add(qName);
      imports.add("<?import " + qName + "?>");
    }
  }
  final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());

  final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", XmlFileType.INSTANCE, StringUtil.join(imports, "\n"));
  final XmlDocument document = dummyFile.getDocument();
  final XmlProlog newImportList = document.getProlog();
  if (newImportList == null) return EmptyRunnable.getInstance();
  return new Runnable() {
    @Override
    public void run() {
      final XmlDocument xmlDocument = ((XmlFile)file).getDocument();
      final XmlProlog prolog = xmlDocument.getProlog();
      if (prolog != null) {
        final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class);
        for (final XmlProcessingInstruction instruction : instructions) {
          final ASTNode node = instruction.getNode();
          final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME);
          if (nameNode != null && nameNode.getText().equals("import")) {
            instruction.delete();
          }
        }
        prolog.add(newImportList);
      } else {
        document.addBefore(newImportList, document.getRootTag());
      }
    }
  };
}