【发布时间】:2016-09-18 15:36:19
【问题描述】:
我目前正在为自定义转换语言编写一个 Eclipse 编辑器插件。我已经可以通过错误下方的红色卷线显示语法错误,如下所示:1,它们也显示在问题视图中。现在我试图为语法错误实现悬停,就像它在这里: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();
}
}
}
【问题讨论】:
-
这是什么语言的?您所看到的是 Java 本身的一部分;它不能真的只是被编辑。
-
好的,我去查一下。
-
请问,它会在“问题”选项卡中弹出吗?或者它没有显示在任何地方?没有任何源代码,帮助你有点困难。
-
再次编辑问题,提前感谢您的帮助。目前它没有显示在问题选项卡中。
-
我不是最好的 eclipse 插件,但请尝试阅读以下内容:stackoverflow.com/questions/4166126/…
标签: java eclipse eclipse-plugin eclipse-pde