Support advanced multiline strings in lua e.g. --[=[ something ]=] #203
Replies: 13 comments
-
Honestly, when I was searching through Currently, I also don't know how to solve this but this is certainly going to be fun to implement. Would you mind sharing any kind of guide where I can read more about those comments? |
Beta Was this translation helpful? Give feedback.
-
It's actually a
|
Beta Was this translation helpful? Give feedback.
-
Interesting I'll try to implement this. Once I handled some edge cases in the current logic. I'll update here once I start working on it :) |
Beta Was this translation helpful? Give feedback.
-
Hi there, I'm a big fan of this plugin and would very much like to have this as a feature; far too many times I've decided to comment out a few lines just to find that some nested comment breaks compatibility. I don't really know much about the internal code structure, but I'm willing to try my hand at making this a reality. AFAIK Lua patterns aren't PCRE, but I was playing around with the Tree-sitter playground and think that if you converted a comment to a single string, you could pattern-match it like the following: -- Extract the comment text from a Tree-sitter node
local comment = "--[[ some comment\nmore text\n ]=]"
-- Match it and capture the amount of equal signs
local ok, _, open, close = comment:find("^--%[(=*)%[.-%](=*)%]$")
-- Check if a match is found, and if the equals signs are "balanced"
if ok and (#open == #close) then
local count = #open + 1
local comment = {
"--[" .. ("="):rep(count) .. "[",
"]" .. ("="):rep(count) .. "]",
}
end Obviously it would be a lot more involved, but hopefully somebody could give me a few pointers around the codebase? |
Beta Was this translation helpful? Give feedback.
-
The main reason that I didn't make the commenstring, inside the plugin, regex based because I need compatibility and ability to fallback to I think, the only way to pull this off is using |
Beta Was this translation helpful? Give feedback.
-
I don't think I'm quite following why having |
Beta Was this translation helpful? Give feedback.
-
I was wrong about the length and And I believe your, earlier, snippet doesn't consider the initial commenting.
Nope, not at all. On the contrary, i quite enjoy a healthy discussion. |
Beta Was this translation helpful? Give feedback.
-
Oh wow this is a bit similar to the internals for my plugin haha. I'm afraid I still don't quite get why the left/right part has to be static in order to comment the line; the code snippet you sent seems like it would work just fine if the left/right were different, since you're just appending strings to the beginning/end of a table. |
Beta Was this translation helpful? Give feedback.
-
I don't think so, let me ask you how would you append a pattern i.e "^--%[(=*)%.-%%]$" to a string/line? Just curious. |
Beta Was this translation helpful? Give feedback.
-
Ah, let me try and rephrase what my initial idea was:
Sorry if I'm still misunderstanding the underlying code structure (and so over-simplifying the problem), but this is just one of the ideas that I thought of |
Beta Was this translation helpful? Give feedback.
-
I am assuming you are talking about treesitter for nodes calculation; Inside the plugin treesitter is only used for calculating embedded language and this sounds way more work. And about the pattern, I have to store the pattern and I have to remind myself of the use-case. So TBH, everything sounds complicated and requires more maintenance on my end (for something that I probably never use). Anyway, here is a brief architecture of the plugin
Edit: I hope this will give you an idea of how things are implemented inside the plugin :) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the high-level description! Yeah it does seem like quite a bit of extra work, especially because it seems like this would need to be "hard-coded" to a certain extent for Lua in particular. I can't really think of any other programming languages at the moment that have this sort of behavior. Your point about code maintenance makes sense; thanks for taking the time to explain your thought processes and for such a great plugin! |
Beta Was this translation helpful? Give feedback.
-
I am moving this from issues to discussion for other people to come up with solution for this. As I think it might be not feasible to support this natively. |
Beta Was this translation helpful? Give feedback.
-
Just from reading the readme it sounds like this might be possible using the
pre_hook
, but I'm not sure exactly how I'd go about it.I have copied the text below from my kommentary request for the same functionality (b3nj5m1n/kommentary#67)
Lua supports semi-arbitrary multiline comment strings in the form of matching numbers of equals signs. Meaning
--[==[
,--[===[
, and--[=[
will only end the comment when it encounters]==]
,]===]
, and]=]
respectively.It would be a great idea to match an opening regex like
--\[\(=*\)\[
(or--\[(=*)\[
in PCRE style just to make sure it's clear) to the closing]\1]
(using the capture group from the opening regex to guarantee the same number of=
)This may not be as important when creating the comments, but is important to effectively uncomment them.
Beta Was this translation helpful? Give feedback.
All reactions