Search in sources :

Example 41 with BadPositionCategoryException

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

the class RuleBasedPartitioner method documentChanged2.

@Override
public IRegion documentChanged2(DocumentEvent e) {
    try {
        IDocument d = e.getDocument();
        Position[] category = d.getPositions(fPositionCategory);
        int first = 0;
        int reparseStart = 0;
        int originalSize = category.length;
        if (originalSize > 0) {
            /*
				 * determine character position at which the scanner starts:
				 * first position behind the last non-default partition the actual position is not involved with
				 */
            first = d.computeIndexInCategory(fPositionCategory, e.getOffset());
            Position p = null;
            do {
                --first;
                if (first < 0)
                    break;
                p = category[first];
            } while (p.overlapsWith(e.getOffset(), e.getLength()) || (e.getOffset() == fPreviousDocumentLength && (p.getOffset() + p.getLength() == fPreviousDocumentLength)));
            fPositionUpdater.update(e);
            for (Position element : category) {
                p = element;
                if (p.isDeleted) {
                    rememberDeletedOffset(e.getOffset());
                    break;
                }
            }
            category = d.getPositions(fPositionCategory);
            if (first >= 0) {
                p = category[first];
                reparseStart = p.getOffset() + p.getLength();
            }
            ++first;
        }
        fScanner.setRange(d, reparseStart, d.getLength() - reparseStart);
        int lastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            String contentType = getTokenContentType(token);
            if (!isSupportedContentType(contentType)) {
                token = fScanner.nextToken();
                continue;
            }
            int start = fScanner.getTokenOffset();
            int length = fScanner.getTokenLength();
            lastScannedPosition = start + length - 1;
            // remove all affected positions
            while (first < category.length) {
                TypedPosition p = (TypedPosition) category[first];
                if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
                    rememberRegion(p.offset, p.length);
                    d.removePosition(fPositionCategory, p);
                    ++first;
                } else
                    break;
            }
            // if position already exists we are done
            if (d.containsPosition(fPositionCategory, start, length))
                return createRegion();
            // insert the new type position
            try {
                d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
                rememberRegion(start, length);
            } catch (BadPositionCategoryException x) {
            } catch (BadLocationException x) {
            }
            token = fScanner.nextToken();
        }
        // remove all positions behind lastScannedPosition since there aren't any further types
        if (lastScannedPosition != reparseStart) {
            // if this condition is not met, nothing has been scanned because of a delete
            ++lastScannedPosition;
        }
        first = d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
        TypedPosition p;
        while (first < category.length) {
            p = (TypedPosition) category[first++];
            d.removePosition(fPositionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException x) {
    // should never happen on connected documents
    } catch (BadLocationException x) {
    }
    return createRegion();
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 42 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project linuxtools by eclipse.

the class SpecfileChangelogFormatter method mergeChangelog.

@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
    if (changelog instanceof SpecfileEditor) {
        SpecfileEditor specEditor = (SpecfileEditor) changelog;
        IDocument doc = specEditor.getDocumentProvider().getDocument(specEditor.getEditorInput());
        String[] positionCategories = doc.getPositionCategories();
        String contentTypesPositionCategory = null;
        // we need to find the one we want
        for (String positionCategory : positionCategories) {
            if (positionCategory.startsWith("__content_types_category")) {
                // $NON-NLS-1$
                contentTypesPositionCategory = positionCategory;
            }
        }
        if (contentTypesPositionCategory != null) {
            try {
                Position[] sectionPositions = doc.getPositions(contentTypesPositionCategory);
                ITypedRegion changelogPartition = null;
                for (Position position : sectionPositions) {
                    int offset = position.getOffset();
                    ITypedRegion partition = doc.getPartition(offset);
                    if (partition.getType().equals(SpecfilePartitionScanner.SPEC_CHANGELOG)) {
                        changelogPartition = partition;
                    }
                }
                // Temporary buffer for changelog text
                StringBuilder buf = new StringBuilder();
                String changelogText = EMPTY_STRING;
                String[] changelogLines = new String[] {};
                int offset = doc.getLength();
                int length = 0;
                // there was no changelog partition add it.
                if (changelogPartition == null) {
                    // make sure there are at least 2 newlines before
                    // the changelog section
                    String endString = doc.get(doc.getLength() - 2, 2);
                    if (endString.charAt(0) != '\n') {
                        buf.append('\n');
                    }
                    if (endString.charAt(1) != '\n') {
                        buf.append('\n');
                    }
                    // $NON-NLS-1$
                    buf.append("%changelog\n");
                // or get the old text and add the header
                } else {
                    offset = changelogPartition.getOffset();
                    length = changelogPartition.getLength();
                    changelogText = doc.get(offset, length);
                    // get old changelog text
                    // $NON-NLS-1$
                    changelogLines = changelogText.split("\n");
                    // add the %changelog header
                    buf.append(changelogLines[0]).append('\n');
                }
                // now add the entry stub
                buf.append(dateLine);
                buf.append('\n');
                // $NON-NLS-1$
                buf.append("- \n");
                // set the cursor at the end of the entry,
                // count back 2 '\n's
                int newCursorOffset = offset + buf.length() - 1;
                for (int i = 1; i < changelogLines.length; i++) {
                    buf.append('\n').append(changelogLines[i]);
                }
                // always terminate the file with a new line
                if (changelogLines.length > 0) {
                    buf.append('\n');
                }
                doc.replace(offset, length, buf.toString());
                specEditor.selectAndReveal(newCursorOffset, 0);
                specEditor.setFocus();
            } catch (BadPositionCategoryException | BadLocationException e) {
                SpecfileLog.logError(e);
            }
        }
    }
    return EMPTY_STRING;
}
Also used : Position(org.eclipse.jface.text.Position) SpecfileEditor(org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileEditor) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)42 Position (org.eclipse.jface.text.Position)30 BadLocationException (org.eclipse.jface.text.BadLocationException)22 TypedPosition (org.eclipse.jface.text.TypedPosition)18 IDocument (org.eclipse.jface.text.IDocument)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)10 TypedRegion (org.eclipse.jface.text.TypedRegion)8 ArrayList (java.util.ArrayList)7 IRegion (org.eclipse.jface.text.IRegion)7 Point (org.eclipse.swt.graphics.Point)4 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1