Search in sources :

Example 81 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class AbstractStructuredSelectHandler method getIndexedRegion.

/**
 * This method will probably be removed and replaced by using new
 * selection provider
 *
 * @param document
 * @param offset
 * @return
 */
protected IndexedRegion getIndexedRegion(IDocument document, int offset) {
    IndexedRegion indexedRegion = null;
    int lastOffset = offset;
    IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
    if (model != null) {
        try {
            indexedRegion = model.getIndexedRegion(lastOffset);
            while (indexedRegion == null && lastOffset >= 0) {
                lastOffset--;
                indexedRegion = model.getIndexedRegion(lastOffset);
            }
        } finally {
            model.releaseFromRead();
        }
    }
    return indexedRegion;
}
Also used : IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 82 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class AbstractStructuredFoldingStrategy method reconcile.

/**
 * <p><b>NOTE 1:</b> This implementation of reconcile ignores the given {@link IRegion} and instead gets all of the
 * structured document regions effected by the range of the given {@link DirtyRegion}.</p>
 *
 * <p><b>NOTE 2:</b> In cases where multiple {@link IRegion} maybe dirty it is more efficient to pass one
 * {@link DirtyRegion} contain all of the {@link IRegion}s then one {@link DirtyRegion} for each IRegion.
 * Case in point, when processing the entire document it is <b>recommended</b> that this function be
 * called only <b>once</b> with one {@link DirtyRegion} that spans the entire document.</p>
 *
 * @param dirtyRegion the region that needs its folding annotations processed
 * @param subRegion ignored
 *
 * @see org.eclipse.wst.sse.ui.internal.reconcile.AbstractStructuredTextReconcilingStrategy#reconcile(org.eclipse.jface.text.reconciler.DirtyRegion, org.eclipse.jface.text.IRegion)
 */
public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
    IStructuredModel model = null;
    if (fProjectionAnnotationModel != null) {
        try {
            model = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument());
            if (model != null) {
                // use the structured doc to get all of the regions effected by the given dirty region
                IStructuredDocument structDoc = model.getStructuredDocument();
                IStructuredDocumentRegion[] structRegions = structDoc.getStructuredDocumentRegions(dirtyRegion.getOffset(), dirtyRegion.getLength());
                Set indexedRegions = getIndexedRegions(model, structRegions);
                // these are what are passed off to the annotation model to
                // actually create and maintain the annotations
                List modifications = new ArrayList();
                List deletions = new ArrayList();
                Map additions = new HashMap();
                boolean isInsert = dirtyRegion.getType().equals(DirtyRegion.INSERT);
                boolean isRemove = dirtyRegion.getType().equals(DirtyRegion.REMOVE);
                // find and mark all folding annotations with length 0 for deletion
                markInvalidAnnotationsForDeletion(dirtyRegion, deletions);
                // reconcile each effected indexed region
                Iterator indexedRegionsIter = indexedRegions.iterator();
                while (indexedRegionsIter.hasNext() && fProjectionAnnotationModel != null) {
                    IndexedRegion indexedRegion = (IndexedRegion) indexedRegionsIter.next();
                    // only try to create an annotation if the index region is a valid type
                    if (indexedRegionValidType(indexedRegion)) {
                        FoldingAnnotation annotation = new FoldingAnnotation(indexedRegion, false);
                        // else if REMOVE add annotation to the deletion list
                        if (isInsert) {
                            Annotation existingAnno = getExistingAnnotation(indexedRegion);
                            // else modify an old one, which could include deletion
                            if (existingAnno == null) {
                                Position newPos = calcNewFoldPosition(indexedRegion);
                                if (newPos != null && newPos.length > 0) {
                                    additions.put(annotation, newPos);
                                }
                            } else {
                                updateAnnotations(existingAnno, indexedRegion, additions, modifications, deletions);
                            }
                        } else if (isRemove) {
                            deletions.add(annotation);
                        }
                    }
                }
                // be sure projection has not been disabled
                if (fProjectionAnnotationModel != null) {
                    // send the calculated updates to the annotations to the annotation model
                    fProjectionAnnotationModel.modifyAnnotations((Annotation[]) deletions.toArray(new Annotation[1]), additions, (Annotation[]) modifications.toArray(new Annotation[0]));
                }
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) Iterator(java.util.Iterator) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) List(java.util.List) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) HashMap(java.util.HashMap) Map(java.util.Map)

