Search in sources :

Example 11 with Segment

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

the class RefManagerImpl method export.

@Override
public Element export(@NotNull RefEntity refEntity, @NotNull final Element element, final int actualLine) {
    refEntity = getRefinedElement(refEntity);
    Element problem = new Element("problem");
    if (refEntity instanceof RefDirectory) {
        Element fileElement = new Element("file");
        VirtualFile virtualFile = ((PsiDirectory) ((RefDirectory) refEntity).getElement()).getVirtualFile();
        fileElement.addContent(virtualFile.getUrl());
        problem.addContent(fileElement);
    } else if (refEntity instanceof RefElement) {
        final RefElement refElement = (RefElement) refEntity;
        final SmartPsiElementPointer pointer = refElement.getPointer();
        PsiFile psiFile = pointer.getContainingFile();
        if (psiFile == null)
            return null;
        Element fileElement = new Element("file");
        Element lineElement = new Element("line");
        final VirtualFile virtualFile = psiFile.getVirtualFile();
        LOG.assertTrue(virtualFile != null);
        fileElement.addContent(virtualFile.getUrl());
        if (actualLine == -1) {
            final Document document = PsiDocumentManager.getInstance(pointer.getProject()).getDocument(psiFile);
            LOG.assertTrue(document != null);
            final Segment range = pointer.getRange();
            lineElement.addContent(String.valueOf(range != null ? document.getLineNumber(range.getStartOffset()) + 1 : -1));
        } else {
            lineElement.addContent(String.valueOf(actualLine + 1));
        }
        problem.addContent(fileElement);
        problem.addContent(lineElement);
        appendModule(problem, refElement.getModule());
    } else if (refEntity instanceof RefModule) {
        final RefModule refModule = (RefModule) refEntity;
        final VirtualFile moduleFile = refModule.getModule().getModuleFile();
        final Element fileElement = new Element("file");
        fileElement.addContent(moduleFile != null ? moduleFile.getUrl() : refEntity.getName());
        problem.addContent(fileElement);
        appendModule(problem, refModule);
    }
    for (RefManagerExtension extension : myExtensions.values()) {
        extension.export(refEntity, problem);
    }
    new SmartRefElementPointerImpl(refEntity, true).writeExternal(problem);
    element.addContent(problem);
    return problem;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightElement(com.intellij.psi.impl.light.LightElement) Element(org.jdom.Element) RefManagerExtension(com.intellij.codeInspection.lang.RefManagerExtension) Document(com.intellij.openapi.editor.Document) Segment(com.intellij.openapi.util.Segment)

Example 12 with Segment

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

the class FindResultUsageInfo method isValid.

@Override
public boolean isValid() {
    if (!super.isValid())
        return false;
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(getPsiFile());
    if (document == null) {
        myCachedResult = null;
        return false;
    }
    Boolean cachedResult = myCachedResult;
    if (document.getModificationStamp() == myTimestamp && cachedResult != null) {
        return cachedResult;
    }
    myTimestamp = document.getModificationStamp();
    Segment segment = getSegment();
    if (segment == null) {
        myCachedResult = false;
        return false;
    }
    VirtualFile file = getPsiFile().getVirtualFile();
    Segment searchOffset;
    if (myAnchor != null) {
        searchOffset = myAnchor.getRange();
        if (searchOffset == null) {
            myCachedResult = false;
            return false;
        }
    } else {
        searchOffset = segment;
    }
    int offset = searchOffset.getStartOffset();
    Long data = myFindModel.getUserData(DOCUMENT_TIMESTAMP_KEY);
    if (data == null || data != myTimestamp) {
        data = myTimestamp;
        FindManagerImpl.clearPreviousFindData(myFindModel);
    }
    myFindModel.putUserData(DOCUMENT_TIMESTAMP_KEY, data);
    FindResult result;
    do {
        result = myFindManager.findString(document.getCharsSequence(), offset, myFindModel, file);
        offset = result.getEndOffset() == offset ? offset + 1 : result.getEndOffset();
        if (!result.isStringFound()) {
            myCachedResult = false;
            return false;
        }
    } while (result.getStartOffset() < segment.getStartOffset());
    boolean ret = segment.getStartOffset() == result.getStartOffset() && segment.getEndOffset() == result.getEndOffset();
    myCachedResult = ret;
    return ret;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) Segment(com.intellij.openapi.util.Segment) FindResult(com.intellij.find.FindResult)

Example 13 with Segment

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

the class InjectedSelfElementInfo method restoreElement.

@Override
public PsiElement restoreElement() {
    PsiFile hostFile = myHostContext.getContainingFile();
    if (hostFile == null || !hostFile.isValid())
        return null;
    PsiElement hostContext = myHostContext.getElement();
    if (hostContext == null)
        return null;
    Segment segment = myInjectedFileRangeInHostFile.getPsiRange();
    if (segment == null)
        return null;
    PsiFile injectedPsi = getInjectedFileIn(hostContext, hostFile, TextRange.create(segment));
    ProperTextRange rangeInInjected = hostToInjected(true, segment, injectedPsi, myAffixOffsets);
    if (rangeInInjected == null)
        return null;
    return myType.findPsiElement(injectedPsi, rangeInInjected.getStartOffset(), rangeInInjected.getEndOffset());
}
Also used : ProperTextRange(com.intellij.openapi.util.ProperTextRange) Segment(com.intellij.openapi.util.Segment)

Example 14 with Segment

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

the class ShredImpl method getRangeInsideHost.

@Override
@NotNull
public TextRange getRangeInsideHost() {
    PsiLanguageInjectionHost host = getHost();
    Segment psiRange = relevantRangeInHost.getPsiRange();
    TextRange textRange = psiRange == null ? null : TextRange.create(psiRange);
    if (host == null) {
        if (textRange != null)
            return textRange;
        Segment fromSP = hostElementPointer.getPsiRange();
        if (fromSP != null)
            return TextRange.create(fromSP);
        return new TextRange(0, 0);
    }
    TextRange hostTextRange = host.getTextRange();
    textRange = textRange == null ? null : textRange.intersection(hostTextRange);
    if (textRange == null)
        return new ProperTextRange(0, hostTextRange.getLength());
    return textRange.shiftRight(-hostTextRange.getStartOffset());
}
Also used : ProperTextRange(com.intellij.openapi.util.ProperTextRange) ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) Segment(com.intellij.openapi.util.Segment) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with Segment

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

