Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Programmatic serialization not using formatter preferences
Programmatic serialization not using formatter preferences [message #1835746] Fri, 11 December 2020 12:05 Go to next message
Elie Richa is currently offline Elie RichaFriend
Messages: 34
Registered: February 2016
Member
Hello folks,

I'm building model trees programmatically in an Eclipse context and serializing them with Xtext (more details here).

I'm observing that serialization does indeed call my Formatter2 implementation but it seems to be not using the right formatter preferences.

I gets tabs for indentation even though the Eclipse preferences are set to use spaces for indentation.

My resources are in a resource set bound to an Eclipse project. I tried providing my own Serializer implementation and overrode serialize(EObject obj, Appendable appendable, SaveOptions options) to inject preferences from an IPreferenceValuesProvider into the formatter request, but it didn't change the behavior.

How can I make the serialization use the context formatting preferences?

Thanks in advance!


Elie Richa, Ph.D
Software Engineer, AdaCore
https://www.adacore.com
Re: Programmatic serialization not using formatter preferences [message #1835748 is a reply to message #1835746] Fri, 11 December 2020 12:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13983
Registered: July 2009
Senior Member
you may debug the org.eclipse.xtext.formatting2.FormatterPreferenceValuesProvider.getPreferenceValues(Resource)

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Programmatic serialization not using formatter preferences [message #1835750 is a reply to message #1835748] Fri, 11 December 2020 12:56 Go to previous message
Elie Richa is currently offline Elie RichaFriend
Messages: 34
Registered: February 2016
Member
That helped. I noticed that FormatterPreferenceValues was not being called during my programmatic serialization. That's because I was injecting a non-annotated IPreferenceValuesProvider. Debugging a regular IDE formatting request, I found that the provider should be annotated with @FormatterPreferences in order to get a FormatterPreferenceValuesProvider.

So to sum up, the solution was to override the Serializer as follows:

class BLSerializer extends Serializer {
    @Inject Provider<FormatterRequest> formatterRequestProvider
    @Inject Provider<IFormatter2> formatter2Provider
    @Inject @FormatterPreferences IPreferenceValuesProvider prefsProv

    override void serialize(EObject obj, Appendable appendable,
        SaveOptions options) throws IOException {
        var ITextRegionAccess regionAccess = serializeToRegions(obj)
        var FormatterRequest request = formatterRequestProvider.get()
        request.setFormatUndefinedHiddenRegionsOnly(!options.isFormatting())
        request.setTextRegionAccess(regionAccess)

        val resource = obj.eResource
        if (resource !== null) {
            request.preferences = TypedPreferenceValues.castOrWrap(
                prefsProv.getPreferenceValues(resource))
        }

        var IFormatter2 formatter2 = formatter2Provider.get()
        var List<ITextReplacement> replacements = formatter2.format(request)
        regionAccess.getRewriter().renderToAppendable(replacements, appendable)
    }
}


Thanks again Christian!

Is it worth integrating something like that in the default Serializer? I can try to make a submission if it helps.


Elie Richa, Ph.D
Software Engineer, AdaCore
https://www.adacore.com
Previous Topic:Adding a new resource to the IResourceDescriptions
Next Topic:use generated classes in DSL file, problem of missing type
Goto Forum:
  


Current Time: Thu Jun 17 02:00:46 GMT 2021

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

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

Back to the top