Example 83 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class StructuredRegionProcessor method getOuterRegion.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor#getOuterRegion(org.eclipse.jface.text.reconciler.DirtyRegion,
	 *      org.eclipse.jface.text.reconciler.DirtyRegion)
	 */
protected DirtyRegion getOuterRegion(DirtyRegion root, DirtyRegion possible) {
    // first try simple region check if one region contains the other
    DirtyRegion outer = super.getOuterRegion(root, possible);
    if (outer == null && fCurrentDoc != null) {
        IStructuredModel sModel = null;
        try {
            sModel = getStructuredModelForRead(fCurrentDoc);
            if (sModel != null) {
                // now compare nodes
                IndexedRegion rootRegion = sModel.getIndexedRegion(root.getOffset());
                IndexedRegion possRegion = sModel.getIndexedRegion(possible.getOffset());
                if (rootRegion != null && possRegion != null) {
                    int rootStart = rootRegion.getStartOffset();
                    int possStart = possRegion.getStartOffset();
                    // possregion
                    if (rootStart <= possStart) {
                        // check if possregion is inside rootregion
                        outer = _getOuterRegion(root, possible, sModel, rootStart, possStart);
                    } else {
                        // otherwise if rootregion is inside possregion
                        outer = _getOuterRegion(possible, root, sModel, possStart, rootStart);
                    }
                }
            }
        } finally {
            if (sModel != null) {
                sModel.releaseFromRead();
            }
        }
    }
    return outer;
}
Also used : DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 84 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class StructuredRegionProcessor method process.

protected void process(DirtyRegion dirtyRegion) {
    if (!isInstalled() || isInRewriteSession() || dirtyRegion == null || getDocument() == null)
        return;
    // unhook old lifecycle listener
    if (fCurrentDoc != null) {
        IStructuredModel sModel = null;
        try {
            sModel = getStructuredModelForRead(fCurrentDoc);
            // use structured model to determine area to process
            if (sModel != null) {
                int start = dirtyRegion.getOffset();
                int end = start + dirtyRegion.getLength();
                IndexedRegion irStart = sModel.getIndexedRegion(start);
                IndexedRegion irEnd = sModel.getIndexedRegion(end);
                if (irStart != null) {
                    start = Math.min(start, irStart.getStartOffset());
                }
                if (irEnd != null) {
                    end = Math.max(end, irEnd.getEndOffset());
                }
                super.process(createDirtyRegion(start, end - start, DirtyRegion.INSERT));
            } else {
                super.process(dirtyRegion);
            }
        } finally {
            if (sModel != null) {
                sModel.releaseFromRead();
            }
        }
    } else {
        super.process(dirtyRegion);
    }
}
Also used : IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 85 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class DelegatingSourceValidator method computeStartAndEndLocation.

/**
 * Calculates the "better" offsets.
 *
 * @param startOffset -
 *            the offset given by Xerces
 * @param errorMessage -
 *            the Xerces error Message
 * @param selectionStrategy -
 *            the selectionStrategy
 * @param document -
 *            the document
 * @return int[] - position 0 has the start offset of the squiggle range,
 *         position 1 has the endOffset
 */
/*
	 * The way the offsets is calculated is: - find the indexed region
	 * (element) closest to the given offset - if we are between two elements,
	 * choosing left or right element will depend on parameter 'errorSide' -
	 * based on the selectionStrategy choose the underlining strategy (eg
	 * START_TAG means underline the start tag of that element) - use
	 * information from nameOrValue and the JSON to get better offsets
	 * 
	 */
protected int[] computeStartAndEndLocation(int startOffset, String selectionStrategy, String errorSide, String nameOrValue, IJSONDocument document) {
    try {
        int[] startEndPositions = new int[2];
        IndexedRegion region = document.getModel().getIndexedRegion(startOffset);
        IndexedRegion prevRegion = document.getModel().getIndexedRegion(startOffset - 1);
        if (prevRegion != region) {
            // exactly where we need to be.
            if (ERROR_SIDE_LEFT.equals(errorSide)) {
                region = prevRegion;
            }
        }
        // }
        return startEndPositions;
    } finally // catch (Exception e) { // e.printStackTrace();
    // }
    {
    }
// return null;
}
Also used : IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7