the class ShredImpl method equals.

@Override
public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    PsiLanguageInjectionHost.Shred shred = (PsiLanguageInjectionHost.Shred) o;
    PsiLanguageInjectionHost host = getHost();
    Segment hostRangeMarker = getHostRangeMarker();
    Segment hostRangeMarker2 = shred.getHostRangeMarker();
    return host != null && host.equals(shred.getHost()) && prefix.equals(shred.getPrefix()) && suffix.equals(shred.getSuffix()) && range.equals(shred.getRange()) && hostRangeMarker != null && hostRangeMarker2 != null && TextRange.create(hostRangeMarker).equals(TextRange.create(hostRangeMarker2));
}
Also used : Segment(com.intellij.openapi.util.Segment)

Aggregations

Segment (com.intellij.openapi.util.Segment)21 NotNull (org.jetbrains.annotations.NotNull)7 TextRange (com.intellij.openapi.util.TextRange)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)3 Document (com.intellij.openapi.editor.Document)3 ProperTextRange (com.intellij.openapi.util.ProperTextRange)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)2 UsageInfo (com.intellij.usageView.UsageInfo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2 Issue (com.android.tools.lint.detector.api.Issue)1 RefManagerExtension (com.intellij.codeInspection.lang.RefManagerExtension)1 FindResult (com.intellij.find.FindResult)1 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)1 FileASTNode (com.intellij.lang.FileASTNode)1 Language (com.intellij.lang.Language)1 Editor (com.intellij.openapi.editor.Editor)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1