Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Spaces for indentation
Spaces for indentation [message #1821459] Thu, 13 February 2020 12:15 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
Hi

The questions was asked several times before. However I can't find a working solution. Can you suggest please how to use spaces for indentation? Is it possible to use tabs/spaces depending on Eclipse settings?

[Updated on: Thu, 13 February 2020 12:18]

Report message to a moderator

Re: Spaces for indentation [message #1821460 is a reply to message #1821459] Thu, 13 February 2020 12:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

depends on where you are but in eclipse this should work autmatically if you configure it. (use spaces for tabs unter text editors)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Spaces for indentation [message #1821461 is a reply to message #1821460] Thu, 13 February 2020 12:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
so you might use this for debugging:

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.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Spaces for indentation [message #1821463 is a reply to message #1821460] Thu, 13 February 2020 12:34 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
Thanks!

It seems after setting "Preferences -> General -> Editors -> Text Editors -> Insert spaces for tabs" Xtext uses spaces for indentation.

However when I add elements to a model using an external editor (Eclipse Sirius). Please see an attached screenshot. The first element is added without indentation (f1), the rest added elements are intented with tabs.

Here is a grammar fragment:

Model returns Model: {Model}
    'Model' name=EString
    '{'
        ('Version' version=EString)?
        ('Description' description=EString)?
        elements+=ModelElement*
    '}';


And formatter:

package epc.text.formatting2

import epc.Model
import epc.ModelElement
import org.eclipse.xtext.formatting2.AbstractFormatter2
import org.eclipse.xtext.formatting2.IFormattableDocument

class EpcFormatter extends AbstractFormatter2 {

    def dispatch void format(Model stmt, extension IFormattableDocument document) {
        interior(
            stmt.regionFor.keyword('{').prepend[oneSpace].append[newLine],
            stmt.regionFor.keyword('}').prepend[newLine].append[newLine]
        )[indent]

        for (element : stmt.elements) {
            element.format(document)
        }
    }

    def dispatch void format(ModelElement stmt, extension IFormattableDocument document) {
        stmt.prepend[indent].append[newLine]
    }
}
Re: Spaces for indentation [message #1821465 is a reply to message #1821463] Thu, 13 February 2020 12:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,
is your formatter called at all?
did you debug what is uses?

besides that you needs to have a look what edits are produced any why.
unfortunately the code is hard to understand and has a notable number of bugs.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 13 February 2020 12:43]

Report message to a moderator

Re: Spaces for indentation [message #1821468 is a reply to message #1821465] Thu, 13 February 2020 13:04 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
Yes, formatter is called. When I select all text in a text buffer and press Ctrl-Shift-F the code is formatted as expected.

Can you tell me what bugs the code has? I didn't found a documentation on AbstractFormatter2 , just a several examples. So formatting is a black-box for me.

Here is a full project: https://github.com/AresEkb/epc
Re: Spaces for indentation [message #1821473 is a reply to message #1821468] Thu, 13 February 2020 13:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sorry you might find some at xtext-core. you have to debug yourself to see what happens there

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Spaces for indentation [message #1821474 is a reply to message #1821473] Thu, 13 February 2020 13:46 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
Thanks for help! I've found several small examples here https://github.com/eclipse/xtext-core/tree/master/org.eclipse.xtext.testlanguages

I think that the formatting API is very cryptic. And the documentation or a big sample for a complex language with several use cases (like nested indentation, etc.) is really required. I understand that resources are limited and this is not a priority.

Thanks for direction, I will debug it.
Re: Spaces for indentation [message #1821501 is a reply to message #1821474] Thu, 13 February 2020 16:55 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
For the documentation of the new formatting API please refer to the following slide share:

https://de.slideshare.net/mobile/meysholdt/xtexts-new-formatter-api
Re: Spaces for indentation [message #1821502 is a reply to message #1821501] Thu, 13 February 2020 16:58 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
For the documentation of the new formatting API please refer to the following slide share:

https://de.slideshare.net/mobile/meysholdt/xtexts-new-formatter-api
Re: Spaces for indentation [message #1822244 is a reply to message #1821502] Mon, 02 March 2020 12:27 Go to previous message
Brandon Lewis is currently offline Brandon LewisFriend
Messages: 268
Registered: May 2012
Senior Member
In case someone winds up on this thread while doing a search on the subject, a practical solution is also documented in this other thread: https://www.eclipse.org/forums/index.php/t/1102362/

I had the same issue that I could not get the formatter to follow my preferences on the initial programmatic generation of my model. A subsequent format request would fix it, but the initial format job always used tabs. When used in command line mode, it was hopeless to get spaces because the user was not interacting and could not perform a interactive manual correction.

There must be a proper fix lurking somewhere, or a bug lurking somewhere, but the thread linked above has information on how to hard code the indentation to spaces which worked for my uses.

Also, FWIW, whitespace languages see multiple spaces and tabs as different block indentations, so it was a real headache for my DSL because the user starts getting errors and can't tell why unless they display whitespace and know exactly what's going on.
Previous Topic:Code creation for data validation
Next Topic:Xtext / Xtend 2.21 Release
Goto Forum:
  


Current Time: Fri Apr 19 04:46:01 GMT 2024

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

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

Back to the top