Search in sources :

Example 51 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class GotoAnnotationAction method getNextAnnotation.

/**
 * Returns the annotation closest to the given range respecting the given
 * direction. If an annotation is found, the annotations current position
 * is copied into the provided annotation position.
 *
 * @param offset
 *            the region offset
 * @param length
 *            the region length
 * @param forward
 *            <code>true</code> for forwards, <code>false</code> for
 *            backward
 * @param annotationPosition
 *            the position of the found annotation
 * @return the found annotation
 */
protected Annotation getNextAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) {
    Annotation nextAnnotation = null;
    Position nextAnnotationPosition = null;
    Annotation containingAnnotation = null;
    Position containingAnnotationPosition = null;
    boolean currentAnnotation = false;
    IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
    int endOfDocument = document.getLength();
    int distance = Integer.MAX_VALUE;
    IAnnotationModel model = getTextEditor().getDocumentProvider().getAnnotationModel(getTextEditor().getEditorInput());
    // external files may not have an annotation model
    if (model != null) {
        Iterator e = model.getAnnotationIterator();
        while (e.hasNext()) {
            Annotation a = (Annotation) e.next();
            if (!isNavigationTarget(a))
                continue;
            Position p = model.getPosition(a);
            if (p == null)
                continue;
            if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) {
                if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) {
                    containingAnnotation = a;
                    containingAnnotationPosition = p;
                    currentAnnotation = p.length == length;
                }
            } else {
                int currentDistance = 0;
                if (forward) {
                    currentDistance = p.getOffset() - offset;
                    if (currentDistance < 0) {
                        currentDistance = endOfDocument + currentDistance;
                    }
                    if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
                        distance = currentDistance;
                        nextAnnotation = a;
                        nextAnnotationPosition = p;
                    }
                } else {
                    currentDistance = offset + length - (p.getOffset() + p.length);
                    if (currentDistance < 0)
                        currentDistance = endOfDocument + currentDistance;
                    if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
                        distance = currentDistance;
                        nextAnnotation = a;
                        nextAnnotationPosition = p;
                    }
                }
            }
        }
    }
    if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) {
        annotationPosition.setOffset(containingAnnotationPosition.getOffset());
        annotationPosition.setLength(containingAnnotationPosition.getLength());
        return containingAnnotation;
    }
    if (nextAnnotationPosition != null) {
        annotationPosition.setOffset(nextAnnotationPosition.getOffset());
        annotationPosition.setLength(nextAnnotationPosition.getLength());
    }
    return nextAnnotation;
}
Also used : Position(org.eclipse.jface.text.Position) Iterator(java.util.Iterator) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) SimpleMarkerAnnotation(org.eclipse.ui.texteditor.SimpleMarkerAnnotation) IDocument(org.eclipse.jface.text.IDocument)

Example 52 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class SourceValidationQuickAssistProcessor method computeQuickAssistProposals.

public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
    ISourceViewer viewer = quickAssistContext.getSourceViewer();
    int documentOffset = quickAssistContext.getOffset();
    int length = viewer != null ? viewer.getSelectedRange().y : 0;
    IAnnotationModel model = viewer.getAnnotationModel();
    if (model == null)
        return null;
    List allProposals = new ArrayList();
    if (model instanceof IAnnotationModelExtension2) {
        Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
        while (iter.hasNext()) {
            List processors = new ArrayList();
            Annotation anno = (Annotation) iter.next();
            if (canFix(anno)) {
                // fix processor attached to it
                if (anno instanceof TemporaryAnnotation) {
                    Object o = ((TemporaryAnnotation) anno).getAdditionalFixInfo();
                    if (o instanceof IQuickAssistProcessor) {
                        processors.add(o);
                    }
                }
                // get all relevant quick fixes for this annotation
                QuickFixRegistry registry = QuickFixRegistry.getInstance();
                processors.addAll(Arrays.asList(registry.getQuickFixProcessors(anno)));
                // set up context
                Map attributes = null;
                if (anno instanceof TemporaryAnnotation) {
                    attributes = ((TemporaryAnnotation) anno).getAttributes();
                }
                Position pos = model.getPosition(anno);
                StructuredTextInvocationContext sseContext = new StructuredTextInvocationContext(viewer, pos.getOffset(), pos.getLength(), attributes);
                // call each processor
                for (int i = 0; i < processors.size(); ++i) {
                    List proposals = new ArrayList();
                    collectProposals((IQuickAssistProcessor) processors.get(i), anno, sseContext, proposals);
                    if (proposals.size() > 0) {
                        allProposals.addAll(proposals);
                    }
                }
            }
        }
    }
    if (allProposals.isEmpty())
        return null;
    return (ICompletionProposal[]) allProposals.toArray(new ICompletionProposal[allProposals.size()]);
}
Also used : Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IQuickAssistProcessor(org.eclipse.jface.text.quickassist.IQuickAssistProcessor) IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) Map(java.util.Map) StructuredTextInvocationContext(org.eclipse.wst.sse.ui.StructuredTextInvocationContext)

