Search in sources :

Example 36 with TextRange

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

the class I18nizeAction method getHandler.

@Nullable
public static I18nQuickFixHandler getHandler(final AnActionEvent e) {
    final Editor editor = getEditor(e);
    if (editor == null)
        return null;
    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    if (psiFile == null)
        return null;
    TextRange range = JavaI18nUtil.getSelectedRange(editor, psiFile);
    if (range == null)
        return null;
    final PsiLiteralExpression literalExpression = getEnclosingStringLiteral(psiFile, editor);
    PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
    if (element == null)
        return null;
    if (I18nizeConcatenationQuickFix.getEnclosingLiteralConcatenation(element) != null) {
        return new I18nizeConcatenationQuickFix();
    } else if (literalExpression != null && literalExpression.getTextRange().contains(range)) {
        return new I18nizeQuickFix();
    }
    for (I18nizeHandlerProvider handlerProvider : I18nizeHandlerProvider.EP_NAME.getExtensions()) {
        I18nQuickFixHandler handler = handlerProvider.getHandler(psiFile, editor, range);
        if (handler != null) {
            return handler;
        }
    }
    return null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with TextRange

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

the class PyParameterInfoHandler method updateParameterInfo.

/**
   <b>Note: instead of parameter index, we directly store parameter's offset for later use.</b><br/>
   We cannot store an index since we cannot determine what is an argument until we actually map arguments to parameters.
   This is because a tuple in arguments may be a whole argument or map to a tuple parameter.
   */
@Override
public void updateParameterInfo(@NotNull PyArgumentList argumentList, @NotNull UpdateParameterInfoContext context) {
    if (context.getParameterOwner() != argumentList) {
        context.removeHint();
        return;
    }
    // align offset to nearest expression; context may point to a space, etc.
    final List<PyExpression> flattenedArguments = PyUtil.flattenedParensAndLists(argumentList.getArguments());
    // this is already shifted backwards to skip spaces
    final int allegedCursorOffset = context.getOffset();
    if (!argumentList.getTextRange().contains(allegedCursorOffset) && argumentList.getText().endsWith(")")) {
        context.removeHint();
        return;
    }
    final PsiFile file = context.getFile();
    final CharSequence chars = file.getViewProvider().getContents();
    int offset = -1;
    for (PyExpression argument : flattenedArguments) {
        final TextRange range = argument.getTextRange();
        // widen the range to include all whitespace around the argument
        final int left = CharArrayUtil.shiftBackward(chars, range.getStartOffset() - 1, " \t\r\n");
        int right = CharArrayUtil.shiftForwardCarefully(chars, range.getEndOffset(), " \t\r\n");
        if (argument.getParent() instanceof PyListLiteralExpression || argument.getParent() instanceof PyTupleExpression) {
            right = CharArrayUtil.shiftForward(chars, range.getEndOffset(), " \t\r\n])");
        }
        if (left <= allegedCursorOffset && right >= allegedCursorOffset) {
            offset = range.getStartOffset();
            break;
        }
    }
    context.setCurrentParameter(offset);
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange)

Example 38 with TextRange

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

the class PyReplaceExpressionUtil method getPositionInRanges.

private static int getPositionInRanges(@NotNull List<TextRange> ranges, @NotNull TextRange range) {
    final int end = range.getEndOffset();
    final int size = ranges.size();
    for (int i = 0; i < size; i++) {
        final TextRange r = ranges.get(i);
        if (end < r.getStartOffset()) {
            return i;
        }
    }
    return size;
}
Also used : TextRange(com.intellij.openapi.util.TextRange)

Example 39 with TextRange

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

the class DomUtil method getOriginalElement.

@NotNull
public static <T extends DomElement> T getOriginalElement(@NotNull final T domElement) {
    final XmlElement psiElement = domElement.getXmlElement();
    if (psiElement == null)
        return domElement;
    final PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
    final TextRange range = psiElement.getTextRange();
    final PsiElement element = psiFile.findElementAt(range.getStartOffset());
    final int maxLength = range.getLength();
    final boolean isAttribute = psiElement instanceof XmlAttribute;
    final Class<? extends XmlElement> clazz = isAttribute ? XmlAttribute.class : XmlTag.class;
    final DomManager domManager = domElement.getManager();
    DomElement current = null;
    for (XmlElement next = PsiTreeUtil.getParentOfType(element, clazz, false); next != null && next.getTextLength() <= maxLength; next = PsiTreeUtil.getParentOfType(next, clazz, true)) {
        current = isAttribute ? domManager.getDomElement((XmlAttribute) next) : domManager.getDomElement((XmlTag) next);
        if (current != null && domElement.getClass() != current.getClass())
            current = null;
    }
    return (T) current;
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 40 with TextRange

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

the class DomTarget method getTarget.

@Nullable
public static DomTarget getTarget(DomElement element, GenericDomValue nameElement) {
    if (nameElement instanceof GenericAttributeValue) {
        final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) nameElement;
        final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
        if (attributeValue == null) {
            return null;
        }
        final int length = attributeValue.getTextLength();
        if (length >= 2) {
            return new DomTarget(element, attributeValue, new TextRange(1, length - 1), nameElement);
        }
    }
    final XmlTag tag = nameElement.getXmlTag();
    if (tag == null) {
        return null;
    }
    XmlTagValue tagValue = tag.getValue();
    if (StringUtil.isEmpty(tagValue.getTrimmedText())) {
        return null;
    }
    return new DomTarget(element, tag, XmlTagUtil.getTrimmedValueRange(tag), nameElement);
}
Also used : XmlTagValue(com.intellij.psi.xml.XmlTagValue) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) 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