Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting multiple parser rules within curly brackets
Formatting multiple parser rules within curly brackets [message #1786093] Wed, 25 April 2018 12:58 Go to next message
Yoan Dimitrov is currently offline Yoan DimitrovFriend
Messages: 1
Registered: April 2018
Junior Member
I have the following grammar simplified:

Match:
	'match'
	'{'
	target=Target
	subject=Subject
	action=Action
	matcher=Matcher
	'}';

Target:
	'target' '=' value=STRING;

Subject:
	'subject' '=' value=STRING]

Action:
	'action' '=' value=STRING;

Matcher:
	'contextMatcher' '=' value=ID;


My formatted text should look something like this:


match {
	target = ".*"	
	subject = "foo"
	action = ".*"
	contextMatcher = MATCH_ALWAYS
}



But right now it looks like this (Only the first parser rule is being indented):


match {
	target = ".*"	
subject = "foo"
action = ".*"
contextMatcher = MATCH_ALWAYS
}



I tried the following in my formatter but it didn't work:

def dispatch void format(Match match, extension IFormattableDocument document) {

	match.regionFor.keyword('match').append[oneSpace]
	match.regionFor.keyword(matchAccess.leftCurlyBracketKeyword_1).append[newLine]
	match.regionFor.keyword(matchAccess.rightCurlyBracketKeyword_6).prepend[newLine]
	interior(match.regionFor.keyword(matchAccess.leftCurlyBracketKeyword_1),match.regionFor.keyword(matchAccess.rightCurlyBracketKeyword_6))[indent]
}



Does anyone have an idea what I can do?
Re: Formatting multiple parser rules within curly brackets [message #1786139 is a reply to message #1786093] Thu, 26 April 2018 04:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
hi,

i am not sure if this is intended or not.
can you please provide a bug for that at github.com/eclipse/xtext-core

it works if you explicitely care about whitespace for the body parts

match.target.regionFor.keyword("target").prepend[newLine]
match.subject.regionFor.keyword("subject").prepend[newLine]
match.action.regionFor.keyword("action").prepend[newLine]
match.matcher.regionFor.keyword("contextMatcher").prepend[newLine]


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

[Updated on: Thu, 26 April 2018 04:57]

Report message to a moderator

Re: Formatting multiple parser rules within curly brackets [message #1790779 is a reply to message #1786139] Sat, 16 June 2018 16:46 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Ran into the same. Opened: an issue in git hub

Re: Formatting multiple parser rules within curly brackets [message #1791056 is a reply to message #1790779] Fri, 22 June 2018 09:32 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
I have managed to get formatting to work as desired. As per the github issue response, I now prepend new lines for every semantic region in the indented block. However, I have found an edge case which does not work: comments before the closing brace are not indented (they seem to follow the indentation level of the brace that follows, rather than the semantic region before the comment).

Example of sample input:

greetings {
		// hello...
	Hello X !
		Hello Y !	Hello Z !
					// ...world!
}


Expected formatting:

greetings {
	// hello...
	Hello X !
	Hello Y !
	Hello Z !
	// ...world!
}


Actual formatting:

greetings {
	// hello...
	Hello X !
	Hello Y !
	Hello Z !
// ...world!
}


How can I tell my formatter that indentation applies to comments as well? As per Christian's reply in the github issue I have now:

    def dispatch void format(Model model, extension IFormattableDocument document) {
		model.regionFor.keyword("{").append[newLine]
		model.regionFor.keyword("}").prepend[newLine]
		interior(
			model.regionFor.keyword("{"),
			model.regionFor.keyword("}")
		)[indent]
		for (greeting : model.greetings) {
			greeting.prepend[newLine]
		}
                // HOW DO I PREPEND NEW LINE TO COMMENTS?
	}


Interestingly, this only occurs for comments at the very end of the block, before the closing brace. So other comments seem to follow the indentation of the semantic regions around them.

[Updated on: Fri, 22 June 2018 09:34]

Report message to a moderator

Re: Formatting multiple parser rules within curly brackets [message #1791057 is a reply to message #1791056] Fri, 22 June 2018 09:38 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
I also noticed that his applies to xtend source code itself. In my formatter class using xtend 2.14, after adding a comment and pressing Ctrl-Shift-F, I get:

class MyDslFormatter extends AbstractFormatter2 {

    @Inject extension MyDslGrammarAccess

    def dispatch void format(Model model, extension IFormattableDocument document) {
        model.regionFor.keyword("{").append[newLine]
        model.regionFor.keyword("}").prepend[newLine]
        interior(
            model.regionFor.keyword("{"),
            model.regionFor.keyword("}"),
            [indent]
        // it's for all kinds of braces!
        )
        for (greeting : model.greetings) {
            greeting.prepend[newLine]
        // this comment is indented at the closing brace level by xtend!
        }
    // this comment is indented at the closing brace level by xtend!
    }
}


Is this really intended behaviour for xtend?
Re: Formatting multiple parser rules within curly brackets [message #1791058 is a reply to message #1791057] Fri, 22 June 2018 09:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
no the comment formatting is buggy as hell and i am sure if you go through the issues at github.com/eclipse/xtext-core and github.com/eclipse/xtext-xtend you will already find an issue describing this.
please comment on the existing issue (cannot go through them right now) or create a new one


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting multiple parser rules within curly brackets [message #1791059 is a reply to message #1791058] Fri, 22 June 2018 10:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
did not find an issue but this discussion:
https://github.com/eclipse/xtext-core/pull/647


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting multiple parser rules within curly brackets [message #1791061 is a reply to message #1791059] Fri, 22 June 2018 10:10 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
besides that you can try to digg into customization of SinglelineDocCommentReplacer / SinglelineCommentReplacer

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:XtextLinkingDiagnostic Error while loading xtext model
Next Topic:Parsing in reconciler failed. / [Model.feature] does not exist
Goto Forum:
  


Current Time: Fri Mar 29 12:19:48 GMT 2024

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

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

Back to the top