Search in sources :

Example 16 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class QuickDiffRestoreAction method getModel.

/**
 * Returns the annotation model of the document displayed in this action's editor, if it
 * implements the {@link IAnnotationModelExtension IAnnotationModelExtension} interface.
 *
 * @return the displayed document's annotation model if it is an <code>IAnnotationModelExtension</code>, or <code>null</code>
 */
private IAnnotationModelExtension getModel() {
    if (getTextEditor() == null)
        return null;
    IDocumentProvider provider = getTextEditor().getDocumentProvider();
    IEditorInput editorInput = getTextEditor().getEditorInput();
    IAnnotationModel m = provider.getAnnotationModel(editorInput);
    if (m instanceof IAnnotationModelExtension)
        return (IAnnotationModelExtension) m;
    return null;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput)

Example 17 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class ReferenceSelectionAction method getDiffer.

/**
 * Fetches the differ installed with the current editor's document's annotation model. If none
 * is installed yet, and <code>createIfNeeded</code> is true, one is created and attached to the
 * model.
 *
 * @param createIfNeeded when set to <code>true</code>, a new differ will be created if needed.
 * @return the differ installed with the annotation model, or <code>null</code>.
 */
private DocumentLineDiffer getDiffer(boolean createIfNeeded) {
    // get annotation model
    if (fEditor == null)
        return null;
    IDocumentProvider provider = fEditor.getDocumentProvider();
    IEditorInput editorInput = fEditor.getEditorInput();
    if (provider == null || editorInput == null)
        return null;
    IAnnotationModel m = provider.getAnnotationModel(editorInput);
    IAnnotationModelExtension model = null;
    if (m instanceof IAnnotationModelExtension) {
        model = (IAnnotationModelExtension) m;
    } else {
        return null;
    }
    // get differ
    DocumentLineDiffer differ = (DocumentLineDiffer) model.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
    // create if needed
    if (differ == null && createIfNeeded) {
        differ = new DocumentLineDiffer();
        model.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, differ);
    }
    return differ;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput)

Example 18 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class HighlightStrategy method applyHighlights.

private void applyHighlights(int offset) {
    if (sourceViewer == null || !enabled) {
        return;
    }
    String text = document.get();
    offset = ((ITextViewerExtension5) sourceViewer).widgetOffset2ModelOffset(offset);
    int wordStartOffset = Math.max(text.lastIndexOf('/', offset), text.lastIndexOf('<', offset)) + 1;
    int wordEndOffset = findEndingOffset(text, offset);
    if (wordEndOffset <= wordStartOffset || wordEndOffset == -1 || wordStartOffset == -1)
        return;
    String word = text.substring(wordStartOffset, wordEndOffset);
    if (word.indexOf('>') != -1 || word.indexOf('<') != -1) {
        removeOccurrenceAnnotations();
        return;
    }
    Matcher m = TAG_NAME_PATTERN.matcher(text);
    Map<Annotation, Position> annotationMap = new HashMap<>();
    while (m.find()) {
        if (m.group(1).equals(word)) {
            annotationMap.put(new Annotation(ANNOTATION_TYPE, false, null), new Position(m.start(1), m.end(1) - m.start(1)));
        }
    }
    if (annotationMap.size() < 2) {
        removeOccurrenceAnnotations();
        return;
    }
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    synchronized (getLockObject(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
        } else {
            removeOccurrenceAnnotations();
            Iterator<Entry<Annotation, Position>> iter = annotationMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<Annotation, Position> mapEntry = iter.next();
                annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
            }
        }
        fOccurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
    }
}
Also used : Entry(java.util.Map.Entry) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 19 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class SpellCheckDocumentListener method documentChanged.

