Validation 如何访问项目';我的Xtext验证器的首选项?

Validation 如何访问项目';我的Xtext验证器的首选项?,validation,preferences,xtext,Validation,Preferences,Xtext,我想让我的Xtext验证器可以通过几个首选项进行配置,但我找不到一种方法来实现 是否有一个我可以这样做的例子?至少有两种可能性: 将验证器移动到ui项目并直接使用首选项 在运行时项目中创建一个类似ValidatorGuard的接口,使用#isEnabled(..)和返回“true”的默认实现/使用一些属性。在UI项目中实现该接口,您也可以从PreferenceStore中读取信息,以决定某些验证的启用状态 这并不像看上去那么简单: import org.apache.log4j.Logger;

我想让我的Xtext验证器可以通过几个首选项进行配置,但我找不到一种方法来实现


是否有一个我可以这样做的例子?

至少有两种可能性:

  • 将验证器移动到ui项目并直接使用首选项

  • 在运行时项目中创建一个类似ValidatorGuard的接口,使用#isEnabled(..)和返回“true”的默认实现/使用一些属性。在UI项目中实现该接口,您也可以从PreferenceStore中读取信息,以决定某些验证的启用状态


  • 这并不像看上去那么简单:

    import org.apache.log4j.Logger;
    import org.eclipse.core.resources.IProject;
    import org.eclipse.core.resources.IResource;
    import org.eclipse.core.resources.IWorkspace;
    import org.eclipse.core.resources.ProjectScope;
    import org.eclipse.core.resources.ResourcesPlugin;
    import org.eclipse.core.runtime.Path;
    import org.eclipse.core.runtime.Platform;
    import org.eclipse.core.runtime.preferences.IPreferencesService;
    import org.eclipse.core.runtime.preferences.IScopeContext;
    import org.eclipse.emf.common.util.URI;
    import com.google.inject.Singleton;
    
    @Singleton
    public class I18nPreferences {
    
        private final static Logger log = Logger.getLogger( I18nPreferences.class );
    
        public final static String I18N_QUALIFIER = "com.pany.eclipse.dsl.Dsl"; // name of the prefs file without extension
        public final static String VALIDATION_IGNORE_PATTERN_KEY = "validation.ignore";
    
        /** Get validation configuration for a resource */
        public ValidationConfiguration validationConfiguration( URI uri ) {
    
            // URI looks like this: 
            // platform:/resource/project/src/.../file.dsl
            log.debug( "Search config for " + uri );
            ValidationConfiguration config = new ValidationConfiguration();
    
            IPreferencesService service = Platform.getPreferencesService();
            if( null == service ) {
                // Probably not running under Eclipse
                log.debug( "No IPreferencesService" );
                return config;
            }
    
            String platformString = uri.toPlatformString( true );
            if( null == platformString ) {
                // Probably not running from the Eclipse UI. Might be a unit test or something
                return config;
            }
    
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            String s = uri.isPlatformResource() ? uri.toPlatformString( true ) : uri.toString();
            Path path = new Path( s );
            IResource resource = workspace.getRoot().getFile( path );
    
            IProject project = null == resource ? null : resource.getProject();
            if( null == resource || null == project ) {
                log.debug( "Can't locate project " + uri + " in workspace" );
                return config;
            }
    
            log.debug( "Loading preferences for " + project.getName() );
    
            IScopeContext[] contexts = { new ProjectScope( resource.getProject() ) };
    
            String defaultValue = "";
            String patterns = service.getString( I18N_QUALIFIER, VALIDATION_IGNORE_PATTERN_KEY, defaultValue, contexts );
            log.debug( "Found pattern: " + patterns );
    
            config.setIgnoreString( patterns );
    
            return config;
        }
    }