Search in sources :

Example 21 with TextRange

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

the class XmlNsPrefixAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
        return;
    PsiElement parent = element.getParent();
    if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
        return;
    TextRange elementRange = element.getTextRange();
    List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
    for (SchemaPrefixReference ref : references) {
        TextRange rangeInElement = ref.getRangeInElement();
        if (rangeInElement.isEmpty())
            continue;
        TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
        if (!range.intersects(elementRange))
            continue;
        holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 22 with TextRange

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

the class XmlUnboundNsPrefixInspection method checkUnboundNamespacePrefix.

private static void checkUnboundNamespacePrefix(final XmlElement element, final XmlTag context, String namespacePrefix, final XmlToken token, final ProblemsHolder holder, boolean isOnTheFly) {
    if (namespacePrefix.isEmpty() && (!(element instanceof XmlTag) || !(element.getParent() instanceof XmlDocument)) || XML.equals(namespacePrefix)) {
        return;
    }
    final String namespaceByPrefix = context.getNamespaceByPrefix(namespacePrefix);
    if (!namespaceByPrefix.isEmpty()) {
        return;
    }
    PsiFile psiFile = context.getContainingFile();
    if (!(psiFile instanceof XmlFile))
        return;
    final XmlFile containingFile = (XmlFile) psiFile;
    if (!HighlightingLevelManager.getInstance(containingFile.getProject()).shouldInspect(containingFile))
        return;
    final XmlExtension extension = XmlExtension.getExtension(containingFile);
    if (extension.getPrefixDeclaration(context, namespacePrefix) != null) {
        return;
    }
    final String localizedMessage = isOnTheFly ? XmlErrorMessages.message("unbound.namespace", namespacePrefix) : XmlErrorMessages.message("unbound.namespace.no.param");
    if (namespacePrefix.isEmpty()) {
        final XmlTag tag = (XmlTag) element;
        if (!XmlUtil.JSP_URI.equals(tag.getNamespace())) {
            LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
            reportTagProblem(tag, localizedMessage, null, ProblemHighlightType.INFORMATION, fix, holder);
        }
        return;
    }
    final int prefixLength = namespacePrefix.length();
    final TextRange range = new TextRange(0, prefixLength);
    final HighlightInfoType infoType = extension.getHighlightInfoType(containingFile);
    final ProblemHighlightType highlightType = infoType == HighlightInfoType.ERROR ? ProblemHighlightType.ERROR : ProblemHighlightType.LIKE_UNKNOWN_SYMBOL;
    if (element instanceof XmlTag) {
        LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
        reportTagProblem(element, localizedMessage, range, highlightType, fix, holder);
    } else if (element instanceof XmlAttribute) {
        LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(element, namespacePrefix, token) : null;
        XmlAttribute attribute = (XmlAttribute) element;
        holder.registerProblem(attribute.getNameElement(), localizedMessage, highlightType, range, fix);
    } else {
        holder.registerProblem(element, localizedMessage, highlightType, range);
    }
}
Also used : XmlExtension(com.intellij.xml.XmlExtension) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Example 23 with TextRange

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

the class XmlFormatterTestBase method checkFormattingDoesNotProduceException.

protected void checkFormattingDoesNotProduceException(String name) throws Exception {
    String text = loadFile(name + ".xml", null);
    final XmlFileImpl file = (XmlFileImpl) createFile(name + ".xml", text);
    myTextRange = new TextRange(10000, 10001);
    CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
    myTextRange = new TextRange(1000000, 1000001);
    CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
    myTextRange = new TextRange(0, text.length());
    CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> performFormatting(file)), "", "");
}
Also used : TextRange(com.intellij.openapi.util.TextRange) XmlFileImpl(com.intellij.psi.impl.source.xml.XmlFileImpl)

Example 24 with TextRange

use of com.intellij.openapi.util.TextRange in project kotlin by JetBrains.

the class KtInvokeFunctionReference method getRanges.

@Override
public List<TextRange> getRanges() {
    List<TextRange> list = new ArrayList<TextRange>();
    KtValueArgumentList valueArgumentList = getExpression().getValueArgumentList();
    if (valueArgumentList != null) {
        if (valueArgumentList.getArguments().size() > 0) {
            ASTNode valueArgumentListNode = valueArgumentList.getNode();
            ASTNode lPar = valueArgumentListNode.findChildByType(KtTokens.LPAR);
            if (lPar != null) {
                list.add(getRange(lPar));
            }
            ASTNode rPar = valueArgumentListNode.findChildByType(KtTokens.RPAR);
            if (rPar != null) {
                list.add(getRange(rPar));
            }
        } else {
            list.add(getRange(valueArgumentList.getNode()));
        }
    }
    List<KtLambdaArgument> functionLiteralArguments = getExpression().getLambdaArguments();
    for (KtLambdaArgument functionLiteralArgument : functionLiteralArguments) {
        KtLambdaExpression functionLiteralExpression = functionLiteralArgument.getLambdaExpression();
        list.add(getRange(functionLiteralExpression.getLeftCurlyBrace()));
        ASTNode rightCurlyBrace = functionLiteralExpression.getRightCurlyBrace();
        if (rightCurlyBrace != null) {
            list.add(getRange(rightCurlyBrace));
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange)

Example 25 with TextRange

use of com.intellij.openapi.util.TextRange in project kotlin by JetBrains.

the class KotlinNotSurrounder method surroundExpression.

@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
    KtPrefixExpression prefixExpr = (KtPrefixExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression("!(a)");
    KtParenthesizedExpression parenthesizedExpression = (KtParenthesizedExpression) prefixExpr.getBaseExpression();
    assert parenthesizedExpression != null : "JetParenthesizedExpression should exists for " + prefixExpr.getText() + " expression";
    KtExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
    assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
    expressionWithoutParentheses.replace(expression);
    expression = (KtExpression) expression.replace(prefixExpr);
    CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
    int offset = expression.getTextRange().getEndOffset();
    return new TextRange(offset, offset);
}
Also used : KtPrefixExpression(org.jetbrains.kotlin.psi.KtPrefixExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) TextRange(com.intellij.openapi.util.TextRange) KtParenthesizedExpression(org.jetbrains.kotlin.psi.KtParenthesizedExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31