Home » Modeling » Epsilon » EGL undesirable trim on return template operations
EGL undesirable trim on return template operations [message #1847587] |
Tue, 02 November 2021 02:00 |
Samantha Swift Messages: 2 Registered: September 2021 |
Junior Member |
|
|
Hi,
When importing template operations between EGL files, if the operation ends in a newline or whitespace character, the resulting returned string has this newline trimmed. It is desirable to have the operation return the result of the function including the newline characters. Additionally, when the same function is completed within a single file, the desirable output is produced. The trim only appears apply when the operation is imported.
To replicate the bug, the two simple EGL files are as follows
test.egl
[% import "op.egl"; %]
[%= printLine() %]
x
op.egl
[% @template
operation printLine() { %]
[% for (i in Sequence{1..3}) { %]
[% } %]
[% } %]
The expected output would be
However, it produces
-
Attachment: op.egl
(Size: 0.09KB, Downloaded 45 times) -
Attachment: test.egl
(Size: 0.04KB, Downloaded 42 times)
|
|
|
Re: EGL undesirable trim on return template operations [message #1847603 is a reply to message #1847587] |
Tue, 02 November 2021 09:36 |
|
Hi Samantha,
This is definitely not the expected behaviour but as far as I can tell, it is consistent across both imported and in-file template operations. It seems to be boiling down to the following behaviour of String's split method which is somewhat unexpected for me.
System.out.println("\r\n\r\nx".split("\r\n").length); // Prints 3
System.out.println("x\r\n\r\n".split("\r\n").length); // Prints 1
System.out.println("\r\n\r\n".split("\r\n").length); // Prints 0
Java 11's String.lines() seems to be more consistent with the behaviour I'd expect so we'll switch to that one shortly.
In any event this only seems to be affecting output regions ([%=%]) where the emitted content ends with new line characters - which is probably why it has gone unnoticed so far.
Best,
Dimitris
|
|
| | |
Re: EGL undesirable trim on return template operations [message #1847871 is a reply to message #1847749] |
Wed, 10 November 2021 23:28 |
|
Hi Samantha and Jörn,
I've had a closer look into this and if we were to change EGL's behaviour as discussed, then the following template
[%=hello()%]World
[%
@template
operation hello(){%]
Hello
[%}%]
which now produces
will now instead produce
which can potentially break existing EGL generators.
Is the current behaviour a major inconvenience for you or was this an oddity you thought you'd report?
Many thanks,
Dimitris
|
|
|
Re: EGL undesirable trim on return template operations [message #1847872 is a reply to message #1847871] |
Wed, 10 November 2021 23:44 |
|
Hi all,
I am with Dimitris on this one. That change would break a lot of existing code.
My 2 cents: As a "best practice", I tend to make template operations free of any pre/post white space formatting. This makes them easier to reuse in places where the white space is context specific, e.g. level of indentation or separation from other blocks.
Cheers,
Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech
[Updated on: Wed, 10 November 2021 23:50] by Moderator Report message to a moderator
|
|
| |
Re: EGL undesirable trim on return template operations [message #1847874 is a reply to message #1847872] |
Thu, 11 November 2021 00:14 |
|
Our templates are automatically inferred, so we cannot follow the 'best practice'.
We cannot use the existing behaviour inour inference, because it is inconsistent. We are using templates as they are intended to express blocks of templated text. That is why they exist. If we are to use operations, we might as well drop Epsilon templating.
|
|
|
Re: EGL undesirable trim on return template operations [message #1847875 is a reply to message #1847873] |
Thu, 11 November 2021 00:19 |
|
Hi Jörn,
I'm not entirely convinced that this behaviour is accidental as there are actually relevant unit tests (in org.eclipse.epsilon.egl.test.acceptance.operations.template.TemplateOperations) that assert that the expected result does not contain additional new lines, and which broke when I attempted to change to the alternative behaviour. I'm not the original developer of EGL so I can't be entirely sure, but the more I think about it, the more the current behaviour makes sense to me. Under the alternative behaviour to make the hello() template operation not emit the extra new line, one would need to use LaTeX-style tricks such as the following, neither of which looks particularly nice.
[%=hello()%]World
[%
@template
operation hello(){%]
Hello[*
*]
[%}%]
[%=hello()%]World
[%
@template
operation hello(){%]
Hello[%}%]
Best,
Dimitris
|
|
|
Re: EGL undesirable trim on return template operations [message #1847876 is a reply to message #1847875] |
Thu, 11 November 2021 00:34 |
|
If you definitely need the alternative behaviour in your templates, there shouldn't be a need to fork. You should be able to subclass EgxModule and EglFileGeneratingTemplateFactory, and override IEglContext's newOutputBuffer() to return a custom output buffer with an overridden printdyn() implementation that adds a second (-1) argument to split as follows.
@Override
public void printdyn(Object o) {
final String indentation = calculateIndentationToMatch(getLastLineInBuffer());
final String newLine = getNewline();
final String[] lines = StringUtil.toString(o).split(newLine, -1); // <- The line you'd need to change
for (int i = 0; i < lines.length; i++) {
if (i == 0) {
// Any text before the first newline character should not be
// placed on a newline nor indented
buffer.append(lines[i]);
} else {
buffer.append(newLine + indentation + lines[i]);
}
}
Best,
Dimitris
|
|
| | | |
Re: EGL undesirable trim on return template operations [message #1847883 is a reply to message #1847879] |
Thu, 11 November 2021 07:08 |
|
Hi Jörn,
Just to be clear, ignoring the empty new lines has nothing to do with @template operations; the behaviour comes from [%=%] instead. Therefore, if you replace [%=hello()%] with [%out.print(hello());%], the following template
[%out.print(hello());%]
World
[%
@template
operation hello(){%]
Hello
[%}%]
will produce
instead. Does this help?
Best,
Dimitris
|
|
| |
Goto Forum:
Current Time: Sat Apr 27 11:04:05 GMT 2024
Powered by FUDForum. Page generated in 0.03883 seconds
|