Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Programmatic serialization not using formatter preferences
Programmatic serialization not using formatter preferences [message #1835746] Fri, 11 December 2020 07:05 Go to next message
Eclipse UserFriend
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!
Re: Programmatic serialization not using formatter preferences [message #1835748 is a reply to message #1835746] Fri, 11 December 2020 07:08 Go to previous messageGo to next message
Eclipse UserFriend
you may debug the org.eclipse.xtext.formatting2.FormatterPreferenceValuesProvider.getPreferenceValues(Resource)
Re: Programmatic serialization not using formatter preferences [message #1835750 is a reply to message #1835748] Fri, 11 December 2020 07:56 Go to previous message
Eclipse UserFriend
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.
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 May 15 15:44:00 EDT 2025

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

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

Back to the top