Example 53 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class HTMLAttributeValidationQuickFixProcessor method computeQuickAssistProposals.

/*
	 * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#computeQuickAssistProposals(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)
	 */
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    ISourceViewer viewer = invocationContext.getSourceViewer();
    int documentOffset = invocationContext.getOffset();
    int length = viewer != null ? viewer.getSelectedRange().y : 0;
    IAnnotationModel model = viewer.getAnnotationModel();
    if (model == null)
        return null;
    List proposals = new ArrayList();
    if (model instanceof IAnnotationModelExtension2) {
        Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
        while (iter.hasNext()) {
            Annotation anno = (Annotation) iter.next();
            if (canFix(anno)) {
                int offset = -1;
                if (anno instanceof TemporaryAnnotation) {
                    offset = ((TemporaryAnnotation) anno).getPosition().getOffset();
                } else if (anno instanceof MarkerAnnotation) {
                    offset = ((MarkerAnnotation) anno).getMarker().getAttribute(IMarker.CHAR_START, -1);
                }
                if (offset == -1)
                    continue;
                IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
                if (!(node instanceof Element))
                    continue;
                Object adapter = (node instanceof IAdaptable ? ((IAdaptable) node).getAdapter(IResource.class) : null);
                IProject project = (adapter instanceof IResource ? ((IResource) adapter).getProject() : null);
                IScopeContext[] fLookupOrder = new IScopeContext[] { new InstanceScope(), new DefaultScope() };
                if (project != null) {
                    ProjectScope projectScope = new ProjectScope(project);
                    if (projectScope.getNode(getPreferenceNodeQualifier()).getBoolean(getProjectSettingsKey(), false))
                        fLookupOrder = new IScopeContext[] { projectScope, new InstanceScope(), new DefaultScope() };
                }
                boolean ignore = fPreferenceService.getBoolean(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES, HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES_DEFAULT, fLookupOrder);
                String ignoreList = fPreferenceService.getString(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE, HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE_DEFAULT, fLookupOrder);
                Set result = new HashSet();
                if (ignoreList.trim().length() > 0) {
                    // $NON-NLS-1$
                    String[] names = ignoreList.split(",");
                    for (int i = 0; names != null && i < names.length; i++) {
                        String name = names[i] == null ? null : names[i].trim();
                        if (name != null && name.length() > 0)
                            result.add(name.toLowerCase());
                    }
                }
                String name = getAttributeName(node, offset);
                if (name == null)
                    continue;
                // If ignore == false. then show a quick fix anyway (due to allow to turn 'ignore' option on)
                if (!ignore || shouldShowQuickFix(result, name.toLowerCase())) {
                    IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(name.toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAttribute, name), HTMLUIMessages.DoNotValidateAttributeAddInfo, node);
                    if (!proposals.contains(p))
                        proposals.add(p);
                }
                int dashIndex = name.indexOf('-');
                while (dashIndex != -1) {
                    StringBuffer namePattern = new StringBuffer(name.substring(0, dashIndex + 1)).append('*');
                    // a more common pattern is already created
                    if (ignore && result.contains(namePattern.toString().toLowerCase()))
                        break;
                    IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(namePattern.toString().toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAllAttributes, namePattern.toString()), HTMLUIMessages.DoNotValidateAllAttributesAddInfo, node);
                    if (!proposals.contains(p))
                        proposals.add(p);
                    dashIndex = name.indexOf('-', dashIndex + 1);
                }
            }
        }
    }
    if (proposals.isEmpty())
        return null;
    return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ProjectScope(org.eclipse.core.resources.ProjectScope) IAdaptable(org.eclipse.core.runtime.IAdaptable) HashSet(java.util.HashSet) Set(java.util.Set) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IScopeContext(org.eclipse.core.runtime.preferences.IScopeContext) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) DefaultScope(org.eclipse.core.runtime.preferences.DefaultScope) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IProject(org.eclipse.core.resources.IProject) IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IResource(org.eclipse.core.resources.IResource)

