-
Notifications
You must be signed in to change notification settings - Fork 131
Preserve comments during lexing #1002
8000 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
Conversation
Teal Playground URL: https://1002--teal-playground-preview.netlify.app |
@@ -6,6 +6,30 @@ local function string_trim(s) | |||
return (s:gsub("^%s*(.-)%s*$", "%1")) | |||
end | |||
|
|||
--- removes leading whitespace from every line of a multiline string | |||
local function dedent(s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of copying them around, you can add those utility functions to spec/util.lua, then localize them at the top with local dedent = util.dedent
for brevity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
spec/util.lua
Outdated
end | ||
|
||
for i, line in ipairs(lines) do | ||
lines[i] = line:gsub("^" .. string.rep(" ", min), "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to strip the first min
characters (which you already determined are whitespace), you can just do:
lines[i] = line:gsub("^" .. string.rep(" ", min), "") | |
lines[i] = line:sub(min + 1) |
Also, weird edge case but: if s
was something like "\n"
, wouldn't min
end up staying as math.huge
? Perhaps an if min == math.huge them min = 0 end
before the loop should be in order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Should've caught that, sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens! "Who watches tests the Watchmen test code?" :)
end | ||
|
||
if min == math.huge then | ||
return s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better solution! 👍
Part of tealdoc project. Stores comments inside of
comments
field ofToken
. Always attaches comments to the leading token.