下面列出了org.eclipse.jface.text.source.projection.ProjectionAnnotation#org.eclipse.xtext.ui.editor.validation.XtextAnnotation 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean canFix(Annotation annotation) {
if (annotation.isMarkedDeleted())
return false;
// non-persisted annotation
if (annotation instanceof XtextAnnotation) {
XtextAnnotation a = (XtextAnnotation) annotation;
return getResolutionProvider().hasResolutionFor(a.getIssueCode());
}
// persisted markerAnnotation
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
if (!markerAnnotation.isQuickFixableStateSet())
markerAnnotation.setQuickFixable(getResolutionProvider().hasResolutionFor(
issueUtil.getCode(markerAnnotation)));
return markerAnnotation.isQuickFixable();
}
if (annotation instanceof SpellingAnnotation) {
return true;
}
return false;
}
/**
* Instantiates a new MarkerIdentity.
*
* @param annotation
* the Annotation
* @return MarkerIdentity - this
*/
public MarkerIdentity create(final Annotation annotation) {
MarkerIdentity result = provider.get();
if (annotation instanceof XtextAnnotation) {
Issue issue = ((XtextAnnotation) annotation).getIssue();
result.start = issue.getOffset();
result.end = result.start == ATTRIBUTE_MISSING ? ATTRIBUTE_MISSING : result.start + issue.getLength();
result.message = issue.getMessage();
} else if (annotation instanceof org.eclipse.ui.texteditor.MarkerAnnotation) {
result.start = MarkerUtilities.getCharStart(((org.eclipse.ui.texteditor.MarkerAnnotation) annotation).getMarker());
result.end = MarkerUtilities.getCharEnd(((org.eclipse.ui.texteditor.MarkerAnnotation) annotation).getMarker());
result.message = MarkerUtilities.getMessage(((org.eclipse.ui.texteditor.MarkerAnnotation) annotation).getMarker());
} else {
result.end = ATTRIBUTE_MISSING;
result.start = ATTRIBUTE_MISSING;
result.message = null; // NOPMD
}
result.problemCode = issueUtil.getCode(annotation);
result.problemURI = issueUtil.getUriToProblem(annotation);
result.resourceURI = result.problemURI == null ? null : result.problemURI.trimFragment();
return result;
}
public Issue getIssueFromAnnotation(Annotation annotation) {
if (annotation instanceof XtextAnnotation) {
XtextAnnotation xtextAnnotation = (XtextAnnotation) annotation;
return xtextAnnotation.getIssue();
} else if(annotation instanceof MarkerAnnotation) {
MarkerAnnotation markerAnnotation = (MarkerAnnotation)annotation;
return createIssue(markerAnnotation.getMarker());
} else
return null;
}
public String getCode(Annotation annotation) {
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation ma = (MarkerAnnotation) annotation;
return getCode(ma.getMarker());
}
if (annotation instanceof XtextAnnotation) {
XtextAnnotation xa = (XtextAnnotation) annotation;
return xa.getIssueCode();
}
return null;
}
public String[] getIssueData(Annotation annotation) {
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation ma = (MarkerAnnotation) annotation;
return getIssueData(ma.getMarker());
}
if (annotation instanceof XtextAnnotation) {
XtextAnnotation xa = (XtextAnnotation) annotation;
return xa.getIssueData();
}
return null;
}
public URI getUriToProblem(Annotation annotation) {
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation ma = (MarkerAnnotation) annotation;
return getUriToProblem(ma.getMarker());
}
if (annotation instanceof XtextAnnotation) {
XtextAnnotation xa = (XtextAnnotation) annotation;
return xa.getUriToProblem();
}
return null;
}
@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()));
}
/**
* Check whether the given annotation type is supported, i.e. {@link #canFix(Annotation)} might return {@code true}.
* This could be made protected in a future release.
*/
private boolean isSupported(Annotation annotation) {
return !annotation.isMarkedDeleted()
&& (annotation instanceof XtextAnnotation || annotation instanceof MarkerAnnotation
|| annotation instanceof SpellingAnnotation);
}