Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Changing Formatter2 Indentation Preferences from Tab to Spaces?
Changing Formatter2 Indentation Preferences from Tab to Spaces? [message #1821314] Mon, 10 February 2020 18:20 Go to next message
Brandon Lewis is currently offline Brandon LewisFriend
Messages: 206
Registered: May 2012
Senior Member
This came up at the end of another long thread of mine, so I'm starting a new thread for more clear tracking.

I'm still too stupid to be able to figure out something that seems like it should be simple and seems like someone on planet earth has run into this before. But I've yet to find an example set of code outside of Xtext that has an example.

Trouble:

I have a whitespace language that uses indentation for blocking. I programmatically create an object and save it and the formatter takes over to serialize the object in my DSL.

Trouble is, the formatter ALWAYS uses tabs on indentation. So if the user has their editor settings set to "insert spaces for tabs", my DSL parser starts to die if the user makes any hand edits. The end user can only debug if they are displaying whitespace in their editor, they have no idea why the parser is dying. As the author of the language, this has made me curse out loud on multiple occasions when I can't figure out why I can't get a file to compile.

The user can ask the editor to re-format the file, and THEN AND ONLY THEN, will the formatter honor the user's "insert spaces for tabs" setting. But 1) there's no way to ask a user to remember to do this and 2) it doesn't work when using my tool in command line mode

In my formatter, getting the preference value is easy:

def dispatch void format(ComponentType comp, extension IFormattableDocument document) {

var indentation= preferences.getPreference(FormatterPreferenceKeys.indentation)
// debugger says indentation is equal to "\t"

Setting it is confusing the hell out of me. The API docs say:

"To set a values for one of these keys, use FormatterRequest.setPreferences(ITypedPreferenceValues)."

// How do I do this equivalent pseudo code?
//preferences.setPreference(FormatterPreferenceKeys.indentation, " ")

// Or better
//preferences.setPreferences(FormatterPreferenceKeys.indentation, <whatever the user has set to their editor setting>)


It was previously mentioned to search on IWhitespaceInformationProvider, but I've found even fewer examples of that in the code base and web examples.

I've spent a couple days on this problem. It's strange that it's so elusive. How do people not run into this all the time?


[Updated on: Mon, 10 February 2020 18:22]

Report message to a moderator

Re: Changing Formatter2 Indentation Preferences from Tab to Spaces? [message #1821321 is a reply to message #1821314] Mon, 10 February 2020 19:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13641
Registered: July 2009
Senior Member
sorry. i dont have time to setup and example

org.eclipse.xtext.ui.editor.formatting2.ContentFormatter.initRequest(IXtextDocument, IRegion, XtextResource, FormatterRequest)

this one calls the method. it uses FormatterPreferenceValuesProvider which is usually bound in abstractyourdsluimodule

public void configureFormatterPreferences(Binder binder) {
binder.bind(IPreferenceValuesProvider.class).annotatedWith(FormatterPreferences.class).to(FormatterPreferenceValuesProvider.class);
}

FormatterPreferenceValuesProvider delegates to IWhitespaceInformationProvider which is bound in eclipse to
PreferenceStoreWhitespaceInformationProvider by default.

which again delegates to PreferenceStoreIndentationInformation

which reads spaces for tabs.


with the information you have i have no idea where you stuck


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: Changing Formatter2 Indentation Preferences from Tab to Spaces? [message #1821322 is a reply to message #1821321] Mon, 10 February 2020 19:18 Go to previous messageGo to next message
Axel Guckelsberger is currently offline Axel GuckelsbergerFriend
Messages: 347
Registered: July 2009
Senior Member
This works for me:

package org.acme.mydsl.formatting2

import org.eclipse.xtext.formatting.IIndentationInformation

class MyDslIndentationInformation implements IIndentationInformation {
    /**
     * Use spaces instead of tabs always.
     */
    override getIndentString() {
        '    '
    }
}

[Updated on: Mon, 10 February 2020 19:18]

Report message to a moderator

Re: Changing Formatter2 Indentation Preferences from Tab to Spaces? [message #1821323 is a reply to message #1821322] Mon, 10 February 2020 19:20 Go to previous messageGo to next message
Axel Guckelsberger is currently offline Axel GuckelsbergerFriend
Messages: 347
Registered: July 2009
Senior Member
For the formatter:

import org.eclipse.xtext.formatting2.FormatterPreferenceKeys
import org.eclipse.xtext.preferences.MapBasedPreferenceValues

...

    def dispatch void format(Application it, extension IFormattableDocument document) {
        // use spaces instead of tabs
        val preferences = getPreferences
	val newMap = Maps.<String, String> newLinkedHashMap
	newMap.put(FormatterPreferenceKeys.indentation.id, '    ') // $NON-NLS-1$
	val result = new MapBasedPreferenceValues(preferences, newMap)
	request.preferences = result

Re: Changing Formatter2 Indentation Preferences from Tab to Spaces? [message #1821325 is a reply to message #1821323] Mon, 10 February 2020 20:16 Go to previous message
Brandon Lewis is currently offline Brandon LewisFriend
Messages: 206
Registered: May 2012
Senior Member
^^^^

YES YES YES

THANKS THANKS THANKS!

Copy/Pasted the last post and it has put an end to my agony. :-)
Previous Topic:Serializing several resources with cross references
Next Topic:Keep the Grammar structure same as before saving the file
Goto Forum:
  


Current Time: Tue Oct 27 21:23:02 GMT 2020

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

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

Back to the top