Search in sources :

Example 31 with TextRange

use of com.intellij.openapi.util.TextRange 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 32 with TextRange

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

the class JavaContext method getUastLocation.

@NonNull
public Location getUastLocation(@Nullable UElement node) {
    if (node == null) {
        return Location.NONE;
    }
    if (node instanceof UElementWithLocation) {
        UFile file = UastUtils.getContainingFile(node);
        if (file == null) {
            return Location.NONE;
        }
        File ioFile = UastUtils.getIoFile(file);
        if (ioFile == null) {
            return Location.NONE;
        }
        UElementWithLocation segment = (UElementWithLocation) node;
        return Location.create(ioFile, file.getPsi().getText(), segment.getStartOffset(), segment.getEndOffset());
    } else {
        PsiElement psiElement = node.getPsi();
        if (psiElement != null) {
            TextRange range = psiElement.getTextRange();
            UFile containingFile = getUFile();
            if (containingFile == null) {
                return Location.NONE;
            }
            File file = this.file;
            if (!containingFile.equals(getUFile())) {
                // Reporting an error in a different file.
                if (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.getPsi().getVirtualFile();
                if (virtualFile == null) {
                    return Location.NONE;
                }
                file = VfsUtilCore.virtualToIoFile(virtualFile);
            }
            return Location.create(file, getContents(), range.getStartOffset(), range.getEndOffset());
        }
    }
    return Location.NONE;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UElementWithLocation(org.jetbrains.uast.psi.UElementWithLocation) TextRange(com.intellij.openapi.util.TextRange) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 33 with TextRange

use of com.intellij.openapi.util.TextRange 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)

Example 34 with TextRange

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

the class GenericDomValueReference method createTextRange.

protected TextRange createTextRange() {
    if (myGenericValue instanceof GenericAttributeValue) {
        final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) myGenericValue;
        final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
        if (attributeValue == null) {
            return TextRange.from(0, genericAttributeValue.getXmlAttribute().getTextLength());
        }
        final int length = attributeValue.getTextLength();
        return length < 2 ? TextRange.from(0, length) : new TextRange(1, length - 1);
    }
    final XmlTag tag = myGenericValue.getXmlTag();
    assert tag != null;
    return XmlTagUtil.getTrimmedValueRange(tag);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ResolvingHint(com.intellij.psi.ResolvingHint) XmlTag(com.intellij.psi.xml.XmlTag)

Example 35 with TextRange

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

the class DomElementAnnotationHolderImpl method createAnnotation.

@Override
@NotNull
public Annotation createAnnotation(@NotNull DomElement element, HighlightSeverity severity, @Nullable String message) {
    final XmlElement xmlElement = element.getXmlElement();
    LOG.assertTrue(xmlElement != null, "No XML element for " + element);
    final TextRange range = xmlElement.getTextRange();
    final int startOffset = range.getStartOffset();
    final int endOffset = message == null ? startOffset : range.getEndOffset();
    final Annotation annotation = new Annotation(startOffset, endOffset, severity, message, null);
    myAnnotations.add(annotation);
    return annotation;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) NotNull(org.jetbrains.annotations.NotNull)

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