Programmatic serialization not using formatter preferences [message #1835746] |
Fri, 11 December 2020 12:05  |
Elie Richa Messages: 72 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 #1835750 is a reply to message #1835748] |
Fri, 11 December 2020 12:56  |
Elie Richa Messages: 72 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
|
|
|
Powered by
FUDForum. Page generated in 0.02059 seconds