Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(template): Preserve line numbers #468

Merged

Conversation

RiskoZoSlovenska
Copy link
Contributor

@RiskoZoSlovenska RiskoZoSlovenska commented Feb 15, 2024

This PR tweaks the template module to build intermediate template code where instructions have the same line numbers as in the original template.

For example, with the old behaviour, the template

# local foo = 1
a $(foo + 1) b
# bar()

would have resulted in the following intermediate code:

return function()
local __R_size, __R_table, __tostring = 0, {}, __tostring
 local foo = 1
__R_size = __R_size + 1; __R_table[__R_size] = "a "
__R_size = __R_size + 1; __R_table[__R_size] = __tostring((foo + 1) or '')
__R_size = __R_size + 1; __R_table[__R_size] = " b\
"
 bar()
return __R_table
end

If bar() were to produce an error, Lua would report it as occurring on line 8, which is completely unhelpful. However, after this PR, the above template will produce the following code:

return function() local __R_size, __R_table, __tostring = 0, {}, __tostring;  local foo = 1
 __R_size = __R_size + 1; __R_table[__R_size] = "a "; __R_size = __R_size + 1; __R_table[__R_size] = __tostring((foo + 1) or ''); __R_size = __R_size + 1; __R_table[__R_size] = " b\
"; bar()
return __R_table; end

While less readable, any errors will now be reported as occurring on the same line as in the original template, which greatly eases debugging, since it's often easy to pinpoint the source of error just by looking at the right line in the template string.

Essentially, this PR only swaps out some newlines for spaces and semicolons; the actual rendering behaviour of templates should remain unchanged. It also works when the newline option is truthy; stripped newlines aren't removed completely, just moved out of the appender statements and into the intermediate Lua code.

@RiskoZoSlovenska RiskoZoSlovenska force-pushed the template-preserve-line-numbers branch from ff3ecb4 to 63406a0 Compare February 29, 2024 17:22
@RiskoZoSlovenska
Copy link
Contributor Author

CI failures appear to be unrelated and transient

@Tieske Tieske merged commit d87233e into lunarmodules:master Apr 15, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants