Search in sources :

Example 51 with StructuredDocumentEvent

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

the class StructuredDocumentReParser method _checkForCriticalWord.

/**
 * Common utility for checking for critical word such as " <SCRIPT>"
 */
private StructuredDocumentEvent _checkForCriticalWord(String criticalTarget, boolean checkEnd) {
    StructuredDocumentEvent result = null;
    int documentLength = fStructuredDocument.getLength();
    int propLen = fLengthToReplace;
    if (propLen > documentLength)
        propLen = documentLength;
    int startNeighborhood = fStart - criticalTarget.length();
    int adjustInsert = 0;
    if (startNeighborhood < 0) {
        adjustInsert = 0 - startNeighborhood;
        startNeighborhood = 0;
    }
    int endNeighborhood = fStart + fLengthToReplace + criticalTarget.length() - 1;
    if (endNeighborhood > documentLength)
        endNeighborhood = documentLength - 1;
    // + 1;
    int oldlen = endNeighborhood - startNeighborhood;
    if (oldlen + startNeighborhood > documentLength) {
        oldlen = documentLength - startNeighborhood;
    }
    String oldText = fStructuredDocument.get(startNeighborhood, oldlen);
    String peek = StringUtils.paste(oldText, fChanges, criticalTarget.length() - adjustInsert, fLengthToReplace);
    boolean isCriticalString = checkTagNames(oldText, criticalTarget, checkEnd);
    boolean toBeCriticalString = checkTagNames(peek, criticalTarget, checkEnd);
    if (// OR if both are
    (isCriticalString != toBeCriticalString) || // tag ('>')
    ((isCriticalString && toBeCriticalString) && (changeInIsEndedState(oldText, peek)))) {
        // if it involves a change of a critical string (making one where
        // there wasn't, or removing
        // one where there was one) then reparse everthing.
        result = reparse(0, documentLength - 1);
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)

Example 52 with StructuredDocumentEvent

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

the class StructuredDocumentReParser method regionCheck.

/**
 * If only one node is involved, sees how many regions are changed. If
 * only one, then its a 'regionChanged' event ... if more than one, its a
 * 'regionsReplaced' event.
 */
protected StructuredDocumentEvent regionCheck(IStructuredDocumentRegion oldNode, IStructuredDocumentRegion newNode) {
    // 
    StructuredDocumentEvent result = null;
    ITextRegionList oldRegions = oldNode.getRegions();
    ITextRegionList newRegions = newNode.getRegions();
    ITextRegion[] oldRegionsArray = oldRegions.toArray();
    ITextRegion[] newRegionsArray = newRegions.toArray();
    // 
    // for the 'regionsReplaced' event, we don't care if
    // the regions changed due to type, or text,
    // we'll just collect all those that are not equal
    // into the old and new region lists.
    // Note: we, of course, assume that old and new regions
    // are basically contiguous -- and we force it to be so,
    // even if not literally so, by starting at beginning to
    // find first difference, and then starting at end to find
    // last difference. Everything in between we assume is different.
    // 
    // going up is easy, we start at zero in each, and continue
    // till regions are not the same.
    int startOfDifferences = _computeStartOfDifferences(oldNode, oldRegions, newNode, newRegions);
    int endOfDifferencesOld = -1;
    int endOfDifferencesNew = -1;
    // then some portion of the lists are identical
    if ((startOfDifferences >= oldRegions.size()) || (startOfDifferences >= newRegions.size())) {
        if (oldRegions.size() < newRegions.size()) {
            // INSERT CASE
            // then there are new regions to add
            // these lengths will cause the vector of old ones to not
            // have any elements, and the vector of new regions to have
            // just the new ones.
            startOfDifferences = oldRegionsArray.length;
            endOfDifferencesOld = oldRegionsArray.length - 1;
            endOfDifferencesNew = newRegionsArray.length - 1;
        } else {
            if (oldRegions.size() > newRegions.size()) {
                // DELETE CASE
                // then there are old regions to delete
                // these lengths will cause the vector of old regions to
                // contain the ones to delete, and the vector of new
                // regions
                // not have any elements
                startOfDifferences = newRegionsArray.length;
                endOfDifferencesOld = oldRegionsArray.length - 1;
                endOfDifferencesNew = newRegionsArray.length - 1;
            } else {
                // else the lists are identical!
                // unlikely event, probably error in current design, since
                // we check for identity at the very beginning of
                // reparsing.
                result = new NoChangeEvent(fStructuredDocument, fRequester, fChanges, fStart, fLengthToReplace);
            }
        }
    } else {
        if ((startOfDifferences > -1) && (endOfDifferencesOld < 0) && (endOfDifferencesNew < 0)) {
            // We found a normal startOfDiffernces, but have not yet found
            // the ends.
            // We'll look for the end of differences by going backwards
            // down the two lists.
            // Here we need a seperate index for each array, since they
            // may
            // be (and
            // probably are) of different lengths.
            int indexOld = oldRegionsArray.length - 1;
            int indexNew = newRegionsArray.length - 1;
            while ((indexOld >= startOfDifferences) && (_greaterThanEffectedRegion(oldNode, oldRegionsArray[indexOld]))) {
                if ((!(oldNode.sameAs(oldRegionsArray[indexOld], newNode, newRegionsArray[indexNew], fLengthDifference)))) {
                    // endOfDifferencesNew = indexTwo;
                    break;
                }
                indexOld--;
                indexNew--;
            }
            endOfDifferencesOld = indexOld;
            endOfDifferencesNew = indexNew;
        }
    }
    // result != null means the impossible case above occurred
    if (result == null) {
        // Now form the two vectors of different regions
        ITextRegionList holdOldRegions = new TextRegionListImpl();
        ITextRegionList holdNewRegions = new TextRegionListImpl();
        if (startOfDifferences > -1 && endOfDifferencesOld > -1) {
            for (int i = startOfDifferences; i <= endOfDifferencesOld; i++) {
                holdOldRegions.add(oldRegionsArray[i]);
            }
        }
        if (startOfDifferences > -1 && endOfDifferencesNew > -1) {
            for (int i = startOfDifferences; i <= endOfDifferencesNew; i++) {
                holdNewRegions.add(newRegionsArray[i]);
            }
        }
        if (holdOldRegions.size() == 0 && holdNewRegions.size() == 0) {
            // then this means the regions were identical, which means
            // someone
            // pasted exactly the same thing they had selected, or !!!
            // someone deleted the end bracket of the tag. !!!?
            result = new NoChangeEvent(fStructuredDocument, fRequester, fChanges, fStart, fLengthToReplace);
        } else {
            // old instance of old node
            if ((holdOldRegions.size() == 1) && (holdNewRegions.size() == 1) && _regionsSameKind((holdNewRegions.get(0)), (holdOldRegions.get(0)))) {
                ITextRegion newOldRegion = swapNewForOldRegion(oldNode, holdOldRegions.get(0), newNode, holdNewRegions.get(0));
                // -- need to update any down stream regions, within this
                // 'oldNode'
                updateDownStreamRegions(oldNode, newOldRegion);
                result = new RegionChangedEvent(fStructuredDocument, fRequester, oldNode, newOldRegion, fChanges, fStart, fLengthToReplace);
            } else {
                replaceRegions(oldNode, holdOldRegions, newNode, holdNewRegions);
                // -- need to update any down stream regions, within this
                // 'oldNode'
                // don't need with the way replaceRegions is implemented.
                // It handles.
                // if(holdNewRegions.size() > 0)
                // updateDownStreamRegions(oldNode, (ITextRegion)
                // holdNewRegions.lastElement());
                result = new RegionsReplacedEvent(fStructuredDocument, fRequester, oldNode, holdOldRegions, holdNewRegions, fChanges, fStart, fLengthToReplace);
            }
        }
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionsReplacedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionsReplacedEvent) StructuredDocumentRegionsReplacedEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentRegionsReplacedEvent) NoChangeEvent(org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)

Example 53 with StructuredDocumentEvent

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

the class StructuredDocumentReParser method checkForTransitionToOpen.

/**
 * Checks if the start region has become self-closing. e.g., &lt;style/&gt; -&gt; &lt;style&gt;
 */
private StructuredDocumentEvent checkForTransitionToOpen(String tagName) {
    StructuredDocumentEvent result = null;
    if (dirtyStart.getText().toLowerCase().indexOf(tagName.toLowerCase()) >= 0) {
        // within a start-tag
        final int documentLength = fStructuredDocument.getLength();
        int end = fStart + fLengthToReplace + fChanges.length() + 1;
        if (end > documentLength)
            end = documentLength - 1;
        final String oldText = fStructuredDocument.get(fStart, 2);
        final String peek = StringUtils.paste(oldText, fChanges, 0, fLengthToReplace);
        if ("/>".equals(oldText) && ">".equals(peek)) {
            // Reparse afterwards if the block tag went from self-closing to open
            result = reparse(dirtyStart.getStart(), documentLength - 1);
        }
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)

Example 54 with StructuredDocumentEvent

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

the class StructuredDocumentReParser method regionCheck.

/**
 * If only one node is involved, sees how many regions are changed. If
 * only one, then its a 'regionChanged' event ... if more than one, its a
 * 'regionsReplaced' event.
 */
protected StructuredDocumentEvent regionCheck(CoreNodeList oldNodes, CoreNodeList newNodes) {
    if (Debug.debugStructuredDocument)
        // $NON-NLS-1$
        System.out.println("IStructuredDocument::regionsReplacedCheck");
    // $NON-NLS-1$
    // $NON-NLS-1$
    // the "regionsReplaced" event could only be true if and only if the
    // nodelists
    // are each only "1" in length.
    StructuredDocumentEvent result = null;
    int oldLength = oldNodes.getLength();
    int newLength = newNodes.getLength();
    if ((oldLength != 1) || (newLength != 1)) {
        result = null;
    } else {
        IStructuredDocumentRegion oldNode = oldNodes.item(0);
        IStructuredDocumentRegion newNode = newNodes.item(0);
        result = regionCheck(oldNode, newNode);
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)

Example 55 with StructuredDocumentEvent

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

the class StructuredDocumentReParser method reparse.

/**
 * The core reparsing method ... after the dirty start and dirty end have
 * been calculated elsewhere.
 */
protected StructuredDocumentEvent reparse(IStructuredDocumentRegion dirtyStart, IStructuredDocumentRegion dirtyEnd) {
    StructuredDocumentEvent result = null;
    int rescanStart = -1;
    int rescanEnd = -1;
    boolean firstTime = false;
    // 
    // "save" the oldNodes (that may be replaced) in a list
    CoreNodeList oldNodes = formOldNodes(dirtyStart, dirtyEnd);
    if (dirtyStart == null || dirtyEnd == null) {
        // dirtyStart or dirty end are null, then that means we didn't
        // have
        // a
        // cached node, which means we have an empty document, so we
        // just need to rescan the changes
        rescanStart = 0;
        rescanEnd = fChanges.length();
        firstTime = true;
    } else {
        // set the start of the text to rescan
        rescanStart = dirtyStart.getStart();
        // 
        // set the end of the text to rescan
        // notice we use the same rationale as for the rescanStart,
        // with the added caveat that length has to be added to it,
        // to compensate for the new text which has been added or deleted.
        // If changes has zero length, then "length" will be negative,
        // since
        // we are deleting text. Otherwise, use the difference between
        // what's selected to be replaced and the length of the new text.
        rescanEnd = dirtyEnd.getEnd() + fLengthDifference;
    }
    // now that we have the old stuff "saved" away, update the document
    // with the changes.
    // FUTURE_TO_DO -- don't fire "document changed" event till later
    fStructuredDocument.updateDocumentData(fStart, fLengthToReplace, fChanges);
    // ------------------ now the real work
    result = core_reparse(rescanStart, rescanEnd, oldNodes, firstTime);
    // some opitmization they can do
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)

Aggregations

StructuredDocumentEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent)59 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)23 RegionChangedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)22 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 Document (org.w3c.dom.Document)20 Node (org.w3c.dom.Node)20 NullInputStream (org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream)18 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)18 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)18 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)8 NoChangeEvent (org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)6 IExecutionDelegate (org.eclipse.wst.sse.core.internal.IExecutionDelegate)3 StructuredDocumentRegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentRegionsReplacedEvent)3 RegionsReplacedEvent (org.eclipse.wst.sse.core.internal.provisional.events.RegionsReplacedEvent)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 List (java.util.List)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IRegion (org.eclipse.jface.text.IRegion)1