Search in sources :

Example 11 with PsiJavaFile

use of com.intellij.psi.PsiJavaFile in project intellij-plugins by JetBrains.

the class CucumberGotoRelatedFileTest method testGotoRelated.

public void testGotoRelated() {
    CucumberStepsIndex.getInstance(getProject()).reset();
    myFixture.copyDirectoryToProject("gotoRelated", "");
    myFixture.configureByFile("gotoRelated/test.feature");
    List<GotoRelatedItem> items = GotoRelatedSymbolAction.getItems(myFixture.getFile(), myFixture.getEditor(), null);
    assertEquals(1, items.size());
    PsiElement gotoElement = items.get(0).getElement();
    assertTrue(gotoElement instanceof PsiJavaFile);
    assertEquals("ShoppingStepdefs.java", ((PsiJavaFile) gotoElement).getName());
}
Also used : PsiJavaFile(com.intellij.psi.PsiJavaFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PsiJavaFile

use of com.intellij.psi.PsiJavaFile in project android by JetBrains.

the class AndroidLineMarkerProvider method getMarkerInfo.

@Nullable
private static MyMarkerInfo getMarkerInfo(@NotNull PsiElement element) {
    if (!(element instanceof XmlFile) && !(element instanceof PsiJavaFile)) {
        return null;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        return null;
    }
    if (element instanceof PsiJavaFile) {
        final PsiClass[] classes = ((PsiJavaFile) element).getClasses();
        if (classes.length == 1) {
            final PsiClass aClass = classes[0];
            final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
            if (nameIdentifier != null) {
                final Computable<List<GotoRelatedItem>> computable = AndroidGotoRelatedProvider.getLazyItemsForClass(aClass, facet, true);
                return computable != null ? new MyMarkerInfo(nameIdentifier, computable, "Related XML file", AllIcons.FileTypes.Xml) : null;
            }
        }
    } else {
        final XmlTag rootTag = ((XmlFile) element).getRootTag();
        final Computable<List<GotoRelatedItem>> computable = AndroidGotoRelatedProvider.getLazyItemsForXmlFile((XmlFile) element, facet);
        return computable != null ? new MyMarkerInfo(rootTag != null ? rootTag : element, computable, "Related context Java file", AllIcons.Nodes.Class) : null;
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiJavaFile(com.intellij.psi.PsiJavaFile) List(java.util.List) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with PsiJavaFile

use of com.intellij.psi.PsiJavaFile in project android by JetBrains.

the class LombokPsiConverterTest method check.

private static void check(PsiFile psiFile, @Language("JAVA") String source) {
    assertTrue(psiFile.getClass().getName(), psiFile instanceof PsiJavaFile);
    PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
    CompilationUnit node = LombokPsiConverter.convert(psiJavaFile);
    assertNotNull(node);
    String actualStructure;
    if (CHECK_POSITIONS) {
        StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
        node.accept(new SourcePrinter(structureFormatter));
        actualStructure = structureFormatter.finish();
    }
    TextFormatter formatter = new TextFormatter();
    node.accept(new SourcePrinter(formatter));
    String actual = formatter.finish();
    Node expectedNode = parse(source);
    assertNotNull(expectedNode);
    if (CHECK_POSITIONS) {
        StructureFormatter structureFormatter = StructureFormatter.formatterWithPositions();
        expectedNode.accept(new SourcePrinter(structureFormatter));
        String masterStructure = structureFormatter.finish();
        assertEquals(masterStructure, actualStructure);
    }
    formatter = new TextFormatter();
    expectedNode.accept(new SourcePrinter(formatter));
    String master = formatter.finish();
    assertEquals(master, actual);
    // Check for resilience to error nodes being present in the AST
    Project project = psiFile.getProject();
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
    final Document document = manager.getDocument(psiFile);
    assertNotNull(document);
    // fixed seed for test reproducibility
    final Random random = new Random(0L);
    for (int i = 0; i < 500; i++) {
        WriteCommandAction.runWriteCommandAction(project, new Runnable() {

            @Override
            public void run() {
                int pos = random.nextInt(document.getTextLength() - 1);
                char ch = (char) (random.nextInt(64) + 32);
                double operation = random.nextDouble();
                if (operation < 0.33) {
                    document.insertString(pos, Character.toString(ch));
                } else if (operation < 0.67) {
                    document.replaceString(pos, pos + 1, Character.toString(ch));
                } else {
                    document.deleteString(pos, pos + 1);
                }
                manager.commitDocument(document);
            }
        });
        node = LombokPsiConverter.convert(psiJavaFile);
        assertNotNull(psiJavaFile.getText(), node);
    }
}
Also used : CompilationUnit(lombok.ast.CompilationUnit) SourcePrinter(lombok.ast.printer.SourcePrinter) TextFormatter(lombok.ast.printer.TextFormatter) StructureFormatter(lombok.ast.printer.StructureFormatter) Node(lombok.ast.Node) PsiJavaFile(com.intellij.psi.PsiJavaFile) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) Random(java.util.Random) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 14 with PsiJavaFile

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

the class ReplaceFullyQualifiedNameWithImportIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) {
    PsiJavaCodeReferenceElement reference = (PsiJavaCodeReferenceElement) element;
    PsiElement target = reference.resolve();
    if (!(target instanceof PsiClass)) {
        PsiElement parent = reference.getParent();
        while (parent instanceof PsiJavaCodeReferenceElement) {
            reference = (PsiJavaCodeReferenceElement) parent;
            target = reference.resolve();
            if (target instanceof PsiClass) {
                break;
            }
            parent = parent.getParent();
        }
    }
    if (!(target instanceof PsiClass)) {
        return;
    }
    final PsiClass aClass = (PsiClass) target;
    final String qualifiedName = aClass.getQualifiedName();
    if (qualifiedName == null) {
        return;
    }
    final PsiJavaFile file = PsiTreeUtil.getParentOfType(reference, PsiJavaFile.class);
    if (file == null) {
        return;
    }
    ImportUtils.addImportIfNeeded(aClass, reference);
    final String fullyQualifiedText = reference.getText();
    final UnnecessaryFullyQualifiedNameInspection.QualificationRemover qualificationRemover = new UnnecessaryFullyQualifiedNameInspection.QualificationRemover(fullyQualifiedText);
    file.accept(qualificationRemover);
    final Collection<PsiElement> shortenedElements = qualificationRemover.getShortenedElements();
    final int elementCount = shortenedElements.size();
    final String text;
    if (elementCount == 1) {
        text = IntentionPowerPackBundle.message("1.fully.qualified.name.status.bar.escape.highlighting.message");
    } else {
        text = IntentionPowerPackBundle.message("multiple.fully.qualified.names.status.bar.escape.highlighting.message", Integer.valueOf(elementCount));
    }
    HighlightUtil.highlightElements(shortenedElements, text);
}
Also used : PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile) UnnecessaryFullyQualifiedNameInspection(com.siyeh.ig.style.UnnecessaryFullyQualifiedNameInspection) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiJavaFile

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

