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;
}
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();
}
}
}
}
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;
}
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);
}
}
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;
}
Aggregations