我目前正在为自定义转换语言编写Eclipse编辑器插件。我已经可以通过错误下方的红色红线显示语法错误,如下所示:1它们也会显示在PRoblems视图中。现在我试图实现一个悬停的语法错误,就像它在这里:2。您有任何想法或教程如何做到这一点?是否必须在标记中添加确切的行号?我知道那里可能有很多教程,但是他们没有详细解释,所以请帮帮我!=) 此时显示语法错误的代码是:
package de.se_rwth.langeditor.texteditor.errorhighlighting;
import java.io.IOException;
import java.util.Set;
import javax.annotation.Nullable;
import org.antlr.v4.runtime.misc.Interval;
import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.swt.widgets.Display;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import de.se_rwth.langeditor.injection.TextEditorScoped;
import de.se_rwth.langeditor.modelstates.ModelState;
import de.se_rwth.langeditor.modelstates.ObservableModelStates;
@TextEditorScoped
public class ErrorHighlighter {
private final IAnnotationModel annotationModel;
private String content = "";
private final Set<Annotation> annotations = Sets.newConcurrentHashSet();
private static final String MARKERID = "org.eclipse.rwth.syntaxerror";
@Inject
public ErrorHighlighter(@Nullable IAnnotationModel annotationModel, IStorage storage,
ObservableModelStates observableModelStates) {
this.annotationModel = annotationModel;
if (annotationModel != null) {
observableModelStates.getModelStates().stream()
.filter(modelState -> modelState.getStorage().equals(storage))
.forEach(this::acceptModelState);
observableModelStates.addStorageObserver(storage, this::acceptModelState);
}
}
public void acceptModelState(ModelState modelState) {
for (Annotation annotation : annotations) {
annotationModel.removeAnnotation(annotation);
annotations.remove(annotation);
}
IMarker[] problems = null;
int depth = IResource.DEPTH_INFINITE;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
try { //Remove all problem Markers when rebuilding the Model
problems = workspace.getRoot().findMarkers(IMarker.PROBLEM, true, depth);
for(IMarker m: problems){
m.delete();
}
} catch (CoreException e) {
e.printStackTrace();
}
try {
content = IOUtils.toString(modelState.getStorage().getContents(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
displaySyntaxErrors(modelState);
displayAdditionalErrors(modelState);
}
private void displaySyntaxErrors(ModelState modelState) {
ImmutableMultimap<Interval, String> syntaxErrors = modelState.getSyntaxErrors();
for (Interval interval: syntaxErrors.keys()) {
for (String message : syntaxErrors.get(interval)) {
Display.getDefault().asyncExec(() -> displayError(interval, message));
}
}
}
private void displayAdditionalErrors(ModelState modelState) {
Multimap<Interval, String> additionalErrors = modelState.getAdditionalErrors();
for (Interval interval: additionalErrors.keys()) {
for (String message : additionalErrors.get(interval)) {
Display.getDefault().asyncExec(() -> displayError(interval, message));
}
}
}
private void displayError(Interval interval, String message) {
int startIndex = interval.a;
int stopIndex = interval.b + 1;
Annotation annotation =
new Annotation("org.eclipse.ui.workbench.texteditor.error", false, message);
annotations.add(annotation);
annotationModel.addAnnotation(annotation, new Position(startIndex, stopIndex - startIndex));
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IMarker marker;
try { //create Marker to display Syntax Errors in Problems View
marker = workspace.getRoot().createMarker(MARKERID);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.CHAR_START, startIndex);
marker.setAttribute(IMarker.CHAR_END, stopIndex);
marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
int lineNumber = 0;
if(!content.isEmpty()){ //Convert StartIndex to Line Number
String[] lines = content.substring(0, startIndex).split("\r\n|\r|\n");
lineNumber = lines.length;
}
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
} catch (CoreException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
回答我自己的问题:我错过了覆盖
ITextHover getTextHover(ISourceViewer sourceViewer, String contentType)
方法。所以插入后
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
return new DefaultTextHover(sourceViewer);
}
进入SourceViewerConfiguration,显示悬停。