Search in sources :

Example 16 with PsiJavaFile

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

the class GotoClassTest method testGotoClass.

public void testGotoClass() throws Exception {
    PsiJavaFile file = (PsiJavaFile) myFixture.configureByText("Foo.java", "public class Foo {\n" + "}\n" + "\n" + "class Bar {}\n");
    VirtualFile virtualFile = file.getVirtualFile();
    assertNotNull(virtualFile);
    myManager.openFile(virtualFile, true);
    assertEquals(0, getOffset(virtualFile));
    myManager.closeAllFiles();
    PsiClass psiClass = file.getClasses()[1];
    int identifierOffset = psiClass.getNameIdentifier().getTextOffset();
    NavigationUtil.activateFileWithPsiElement(psiClass);
    assertEquals(identifierOffset, getOffset(virtualFile));
    // it's still inside the class, so keep it
    getEditor(virtualFile).getCaretModel().moveToOffset(identifierOffset + 3);
    myManager.closeAllFiles();
    NavigationUtil.activateFileWithPsiElement(psiClass);
    assertEquals(identifierOffset + 3, getOffset(virtualFile));
    getEditor(virtualFile).getCaretModel().moveToOffset(0);
    NavigationUtil.activateFileWithPsiElement(psiClass);
    assertEquals(identifierOffset, getOffset(virtualFile));
    myManager.closeAllFiles();
    // GoTo file should keep offset
    NavigationUtil.activateFileWithPsiElement(file);
    assertEquals(identifierOffset, getOffset(virtualFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile)

Example 17 with PsiJavaFile

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

the class JavaImportOptimizer method processFile.

@Override
@NotNull
public Runnable processFile(final PsiFile file) {
    if (!(file instanceof PsiJavaFile)) {
        return EmptyRunnable.getInstance();
    }
    Project project = file.getProject();
    final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult((PsiJavaFile) file);
    if (newImportList == null)
        return EmptyRunnable.getInstance();
    return new CollectingInfoRunnable() {

        private int myImportListLengthDiff;

        @Override
        public void run() {
            try {
                final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
                final Document document = manager.getDocument(file);
                if (document != null) {
                    manager.commitDocument(document);
                }
                final PsiImportList oldImportList = ((PsiJavaFile) file).getImportList();
                assert oldImportList != null;
                int importsBefore = oldImportList.getAllImportStatements().length;
                oldImportList.replace(newImportList);
                myImportListLengthDiff = importsBefore - newImportList.getAllImportStatements().length;
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }

        @Nullable
        @Override
        public String getUserNotificationInfo() {
            if (myImportListLengthDiff > 0) {
                return "removed " + myImportListLengthDiff + " import" + (myImportListLengthDiff > 1 ? "s" : "");
            }
            return null;
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) PsiJavaFile(com.intellij.psi.PsiJavaFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiImportList(com.intellij.psi.PsiImportList) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PsiJavaFile

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

the class ControlFlowTest method testMethodWithOnlyDoWhileStatementHasExitPoints.

public void testMethodWithOnlyDoWhileStatementHasExitPoints() throws Exception {
    configureFromFileText("a.java", "public class Foo {\n" + "  public void foo() {\n" + "    boolean f;\n" + "    do {\n" + "      f = something();\n" + "    } while (f);\n" + "  }\n" + "}");
    final PsiCodeBlock body = ((PsiJavaFile) getFile()).getClasses()[0].getMethods()[0].getBody();
    ControlFlow flow = ControlFlowFactory.getInstance(getProject()).getControlFlow(body, new LocalsControlFlowPolicy(body), false);
    IntArrayList exitPoints = new IntArrayList();
    ControlFlowUtil.findExitPointsAndStatements(flow, 0, flow.getSize() - 1, exitPoints, ControlFlowUtil.DEFAULT_EXIT_STATEMENTS_CLASSES);
    assertEquals(1, exitPoints.size());
}
Also used : PsiCodeBlock(com.intellij.psi.PsiCodeBlock) PsiJavaFile(com.intellij.psi.PsiJavaFile) IntArrayList(com.intellij.util.containers.IntArrayList)

Example 19 with PsiJavaFile

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

the class ClassInheritorsTest method testStressInPresenceOfPCEs.

public void testStressInPresenceOfPCEs() throws IOException {
    // no write action can go through while we test
    ApplicationManager.getApplication().assertIsDispatchThread();
    int N = 1000;
    PsiJavaFile file0 = (PsiJavaFile) myFixture.addFileToProject("C0.java", "class C0 { }");
    for (int i = 1; i < N; i++) {
        // 10 inheritors
        int extI = i - 1 - (i - 1) % 10;
        myFixture.addClass("class C" + i + " extends C" + extI + " { }");
    }
    PsiClass class0 = file0.getClasses()[0];
    int delayToCancel = 100;
    for (int i = 0; i < 1000; i++) {
        //System.out.println("i = " + i+ "; delayToCancel="+delayToCancel);
        StandardProgressIndicatorBase progress = new StandardProgressIndicatorBase();
        JobScheduler.getScheduler().schedule(progress::cancel, delayToCancel, TimeUnit.MILLISECONDS);
        try {
            Collections.nCopies(Runtime.getRuntime().availableProcessors(), "").stream().parallel().forEach(__ -> {
                Collection<PsiClass> inheritors = Collections.synchronizedSet(new THashSet<>());
                ProgressManager.getInstance().executeProcessUnderProgress(() -> {
                    boolean success = ClassInheritorsSearch.search(class0).forEach(new CommonProcessors.CollectProcessor<>(inheritors));
                    if (N - 1 != inheritors.size() || !success) {
                        assertEquals(N - 1, inheritors.size());
                    }
                }, progress);
            });
            myFixture.getPsiManager().dropResolveCaches();
            //System.out.println("Iterated all");
            delayToCancel--;
        } catch (ProcessCanceledException e) {
            //System.out.println("e = " + e);
            delayToCancel++;
        }
    }
}
Also used : StandardProgressIndicatorBase(com.intellij.openapi.progress.util.StandardProgressIndicatorBase) PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile) CommonProcessors(com.intellij.util.CommonProcessors) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 20 with PsiJavaFile

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

the class ClassNameDiffersFromFileNameInspection method buildFix.

@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
    final PsiJavaFile file = (PsiJavaFile) infos[0];
    final String fileName = file.getName();
    final int prefixIndex = fileName.indexOf((int) '.');
    final String filenameWithoutPrefix = fileName.substring(0, prefixIndex);
    final PsiClass[] classes = file.getClasses();
    for (PsiClass psiClass : classes) {
        final String className = psiClass.getName();
        if (filenameWithoutPrefix.equals(className)) {
            return null;
        }
    }
    return new RenameFix(filenameWithoutPrefix);
}
Also used : RenameFix(com.siyeh.ig.fixes.RenameFix) PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile) Nullable(org.jetbrains.annotations.Nullable)

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