Search in sources :

Example 41 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project intellij-community by JetBrains.

the class JavaFormatterTest method testSpacesIncode.

public void testSpacesIncode() throws Exception {
    final JavaPsiFacade facade = getJavaFacade();
    final LanguageLevel level = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
    LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    try {
        doTextTest("class C<Y, X> {\n" + "}", "class C<Y, X> {\n" + "}");
        getSettings().SPACE_BEFORE_METHOD_LBRACE = true;
        getSettings().KEEP_SIMPLE_METHODS_IN_ONE_LINE = true;
        doTextTest("enum En {\n" + "    A(10) {},\n" + "    B(10) {},\n" + "    C(10);\n" + "\n" + "    En(int i) { }\n" + "}", "enum En {\n" + "    A(10) {},\n" + "    B(10) {},\n" + "    C(10);\n" + "\n" + "    En(int i) { }\n" + "}");
        doTextTest("class C {\n" + "    void foo (Map<?, String> s) {\n" + "        Set<? extends Map<?, String>.Entry<?, String>> temp = s.entries();\n" + "    }\n" + "}", "class C {\n" + "    void foo(Map<?, String> s) {\n" + "        Set<? extends Map<?, String>.Entry<?, String>> temp = s.entries();\n" + "    }\n" + "}");
        doTextTest("class B {\n" + "    public final A<String> myDelegate = new A<String>();\n" + "\n" + "    public List<? extends String> method1() {\n" + "        return myDelegate.method1();\n" + "    }\n" + "\n" + "    public String method2(String t) {\n" + "        return myDelegate.method2(t);\n" + "    }\n" + "}", "class B {\n" + "    public final A<String> myDelegate = new A<String>();\n" + "\n" + "    public List<? extends String> method1() {\n" + "        return myDelegate.method1();\n" + "    }\n" + "\n" + "    public String method2(String t) {\n" + "        return myDelegate.method2(t);\n" + "    }\n" + "}");
    } finally {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(level);
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 42 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project intellij-community by JetBrains.

the class ChangeToEndOfLineCommentIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiComment comment = (PsiComment) element;
    final JavaPsiFacade manager = JavaPsiFacade.getInstance(comment.getProject());
    final PsiElement parent = comment.getParent();
    assert parent != null;
    final PsiElementFactory factory = manager.getElementFactory();
    final String commentText = comment.getText();
    final PsiElement whitespace = comment.getNextSibling();
    final String text = commentText.substring(2, commentText.length() - 2);
    final String[] lines = text.split("\n");
    for (int i = lines.length - 1; i >= 1; i--) {
        final PsiComment nextComment = factory.createCommentFromText("//" + lines[i].trim() + '\n', parent);
        parent.addAfter(nextComment, comment);
    /* if (whitespace != null) {
      final PsiElement newWhiteSpace =
          factory.createWhiteSpaceFromText(whitespace.getText());
      parent.addAfter(newWhiteSpace, comment);
    }  */
    }
    final PsiComment newComment = factory.createCommentFromText("//" + lines[0], parent);
    comment.replace(newComment);
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiComment(com.intellij.psi.PsiComment) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiElement(com.intellij.psi.PsiElement)

Example 43 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project intellij-community by JetBrains.

the class ChangeToCStyleCommentIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiComment selectedComment = (PsiComment) element;
    PsiComment firstComment = selectedComment;
    while (true) {
        final PsiElement prevComment = getPrevNonWhiteSpace(firstComment);
        if (!isEndOfLineComment(prevComment)) {
            break;
        }
        firstComment = (PsiComment) prevComment;
    }
    final JavaPsiFacade manager = JavaPsiFacade.getInstance(selectedComment.getProject());
    final PsiElementFactory factory = manager.getElementFactory();
    String text = getCommentContents(firstComment);
    final List<PsiElement> commentsToDelete = new ArrayList<>();
    PsiElement nextComment = firstComment;
    while (true) {
        nextComment = getNextNonWhiteSpace(nextComment);
        if (!isEndOfLineComment(nextComment)) {
            break;
        }
        text += //to get the whitespace for proper spacing
        nextComment.getPrevSibling().getText() + "  " + getCommentContents((PsiComment) nextComment);
        commentsToDelete.add(nextComment);
    }
    final PsiComment newComment = factory.createCommentFromText("/*" + text + " */", selectedComment.getParent());
    firstComment.replace(newComment);
    for (PsiElement commentToDelete : commentsToDelete) {
        commentToDelete.delete();
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiComment(com.intellij.psi.PsiComment) PsiElementFactory(com.intellij.psi.PsiElementFactory) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 44 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project intellij-community by JetBrains.

the class GradleDslAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof GrReferenceExpression) {
        GrReferenceExpression referenceExpression = (GrReferenceExpression) element;
        final GrExpression qualifier = ResolveUtil.getSelfOrWithQualifier(referenceExpression);
        if (qualifier == null)
            return;
        if (qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof PsiClass)
            return;
        PsiType psiType = GradleResolverUtil.getTypeOf(qualifier);
        if (psiType == null)
            return;
        if (InheritanceUtil.isInheritor(psiType, GradleCommonClassNames.GRADLE_API_NAMED_DOMAIN_OBJECT_COLLECTION)) {
            JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(element.getProject());
            PsiClass defaultGroovyMethodsClass = javaPsiFacade.findClass(GroovyCommonClassNames.DEFAULT_GROOVY_METHODS, element.getResolveScope());
            if (canBeMethodOf(referenceExpression.getReferenceName(), defaultGroovyMethodsClass))
                return;
            final String qualifiedName = TypesUtil.getQualifiedName(psiType);
            final PsiClass containerClass = qualifiedName != null ? javaPsiFacade.findClass(qualifiedName, element.getResolveScope()) : null;
            if (canBeMethodOf(referenceExpression.getReferenceName(), containerClass))
                return;
            PsiElement nameElement = referenceExpression.getReferenceNameElement();
            if (nameElement != null) {
                holder.createInfoAnnotation(nameElement, null).setTextAttributes(MAP_KEY);
            }
        }
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 45 with JavaPsiFacade

use of com.intellij.psi.JavaPsiFacade in project intellij-community by JetBrains.

the class I18NInspectionTest method testEnum.

public void testEnum() throws Exception {
    final JavaPsiFacade facade = getJavaFacade();
    final LanguageLevel effectiveLanguageLevel = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
    LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    try {
        doTest();
    } finally {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(effectiveLanguageLevel);
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Aggregations

JavaPsiFacade (com.intellij.psi.JavaPsiFacade)52 PsiClass (com.intellij.psi.PsiClass)30 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)12 PsiPackage (com.intellij.psi.PsiPackage)8 LanguageLevel (com.intellij.pom.java.LanguageLevel)7 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiMethod (com.intellij.psi.PsiMethod)4 PsiType (com.intellij.psi.PsiType)4 File (java.io.File)3 List (java.util.List)3 Configuration (com.android.tools.idea.configurations.Configuration)2 MergedManifest (com.android.tools.idea.model.MergedManifest)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ClassFilter (com.intellij.ide.util.ClassFilter)2