我编写了一个基于Xtext的插件,当我在工作区中的一个项目中打开文件时,该插件运行良好;但是,当我在工作区外打开文件时(通过文件 - >打开文件...),某些功能无法正常工作:
context.getXtextDocument()
返回null
而我依赖它来获取我的quickfix提案。可能还有其他我缺少的东西,但大多数其他功能,如内容辅助,悬停定义,发生标记等,都可以正常工作。
有没有办法让工作区外部源文件的行为与内部文件相同?另外,是否有一种解决方法可以让我“欺骗”Xtext,以某种方式说服它file是当前项目的内部文件,例如通过以编程方式从项目创建指向它的链接?
This is a related question about the behavior with a workspace-external file,但是我成功地打开了这些文件,只是某些功能无效。
答案 0 :(得分:2)
据我所知,目前还没有办法让工作区外部源文件的行为与内部文件相同。
以下是破解验证的解决方法:
在xxx.ui插件中,XxxUiModule.java添加
public Class<? extends IResourceForEditorInputFactory> bindIResourceForEditorInputFactory() {
return MyJavaClassPathResourceForIEditorInputFactory.class;
}
@Override
public Class<? extends IXtextEditorCallback> bindIXtextEditorCallback() {
return MyNatureAddingEditorCallback.class;
}
创建MyJavaClassPathResourceForIEditorInputFactory.java
// Reenable validation
public class MyJavaClassPathResourceForIEditorInputFactory extends JavaClassPathResourceForIEditorInputFactory {
@Override
protected Resource createResource(java.net.URI uri) {
XtextResource resource = (XtextResource) super.createResource(uri);
resource.setValidationDisabled(false);
return resource;
}
}
创建MyNatureAddingEditorCallback.java
// With reenabled validation the syntax validation starts to work only after the first change made
// Run the validation manually to show the syntax errors straight away
// - CheckMode.ALL below should be probably changed to something else to improve the performance
public class MyNatureAddingEditorCallback extends NatureAddingEditorCallback {
@Inject
private IResourceValidator resourceValidator;
@Inject
private MarkerCreator markerCreator;
@Inject
private MarkerTypeProvider markerTypeProvider;
@Inject
private IssueResolutionProvider issueResolutionProvider;
@Override
public void afterCreatePartControl(XtextEditor editor) {
super.afterCreatePartControl(editor);
validate(editor);
}
private void validate(XtextEditor xtextEditor) {
if (xtextEditor == null) {
return;
}
if (xtextEditor.getInternalSourceViewer() == null) {
return;
}
IValidationIssueProcessor issueProcessor;
IXtextDocument xtextDocument = xtextEditor.getDocument();
IResource resource = xtextEditor.getResource();
if(resource != null)
issueProcessor = new MarkerIssueProcessor(resource, markerCreator, markerTypeProvider);
else
issueProcessor = new AnnotationIssueProcessor(xtextDocument, xtextEditor.getInternalSourceViewer().getAnnotationModel(), issueResolutionProvider);
ValidationJob validationJob = new ValidationJob(resourceValidator, xtextDocument, issueProcessor,
CheckMode.ALL); // Consider changing the CheckMode here
validationJob.schedule();
}
}
另请参阅相应的错误报告: https://bugs.eclipse.org/bugs/show_bug.cgi?id=388399