Search in sources :

Example 86 with PsiFile

use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.

the class SpartanizerAction method getPsiElementFromContext.

/**
     * @param e the action event
     * @return the psiElement extracted from the event's context
     **/
@Nullable
private PsiElement getPsiElementFromContext(AnActionEvent e) {
    PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    return psiFile == null || editor == null ? null : psiFile.findElementAt(editor.getCaretModel().getOffset());
}
Also used : PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 87 with PsiFile

use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.

the class SpartanizerAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement e, @NotNull AnnotationHolder h) {
    try {
        if (!Spartanizer.canTip(e) || e.getContainingFile().getName().contains("Spartanizer"))
            return;
        Annotation annotation = h.createInfoAnnotation(e, "Spartanize This!");
        annotation.registerFix(new IntentionAction() {

            @SuppressWarnings("unchecked")
            @Nls
            @NotNull
            @Override
            public String getText() {
                return Toolbox.getInstance().getTipper(e).description(e);
            }

            @Nls
            @NotNull
            @Override
            public String getFamilyName() {
                return "SpartanizerAction";
            }

            @Override
            public boolean isAvailable(@NotNull Project __, Editor e, PsiFile f) {
                return true;
            }

            @Override
            public void invoke(@NotNull Project p, Editor ed, PsiFile f) throws IncorrectOperationException {
                Spartanizer.spartanizeElement(e);
            }

            @Override
            public boolean startInWriteAction() {
                return false;
            }
        });
        TextAttributesKey.createTextAttributesKey("");
        annotation.setEnforcedTextAttributes((new TextAttributes(null, null, JBColor.BLUE, EffectType.WAVE_UNDERSCORE, 0)));
    } catch (Throwable t) {
        Logger l = new Logger(this.getClass());
        l.error("", t);
    }
}
Also used : Logger(il.org.spartan.Leonidas.plugin.utils.logging.Logger) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) Nls(org.jetbrains.annotations.Nls) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 88 with PsiFile

use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.

the class Playground method spartanizeButtonClicked.

private void spartanizeButtonClicked() {
    if (inputArea.getText().trim().isEmpty()) {
        return;
    }
    Toolbox.getInstance().playground = true;
    PsiFile file = null;
    final boolean[] worked = { true };
    int i;
    for (i = 0; worked[0] && i < before.length; i++, worked[0] = true) {
        file = PsiFileFactory.getInstance(Utils.getProject()).createFileFromText(JavaLanguage.INSTANCE, before[i] + inputArea.getText() + after[i]);
        file.accept(new JavaRecursiveElementVisitor() {

            @Override
            public void visitErrorElement(PsiErrorElement element) {
                worked[0] = false;
                super.visitErrorElement(element);
            }
        });
        if (worked[0]) {
            break;
        }
    }
    if (i < before.length && file != null) {
        Spartanizer.spartanizeFileOnePass(file);
        outputArea.setText(fixString(file.getText(), i));
    } else {
        outputArea.setText(inputArea.getText());
    }
    Toolbox.getInstance().playground = false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiFile(com.intellij.psi.PsiFile) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor)

Example 89 with PsiFile

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

the class AbstractFormatterTest method doTextTest.

public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
    final PsiFile file = createFile("A" + extension, text);
    if (myLineRange != null) {
        DocumentImpl document = new DocumentImpl(text);
        myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    document.replaceString(0, document.getTextLength(), text);
                    manager.commitDocument(document);
                    try {
                        TextRange rangeToUse = myTextRange;
                        if (rangeToUse == null) {
                            rangeToUse = file.getTextRange();
                        }
                        ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            });
        }
    }, "", "");
    if (document == null) {
        fail("Don't expect the document to be null");
        return;
    }
    KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
    manager.commitDocument(document);
    KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 90 with PsiFile

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

the class JavaParser method getLocation.

/**
     * Returns a {@link Location} for the given element
     *
     * @param context information about the file being parsed
     * @param element the element to create a location for
     * @return a location for the given node
     */
// subclasses may want to override/optimize
@SuppressWarnings("MethodMayBeStatic")
@NonNull
public Location getLocation(@NonNull JavaContext context, @NonNull PsiElement element) {
    TextRange range = element.getTextRange();
    UFile uFile = (UFile) getUastContext().convertElementWithParent(element.getContainingFile(), UFile.class);
    if (uFile == null) {
        return Location.NONE;
    }
    PsiFile containingFile = uFile.getPsi();
    File file = context.file;
    if (containingFile != context.getUFile().getPsi()) {
        // Reporting an error in a different file.
        if (context.getDriver().getScope().size() == 1) {
            // Don't bother with this error if it's in a different file during single-file analysis
            return Location.NONE;
        }
        VirtualFile virtualFile = containingFile.getVirtualFile();
        if (virtualFile == null) {
            return Location.NONE;
        }
        file = VfsUtilCore.virtualToIoFile(virtualFile);
    }
    return Location.create(file, context.getContents(), range.getStartOffset(), range.getEndOffset());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) UFile(org.jetbrains.uast.UFile) PsiJavaFile(com.intellij.psi.PsiJavaFile) File(java.io.File) UFile(org.jetbrains.uast.UFile) NonNull(com.android.annotations.NonNull)

Aggregations

PsiFile (com.intellij.psi.PsiFile)1785 VirtualFile (com.intellij.openapi.vfs.VirtualFile)496 PsiElement (com.intellij.psi.PsiElement)468 Project (com.intellij.openapi.project.Project)267 Nullable (org.jetbrains.annotations.Nullable)267 NotNull (org.jetbrains.annotations.NotNull)248 Document (com.intellij.openapi.editor.Document)181 Editor (com.intellij.openapi.editor.Editor)168 XmlFile (com.intellij.psi.xml.XmlFile)126 PsiDirectory (com.intellij.psi.PsiDirectory)114 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)109 Module (com.intellij.openapi.module.Module)108 TextRange (com.intellij.openapi.util.TextRange)88 ArrayList (java.util.ArrayList)84 XmlTag (com.intellij.psi.xml.XmlTag)68 File (java.io.File)58 PsiManager (com.intellij.psi.PsiManager)56 PsiClass (com.intellij.psi.PsiClass)51 List (java.util.List)46 Language (com.intellij.lang.Language)45