Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » formatter2 - problem with nested objects
formatter2 - problem with nested objects [message #1703332] Thu, 30 July 2015 16:59 Go to next message
Meleagros Magnus is currently offline Meleagros MagnusFriend
Messages: 3
Registered: July 2015
Junior Member
Hi all,

I'm new to xtext and try to use the formatter 2 api. I have the following (abstract) Language:


LevelOne:
	'levelone' name= ID  '{'
	leveltwo=LevelTwo
	'}';

LevelTwo:
	'leveltwo' name=ID '{'
	levelthree+=LevelThree*
	leveltwo+=LevelTwo*
	'}';

LevelThree:
	type=[SomthingElse] '{'
		'question' ':' required = ('Y' | 'N')
	'}'	
;


Xtext generates multiple format methods in my custom formatter class which extends AbstractFormatter2. Eclipse creates also two for loops for the lvlthree objects and the sub lvl two objects.


def dispatch void format(LevelTwo lvltwo, extension IFormattableDocument document) {
		lvltwo.regionForKeyword("{").prepend[increaseIndentation].append[newLine]
		
		for (LvlThree lvlthree : lvltwo.getLvlThree()) {
			format(lvlthree , document);
			lvlthree .prepend[newLine]
			lvlthree .regionForKeyword("{").prepend[increaseIndentation].append[newLine]
		}
		for (LvlTwo subLvlTwo : lvltwo.getLvlTwo()) {
			format(subLvlTwo , document);
		}

		lvltwo.regionForKeyword("}").prepend[newLine; decreaseIndentation].append[newLine]
	}



The problem is that the lvlthree formatting statements are called but not executed. I've debugged the code to ensure that the code is executed. I've tried several different formatting options. Is there anything wrong with my formatting code?

The formatting on the lvltwo is working as expected:

lvltwo.regionForKeyword("{").prepend[increaseIndentation].append[newLine]
lvltwo.regionForKeyword("}").prepend[newLine; decreaseIndentation].append[newLine]


Thanks for your help!
Re: formatter2 - problem with nested objects [message #1703641 is a reply to message #1703332] Mon, 03 August 2015 18:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Which exact xtext version do you use? i asume 2.8.3

format(lvlthree , document); this method is nowhere defined. (as dispatch method)
never the less the formatter is called (besides you a missing a decrease for level3

class MyDslFormatter extends AbstractFormatter2 {

	@Inject extension MyDslGrammarAccess

	def dispatch void format(LevelOne levelone, extension IFormattableDocument document) {
		levelone.regionForKeyword("{").prepend[increaseIndentation].append[newLine]
		levelone.regionForKeyword("}").prepend[newLine; decreaseIndentation].append[newLine]
		format(levelone.getLeveltwo(), document);
	}

	def dispatch void format(LevelTwo lvltwo, extension IFormattableDocument document) {
		lvltwo.regionForKeyword("{").prepend[increaseIndentation].append[newLine]

		for (LevelThree lvlthree : lvltwo.getLevelthree()) {
			format(lvlthree, document);

		}
		for (LevelTwo subLvlTwo : lvltwo.getLeveltwo()) {
			format(subLvlTwo, document);
		}

		lvltwo.regionForKeyword("}").prepend[newLine; decreaseIndentation].append[newLine]
	}

	def dispatch void format(LevelThree lvlthree, extension IFormattableDocument document) {
		lvlthree.prepend[newLine]
		lvlthree.regionForKeyword("{").prepend[increaseIndentation].append[newLine]
		lvlthree.regionForKeyword("}").prepend[newLine;decreaseIndentation].append[newLine]
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: formatter2 - problem with nested objects [message #1703644 is a reply to message #1703641] Mon, 03 August 2015 19:05 Go to previous messageGo to next message
Meleagros Magnus is currently offline Meleagros MagnusFriend
Messages: 3
Registered: July 2015
Junior Member
Yes, I'm using 2.8.3. I Also tried it with the dispatch method but with no luck. This method is not generated by xtext. Its also only called when the for loop is defined (which was generated by xtext).
Re: formatter2 - problem with nested objects [message #1703646 is a reply to message #1703644] Mon, 03 August 2015 19:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
did you try my code?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: formatter2 - problem with nested objects [message #1707044 is a reply to message #1703646] Tue, 01 September 2015 18:34 Go to previous messageGo to next message
Meleagros Magnus is currently offline Meleagros MagnusFriend
Messages: 3
Registered: July 2015
Junior Member
OK, the formatter works if I add a name attribute in the LevelThree. But only then. If I remove the name attribute the code is not formatted. I don't unterstand why. I don't need a name attribute there. Without the name attribute only the first levelthree element is formatted not the other ones.

LevelThree:
	name=ID type=[SomthingElse] '{'
		'question' ':' required = ('Y' | 'N')
	'}'	
;

Re: formatter2 - problem with nested objects [message #1707045 is a reply to message #1707044] Tue, 01 September 2015 19:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you file a bug for this

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: formatter2 - problem with nested objects [message #1707047 is a reply to message #1707045] Tue, 01 September 2015 19:43 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
as a workaround you can use

LevelThree:
	{LevelThree} type=[SomthingElse] '{'
		'question' ':' required = ('Y' | 'N')
	'}'	
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Extending Wizard to create a new Xtext-Project
Next Topic:Xtext Generator no NEW LINE
Goto Forum:
  


Current Time: Thu Apr 25 17:04:27 GMT 2024

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

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

Back to the top