Search in sources :

Example 61 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class ProjectionDocument method addMasterDocumentRange.

/**
 * Ensures that the given range of the master document is part of this
 * projection document.
 *
 * @param offsetInMaster the offset of the master document range
 * @param lengthInMaster the length of the master document range
 * @param masterDocumentEvent the master document event which causes this
 *            projection change, or <code>null</code> if none
 * @throws BadLocationException in case the master event is not valid
 */
private void addMasterDocumentRange(int offsetInMaster, int lengthInMaster, DocumentEvent masterDocumentEvent) throws BadLocationException {
    /*
		 * Calling internalAddMasterDocumentRange may cause other master ranges
		 * to become unfolded, resulting in re-entrant calls to this method. In
		 * order to not add a region twice, we have to compute the next region
		 * to add in every iteration.
		 *
		 * To place an upper bound on the number of iterations, we use the number
		 * of fragments * 2 as the limit.
		 */
    int limit = Math.max(getFragments().length * 2, 20);
    while (true) {
        if (limit-- < 0)
            // $NON-NLS-1$
            throw new IllegalArgumentException("safety loop termination");
        IRegion gap = computeFirstUnprojectedMasterRegion(offsetInMaster, lengthInMaster);
        if (gap == null)
            return;
        internalAddMasterDocumentRange(gap.getOffset(), gap.getLength(), masterDocumentEvent);
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 62 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class ProjectionDocument method removeMasterDocumentRange.

/**
 * Ensures that the given range of the master document is not part of this
 * projection document.
 *
 * @param offsetInMaster the offset of the master document range
 * @param lengthInMaster the length of the master document range
 * @throws BadLocationException in case the master event is not valid
 */
public void removeMasterDocumentRange(int offsetInMaster, int lengthInMaster) throws BadLocationException {
    IRegion[] fragments = computeProjectedMasterRegions(offsetInMaster, lengthInMaster);
    if (fragments == null || fragments.length == 0)
        return;
    for (int i = 0; i < fragments.length; i++) {
        IRegion fragment = fragments[i];
        internalRemoveMasterDocumentRange(fragment.getOffset(), fragment.getLength());
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 63 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class ProjectionDocument method normalize.

/**
 * Transforms a document event of the master document into a projection
 * document based document event.
 *
 * @param masterEvent the master document event
 * @return the slave document event
 * @throws BadLocationException in case the master event is not valid
 */
private ProjectionDocumentEvent normalize(DocumentEvent masterEvent) throws BadLocationException {
    if (!isUpdating()) {
        IRegion imageRegion = fMapping.toExactImageRegion(new Region(masterEvent.getOffset(), masterEvent.getLength()));
        if (imageRegion != null)
            return new ProjectionDocumentEvent(this, imageRegion.getOffset(), imageRegion.getLength(), masterEvent.getText(), masterEvent);
        return null;
    }
    ProjectionDocumentEvent event = new ProjectionDocumentEvent(this, fOriginalEvent.getOffset(), fOriginalEvent.getLength(), fOriginalEvent.getText(), masterEvent);
    fOriginalEvent = null;
    return event;
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 64 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class ProjectionMapping method toClosestImageLine.

@Override
public int toClosestImageLine(int originLine) throws BadLocationException {
    try {
        int imageLine = toImageLine(originLine);
        if (imageLine > -1)
            return imageLine;
        Position[] fragments = getFragments();
        if (fragments.length == 0)
            return -1;
        IRegion originLineRegion = fMasterDocument.getLineInformation(originLine);
        int index = fMasterDocument.computeIndexInCategory(fFragmentsCategory, originLineRegion.getOffset());
        if (0 < index && index < fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            int leftDistance = originLineRegion.getOffset() - (exclusiveEnd(left));
            Fragment right = (Fragment) fragments[index];
            int rightDistance = right.getOffset() - (exclusiveEnd(originLineRegion));
            if (leftDistance <= rightDistance)
                originLine = fMasterDocument.getLineOfOffset(left.getOffset() + Math.max(left.getLength() - 1, 0));
            else
                originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == 0) {
            Fragment right = (Fragment) fragments[index];
            originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            originLine = fMasterDocument.getLineOfOffset(exclusiveEnd(left));
        }
        return toImageLine(originLine);
    } catch (BadPositionCategoryException x) {
    }
    return -1;
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IRegion(org.eclipse.jface.text.IRegion)

Example 65 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class ProjectionMapping method getExactCoverage.

@Override
public IRegion[] getExactCoverage(IRegion originRegion) throws BadLocationException {
    int originOffset = originRegion.getOffset();
    int originLength = originRegion.getLength();
    if (originLength == 0) {
        int imageOffset = toImageOffset(originOffset);
        return imageOffset > -1 ? new IRegion[] { new Region(originOffset, 0) } : null;
    }
    int endOffset = originOffset + originLength;
    Position[] fragments = getFragments();
    int firstIndex = findFragmentIndex(originOffset, RIGHT);
    int lastIndex = findFragmentIndex(endOffset - 1, LEFT);
    if (firstIndex == -1 || firstIndex > lastIndex)
        return null;
    int resultLength = lastIndex - firstIndex + 1;
    IRegion[] result = new IRegion[resultLength];
    // first
    result[0] = createOriginStartRegion((Fragment) fragments[firstIndex], originOffset - fragments[firstIndex].getOffset());
    // middles
    for (int i = 1; i < resultLength - 1; i++) result[i] = createOriginRegion((Fragment) fragments[firstIndex + i]);
    // last
    Fragment last = (Fragment) fragments[lastIndex];
    int fragmentEndOffset = exclusiveEnd(last);
    IRegion lastRegion = createOriginEndRegion(last, fragmentEndOffset - endOffset);
    if (resultLength > 1) {
        // first != last
        result[resultLength - 1] = lastRegion;
    } else {
        // merge first and last
        IRegion intersection = getIntersectingRegion(result[0], lastRegion);
        if (intersection == null)
            return null;
        result[0] = intersection;
    }
    return result;
}
Also used : Position(org.eclipse.jface.text.Position) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)341 BadLocationException (org.eclipse.jface.text.BadLocationException)158 Region (org.eclipse.jface.text.Region)151 Test (org.junit.Test)94 IDocument (org.eclipse.jface.text.IDocument)68 Point (org.eclipse.swt.graphics.Point)49 Position (org.eclipse.jface.text.Position)38 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)20 ITypedRegion (org.eclipse.jface.text.ITypedRegion)20 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)18 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)17 IEditorPart (org.eclipse.ui.IEditorPart)13 ArrayList (java.util.ArrayList)11 Document (org.eclipse.jface.text.Document)11 StyledText (org.eclipse.swt.custom.StyledText)10 ITextSelection (org.eclipse.jface.text.ITextSelection)9 IFile (org.eclipse.core.resources.IFile)8 Annotation (org.eclipse.jface.text.source.Annotation)7 HashMap (java.util.HashMap)6 CoreException (org.eclipse.core.runtime.CoreException)6