Bug 474346 - Whitespace swallowed in SEPARATOR of string templates.
Summary: Whitespace swallowed in SEPARATOR of string templates.
Status: NEW
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.8.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-05 12:40 EDT by Jakub Siberski CLA
Modified: 2020-10-02 04:24 EDT (History)
3 users (show)

See Also:


Attachments
Code snippets (2.99 KB, text/plain)
2015-08-05 12:43 EDT, Jakub Siberski CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Siberski CLA 2015-08-05 12:40:30 EDT
When using new line and whitespace in SEPARATORS, whitespace is swallowed inside string template.

Seems like <pre>org.eclipse.xtend2.lib.StringConcatenation.getSignificantContent()</pre> trims whitespace, unfortunately in some cases that is unexpected by the user.

Code snippets below should explain this.
 - CASE1 and RESULT1 chow xtend code and output it generates. (4 spaces in SEPARATOR after line break)
 - CASE2 has modifies SEPARATOR, but one would expect same output as RESULT1, but it is different -> indentation from SEPARATOR is swallowed (see RESULT2)
 - CASE3 adds one non-whitespace character at the end of SEPARATOR, output indentation is fixed (see RESULT3)
 - CASE4 and CASE5 are the same as CASE2 and CASE3 but they use tab instead of spaces in the SEPARATOR, results are as in RESULT2 and RESULT3 appropriately.



CASE1
<code>
	def CharSequence generateMainLevel(){
		val s = "1234";
		'''
		var mainLevel = {«FOR ch : s.toCharArray SEPARATOR ',\n    '»«generateSubLevel(ch)»«ENDFOR»}
		'''
	}
	def CharSequence generateSubLevel(char data){
		'''sublevel_«data»: {value : «data»}'''
	}
</code>
RESULT1
<code>
var mainLevel = {sublevel_1: {value : 1},
    sublevel_2: {value : 2},
    sublevel_3: {value : 3},
    sublevel_4: {value : 4}}
</code>
CASE2
<code>
	def CharSequence generateMainLevel(){
		val s = "1234";
		'''
		var mainLevel = {«FOR ch : s.toCharArray SEPARATOR ''',«"\n"»    '''»«generateSubLevel(ch)»«ENDFOR»}
		'''
	}
	def CharSequence generateSubLevel(char data){
		'''sublevel_«data»: {value : «data»}'''
	}
</code>
RESULT2
<code>
var mainLevel = {sublevel_1: {value : 1},
sublevel_2: {value : 2},
sublevel_3: {value : 3},
sublevel_4: {value : 4}}
</code>

CASE3
<code>
	def CharSequence generateMainLevel(){
		val s = "1234";
		'''
		var mainLevel = {«FOR ch : s.toCharArray SEPARATOR ''',«"\n"»    _'''»«generateSubLevel(ch)»«ENDFOR»}
		'''
	}
	def CharSequence generateSubLevel(char data){
		'''sublevel_«data»: {value : «data»}'''
	}
</code>

RESULT3
<code>
var mainLevel = {sublevel_1: {value : 1},
    _sublevel_2: {value : 2},
    _sublevel_3: {value : 3},
    _sublevel_4: {value : 4}}
</code>
CASE4
<code>
	def CharSequence generateMainLevel(){
		val s = "1234";
		'''
		var mainLevel = {«FOR ch : s.toCharArray SEPARATOR ''',«"\n"»	'''»«generateSubLevel(ch)»«ENDFOR»}
		'''
	}
	def CharSequence generateSubLevel(char data){
		'''sublevel_«data»: {value : «data»}'''
	}
</code>
RESULT4
<code>
var mainLevel = {sublevel_1: {value : 1},
sublevel_2: {value : 2},
sublevel_3: {value : 3},
sublevel_4: {value : 4}}
</code>

CASE5
<code>
	def CharSequence generateMainLevel(){
		val s = "1234";
		'''
		var mainLevel = {«FOR ch : s.toCharArray SEPARATOR ''',«"\n"»	_'''»«generateSubLevel(ch)»«ENDFOR»}
		'''
	}
	def CharSequence generateSubLevel(char data){
		'''sublevel_«data»: {value : «data»}'''
	}
</code>
RESULT5
<code>
var mainLevel = {sublevel_1: {value : 1},
	_sublevel_2: {value : 2},
	_sublevel_3: {value : 3},
	_sublevel_4: {value : 4}}
</code>
Comment 1 Jakub Siberski CLA 2015-08-05 12:43:09 EDT
Created attachment 255650 [details]
Code snippets

Added attachment with code snippets, things are more clear in editor with no line wrapping and with whitespace displayed.
Comment 2 Christian Dietrich CLA 2020-10-02 04:24:10 EDT
see also https://github.com/eclipse/xtext-xtend/issues/1092