Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting list of strings?
Formatting list of strings? [message #1777847] Tue, 05 December 2017 09:52 Go to next message
Patrik Nandorf is currently offline Patrik NandorfFriend
Messages: 165
Registered: January 2013
Location: Sweden
Senior Member
I'm trying to figure out how to format list of strings.

I have the follow grammar:
Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID ':'
		(phrases+=ID*)	
	'!'
;


The formatter code is as follows
def dispatch void format(Model model, extension IFormattableDocument document) {
		for (Greeting greeting : model.getGreetings()) {
			greeting.format;
		}
	}
	
	def dispatch void format(Greeting greeting, extension IFormattableDocument document) {
		var close = greeting.regionFor.keyword(greetingAccess.exclamationMarkKeyword_4)
		var open = greeting.regionFor.keyword(greetingAccess.colonKeyword_2)		
		open.append[newLine]
		close.append[newLine]
		interior(open,close)[indent]
		
		for (String phrase : greeting.phrases) {
			phrase.format;
		}
	}
	
	def dispatch void format(String phrase, extension IFormattableDocument document) {
		/// how do I format the phrase when it is a string
	}



The expected formatting is as follows where each phrase (String) is indented and a newline is appended
Hello John :
	How are you	
	I am fine!
Hello Anne :
	Me too
!
Re: Formatting list of strings? [message #1777888 is a reply to message #1777847] Tue, 05 December 2017 16:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12114
Registered: July 2009
Senior Member
Hi,

this is not possible but you can do something like

def dispatch void format(Greeting greeting, extension IFormattableDocument document) {
var close = greeting.regionFor.keyword(greetingAccess.exclamationMarkKeyword_4)
var open = greeting.regionFor.keyword(greetingAccess.colonKeyword_2)
open.append[newLine]
close.append[newLine]
interior(open,close)[indent]

greeting.allRegionsFor.features(MyDslPackage.Literals.GREETING__PHRASES).forEach[
println(it.text)
prepend[
newLine
]
]
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting list of strings? [message #1777918 is a reply to message #1777888] Wed, 06 December 2017 07:31 Go to previous message
Patrik Nandorf is currently offline Patrik NandorfFriend
Messages: 165
Registered: January 2013
Location: Sweden
Senior Member
Thanks Christian,

I could get you code to work directly but it pushed me in the right direction. Changing the phrases to STRING I can get the formatting to work with:

greeting.allRegionsFor.features(MyDslPackage.Literals.GREETING__PHRASES).forEach[prepend[indent] append[newLine]]

Previous Topic:Accessing Code behind Model
Next Topic:How to convert STRING terminal to ecore::EInt when parse XML using xtext grammar?
Goto Forum:
  


Current Time: Sat Jul 21 17:01:32 GMT 2018

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

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

Back to the top