Xtext example how to configure validation via preferences [message #835738] |
Tue, 03 April 2012 12:02  |
Eclipse User |
|
|
|
I want to make my Xtext validator configurable via a couple of preferences but I couldn't find a way to do it.
I did look at the code in org.eclipse.xtext.builder.preferences but most of it relies on UI code, so I'm not sure how I could do something similar in the DSL part.
Is there an example somewhere or any pointers how I can do that?
|
|
|
|
|
|
|
|
Re: Xtext example how to configure validation via preferences [message #836466 is a reply to message #836294] |
Wed, 04 April 2012 10:10   |
Eclipse User |
|
|
|
When I'm standalone, there is no OSGi, either 
Use case: Validation preferences like the ones from the Java Compiler (whether "xxx" is a keyword, whether it should warn about deprecated code, etc).
After many hours, I found all the necessary pieces. For such a basic functionality, the pieces were astonishingly hard to find.
For example, is this really the most simple way to convert a URI into an IProject?
For reference, see the code below. It should be simple to extend if you want to support command line arguments or global defaults and similar features.
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
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;
public class I18nPreferences {
private final static Logger log = Logger.getLogger( I18nPreferences.class );
private final static String I18N_QUALIFIER = "com.pany.eclipse.i18n_dsl.I18nDsl"; // Name of the prefs file under .settings
private final static String VALIDATION_IGNORE_PATTERN_KEY = "validation.ignore"; // Key in the prefs file
/** 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 s = uri.toString();
s = StringUtils.substringAfter( s, "platform:/resource/" );
String projectName = StringUtils.substringBefore( s, "/" );
log.debug( "Loading preferences for " + projectName );
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject project = (IProject) workspace.getRoot().findMember( projectName );
if( null == project ) {
log.debug( "Can't locate project " + projectName + " in workspace" );
return config;
}
// Only project specific preferences are considered
IScopeContext[] contexts = { new ProjectScope( project ) };
String defaultValue = "";
String patterns = service.getString( I18N_QUALIFIER, VALIDATION_IGNORE_PATTERN_KEY, defaultValue, contexts );
log.debug( "Found pattern: " + patterns );
config.setIgnoreString( patterns );
return config;
}
}
|
|
|
|
|
|
|
|
|
|
|
|
Re: Xtext example how to configure validation via preferences [message #1424457 is a reply to message #835738] |
Mon, 15 September 2014 21:41  |
Eclipse User |
|
|
|
Henrik, thanks for the link to your code. I've been trying to mimic your approach to this problem since I have essentially the same needs. In both the DSL and UI modules I've added similar Provider configuration, and inject the Provider into my Validator in the DSL module. However, I never get the UI's provider implementation in my validator when running the plugin. It appears that the UI module doesn't override configuration in the DSL module.
Is there some additional configuration needed to get this to work?
|
|
|
Powered by
FUDForum. Page generated in 0.26643 seconds