Search in sources :

Example 71 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class XmlDocumentationProvider method getDocumentationElementForLookupItem.

@Override
public PsiElement getDocumentationElementForLookupItem(final PsiManager psiManager, Object object, PsiElement element) {
    if (object instanceof XmlExtension.TagInfo) {
        return ((XmlExtension.TagInfo) object).getDeclaration();
    }
    final PsiElement originalElement = element;
    boolean isAttrCompletion = element instanceof XmlAttribute;
    if (!isAttrCompletion && element instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) element).getTokenType();
        if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_TAG_END) {
            isAttrCompletion = true;
        } else if (element.getParent() instanceof XmlAttribute) {
            isAttrCompletion = true;
        }
    }
    element = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
    if (element instanceof XmlTag) {
        XmlTag xmlTag = (XmlTag) element;
        XmlElementDescriptor elementDescriptor;
        if (isAttrCompletion && object instanceof String) {
            elementDescriptor = xmlTag.getDescriptor();
            if (elementDescriptor != null) {
                final XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor((String) object, xmlTag);
                if (attributeDescriptor != null) {
                    final PsiElement declaration = attributeDescriptor.getDeclaration();
                    if (declaration != null)
                        return declaration;
                }
            }
        }
        if (object == null)
            return null;
        try {
            @NonNls StringBuilder tagText = new StringBuilder(object.toString());
            String namespacePrefix = XmlUtil.findPrefixByQualifiedName(object.toString());
            String namespace = xmlTag.getNamespaceByPrefix(namespacePrefix);
            if (namespace != null && namespace.length() > 0) {
                tagText.append(" xmlns");
                if (namespacePrefix.length() > 0)
                    tagText.append(":").append(namespacePrefix);
                tagText.append("=\"").append(namespace).append("\"");
            }
            XmlTag tagFromText = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagText + "/>");
            XmlElementDescriptor parentDescriptor = xmlTag.getDescriptor();
            elementDescriptor = (parentDescriptor != null) ? parentDescriptor.getElementDescriptor(tagFromText, xmlTag) : null;
            if (elementDescriptor == null) {
                PsiElement parent = xmlTag.getParent();
                if (parent instanceof XmlTag) {
                    parentDescriptor = ((XmlTag) parent).getDescriptor();
                    elementDescriptor = (parentDescriptor != null) ? parentDescriptor.getElementDescriptor(tagFromText, (XmlTag) parent) : null;
                }
            }
            if (elementDescriptor instanceof AnyXmlElementDescriptor) {
                final XmlNSDescriptor nsDescriptor = xmlTag.getNSDescriptor(xmlTag.getNamespaceByPrefix(namespacePrefix), true);
                elementDescriptor = (nsDescriptor != null) ? nsDescriptor.getElementDescriptor(tagFromText) : null;
            }
            // The very special case of xml file
            final PsiFile containingFile = xmlTag.getContainingFile();
            final XmlFile xmlFile = XmlUtil.getContainingFile(xmlTag);
            if (xmlFile != containingFile) {
                final XmlTag rootTag = xmlFile.getDocument().getRootTag();
                if (rootTag != null) {
                    final XmlNSDescriptor nsDescriptor = rootTag.getNSDescriptor(rootTag.getNamespaceByPrefix(namespacePrefix), true);
                    elementDescriptor = (nsDescriptor != null) ? nsDescriptor.getElementDescriptor(tagFromText) : null;
                }
            }
            if (elementDescriptor != null) {
                PsiElement declaration = elementDescriptor.getDeclaration();
                if (declaration != null)
                    declaration.putUserData(DESCRIPTOR_KEY, elementDescriptor);
                return declaration;
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
    if (object instanceof String && originalElement != null) {
        PsiElement result = findDeclWithName((String) object, originalElement);
        if (result == null && element instanceof XmlTag) {
            XmlAttribute attribute = PsiTreeUtil.getParentOfType(originalElement, XmlAttribute.class, false);
            if (attribute != null) {
                XmlAttributeDescriptor descriptor = attribute.getDescriptor();
                if (descriptor != null && descriptor.getDeclaration() instanceof XmlTag) {
                    result = findEnumerationValue((String) object, (XmlTag) descriptor.getDeclaration());
                }
            }
        }
        return result;
    }
    if (object instanceof XmlElementDescriptor) {
        return ((XmlElementDescriptor) object).getDeclaration();
    }
    return null;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) IElementType(com.intellij.psi.tree.IElementType) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 72 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class HtmlDocumentationProvider method createNavigationElementHTML.

public PsiElement createNavigationElementHTML(PsiManager psiManager, String text, PsiElement context) {
    String key = text.toLowerCase(Locale.US);
    final HtmlTagDescriptor descriptor = HtmlDescriptorsTable.getTagDescriptor(key);
    if (descriptor != null && !isAttributeContext(context)) {
        try {
            final XmlTag tagFromText = XmlElementFactory.getInstance(psiManager.getProject()).createTagFromText("<" + key + " xmlns=\"" + XmlUtil.XHTML_URI + "\"/>");
            final XmlElementDescriptor tagDescriptor = tagFromText.getDescriptor();
            return tagDescriptor != null ? tagDescriptor.getDeclaration() : null;
        } catch (IncorrectOperationException ignore) {
        }
    } else {
        XmlTag tagContext = findTagContext(context);
        HtmlAttributeDescriptor myAttributeDescriptor = getDescriptor(key, tagContext);
        if (myAttributeDescriptor != null && tagContext != null) {
            XmlElementDescriptor tagDescriptor = tagContext.getDescriptor();
            XmlAttributeDescriptor attributeDescriptor = tagDescriptor != null ? tagDescriptor.getAttributeDescriptor(text, tagContext) : null;
            return (attributeDescriptor != null) ? attributeDescriptor.getDeclaration() : null;
        }
    }
    return null;
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 73 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class ValueHint method getSelectedExpression.

private static Trinity<PsiElement, TextRange, Value> getSelectedExpression(final Project project, final Editor editor, final Point point, final ValueHintType type) {
    final Ref<PsiElement> selectedExpression = Ref.create(null);
    final Ref<TextRange> currentRange = Ref.create(null);
    final Ref<Value> preCalculatedValue = Ref.create(null);
    PsiDocumentManager.getInstance(project).commitAndRunReadAction(() -> {
        // Point -> offset
        final int offset = calculateOffset(editor, point);
        PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (psiFile == null || !psiFile.isValid()) {
            return;
        }
        int selectionStart = editor.getSelectionModel().getSelectionStart();
        int selectionEnd = editor.getSelectionModel().getSelectionEnd();
        if ((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && (selectionStart <= offset && offset <= selectionEnd)) {
            PsiElement ctx = (selectionStart > 0) ? psiFile.findElementAt(selectionStart - 1) : psiFile.findElementAt(selectionStart);
            try {
                String text = editor.getSelectionModel().getSelectedText();
                if (text != null && ctx != null) {
                    final JVMElementFactory factory = JVMElementFactories.getFactory(ctx.getLanguage(), project);
                    if (factory == null) {
                        return;
                    }
                    selectedExpression.set(factory.createExpressionFromText(text, ctx));
                    currentRange.set(new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd()));
                }
            } catch (IncorrectOperationException ignored) {
            }
        }
        if (currentRange.get() == null) {
            PsiElement elementAtCursor = psiFile.findElementAt(offset);
            if (elementAtCursor == null) {
                return;
            }
            Pair<PsiElement, TextRange> pair = findExpression(elementAtCursor, type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT);
            if (pair == null) {
                if (type == ValueHintType.MOUSE_OVER_HINT) {
                    final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(project).getContext().getDebuggerSession();
                    if (debuggerSession != null && debuggerSession.isPaused()) {
                        final Pair<Method, Value> lastExecuted = debuggerSession.getProcess().getLastExecutedMethod();
                        if (lastExecuted != null) {
                            final Method method = lastExecuted.getFirst();
                            if (method != null) {
                                final Pair<PsiElement, TextRange> expressionPair = findExpression(elementAtCursor, true);
                                if (expressionPair != null && expressionPair.getFirst() instanceof PsiMethodCallExpression) {
                                    final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expressionPair.getFirst();
                                    final PsiMethod psiMethod = methodCallExpression.resolveMethod();
                                    if (psiMethod != null) {
                                        final JVMName jvmSignature = JVMNameUtil.getJVMSignature(psiMethod);
                                        try {
                                            if (method.name().equals(psiMethod.getName()) && method.signature().equals(jvmSignature.getName(debuggerSession.getProcess()))) {
                                                pair = expressionPair;
                                                preCalculatedValue.set(lastExecuted.getSecond());
                                            }
                                        } catch (EvaluateException ignored) {
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (pair == null) {
                return;
            }
            selectedExpression.set(pair.getFirst());
            currentRange.set(pair.getSecond());
        }
    });
    return Trinity.create(selectedExpression.get(), currentRange.get(), preCalculatedValue.get());
}
Also used : JVMName(com.intellij.debugger.engine.JVMName) Method(com.sun.jdi.Method) AbstractValueHint(com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) PrimitiveValue(com.sun.jdi.PrimitiveValue) Value(com.sun.jdi.Value) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 74 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class JavaEditorTextProviderImpl method findExpression.

@Nullable
public Pair<PsiElement, TextRange> findExpression(PsiElement element, boolean allowMethodCalls) {
    PsiElement expression = null;
    PsiElement parent = element.getParent();
    if (parent instanceof PsiLiteralExpression || parent instanceof PsiLambdaExpression) {
        element = parent;
        parent = parent.getParent();
    }
    if (parent instanceof PsiVariable) {
        expression = element;
    } else if (parent instanceof PsiReferenceExpression) {
        final PsiElement pparent = parent.getParent();
        if (parent instanceof PsiMethodReferenceExpression || (pparent instanceof PsiCallExpression && ((PsiCallExpression) pparent).getArgumentList() != null)) {
            // skip arrays
            parent = pparent;
        } else if (pparent instanceof PsiReferenceExpression) {
            if (((PsiReferenceExpression) parent).resolve() instanceof PsiClass) {
                return findExpression(pparent, allowMethodCalls);
            }
        }
        if (allowMethodCalls || !DebuggerUtils.hasSideEffects(parent)) {
            expression = parent;
        }
    } else if (parent instanceof PsiThisExpression) {
        expression = parent;
    } else if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiMethodCallExpression) {
        if (allowMethodCalls) {
            expression = parent.getParent();
        }
    } else if (parent instanceof PsiArrayInitializerExpression) {
        if (allowMethodCalls) {
            PsiNewExpression newExpr = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);
            if (newExpr != null) {
                expression = newExpr;
            }
        }
    } else if (parent instanceof PsiExpression && !(parent instanceof PsiNewExpression) && !(parent instanceof PsiLambdaExpression)) {
        if (allowMethodCalls || !DebuggerUtils.hasSideEffects(parent)) {
            expression = parent;
        }
    } else {
        PsiElement castExpr = PsiTreeUtil.getParentOfType(element, PsiTypeCastExpression.class);
        if (castExpr != null) {
            if (allowMethodCalls || !DebuggerUtils.hasSideEffects(castExpr)) {
                expression = castExpr;
            }
        } else if (allowMethodCalls) {
            PsiElement e = PsiTreeUtil.getParentOfType(element, PsiVariable.class, PsiExpression.class, PsiMethod.class);
            if (e instanceof PsiNewExpression) {
                if (((PsiNewExpression) e).getAnonymousClass() == null) {
                    expression = e;
                }
            }
        }
    }
    if (expression != null) {
        try {
            PsiElement context = element;
            if (parent instanceof PsiParameter) {
                try {
                    context = ((PsiMethod) ((PsiParameter) parent).getDeclarationScope()).getBody();
                } catch (Throwable ignored) {
                }
            } else {
                while (context != null && !(context instanceof PsiStatement) && !(context instanceof PsiClass)) {
                    context = context.getParent();
                }
            }
            TextRange textRange = expression.getTextRange();
            PsiElement psiExpression = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory().createExpressionFromText(expression.getText(), context);
            return Pair.create(psiExpression, textRange);
        } catch (IncorrectOperationException e) {
            LOG.debug(e);
        }
    }
    return null;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 75 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class ContextUtil method getContextElement.

@Nullable
public static PsiElement getContextElement(final StackFrameContext context, final SourcePosition position) {
    if (LOG.isDebugEnabled()) {
        final SourcePosition sourcePosition = getSourcePosition(context);
        LOG.assertTrue(Comparing.equal(sourcePosition, position));
    }
    return ReadAction.compute(() -> {
        final PsiElement element = getContextElement(position);
        if (element == null) {
            return null;
        }
        // further code is java specific, actually
        if (element.getLanguage().getAssociatedFileType() != DefaultCodeFragmentFactory.getInstance().getFileType()) {
            return element;
        }
        final StackFrameProxyImpl frameProxy = (StackFrameProxyImpl) context.getFrameProxy();
        if (frameProxy == null) {
            return element;
        }
        try {
            List<LocalVariableProxyImpl> list = frameProxy.visibleVariables();
            PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(element.getProject()).getResolveHelper();
            StringBuilder buf = null;
            for (LocalVariableProxyImpl localVariable : list) {
                final String varName = localVariable.name();
                if (resolveHelper.resolveReferencedVariable(varName, element) == null) {
                    if (buf == null) {
                        buf = new StringBuilder("{");
                    }
                    buf.append(localVariable.getVariable().typeName()).append(" ").append(varName).append(";");
                }
            }
            if (buf == null) {
                return element;
            }
            buf.append('}');
            final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(element.getProject()).getElementFactory();
            final PsiCodeBlock codeBlockFromText = elementFactory.createCodeBlockFromText(buf.toString(), element);
            final PsiStatement[] statements = codeBlockFromText.getStatements();
            for (PsiStatement statement : statements) {
                if (statement instanceof PsiDeclarationStatement) {
                    PsiDeclarationStatement declStatement = (PsiDeclarationStatement) statement;
                    PsiElement[] declaredElements = declStatement.getDeclaredElements();
                    for (PsiElement declaredElement : declaredElements) {
                        declaredElement.putUserData(IS_JSP_IMPLICIT, Boolean.TRUE);
                    }
                }
            }
            return codeBlockFromText;
        } catch (IncorrectOperationException | EvaluateException ignored) {
            return element;
        }
    });
}
Also used : StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) LocalVariableProxyImpl(com.intellij.debugger.jdi.LocalVariableProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) SourcePosition(com.intellij.debugger.SourcePosition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)485 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 Nullable (org.jetbrains.annotations.Nullable)54 PsiElement (com.intellij.psi.PsiElement)50 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Document (com.intellij.openapi.editor.Document)37 ASTNode (com.intellij.lang.ASTNode)35 PsiFile (com.intellij.psi.PsiFile)35 Editor (com.intellij.openapi.editor.Editor)33 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 ArrayList (java.util.ArrayList)31 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 IOException (java.io.IOException)24 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 XmlTag (com.intellij.psi.xml.XmlTag)20 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)19 Module (com.intellij.openapi.module.Module)18