Search in sources :

Example 1 with UElementWithLocation

use of org.jetbrains.uast.psi.UElementWithLocation 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 2 with UElementWithLocation

use of org.jetbrains.uast.psi.UElementWithLocation in project kotlin by JetBrains.

the class IntellijLintUtils method getUastLocation.

/**
   * Gets the location of the given element
   *
   * @param file the file containing the location
   * @param element the element to look up the location for
   * @return the location of the given element
   */
@NonNull
public static Location getUastLocation(@NonNull File file, @NonNull UElement element) {
    //noinspection ConstantConditions
    PsiFile containingPsiFile = UastUtils.getContainingFile(element).getPsi();
    assert containingPsiFile.getVirtualFile() == null || FileUtil.filesEqual(VfsUtilCore.virtualToIoFile(containingPsiFile.getVirtualFile()), file);
    if (element instanceof UClass) {
        // Point to the name rather than the beginning of the javadoc
        UClass clz = (UClass) element;
        UElement nameIdentifier = clz.getUastAnchor();
        if (nameIdentifier != null) {
            element = nameIdentifier;
        }
    }
    TextRange textRange = null;
    PsiElement psi = element.getPsi();
    if (psi != null) {
        textRange = psi.getTextRange();
    } else if (element instanceof UElementWithLocation) {
        UElementWithLocation elementWithLocation = (UElementWithLocation) element;
        textRange = new TextRange(elementWithLocation.getStartOffset(), elementWithLocation.getEndOffset());
    }
    if (textRange == null) {
        return Location.NONE;
    }
    Position start = new DefaultPosition(-1, -1, textRange.getStartOffset());
    Position end = new DefaultPosition(-1, -1, textRange.getEndOffset());
    return Location.create(file, start, end);
}
Also used : UElementWithLocation(org.jetbrains.uast.psi.UElementWithLocation) TextRange(com.intellij.openapi.util.TextRange) NonNull(com.android.annotations.NonNull)

Aggregations

NonNull (com.android.annotations.NonNull)2 TextRange (com.intellij.openapi.util.TextRange)2 UElementWithLocation (org.jetbrains.uast.psi.UElementWithLocation)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 File (java.io.File)1