Search in sources :

Example 66 with BadLocationException

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

the class AnnotationModel method modifyAnnotationPosition.

/**
 * Modifies the associated position of the given annotation to the given
 * position. If the annotation is not yet managed by this annotation model,
 * the annotation is added. When the position is <code>null</code>, the
 * annotation is removed from the model.
 * <p>
 * If requested, all annotation model change listeners will be informed
 * about the change.
 *
 * @param annotation the annotation whose associated position should be
 *            modified
 * @param position the position to whose values the associated position
 *            should be changed
 * @param fireModelChanged indicates whether to notify all model listeners
 * @since 3.0
 */
protected void modifyAnnotationPosition(Annotation annotation, Position position, boolean fireModelChanged) {
    if (position == null) {
        removeAnnotation(annotation, fireModelChanged);
    } else {
        Position p = fAnnotations.get(annotation);
        if (p != null) {
            if (position.getOffset() != p.getOffset() || position.getLength() != p.getLength()) {
                fDocument.removePosition(p);
                p.setOffset(position.getOffset());
                p.setLength(position.getLength());
                try {
                    fDocument.addPosition(p);
                } catch (BadLocationException e) {
                // ignore invalid position
                }
            }
            synchronized (getLockObject()) {
                getAnnotationModelEvent().annotationChanged(annotation);
            }
            if (fireModelChanged)
                fireModelChanged();
        } else {
            try {
                addAnnotation(annotation, position, fireModelChanged);
            } catch (BadLocationException x) {
            // ignore invalid position
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 67 with BadLocationException

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

the class GotoLineTest method goToLine.

private void goToLine(int line, int expectedResult) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
    try {
        IEditorPart part = IDE.openEditor(page, fFile);
        if (part instanceof ITextEditor) {
            ITextEditor editor = (ITextEditor) part;
            IAction action = editor.getAction(ITextEditorActionConstants.GOTO_LINE);
            Accessor accessor = new Accessor(action, GotoLineAction.class);
            accessor.invoke("gotoLine", new Class[] { int.class }, new Integer[] { Integer.valueOf(line) });
            Control control = part.getAdapter(Control.class);
            if (control instanceof StyledText) {
                int caretLine = -1;
                StyledText styledText = (StyledText) control;
                int caret = styledText.getCaretOffset();
                try {
                    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
                    caretLine = document.getLineOfOffset(caret);
                } catch (BadLocationException e1) {
                    fail();
                }
                assertEquals(expectedResult, caretLine);
            } else
                fail();
        } else
            fail();
    } catch (PartInitException e) {
        fail();
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) Control(org.eclipse.swt.widgets.Control) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) Accessor(org.eclipse.text.tests.Accessor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 68 with BadLocationException

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

the class EditorAccessHighlighter method addHighlights.

@Override
public void addHighlights(Match[] matches) {
    Map<IAnnotationModel, HashMap<Annotation, Position>> mapsByAnnotationModel = new HashMap<>();
    for (Match match : matches) {
        int offset = match.getOffset();
        int length = match.getLength();
        if (offset >= 0 && length >= 0) {
            try {
                Position position = createPosition(match);
                if (position != null) {
                    Map<Annotation, Position> map = getMap(mapsByAnnotationModel, match);
                    if (map != null) {
                        Annotation annotation = match.isFiltered() ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null);
                        fMatchesToAnnotations.put(match, annotation);
                        map.put(annotation, position);
                    }
                }
            } catch (BadLocationException e) {
                SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.EditorAccessHighlighter_error_badLocation, e));
            }
        }
    }
    for (Entry<IAnnotationModel, HashMap<Annotation, Position>> entry : mapsByAnnotationModel.entrySet()) {
        addAnnotations(entry.getKey(), entry.getValue());
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 69 with BadLocationException

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

the class PositionTracker method dirtyStateChanged.

@Override
public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
    if (isDirty)
        return;
    final int[] trackCount = new int[1];
    doForExistingMatchesIn(buffer, new IFileBufferMatchOperation() {

        @Override
        public void run(ITextFileBuffer textBuffer, Match match) {
            trackCount[0]++;
            Position pos = fMatchesToPositions.get(match);
            if (pos != null) {
                if (pos.isDeleted()) {
                    AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
                    // might be that the containing element has been removed.
                    if (result != null) {
                        result.removeMatch(match);
                    }
                    untrackPosition(textBuffer, match);
                } else {
                    if (match.getBaseUnit() == Match.UNIT_LINE) {
                        try {
                            pos = convertToLinePosition(pos, textBuffer.getDocument());
                        } catch (BadLocationException e) {
                            SearchPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, e.getLocalizedMessage(), e));
                        }
                    }
                    match.setOffset(pos.getOffset());
                    match.setLength(pos.getLength());
                }
            }
        }
    });
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 70 with BadLocationException

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

the class PositionTracker method trackPosition.

private void trackPosition(AbstractTextSearchResult result, ITextFileBuffer fb, Match match) {
    int offset = match.getOffset();
    int length = match.getLength();
    if (offset < 0 || length < 0)
        return;
    try {
        IDocument doc = fb.getDocument();
        Position position = new Position(offset, length);
        if (match.getBaseUnit() == Match.UNIT_LINE) {
            position = convertToCharacterPosition(position, doc);
        }
        doc.addPosition(position);
        fMatchesToSearchResults.put(match, result);
        fMatchesToPositions.put(match, position);
        addFileBufferMapping(fb, match);
    } catch (BadLocationException e) {
        // the match is outside the document
        result.removeMatch(match);
    }
}
Also used : Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)455 IDocument (org.eclipse.jface.text.IDocument)196 IRegion (org.eclipse.jface.text.IRegion)161 Test (org.junit.Test)102 Position (org.eclipse.jface.text.Position)101 Region (org.eclipse.jface.text.Region)68 Point (org.eclipse.swt.graphics.Point)61 Document (org.eclipse.jface.text.Document)47 CoreException (org.eclipse.core.runtime.CoreException)34 ArrayList (java.util.ArrayList)27 ITypedRegion (org.eclipse.jface.text.ITypedRegion)27 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)22 DocumentEvent (org.eclipse.jface.text.DocumentEvent)21 ITextSelection (org.eclipse.jface.text.ITextSelection)21 StyledText (org.eclipse.swt.custom.StyledText)18 StyledString (org.eclipse.jface.viewers.StyledString)17 IStatus (org.eclipse.core.runtime.IStatus)16 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)15 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)15 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15