the class JavaFileEditorManagerTest method testFoldingIsNotBlinkingOnNavigationToSingleLineMethod.

public void testFoldingIsNotBlinkingOnNavigationToSingleLineMethod() {
    VirtualFile file = getFile("/src/Bar.java");
    PsiJavaFile psiFile = (PsiJavaFile) getPsiManager().findFile(file);
    assertNotNull(psiFile);
    PsiMethod method = psiFile.getClasses()[0].getMethods()[0];
    method.navigate(true);
    FileEditor[] editors = myManager.getEditors(file);
    assertEquals(1, editors.length);
    Editor editor = ((TextEditor) editors[0]).getEditor();
    EditorTestUtil.waitForLoading(editor);
    FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
    assertEquals(2, regions.length);
    assertTrue(regions[0].isExpanded());
    assertTrue(regions[1].isExpanded());
    CodeInsightTestFixtureImpl.instantiateAndRun(psiFile, editor, new int[] { Pass.UPDATE_ALL, Pass.LOCAL_INSPECTIONS }, false);
    regions = editor.getFoldingModel().getAllFoldRegions();
    assertEquals(2, regions.length);
    assertTrue(regions[0].isExpanded());
    assertTrue(regions[1].isExpanded());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiMethod(com.intellij.psi.PsiMethod) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiJavaFile(com.intellij.psi.PsiJavaFile) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Aggregations

PsiJavaFile (com.intellij.psi.PsiJavaFile)24 PsiClass (com.intellij.psi.PsiClass)10 PsiFile (com.intellij.psi.PsiFile)7 Nullable (org.jetbrains.annotations.Nullable)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 PsiElement (com.intellij.psi.PsiElement)2 PsiMethod (com.intellij.psi.PsiMethod)2 List (java.util.List)2 CompilationUnit (lombok.ast.CompilationUnit)2 SourcePrinter (lombok.ast.printer.SourcePrinter)2 TextFormatter (lombok.ast.printer.TextFormatter)2 NotNull (org.jetbrains.annotations.NotNull)2 SubLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement)1 LighterASTNode (com.intellij.lang.LighterASTNode)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 CaretModel (com.intellij.openapi.editor.CaretModel)1