|
|
|
|
|
|
Re: Xtext example how to configure validation via preferences [message #836466 is a reply to message #836294] |
Wed, 04 April 2012 14:10 |
Aaron Digulla Messages: 258 Registered: July 2009 Location: Switzerland |
Senior Member |
|
|
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 #837826 is a reply to message #837330] |
Fri, 06 April 2012 06:23 |
Alexander Nittka Messages: 1193 Registered: July 2009 |
Senior Member |
|
|
Quote:Of course it's essential. What's the point of having validation that only works in an Eclipse editor?
Sorry for asking! I might ask back, what's the point of essential validation if you can turn it off?
Alex
P.S.: Just as an example. We have a use case, where additional warnings are shown to the user when editing the model in the UI. These are not semantic errors of the model but indicate to the user, that the results may be not intended. This validation makes no sense when generating code from the model in standalone mode.
[Updated on: Fri, 06 April 2012 06:36] Report message to a moderator
|
|
|
|
Re: Xtext example how to configure validation via preferences [message #840775 is a reply to message #837416] |
Tue, 10 April 2012 13:51 |
Aaron Digulla Messages: 258 Registered: July 2009 Location: Switzerland |
Senior Member |
|
|
Henrik Lindberg wrote on Thu, 05 April 2012 18:34I have implemented different types of validation that a user can control
per workspace or per project. My code is in cloudsmith/geppetto at github.
Thanks, this was really useful. I managed to figure out most my bugs using your code.
I did refactor the AbstractRebuildingPreferencePage and added two abstract methods to check whether a rebuild should happend and to create the job. That way, the code is now reusable in different projects.
The build job is now DSL agnostic, too. You only need to supply it a DslResourceLocator. The locator searches a project or the workspace of DSL resources, so the job can do an INCREMENTAL_BUILD.
Here is the gist: https://gist.github.com/2351505
[Updated on: Tue, 10 April 2012 13:52] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05487 seconds