private ValidationJob newValidationJob(final XtextEditor editor) { final IXtextDocument document = editor.getDocument(); final IAnnotationModel annotationModel = editor.getInternalSourceViewer().getAnnotationModel(); final IssueResolutionProvider issueResolutionProvider = getService(editor, IssueResolutionProvider.class); final MarkerTypeProvider markerTypeProvider = getService(editor, MarkerTypeProvider.class); final MarkerCreator markerCreator = getService(editor, MarkerCreator.class); final IValidationIssueProcessor issueProcessor = new CompositeValidationIssueProcessor( new AnnotationIssueProcessor(document, annotationModel, issueResolutionProvider), new MarkerIssueProcessor(editor.getResource(), markerCreator, markerTypeProvider)); return editor.getDocument().modify(resource -> { final IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider(); final IResourceValidator resourceValidator = serviceProvider.getResourceValidator(); return new ValidationJob(resourceValidator, editor.getDocument(), issueProcessor, ALL); }); }
/*** * Returns true if it exists a marker annotation in the given offset and false * otherwise. * * @param textViewer * @param offset * @return true if it exists a marker annotation in the given offset and false * otherwise. */ private static boolean hasProblem(ITextViewer textViewer, int offset) { if (!(textViewer instanceof ISourceViewer)) { return false; } IAnnotationModel annotationModel = ((ISourceViewer) textViewer).getAnnotationModel(); Iterator<Annotation> iter = (annotationModel instanceof IAnnotationModelExtension2) ? ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(offset, 1, true, true) : annotationModel.getAnnotationIterator(); while (iter.hasNext()) { Annotation ann = iter.next(); if (ann instanceof MarkerAnnotation) { return true; } } return false; }
@SuppressWarnings("rawtypes") @Override public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { IAnnotationModel model = sourceViewer.getAnnotationModel(); Iterator iterator = model.getAnnotationIterator(); while (iterator.hasNext()) { Annotation annotation = (Annotation) iterator.next(); Position position = model.getPosition(annotation); try { int lineOfAnnotation = sourceViewer.getDocument(). getLineOfOffset(position.getOffset()); if (lineNumber == lineOfAnnotation) { return annotation.getText(); } } catch (BadLocationException e) { // TODO: handle exception } } return null; }
private List<de.darwinspl.preferences.resource.dwprofile.IDwprofileQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<de.darwinspl.preferences.resource.dwprofile.IDwprofileQuickFix> foundFixes = new ArrayList<de.darwinspl.preferences.resource.dwprofile.IDwprofileQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<de.darwinspl.preferences.resource.dwprofile.IDwprofileQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.feature.expression.resource.hyexpression.IHyexpressionQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.feature.expression.resource.hyexpression.IHyexpressionQuickFix> foundFixes = new ArrayList<eu.hyvar.feature.expression.resource.hyexpression.IHyexpressionQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.feature.expression.resource.hyexpression.IHyexpressionQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.context.contextValidity.resource.hyvalidityformula.IHyvalidityformulaQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.context.contextValidity.resource.hyvalidityformula.IHyvalidityformulaQuickFix> foundFixes = new ArrayList<eu.hyvar.context.contextValidity.resource.hyvalidityformula.IHyvalidityformulaQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.context.contextValidity.resource.hyvalidityformula.IHyvalidityformulaQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.dataValues.resource.hydatavalue.IHydatavalueQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.dataValues.resource.hydatavalue.IHydatavalueQuickFix> foundFixes = new ArrayList<eu.hyvar.dataValues.resource.hydatavalue.IHydatavalueQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.dataValues.resource.hydatavalue.IHydatavalueQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.feature.mapping.resource.hymapping.IHymappingQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.feature.mapping.resource.hymapping.IHymappingQuickFix> foundFixes = new ArrayList<eu.hyvar.feature.mapping.resource.hymapping.IHymappingQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.feature.mapping.resource.hymapping.IHymappingQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.feature.constraint.resource.hyconstraints.IHyconstraintsQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.feature.constraint.resource.hyconstraints.IHyconstraintsQuickFix> foundFixes = new ArrayList<eu.hyvar.feature.constraint.resource.hyconstraints.IHyconstraintsQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.feature.constraint.resource.hyconstraints.IHyconstraintsQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
private List<eu.hyvar.mspl.manifest.resource.hymanifest.IHymanifestQuickFix> getQuickFixes(ISourceViewer sourceViewer, int offset, int length) { List<eu.hyvar.mspl.manifest.resource.hymanifest.IHymanifestQuickFix> foundFixes = new ArrayList<eu.hyvar.mspl.manifest.resource.hymanifest.IHymanifestQuickFix>(); IAnnotationModel model = annotationModelProvider.getAnnotationModel(); if (model == null) { return foundFixes; } Iterator<?> iter = model.getAnnotationIterator(); while (iter.hasNext()) { Annotation annotation = (Annotation) iter.next(); Position position = model.getPosition(annotation); if (offset >= 0) { if (!position.overlapsWith(offset, length)) { continue; } } Collection<eu.hyvar.mspl.manifest.resource.hymanifest.IHymanifestQuickFix> quickFixes = getQuickFixes(annotation); if (quickFixes != null) { foundFixes.addAll(quickFixes); } } return foundFixes; }
/** * Attaches a coverage annotation model for the given editor if the editor can * be annotated. Does nothing if the model is already attached. * * @param editor * Editor to attach a annotation model to */ public static void attach(ITextEditor editor) { IDocumentProvider provider = editor.getDocumentProvider(); // there may be text editors without document providers (SF #1725100) if (provider == null) return; IAnnotationModel model = provider.getAnnotationModel(editor .getEditorInput()); if (!(model instanceof IAnnotationModelExtension)) return; IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; IDocument document = provider.getDocument(editor.getEditorInput()); CoverageAnnotationModel coveragemodel = (CoverageAnnotationModel) modelex .getAnnotationModel(KEY); if (coveragemodel == null) { coveragemodel = new CoverageAnnotationModel(editor, document); modelex.addAnnotationModel(KEY, coveragemodel); } }
private void ensureAnnotationModelInstalled() { LinkedPositionAnnotations lpa = fCurrentTarget.fAnnotationModel; if (lpa != null) { ITextViewer viewer = fCurrentTarget.getViewer(); if (viewer instanceof ISourceViewer) { ISourceViewer sv = (ISourceViewer) viewer; IAnnotationModel model = sv.getAnnotationModel(); if (model instanceof IAnnotationModelExtension) { IAnnotationModelExtension ext = (IAnnotationModelExtension) model; IAnnotationModel ourModel = ext.getAnnotationModel(getUniqueKey()); if (ourModel == null) { ext.addAnnotationModel(getUniqueKey(), lpa); } } } } }
/** * Returns the actual position of <i>marker</i> or null if the marker was * deleted. Code inspired by * @param marker * @param sourceViewer * @return */ private static int[] getMarkerPosition(IMarker marker, ISourceViewer sourceViewer) { int[] p = new int[2]; p[0] = marker.getAttribute(IMarker.CHAR_START, -1); p[1] = marker.getAttribute(IMarker.CHAR_END, -1); // look up the current range of the marker when the document has been edited IAnnotationModel model= sourceViewer.getAnnotationModel(); if (model instanceof AbstractMarkerAnnotationModel) { AbstractMarkerAnnotationModel markerModel= (AbstractMarkerAnnotationModel) model; Position pos= markerModel.getMarkerPosition(marker); if (pos != null && !pos.isDeleted()) { // use position instead of marker values p[0] = pos.getOffset(); p[1] = pos.getOffset() + pos.getLength(); } if (pos != null && pos.isDeleted()) { // do nothing if position has been deleted return null; } } return p; }
/** * Creates and returns a background job which searches and highlights all \label and \*ref. * @param document * @param model * @param refName The name of the reference * @return The job */ private Job createMatchReferenceJob(final IDocument document, final IAnnotationModel model, final String refName) { return new Job("Update Annotations") { public IStatus run(IProgressMonitor monitor) { String text = document.get(); String refNameRegExp = refName.replaceAll("\\*", "\\\\*"); final String simpleRefRegExp = "\\\\([a-zA-Z]*ref|label)\\s*\\{" + refNameRegExp + "\\}"; Matcher m = (Pattern.compile(simpleRefRegExp)).matcher(text); while (m.find()) { if (monitor.isCanceled()) return Status.CANCEL_STATUS; IRegion match = LatexParserUtils.getCommand(text, m.start()); //Test if it is a real LaTeX command if (match != null) { IRegion fi = new Region(m.start(), m.end()-m.start()); createNewAnnotation(fi, "References", model); } } return Status.OK_STATUS; } }; }
private void initializeSourceViewer(final IEditorInput input) { final IDocumentProvider documentProvider = getDocumentProvider(); final IAnnotationModel model = documentProvider.getAnnotationModel(input); final IDocument document = documentProvider.getDocument(input); if (document != null) { fSourceViewer.setDocument(document, model); fSourceViewer.setEditable(isEditable()); fSourceViewer.showAnnotations(model != null); } if (fElementStateListener instanceof IElementStateListenerExtension) { boolean isStateValidated = false; if (documentProvider instanceof IDocumentProviderExtension) isStateValidated = ((IDocumentProviderExtension) documentProvider).isStateValidated(input); final IElementStateListenerExtension extension = (IElementStateListenerExtension) fElementStateListener; extension.elementStateValidationChanged(input, isStateValidated); } }
private void addProposals(final SubMenuManager quickFixMenu) { IAnnotationModel sourceModel = sourceViewer.getAnnotationModel(); Iterator annotationIterator = sourceModel.getAnnotationIterator(); while (annotationIterator.hasNext()) { Annotation annotation = (Annotation) annotationIterator.next(); boolean isDeleted = annotation.isMarkedDeleted(); boolean isIncluded = includes(sourceModel.getPosition(annotation), getTextWidget().getCaretOffset()); boolean isFixable = sourceViewer.getQuickAssistAssistant().canFix( annotation); if (!isDeleted && isIncluded && isFixable) { IQuickAssistProcessor processor = sourceViewer .getQuickAssistAssistant() .getQuickAssistProcessor(); IQuickAssistInvocationContext context = sourceViewer .getQuickAssistInvocationContext(); ICompletionProposal[] proposals = processor .computeQuickAssistProposals(context); for (ICompletionProposal proposal : proposals) quickFixMenu.add(createQuickFixAction(proposal)); } } }
private void initializeSourceViewer(IEditorInput input) { IDocumentProvider documentProvider = getDocumentProvider(); IAnnotationModel model = documentProvider.getAnnotationModel(input); IDocument document = documentProvider.getDocument(input); if (document != null) { fSourceViewer.setDocument(document, model); fSourceViewer.setEditable(isEditable()); fSourceViewer.showAnnotations(model != null); } if (fElementStateListener instanceof IElementStateListenerExtension) { boolean isStateValidated = false; if (documentProvider instanceof IDocumentProviderExtension) isStateValidated = ((IDocumentProviderExtension) documentProvider) .isStateValidated(input); IElementStateListenerExtension extension = (IElementStateListenerExtension) fElementStateListener; extension.elementStateValidationChanged(input, isStateValidated); } }
/** * Gets the current {@link Position} of the marker in the * {@link TLAEditor} showing the module that contains the * marker. Returns null if there is no editor open on that module. * This method assumes that {@link TLAEditor}s are synchronized. That * is, multiple editors on the same module are synchronized. At the time * of writing this method (June 2010), they are synchronized. * * @param marker * @return */ public static Position getMarkerPosition(IMarker marker) { TLAEditor editor = findTLAEditor(marker.getResource()); if (editor != null) { IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput()); /* * From exploration of eclipse's code, I've determined that this * should be an instance of ResourceMarkerAnnotationModel. If this is * not always true, then we need to figure out a way to get a hold of the * annotation model that manages positions of markers in the editor. */ if (annotationModel instanceof ResourceMarkerAnnotationModel) { return ((ResourceMarkerAnnotationModel) annotationModel).getMarkerPosition(marker); } else { Activator.getDefault().logDebug("Cannot get the annotation model that manages marker positions for the marker on " + marker.getResource()); } } return null; }
/** * Returns the annotation overlapping with the given range or <code>null</code>. * * @param offset the region offset * @param length the region length * @return the found annotation or <code>null</code> */ private Annotation getAnnotation(int offset, int length) { IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput()); if (model == null) { return null; } Iterator<Annotation> e = new AnnotationIterator(model, true, false); while (e.hasNext()) { Annotation a = e.next(); Position p = model.getPosition(a); if (p != null && p.overlapsWith(offset, length)) { return a; } } return null; }
private void handleCompilerExceptions(MplAnnotationType type, ImmutableListMultimap<File, CompilerException> exceptions) { for (Entry<File, Collection<CompilerException>> entry : exceptions.asMap().entrySet()) { File file = entry.getKey(); TextEditor editor = editors.get(file.toPath()); if (editor != null) { IAnnotationModel annotationModel = editor.getSourceViewer().getAnnotationModel(); for (CompilerException ex : entry.getValue()) { MplAnnotation annotation = new MplAnnotation(type, ex.getLocalizedMessage()); MplSource source = ex.getSource(); Position position = new Position(source.getStartIndex(), source.getLength()); annotationModel.addAnnotation(annotation, position); } } } }
private static SourceViewerConfiguration createConfiguration(Input<?> input, IDocument document, EditingContext editingContext, AppMemento appMemento) { ThreadSynchronize threadSynchronize = null; MplPresentationReconciler reconciler = new MplPresentationReconciler(); ProposalComputer proposalComputer = new MplProposalComputer(document, editingContext); IAnnotationModel annotationModel = new AnnotationModel(); AnnotationPresenter annotationPresenter = new MplAnnotationPresenter(); HoverInformationProvider hoverInformationProvider = new MplHoverInformationProvider(); CompletionProposalPresenter proposalPresenter = MplGraphicalCompletionProposal::new; SearchProvider searchProvider = null; NavigationProvider navigationProvider = null; EditorOpener editorOpener = null; BehaviorContributor behaviorContributor = null; IContextInformationValidator contextInformationValidator = null; DefaultSourceViewerConfiguration result = new DefaultSourceViewerConfiguration( threadSynchronize, input, reconciler, appMemento, proposalComputer, annotationModel, annotationPresenter, hoverInformationProvider, proposalPresenter, searchProvider, navigationProvider, editorOpener, behaviorContributor, contextInformationValidator); result.getFeatures().add(Feature.SHOW_LINE_NUMBERS); return result; }
private List<AnnotationWithPosition> findAnnotations() { List<AnnotationWithPosition> annos = new ArrayList<AnnotationWithPosition>(); try { IAnnotationModel model = infoControl.getEditor().getViewer().getAnnotationModel(); for (Iterator iterator = model.getAnnotationIterator(); iterator.hasNext();) { Annotation anno = (Annotation) iterator.next(); if (containsAnnotationType(anno)) { Position position = model.getPosition(anno); AnnotationWithPosition newAnno = new AnnotationWithPosition(anno, position); if (!containsAnnotation(annos, newAnno)) { annos.add(newAnno); } } } } catch (Exception e) { QuickAnnotationInformationControl.logError(e); } return annos; }
private void removeOccurrenceAnnotations() { // fMarkOccurrenceModificationStamp= // IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; // fMarkOccurrenceTargetRegion= null; IDocumentProvider documentProvider = getDocumentProvider(); if (documentProvider == null) return; IAnnotationModel annotationModel = documentProvider.getAnnotationModel(getEditorInput()); if (annotationModel == null || fOccurrenceAnnotations == null) return; synchronized (getLockObject(annotationModel)) { if (annotationModel instanceof IAnnotationModelExtension) { ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, null); } else { for (int i = 0, length = fOccurrenceAnnotations.length; i < length; i++) annotationModel.removeAnnotation(fOccurrenceAnnotations[i]); } fOccurrenceAnnotations = null; } }
@Override public void setDocument(IDocument document, IAnnotationModel annotationModel, int modelRangeOffset, int modelRangeLength) { // partial fix for: // https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=1970 // when our document is set, especially to null during close, // immediately uninstall the reconciler. // this is to avoid an unnecessary final "reconcile" // that blocks display thread if (document == null) { if (fReconciler != null) { fReconciler.uninstall(); } } super.setDocument(document, annotationModel, modelRangeOffset, modelRangeLength); }
@Override public String loadTranslation(BTSText text, String language, IAnnotationModel annotationModel) { if (text.getTextContent() == null || text.getTextContent().getTextItems().isEmpty()) return ""; StringBuilder stringBuilder = new StringBuilder(); for (BTSTextItems tItem : text.getTextContent().getTextItems()) { if (tItem instanceof BTSSenctence) { BTSSenctence sentence = (BTSSenctence) tItem; BTSModelAnnotation ma = new BTSSentenceAnnotation(BTSSentenceAnnotation.TYPE,sentence); int start = stringBuilder.length(); stringBuilder.append(createSentenceTranslationLabel(sentence, language)); int len = stringBuilder.length() - start; Position pos = new Position(start, len); if (annotationModel != null) annotationModel.addAnnotation(ma, pos); } } return stringBuilder.toString(); }
private Position appendAmbivalenceToStringBuilder( BTSAmbivalence ambivalence, StringBuilder stringBuilder, IAnnotationModel model, Map<String, List<BTSInterTextReference>> relatingObjectsMap, Map<String, List<Object>> lemmaAnnotationMap) { Position pos = new Position(stringBuilder.length()); stringBuilder.append(AMBIVALENCE_START_SIGN); if (ambivalence.getCases() != null) { for (BTSLemmaCase amCase : ambivalence.getCases()) { appendLemmaCase(amCase, ambivalence, stringBuilder, model, relatingObjectsMap, lemmaAnnotationMap); stringBuilder.append(LEMMA_CASE_SEPARATOR); } } stringBuilder.replace( stringBuilder.length() - LEMMA_CASE_SEPARATOR.length(), stringBuilder.length(), AMBIVALENCE_END_SIGN); pos.setLength(stringBuilder.length() - pos.getOffset()); return pos; }
private void appendAmbivalenceItem(BTSIdentifiableItem item, BTSLemmaCase amCase, BTSAmbivalence ambivalence, StringBuilder stringBuilder, IAnnotationModel model, Map<String, List<BTSInterTextReference>> relatingObjectsMap, Map<String, List<Object>> lemmaAnnotationMap) { Position pos = null; if (item instanceof BTSWord) { BTSWord word = (BTSWord) item; pos = appendWordToStringBuilder(word, stringBuilder); appendWordToModel(word, model, pos, lemmaAnnotationMap); } else if (item instanceof BTSMarker) { BTSMarker marker = (BTSMarker) item; pos = appendMarkerToStringBuilder(marker, stringBuilder); appendMarkerToModel(marker, model, pos); } if (item != null && relatingObjectsMap != null && pos != null && relatingObjectsMap.containsKey(item.get_id())) { createAnnotations(item, model, pos, relatingObjectsMap.get(item.get_id())); } }
private void appendWordToModel(BTSWord word, IAnnotationModel model, Position position, Map<String, List<Object>> lemmaAnnotationMap) { if (model == null) return; BTSModelAnnotation annotation; if (word.getLKey() != null && !"".equals(word.getLKey())) { annotation = new BTSLemmaAnnotation(BTSLemmaAnnotation.TYPE, word); if (lemmaAnnotationMap != null) { add2LemmaAnnotationMap(word.getLKey(), annotation, lemmaAnnotationMap); } } else { annotation = new BTSModelAnnotation(BTSModelAnnotation.TOKEN, (BTSIdentifiableItem) word); } model.addAnnotation(annotation, position); }
@SuppressWarnings("rawtypes") private Annotation getAnnotation(final int offset, final int length) { final IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput()); if (model == null) return null; Iterator iterator; if (model instanceof IAnnotationModelExtension2) { iterator = ((IAnnotationModelExtension2) model).getAnnotationIterator(offset, length, true, true); } else { iterator = model.getAnnotationIterator(); } while (iterator.hasNext()) { final Annotation a = (Annotation) iterator.next(); final Position p = model.getPosition(a); if (p != null && p.overlapsWith(offset, length)) return a; } return null; }
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) { ISourceViewer sourceViewer = invocationContext.getSourceViewer(); if (sourceViewer == null) return new ICompletionProposal[0]; final IDocument document = sourceViewer.getDocument(); if (!(document instanceof IXtextDocument)) return new ICompletionProposal[0]; final IXtextDocument xtextDocument = (IXtextDocument) document; final IAnnotationModel annotationModel = sourceViewer.getAnnotationModel(); List<ICompletionProposal> result = Lists.newArrayList(); try { Set<Annotation> applicableAnnotations = getApplicableAnnotations(xtextDocument, annotationModel, invocationContext.getOffset()); result = createQuickfixes(invocationContext, applicableAnnotations); selectAndRevealQuickfix(invocationContext, applicableAnnotations, result); } catch (BadLocationException e) { errorMessage = e.getMessage(); } sortQuickfixes(result); return result.toArray(new ICompletionProposal[result.size()]); }
/** * @since 2.3 */ protected List<ICompletionProposal> createQuickfixes(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations) { List<ICompletionProposal> result = Lists.newArrayList(); ISourceViewer sourceViewer = invocationContext.getSourceViewer(); IAnnotationModel annotationModel = sourceViewer.getAnnotationModel(); IXtextDocument xtextDocument = XtextDocumentUtil.get(sourceViewer); for(Annotation annotation : applicableAnnotations) { if (annotation instanceof SpellingAnnotation) { SpellingProblem spellingProblem = ((SpellingAnnotation) annotation).getSpellingProblem(); result.addAll(asList(spellingProblem.getProposals())); } else { final Issue issue = issueUtil.getIssueFromAnnotation(annotation); if (issue != null) { Iterable<IssueResolution> resolutions = getResolutions(issue, xtextDocument); if (resolutions.iterator().hasNext()) { Position pos = annotationModel.getPosition(annotation); for (IssueResolution resolution : resolutions) { result.add(create(pos, resolution)); } } } } } return result; }
/** * @since 2.3 */ protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) { if (completionProposals.isEmpty()) { return; } if (invocationContext instanceof QuickAssistInvocationContext && !((QuickAssistInvocationContext) invocationContext).isSuppressSelection()) { ISourceViewer sourceViewer = invocationContext.getSourceViewer(); IAnnotationModel annotationModel = sourceViewer.getAnnotationModel(); Iterator<Annotation> iterator = applicableAnnotations.iterator(); while(iterator.hasNext()){ Position pos = annotationModel.getPosition(iterator.next()); if (pos != null) { sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength()); sourceViewer.revealRange(pos.getOffset(), pos.getLength()); break; } } } }
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 = getEditor(marker.getResource()); if(editor == null) return emptyResult; IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput()); if(annotationModel != null && !isMarkerStillValid(marker, annotationModel)) return emptyResult; final Iterable<IssueResolution> resolutions = getResolutions(getIssueUtil().createIssue(marker), editor.getDocument()); return getAdaptedResolutions(Lists.newArrayList(resolutions)); }
/** * Load annotations2 editor. * * @param editorModel the editor model * @param model the model * @param monitor */ @SuppressWarnings("rawtypes") private void loadAnnotations2Editor(IAnnotationModel editorModel, IAnnotationModel model, IProgressMonitor monitor) { Iterator i = model.getAnnotationIterator(); Issue issue; issue = new Issue.IssueImpl(); if (monitor != null) { if (monitor.isCanceled()) return; monitor.beginTask("Load visible annotations to Text-Editor", IProgressMonitor.UNKNOWN); } while (i.hasNext()) { Object a = i.next(); Position pos = model.getPosition((Annotation) a); loadSingleAnnotation2Editor(editorModel, (BTSModelAnnotation)a, pos, issue); if (monitor != null) { monitor.worked(1); } } }
protected void updateModelFromTranscription() { if (selectedLemmaEntry != null) { IAnnotationModel am = embeddedEditor.getViewer() .getAnnotationModel(); IXtextDocument document = embeddedEditor.getDocument(); EList<EObject> objects = document .readOnly(new IUnitOfWork<EList<EObject>, XtextResource>() { @Override public EList<EObject> exec(XtextResource state) throws Exception { return state.getContents(); } }); EObject eo = objects.get(0); if (eo instanceof TextContent) { textContent = lemmaEditorController.updateModelFromTextContent(textContent, eo, am); } } }
private List<GWTJavaProblem> getGWTProblemsInEditor(CompilationUnitEditor editor) throws Exception { List<GWTJavaProblem> problems = new ArrayList<GWTJavaProblem>(); Field annotationProblemField = CompilationUnitDocumentProvider.ProblemAnnotation.class.getDeclaredField("fProblem"); annotationProblemField.setAccessible(true); IEditorInput editorInput = editor.getEditorInput(); IDocumentProvider documentProvider = editor.getDocumentProvider(); IAnnotationModel annotationModel = documentProvider.getAnnotationModel(editorInput); Iterator<?> iter = annotationModel.getAnnotationIterator(); while (iter.hasNext()) { Object annotation = iter.next(); if (annotation instanceof CompilationUnitDocumentProvider.ProblemAnnotation) { CompilationUnitDocumentProvider.ProblemAnnotation problemAnnotation = (ProblemAnnotation) annotation; if (problemAnnotation.getMarkerType().equals(GWTJavaProblem.MARKER_ID)) { GWTJavaProblem problem = (GWTJavaProblem) annotationProblemField.get(problemAnnotation); problems.add(problem); } } } return problems; }
/** * Returns one marker which includes the ruler's line of activity. */ protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) { IMarker marker = null; IAnnotationModel model = aViewer.getAnnotationModel(); if (model != null) { Iterator e = model.getAnnotationIterator(); while (e.hasNext()) { Object o = e.next(); if (o instanceof MarkerAnnotation) { MarkerAnnotation a = (MarkerAnnotation) o; if (compareRulerLine(model.getPosition(a), aViewer.getDocument(), aLine) != 0) { marker = a.getMarker(); } } } } return marker; }
/** * Returns one marker which includes the ruler's line of activity. */ protected IMarker getMarkerForLine(ISourceViewer aViewer, int aLine) { IMarker marker = null; IAnnotationModel model = aViewer.getAnnotationModel(); if (model != null) { Iterator e = model.getAnnotationIterator(); while (e.hasNext()) { Object o = e.next(); if (o instanceof MarkerAnnotation) { MarkerAnnotation a = (MarkerAnnotation)o; if (compareRulerLine(model.getPosition(a), aViewer.getDocument(), aLine) != 0) { marker = a.getMarker(); } } } } return marker; }
protected List<IMarker> getMarkersFor(ISourceViewer sourceViewer, int lineOffset, int lineLength) { List<IMarker> result = Lists.newArrayList(); IAnnotationModel annotationModel = sourceViewer.getAnnotationModel(); Iterator annotationIter = annotationModel.getAnnotationIterator(); while (annotationIter.hasNext()) { Object annotation = annotationIter.next(); if (annotation instanceof MarkerAnnotation) { MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation; IMarker marker = markerAnnotation.getMarker(); Position markerPosition = annotationModel.getPosition(markerAnnotation); if (markerPosition != null && markerPosition.overlapsWith(lineOffset, lineLength)) { result.add(marker); } } } return result; }
/** * updateAnnotations * * @param selection */ protected void updateAnnotations(ISelection selection) { if (findOccurrencesJob != null) { findOccurrencesJob.cancel(); } if (selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; IDocument document = getDocument(); IAnnotationModel annotationModel = getAnnotationModel(); if (document != null && annotationModel != null) { findOccurrencesJob = new FindOccurrencesJob(document, textSelection, annotationModel); findOccurrencesJob.schedule(); } } }