@Override
public void documentChanged(final DocumentEvent event) {
    if (this.lastJob != null) {
        this.lastJob.cancel();
    }
    this.lastJob = new Job("Spellcheck") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            ITextFileBuffer iTextFileBuffer = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument());
            if (iTextFileBuffer == null) {
                return Status.CANCEL_STATUS;
            }
            IAnnotationModel model = iTextFileBuffer.getAnnotationModel();
            String text = event.getDocument().get();
            int commentStart = text.indexOf("<comment>");
            if (commentStart < 0) {
                return Status.OK_STATUS;
            }
            commentStart += "<comment>".length();
            int commentEnd = text.indexOf("</comment>", commentStart);
            if (commentEnd <= commentStart) {
                return Status.OK_STATUS;
            }
            Region region = new Region(commentStart, commentEnd - commentStart);
            service.check(event.getDocument(), new Region[] { region }, new SpellingContext(), new ISpellingProblemCollector() {

                private Map<SpellingAnnotation, Position> annotations = new HashMap<>();

                @Override
                public void endCollecting() {
                    Set<SpellingAnnotation> previous = new HashSet<>();
                    model.getAnnotationIterator().forEachRemaining(annotation -> {
                        if (annotation instanceof SpellingAnnotation) {
                            previous.add((SpellingAnnotation) annotation);
                        }
                    });
                    if (model instanceof IAnnotationModelExtension) {
                        ((IAnnotationModelExtension) model).replaceAnnotations(previous.toArray(new SpellingAnnotation[previous.size()]), annotations);
                    } else {
                        previous.forEach(model::removeAnnotation);
                        annotations.forEach(model::addAnnotation);
                    }
                }

                @Override
                public void beginCollecting() {
                }

                @Override
                public void accept(SpellingProblem problem) {
                    this.annotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
                }
            }, monitor);
            return Status.OK_STATUS;
        }
    };
    this.lastJob.setUser(false);
    this.lastJob.setPriority(Job.DECORATE);
    // set a delay before reacting to user action to handle continuous typing
    this.lastJob.schedule(500);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Position(org.eclipse.jface.text.Position) ISpellingProblemCollector(org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) SpellingContext(org.eclipse.ui.texteditor.spelling.SpellingContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Region(org.eclipse.jface.text.Region) Job(org.eclipse.core.runtime.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 20 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class LineNumberColumn method ensureQuickDiffProvider.

/**
 * Ensures that quick diff information is displayed and the quick diff provider is the one with
 * the specified id. If a different quick diff provider is in use, the user may be asked whether
 * he wants to switch.
 *
 * @param diffProviderId the quick diff provider id to use
 * @return <code>true</code> if quick diff could be enabled for the given id,
 *         <code>false</code> otherwise
 */
private boolean ensureQuickDiffProvider(String diffProviderId) {
    if (!isShowingChangeInformation())
        // FIXME pass provider id
        installChangeRulerModel(fDelegate);
    IAnnotationModel annotationModel = fViewer.getAnnotationModel();
    IAnnotationModel oldDiffer = getDiffer();
    if (oldDiffer == null && annotationModel != null)
        // quick diff is enabled, but no differ? not working for whatever reason
        return false;
    if (annotationModel == null)
        annotationModel = new AnnotationModel();
    if (!(annotationModel instanceof IAnnotationModelExtension))
        return false;
    QuickDiff util = new QuickDiff();
    Object oldDifferId = util.getConfiguredQuickDiffProvider(oldDiffer);
    if (oldDifferId.equals(diffProviderId)) {
        if (oldDiffer instanceof ILineDifferExtension)
            ((ILineDifferExtension) oldDiffer).resume();
        return true;
    }
    // Check whether the desired provider is available at all
    IAnnotationModel newDiffer = util.createQuickDiffAnnotationModel(getEditor(), diffProviderId);
    if (util.getConfiguredQuickDiffProvider(newDiffer).equals(oldDifferId)) {
        if (oldDiffer instanceof ILineDifferExtension)
            ((ILineDifferExtension) oldDiffer).resume();
        return true;
    }
    // quick diff is showing with the wrong provider - ask the user whether he wants to switch
    IPreferenceStore store = EditorsUI.getPreferenceStore();
    if (oldDiffer != null && !store.getString(REVISION_ASK_BEFORE_QUICKDIFF_SWITCH_KEY).equals(MessageDialogWithToggle.ALWAYS)) {
        MessageDialogWithToggle toggleDialog = MessageDialogWithToggle.openOkCancelConfirm(fViewer.getTextWidget().getShell(), RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_title, RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_message, RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_rememberquestion, true, store, REVISION_ASK_BEFORE_QUICKDIFF_SWITCH_KEY);
        if (toggleDialog.getReturnCode() != Window.OK)
            return false;
    }
    IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) annotationModel;
    modelExtension.removeAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
    modelExtension.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, newDiffer);
    if (fDelegate instanceof IChangeRulerColumn)
        // picks up the new model attachment
        ((IChangeRulerColumn) fDelegate).setModel(annotationModel);
    return true;
}
Also used : QuickDiff(org.eclipse.ui.texteditor.quickdiff.QuickDiff) IChangeRulerColumn(org.eclipse.jface.text.source.IChangeRulerColumn) ILineDifferExtension(org.eclipse.jface.text.source.ILineDifferExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AnnotationModel(org.eclipse.jface.text.source.AnnotationModel) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Aggregations

IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)30 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)20 Annotation (org.eclipse.jface.text.source.Annotation)14 Position (org.eclipse.jface.text.Position)12 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)6 HashMap (java.util.HashMap)5 IDocument (org.eclipse.jface.text.IDocument)5 Entry (java.util.Map.Entry)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)4 IEditorInput (org.eclipse.ui.IEditorInput)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 IRegion (org.eclipse.jface.text.IRegion)2 ITextViewer (org.eclipse.jface.text.ITextViewer)2 AnnotationModel (org.eclipse.jface.text.source.AnnotationModel)2 IChangeRulerColumn (org.eclipse.jface.text.source.IChangeRulerColumn)2 ILineDifferExtension (org.eclipse.jface.text.source.ILineDifferExtension)2