Search in sources :

Example 76 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.

the class DefaultErlangFoldingStructureProvider method createAnnotationMap.

private Map<Object, List<Tuple>> createAnnotationMap(final IAnnotationModel model) {
    final Map<Object, List<Tuple>> map = new HashMap<>();
    final Iterator<?> e = model.getAnnotationIterator();
    while (e.hasNext()) {
        final Object annotation = e.next();
        if (annotation instanceof ErlangProjectionAnnotation) {
            final ErlangProjectionAnnotation epa = (ErlangProjectionAnnotation) annotation;
            final Position position = model.getPosition(epa);
            final List<Tuple> list = map.computeIfAbsent(epa.getElement(), k -> new ArrayList<>(2));
            list.add(new Tuple(epa, position));
        }
    }
    final Comparator<Tuple> comparator = (o1, o2) -> o1.position.getOffset() - o2.position.getOffset();
    for (final List<Tuple> name : map.values()) {
        final List<Tuple> list = name;
        list.sort(comparator);
    }
    return map;
}
Also used : ErlElementKind(org.erlide.engine.model.ErlElementKind) Arrays(java.util.Arrays) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IElementChangedListener(org.erlide.engine.model.root.IElementChangedListener) CoreException(org.eclipse.core.runtime.CoreException) ErlModelException(org.erlide.engine.model.ErlModelException) Map(java.util.Map) Position(org.eclipse.jface.text.Position) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) IRegion(org.eclipse.jface.text.IRegion) ErlangEngine(org.erlide.engine.ErlangEngine) IEditorInput(org.eclipse.ui.IEditorInput) Collection(java.util.Collection) Assert(org.eclipse.core.runtime.Assert) Set(java.util.Set) ErlLogger(org.erlide.util.ErlLogger) List(java.util.List) IProjectionListener(org.eclipse.jface.text.source.projection.IProjectionListener) IErlElement(org.erlide.engine.model.IErlElement) IErlModel(org.erlide.engine.model.root.IErlModel) Entry(java.util.Map.Entry) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor) PerformanceTuning(org.erlide.ui.util.PerformanceTuning) PreferenceConstants(org.erlide.ui.prefs.PreferenceConstants) Region(org.eclipse.jface.text.Region) ErlModelUtils(org.erlide.ui.util.ErlModelUtils) ISourceRange(org.erlide.engine.model.erlang.ISourceRange) IErlElementDelta(org.erlide.engine.model.root.IErlElementDelta) IErlModule(org.erlide.engine.model.root.IErlModule) HashMap(java.util.HashMap) Annotation(org.eclipse.jface.text.source.Annotation) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ArrayList(java.util.ArrayList) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) IErlComment(org.erlide.engine.model.erlang.IErlComment) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IErlMember(org.erlide.engine.model.erlang.IErlMember) ErlideUIPlugin(org.erlide.ui.internal.ErlideUIPlugin) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) Iterator(java.util.Iterator) IParent(org.erlide.engine.model.IParent) ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel) IErlangFoldingStructureProvider(org.erlide.ui.editors.erl.folding.IErlangFoldingStructureProvider) DocumentCharacterIterator(org.erlide.ui.internal.DocumentCharacterIterator) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) IErlangFoldingStructureProviderExtension(org.erlide.ui.editors.erl.folding.IErlangFoldingStructureProviderExtension) Comparator(java.util.Comparator) SystemConfiguration(org.erlide.util.SystemConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Position(org.eclipse.jface.text.Position) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) List(java.util.List) ArrayList(java.util.ArrayList)

Example 77 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-eclipse by eclipse.

the class IssueDataTest method testIssueData.

