Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to embed new line character in string in xtend?
How to embed new line character in string in xtend? [message #915949] Tue, 18 September 2012 05:21 Go to next message
Eclipse UserFriend
I am generating a class field in my JvmModelinferrer in xtend:

val exp = NodeModelUtils::getNode(rule.expression);
members+=rule.toField("text",rule.newTypeRef('java.lang.String'))[
^static = true
val Procedure1<ITreeAppendable> b = [
append('''"«exp.text.replace('"','').replaceAll("\n"," \\n ")»"''')
]
initializer = b
setFinal(true)
setVisibility(JvmVisibility::PUBLIC)
]


given that the initial exp.text was

'a
b'

I would like the generated field to look like this:

String text = "a \n b";

but instead it look like this:

String text = "a n b";

where as if I dont replace the newline characters at all then I get:

String text = "a
b";

which of course doesnt compile. The problem seems to be that xtend is evaluating the java string, so even though "\\n" gets compiled to "\n" in java, in xtend it evaluates to "n"

How can I get xtend to not evaluate the \n so that it is preserved in the generated java string?
Re: How to embed new line character in string in xtend? [message #915981 is a reply to message #915949] Tue, 18 September 2012 06:51 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Did you try to replace with "\\\\n"? The source code you're writing will get through the lexer too and the backslash needs one more escape.

regards,
Vlad
Re: How to embed new line character in string in xtend? [message #915993 is a reply to message #915981] Tue, 18 September 2012 07:27 Go to previous message
Eclipse UserFriend
worked like a charm Smile, many thanks!
Previous Topic:15 Minute Tutorial Extended
Next Topic:Label Decorators Trace - Common Navigator for Xtext-based Project
Goto Forum:
  


Current Time: Wed Jul 23 07:22:15 EDT 2025

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

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

Back to the top