2013-03-12 14 views
2

我已經編寫了一個基於Xtext的插件,該工具在我的工作區中的其中一個項目中打開文件時效果很好;然而,當我打開一個文件工作區外(通過文件 - >打開文件...),某些功能無法正常工作:基於Xtext的編輯器的某些功能在項目外的文件上不起作用

  • 我得到語法着色,但我不明白的語法錯誤。
  • Quickfix選項不起作用,可能是因爲context.getXtextDocument()返回null,我依靠它來做我的quickfix建議。

可能還有其他一些我缺少的東西,但大多數其他功能,如內容輔助,懸停定義,出現標記等,都可以正常工作。

有沒有辦法讓工作區的外部源文件與內部文件的行爲相同?另外,是否有一種解決方法,可以讓我「哄騙」Xtext,以某種方式說服該文件是當前項目的內部文件,例如通過以編程方式創建項目的鏈接?

This is a related question about the behavior with a workspace-external file,但是我成功打開這些文件就好了,只是有些功能無法正常工作。

回答

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

+1

解決方法似乎運作良好,謝謝! – Oak 2013-03-13 08:12:09

相關問題