Example 54 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class TestStructuredTextEditor method testInitialReconciling.

/**
 * Test receiving the initial reconcile notification when the editor opens
 */
public void testInitialReconciling() throws Exception {
    IFile file = getOrCreateFile(PROJECT_NAME + "/" + "reconcilingtest.xml");
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    final int[] state = new int[2];
    Arrays.fill(state, -1);
    ISourceReconcilingListener listener = new ISourceReconcilingListener() {

        int mod = 0;

        public void reconciled(IDocument document, IAnnotationModel model, boolean forced, IProgressMonitor progressMonitor) {
            state[1] = mod++;
        }

        public void aboutToBeReconciled() {
            state[0] = mod++;
        }
    };
    IEditorPart editor = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
    try {
        assertTrue("Not a StructuredTextEditor", editor instanceof StructuredTextEditor);
        addReconcilingListener((StructuredTextEditor) editor, listener);
        waitForReconcile(state);
        assertTrue("Initial: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
        assertTrue("Initial: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 0);
        assertTrue("Initial: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 1);
    } finally {
        if (editor != null && activePage != null) {
            activePage.closeEditor(editor, false);
        }
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ISourceReconcilingListener(org.eclipse.wst.sse.ui.reconcile.ISourceReconcilingListener) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) IDocument(org.eclipse.jface.text.IDocument)

Example 55 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class TestStructuredTextEditor method testFocusedReconciling.

/**
 * Test that an editor notifies reconciling listeners when the editor gets focus.
 */
public void testFocusedReconciling() throws Exception {
    IFile file = getOrCreateFile(PROJECT_NAME + "/" + "focustest.xml");
    IFile fileAlt = getOrCreateFile(PROJECT_NAME + "/" + "focustestAlt.xml");
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    final int[] state = new int[2];
    Arrays.fill(state, -1);
    ISourceReconcilingListener listener = new ISourceReconcilingListener() {

        int mod = 0;

        public void reconciled(IDocument document, IAnnotationModel model, boolean forced, IProgressMonitor progressMonitor) {
            state[1] = mod++;
        }

        public void aboutToBeReconciled() {
            state[0] = mod++;
        }
    };
    IEditorPart editor = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
    try {
        assertTrue("Not a StructuredTextEditor", editor instanceof StructuredTextEditor);
        addReconcilingListener((StructuredTextEditor) editor, listener);
        waitForReconcile(state);
        assertTrue("Initial: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
        assertTrue("Initial: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 0);
        assertTrue("Initial: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 1);
        IDE.openEditor(activePage, fileAlt, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
        Arrays.fill(state, -1);
        IEditorPart editorPart = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
        assertEquals("Didn't get the original editor back.", editor, editorPart);
        waitForReconcile(state);
        assertTrue("Modified: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
        assertTrue("Modified: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 2);
        assertTrue("Modified: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 3);
    } finally {
        if (editor != null && activePage != null) {
            activePage.closeEditor(editor, false);
        }
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ISourceReconcilingListener(org.eclipse.wst.sse.ui.reconcile.ISourceReconcilingListener) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10