Search in sources :

Example 21 with PsiJavaFile

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

the class EnterInJavadocParamDescriptionHandler method postProcessEnter.

@Override
public Result postProcessEnter(@NotNull final PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
    if (!(file instanceof PsiJavaFile) || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER || !CodeStyleSettingsManager.getSettings(file.getProject()).JD_ALIGN_PARAM_COMMENTS) {
        return Result.Continue;
    }
    final CaretModel caretModel = editor.getCaretModel();
    final int caretOffset = caretModel.getOffset();
    if (!isInJavaDoc(editor, caretOffset)) {
        return Result.Continue;
    }
    final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(file, editor, caretOffset);
    if (pair.first == null || pair.first.parameterDescriptionStartPosition == null) {
        return Result.Continue;
    }
    final LogicalPosition caretPosition = caretModel.getLogicalPosition();
    final LogicalPosition nameEndPosition = pair.first.parameterNameEndPosition;
    if (nameEndPosition.line == caretPosition.line && caretPosition.column <= nameEndPosition.column) {
        return Result.Continue;
    }
    final int descriptionStartColumn = pair.first.parameterDescriptionStartPosition.column;
    final LogicalPosition desiredPosition = new LogicalPosition(caretPosition.line, descriptionStartColumn);
    final Document document = editor.getDocument();
    final CharSequence text = document.getCharsSequence();
    final int offsetAfterLastWs = CharArrayUtil.shiftForward(text, caretOffset, " \t");
    if (editor.offsetToLogicalPosition(offsetAfterLastWs).column < desiredPosition.column) {
        final int lineStartOffset = document.getLineStartOffset(desiredPosition.line);
        final String toInsert = StringUtil.repeat(" ", desiredPosition.column - (offsetAfterLastWs - lineStartOffset));
        ApplicationManager.getApplication().runWriteAction(() -> {
            document.insertString(caretOffset, toInsert);
            PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
        });
    }
    myHelper.navigate(desiredPosition, editor, file.getProject());
    return Result.Stop;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) PsiJavaFile(com.intellij.psi.PsiJavaFile) List(java.util.List) Document(com.intellij.openapi.editor.Document)

Example 22 with PsiJavaFile

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

the class FileClassMacro method expand.

public String expand(DataContext dataContext) {
    //Project project = (Project)dataContext.getData(DataConstants.PROJECT);
    //if (project == null) {
    //  return null;
    //}
    //VirtualFile file = (VirtualFile)dataContext.getData(DataConstantsEx.VIRTUAL_FILE);
    //if (file == null) {
    //  return null;
    //}
    //PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    //if (!(psiFile instanceof PsiJavaFile)) {
    //  return null;
    //}
    final PsiFile javaFile = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (!(javaFile instanceof PsiJavaFile))
        return null;
    PsiClass[] classes = ((PsiJavaFile) javaFile).getClasses();
    if (classes.length == 1) {
        return classes[0].getQualifiedName();
    }
    String fileName = javaFile.getVirtualFile().getNameWithoutExtension();
    for (PsiClass aClass : classes) {
        String name = aClass.getName();
        if (fileName.equals(name)) {
            return aClass.getQualifiedName();
        }
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) PsiJavaFile(com.intellij.psi.PsiJavaFile)

Example 23 with PsiJavaFile

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

the class JavaPsiVisitor method visitFile.

void visitFile(@NonNull final JavaContext context) {
    try {
        final PsiJavaFile javaFile = mParser.parseJavaToPsi(context);
        if (javaFile == null) {
            // with details, location, etc.
            return;
        }
        try {
            context.setJavaFile(javaFile);
            mParser.runReadAction(new Runnable() {

                @Override
                public void run() {
                    for (VisitingDetector v : mAllDetectors) {
                        v.setContext(context);
                        v.getDetector().beforeCheckFile(context);
                    }
                }
            });
            if (!mSuperClassDetectors.isEmpty()) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        SuperclassPsiVisitor visitor = new SuperclassPsiVisitor(context);
                        javaFile.accept(visitor);
                    }
                });
            }
            for (final VisitingDetector v : mFullTreeDetectors) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        JavaElementVisitor visitor = v.getVisitor();
                        javaFile.accept(visitor);
                    }
                });
            }
            if (!mMethodDetectors.isEmpty() || !mResourceFieldDetectors.isEmpty() || !mConstructorDetectors.isEmpty() || !mReferenceDetectors.isEmpty()) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        // TODO: Do we need to break this one up into finer grain
                        // locking units
                        JavaElementVisitor visitor = new DelegatingPsiVisitor(context);
                        javaFile.accept(visitor);
                    }
                });
            } else {
                if (!mNodePsiTypeDetectors.isEmpty()) {
                    mParser.runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            // TODO: Do we need to break this one up into finer grain
                            // locking units
                            JavaElementVisitor visitor = new DispatchPsiVisitor();
                            javaFile.accept(visitor);
                        }
                    });
                }
            }
            mParser.runReadAction(new Runnable() {

                @Override
                public void run() {
                    for (VisitingDetector v : mAllDetectors) {
                        v.getDetector().afterCheckFile(context);
                    }
                }
            });
        } finally {
            mParser.dispose(context, javaFile);
            context.setJavaFile(null);
        }
    } catch (ProcessCanceledException ignore) {
    // Cancelling inspections in the IDE
    } catch (RuntimeException e) {
        if (sExceptionCount++ > MAX_REPORTED_CRASHES) {
            // are tripping up ECJ, they get the picture.
            return;
        }
        if (e.getClass().getSimpleName().equals("IndexNotReadyException")) {
            // See http://b.android.com/176644 for an example.
            return;
        }
        // Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268
        // Don't allow lint bugs to take down the whole build. TRY to log this as a
        // lint error instead!
        StringBuilder sb = new StringBuilder(100);
        sb.append("Unexpected failure during lint analysis of ");
        sb.append(context.file.getName());
        sb.append(" (this is a bug in lint or one of the libraries it depends on)\n");
        sb.append(e.getClass().getSimpleName());
        sb.append(':');
        StackTraceElement[] stackTrace = e.getStackTrace();
        int count = 0;
        for (StackTraceElement frame : stackTrace) {
            if (count > 0) {
                sb.append("<-");
            }
            String className = frame.getClassName();
            sb.append(className.substring(className.lastIndexOf('.') + 1));
            sb.append('.').append(frame.getMethodName());
            sb.append('(');
            sb.append(frame.getFileName()).append(':').append(frame.getLineNumber());
            sb.append(')');
            count++;
            // Only print the top 3-4 frames such that we can identify the bug
            if (count == 4) {
                break;
            }
        }
        // NOT e: this makes for very noisy logs
        Throwable throwable = null;
        //noinspection ConstantConditions
        context.log(throwable, sb.toString());
    }
}
Also used : PsiJavaFile(com.intellij.psi.PsiJavaFile) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 24 with PsiJavaFile

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

the class ActivatorRenameTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    PsiFile activatorFile = myFixture.addFileToProject("src/t1/Activator.java", "package t1;\n\n" + "import org.osgi.framework.*;\n\n" + "public class Activator  implements BundleActivator {\n" + "    public void start(BundleContext context) throws Exception { }\n" + "    public void stop(BundleContext context) throws Exception { }\n" + "}\n");
    myActivator = ((PsiJavaFile) activatorFile).getClasses()[0];
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiJavaFile(com.intellij.psi.PsiJavaFile)

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