@Test
public void testIssueData() throws Exception {
    IFile dslFile = dslFile(getProjectName(), getFileName(), getFileExtension(), MODEL_WITH_LINKING_ERROR);
    XtextEditor xtextEditor = openEditor(dslFile);
    IXtextDocument document = xtextEditor.getDocument();
    IResource file = xtextEditor.getResource();
    List<Issue> issues = getAllValidationIssues(document);
    assertEquals(1, issues.size());
    Issue issue = issues.get(0);
    assertEquals(2, issue.getLineNumber().intValue());
    assertEquals(3, issue.getColumn().intValue());
    assertEquals(PREFIX.length(), issue.getOffset().intValue());
    assertEquals(QuickfixCrossrefTestLanguageValidator.TRIGGER_VALIDATION_ISSUE.length(), issue.getLength().intValue());
    String[] expectedIssueData = new String[] { QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_0, QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_1 };
    assertTrue(Arrays.equals(expectedIssueData, issue.getData()));
    Thread.sleep(1000);
    IAnnotationModel annotationModel = xtextEditor.getDocumentProvider().getAnnotationModel(xtextEditor.getEditorInput());
    AnnotationIssueProcessor annotationIssueProcessor = new AnnotationIssueProcessor(document, annotationModel, new IssueResolutionProvider.NullImpl());
    annotationIssueProcessor.processIssues(issues, new NullProgressMonitor());
    Iterator<?> annotationIterator = annotationModel.getAnnotationIterator();
    // filter QuickDiffAnnotations
    List<Object> allAnnotations = Lists.newArrayList(annotationIterator);
    List<XtextAnnotation> annotations = newArrayList(filter(allAnnotations, XtextAnnotation.class));
    assertEquals(annotations.toString(), 1, annotations.size());
    XtextAnnotation annotation = annotations.get(0);
    assertTrue(Arrays.equals(expectedIssueData, annotation.getIssueData()));
    IssueUtil issueUtil = new IssueUtil();
    Issue issueFromAnnotation = issueUtil.getIssueFromAnnotation(annotation);
    assertTrue(Arrays.equals(expectedIssueData, issueFromAnnotation.getData()));
    new MarkerCreator().createMarker(issue, file, MarkerTypes.FAST_VALIDATION);
    IMarker[] markers = file.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_ZERO);
    String errorMessage = new AnnotatedTextToString().withFile(dslFile).withMarkers(markers).toString().trim();
    assertEquals(errorMessage, 1, markers.length);
    String attribute = (String) markers[0].getAttribute(Issue.DATA_KEY);
    assertNotNull(attribute);
    assertTrue(Arrays.equals(expectedIssueData, Strings.unpack(attribute)));
    Issue issueFromMarker = issueUtil.createIssue(markers[0]);
    assertEquals(issue.getColumn(), issueFromMarker.getColumn());
    assertEquals(issue.getLineNumber(), issueFromMarker.getLineNumber());
    assertEquals(issue.getOffset(), issueFromMarker.getOffset());
    assertEquals(issue.getLength(), issueFromMarker.getLength());
    assertTrue(Arrays.equals(expectedIssueData, issueFromMarker.getData()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) Issue(org.eclipse.xtext.validation.Issue) AnnotatedTextToString(org.eclipse.xtext.ui.testing.util.AnnotatedTextToString) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) AnnotatedTextToString(org.eclipse.xtext.ui.testing.util.AnnotatedTextToString) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IssueUtil(org.eclipse.xtext.ui.util.IssueUtil) AnnotationIssueProcessor(org.eclipse.xtext.ui.editor.validation.AnnotationIssueProcessor) XtextAnnotation(org.eclipse.xtext.ui.editor.validation.XtextAnnotation) IssueResolutionProvider(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider) IMarker(org.eclipse.core.resources.IMarker) MarkerCreator(org.eclipse.xtext.ui.editor.validation.MarkerCreator) IResource(org.eclipse.core.resources.IResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AbstractQuickfixTest(org.eclipse.xtext.ui.testing.AbstractQuickfixTest) Test(org.junit.Test)

Example 78 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-eclipse by eclipse.

the class XtextEditorErrorTickUpdater method getSeverity.

protected Severity getSeverity(XtextEditor xtextEditor) {
    if (xtextEditor == null || xtextEditor.getInternalSourceViewer() == null)
        return null;
    IAnnotationModel model = xtextEditor.getInternalSourceViewer().getAnnotationModel();
    if (model != null) {
        Iterator<Annotation> iterator = model.getAnnotationIterator();
        boolean hasWarnings = false;
        boolean hasInfos = false;
        while (iterator.hasNext()) {
            Annotation annotation = iterator.next();
            if (!annotation.isMarkedDeleted()) {
                Issue issue = issueUtil.getIssueFromAnnotation(annotation);
                if (issue != null) {
                    if (issue.getSeverity() == Severity.ERROR) {
                        return Severity.ERROR;
                    } else if (issue.getSeverity() == Severity.WARNING) {
                        hasWarnings = true;
                    } else if (issue.getSeverity() == Severity.INFO) {
                        hasInfos = true;
                    }
                }
            }
        }
        if (hasWarnings)
            return Severity.WARNING;
        if (hasInfos)
            return Severity.INFO;
    }
    return null;
}
Also used : Issue(org.eclipse.xtext.validation.Issue) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 79 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-eclipse by eclipse.

the class MarkerResolutionGenerator method getResolutions.

@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
    final IMarkerResolution[] emptyResult = new IMarkerResolution[0];
    try {
        if (!marker.isSubtypeOf(MarkerTypes.ANY_VALIDATION))
            return emptyResult;
    } catch (CoreException e) {
        return emptyResult;
    }
    if (!languageResourceHelper.isLanguageResource(marker.getResource())) {
        return emptyResult;
    }
    XtextEditor editor = findEditor(marker.getResource());
    if (editor != null) {
        IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
        if (annotationModel != null && !isMarkerStillValid(marker, annotationModel))
            return emptyResult;
    }
    Issue issue = getIssueUtil().createIssue(marker);
    if (issue == null)
        return emptyResult;
    List<IssueResolution> resolutions = getResolutionProvider().getResolutions(issue);
    List<IMarkerResolution> result = Lists.newArrayList();
    List<IssueResolution> remaining = Lists.newArrayList();
    for (IssueResolution resolution : resolutions) {
        if (resolution.getModification() instanceof IBatchableModification) {
            result.add(adapterFactory.create(marker, resolution));
        } else if (resolution.getModification() instanceof ITextualMultiModification) {
            result.add(textualMultiModificationAdapterFactory.create(marker, resolution));
        } else {
            remaining.add(resolution);
        }
    }
    result.addAll(Lists.newArrayList(getAdaptedResolutions(remaining)));
    return result.toArray(new IMarkerResolution[result.size()]);
}
Also used : Issue(org.eclipse.xtext.validation.Issue) IMarkerResolution(org.eclipse.ui.IMarkerResolution) CoreException(org.eclipse.core.runtime.CoreException) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ITextualMultiModification(org.eclipse.xtext.ui.editor.model.edit.ITextualMultiModification) IBatchableModification(org.eclipse.xtext.ui.editor.model.edit.BatchModification.IBatchableModification)

Example 80 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-eclipse by eclipse.

the class XtextQuickAssistProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    ISourceViewer sourceViewer = invocationContext.getSourceViewer();
    if (sourceViewer == null)
        return new ICompletionProposal[0];
    if (invocationContext instanceof QuickAssistInvocationContext) {
        if (((QuickAssistInvocationContext) invocationContext).isCancelled()) {
            return new ICompletionProposal[0];
        }
    }
    final IDocument document = sourceViewer.getDocument();
    if (document == null)
        return new ICompletionProposal[0];
    final IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    List<ICompletionProposal> result = Lists.newArrayList();
    try {
        Set<Annotation> applicableAnnotations = getApplicableAnnotations(document, annotationModel, invocationContext.getOffset());
        result = createQuickfixes(invocationContext, applicableAnnotations);
        selectAndRevealQuickfix(invocationContext, applicableAnnotations, result);
    } catch (BadLocationException e) {
        errorMessage = e.getMessage();
    } catch (OperationCanceledException e) {
        return new ICompletionProposal[0];
    }
    sortQuickfixes(result);
    return result.toArray(new ICompletionProposal[result.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IQuickAssistInvocationContext(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) XtextAnnotation(org.eclipse.xtext.ui.editor.validation.XtextAnnotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

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