Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext example how to configure validation via preferences
Xtext example how to configure validation via preferences [message #835738] Tue, 03 April 2012 16:02 Go to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
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 #835755 is a reply to message #835738] Tue, 03 April 2012 16:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
hi,

since this is not at all Xtext specific what about reading some general documentation about eclipse preferences e.g
http://www.vogella.de/articles/EclipsePreferences/article.html

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext example how to configure validation via preferences [message #835758 is a reply to message #835755] Tue, 03 April 2012 16:29 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
I know how to work with preferences in general but how do I get the project's preferences with Guice/Xtext?

The Xtext UI uses PreferenceStoreAccessImpl but I can't use that in the DSL.
Re: Xtext example how to configure validation via preferences [message #835759 is a reply to message #835758] Tue, 03 April 2012 16:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
there is no general way for that => you have to do/rebuild it yourself - or file an enhancement that it shall be moved to none ui. in doubt without using guice at all.

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 03 April 2012 16:32]

Report message to a moderator

Re: Xtext example how to configure validation via preferences [message #836290 is a reply to message #835759] Wed, 04 April 2012 10:10 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
This isn't as simple as it looks.

All the demos and examples assume that you're building a UI plugin (i.e. that you either have an Activator class or access to Platform).

By default, neither is accessible from the non-UI part of the DSL and I'm unsure what the side effects are when I start adding random bundles to my non-UI plugins.

This is how far I got:

        IPreferencesService service = Platform.getPreferencesService();
        if( null == service ) {
            // Probably not running under Eclipse
            return new ValidationConfiguration();
        }
        
        IProject project = null; // TODO Where can I get this?
        IScopeContext[] contexts = { new ProjectScope( project ) };
        
        String defaultValue = "";
        String patterns = service.getString( I18N_QUALIFIER, VALIDATION_IGNORE_PATTERN_KEY, defaultValue, contexts );
        
        ValidationConfiguration config = new ValidationConfiguration();
        config.setIgnoreString( patterns );
        
        return config;


Where do I get the correct instance of IProject?
Re: Xtext example how to configure validation via preferences [message #836294 is a reply to message #836290] Wed, 04 April 2012 10:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
If you are standalone there is no IProject.
this is why you should rely on OSGi Preferences and not IPreferencesService

btw why do you want to do this configurability at all?

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext example how to configure validation via preferences [message #836466 is a reply to message #836294] Wed, 04 April 2012 14:10 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
When I'm standalone, there is no OSGi, either Smile

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 #836470 is a reply to message #836466] Wed, 04 April 2012 14:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
Hi,

did you see http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_map_between_an_EMF_Resource_and_an_Eclipse_IFile.3F

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext example how to configure validation via preferences [message #836622 is a reply to message #836470] Wed, 04 April 2012 18:03 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

one question about the configurable validation: is it essential to the validity of a model in standalone mode or would it be enough to have those markers in the UI? In this case, you could simply override the java-validator binding in the UI-project, extending the runtime validation, and add the configurable validation.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Xtext example how to configure validation via preferences [message #837330 is a reply to message #836470] Thu, 05 April 2012 14:39 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Christian Dietrich wrote on Wed, 04 April 2012 16:18
Hi,

did you see http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_map_between_an_EMF_Resource_and_an_Eclipse_IFile.3F


No. Thanks for the link Smile

Alexander Nittka

is it essential to the validity of a model in standalone mode


Of course it's essential. What's the point of having validation that only works in an Eclipse editor? What will I do when I need to run my DSLs from the command line without OSGi?

That said, I struggled a lot with the preferences page for my validation code.

For my page, I extended OptionsConfigurationBlock and PropertyAndPreferencePage. To do that, I needed to cut&paste a lot of code. This code could use some love Smile

I also tried extending LanguageRootPreferencePage but this implementation can't create per-project settings. Yes, there is a combo box to "enable project specific settings" but no matter what I put in there, the prefs are saved to workspace/.metadata/....

[Updated on: Tue, 10 April 2012 09:13]

Report message to a moderator

Re: Xtext example how to configure validation via preferences [message #837416 is a reply to message #835738] Thu, 05 April 2012 16:34 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-03-04 18:02, Aaron Digulla wrote:
> 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?

I have implemented different types of validation that a user can control
per workspace or per project. My code is in cloudsmith/geppetto at github.

I don't like code that looks up properties here there and everywhere
throughout the code, so I have a provider and I inject either one that
is based on preferences (when a workspace is available), or one that is
configured programatically when running headless. My preferences allows
a user to specificy ignore/warning/error, and I pick these up via the
provider in my validation logic.

I also listen for changes to the preferences and queue these up and
perform a rebuild/revalidation when that happens.

Feel free to borrow whatever you need - code is EPL.

Regards
- henrik
Re: Xtext example how to configure validation via preferences [message #837826 is a reply to message #837330] Fri, 06 April 2012 06:23 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
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 #840533 is a reply to message #837826] Tue, 10 April 2012 07:51 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Alexander Nittka wrote on Fri, 06 April 2012 08:23
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.


I have two use cases:

1. I build my bundles with Tycho. This means that my DSL is copied to ${project}/target/classes. If the resource contains errors, I get two entries in the error view. Solution: Ignore anything below ${project}/target/

1. I have a set of resources on the classpath which contains errors (they are used to test whether the correct validation errors are generated when the model is split over several files; i.e. it tests imports, cross-resource linking and validation).

I want the error to show up in my tests but not while I edit the project in Eclipse.

Solution: Tell Eclipse to ignore this resource and configure the validation in the test case to use the defaults.
Re: Xtext example how to configure validation via preferences [message #840775 is a reply to message #837416] Tue, 10 April 2012 13:51 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Henrik Lindberg wrote on Thu, 05 April 2012 18:34
I 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

Re: Xtext example how to configure validation via preferences [message #841042 is a reply to message #840775] Tue, 10 April 2012 20:50 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-10-04 15:51, Aaron Digulla wrote:
> Henrik Lindberg wrote on Thu, 05 April 2012 18:34
>> I 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 of the bugs.
>
Good !

> 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
>
>
Cool, you should keep the Copyright and license header, and add yourself
as contributor though.

Regards
- henrik
Re: Xtext example how to configure validation via preferences [message #841538 is a reply to message #841042] Wed, 11 April 2012 12:47 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Henrik Lindberg wrote on Tue, 10 April 2012 22:50
Cool, you should keep the Copyright and license header, and add yourself
as contributor though.


Fixed; the copyright got lost when I copied the Java code into my own project.
Re: Xtext example how to configure validation via preferences [message #1424457 is a reply to message #835738] Tue, 16 September 2014 01:41 Go to previous message
Todd Jonker is currently offline Todd JonkerFriend
Messages: 2
Registered: July 2009
Junior Member
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?
Previous Topic:[xtext] problem saving a model to dsl file
Next Topic:Qualified name relative to a namespace import?
Goto Forum:
  


Current Time: Sat Dec 07 15:16:07 GMT 2019

Powered by FUDForum. Page generated in 0.02722 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top