-
Notifications
You must be signed in to change notification settings - Fork 31k
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
Auto indent issue: Lua multiline quotes [[...]]
conflict with #210641 which removes brackets before passing text to indent rules
#223606
Comments
Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.91.1. Please try upgrading to the latest version and checking whether this issue remains. Happy Coding! |
[[...]]
does not goes well with #210641 which removes brackets before passing text to indent rules[[...]]
conflict with #210641 which removes brackets before passing text to indent rules
After a second thought, removing all content including the quotes seems NOT viable 🤔
if"true"then
|
Hi, I think I come up with a good solution 😄
My proposed changeI believe the related code logic resides here: vscode/src/vs/editor/common/languages/supports/indentationLineProcessor.ts Lines 214 to 219 in 5b5e760
The simple change will be something like this: let hasOtherTokenType = false;
tokens.forEach((tokenIndex: number) => {
const tokenType = tokens.getStandardTokenType(tokenIndex);
hasOtherTokenType = hasOtherTokenType || !shouldRemoveBracketsFromTokenType(tokenType);
})
tokens.forEach((tokenIndex: number) => {
const tokenType = tokens.getStandardTokenType(tokenIndex);
let text = tokens.getTokenText(tokenIndex);
if (shouldRemoveBracketsFromTokenType(tokenType)) {
if (hasOtherTokenType) {
text = " "; // if this line contents other token type, we replace the whole content with single space
} else {
text = text.replace(bracketsRegExp, '');
}
}
Expected ResultI tested this change locally, and this solves #199223 directly, along with all the test cases that I defined there. 🎉 Please would @aiday-mar consider this with the team when you have time 🙏 |
Does this issue occur when all extensions are disabled?: Yes
I am working on improving Lua's auto indentation rules to fix keywords inside string literal will trigger false positive as described in #199223. If interested, you can see my progress in that issue. However I found another problem during my development.
My regex should be able to handle this case:
then
is not followed by pure trailing space / comment => should not be indentedRoot Cause Analysis
After some searching, I found that a while ago the issue #209862 proposed to remove brackets from string or comments before passing to the regex module, which fix auto indent issue related to bracket detection. And it is implemented and released in PR #210641.
I see some discussion going on here #209519 (comment), and I understand the rationale behind. Nonetheless Lua has a multiline quote
[[...]]
which uses the bracket character, and it can even be nested[==[...]==]
.ref: http://lua-users.org/wiki/StringsTutorial
I believe that the current logic of removing brackets from string or comments is to detect a string token type (in the above case is the☹️
[[then]]
) and then remove all[
/]
from it (as they are defined as bracket in Lua's language config). But then after striping the surrounding[[]]
, thethen
token stands alone. And my regex have no way to deal with this case, which will cause a false positiveMore example
To make things worse, Lua also has a multiline comment
--[[...]]
that can also be written in single line. 😂Take the following as example:
[[
and]]
are stripped, the text becomesif s == --then then
then
is after a--
commentQuick thought
I don't have any bright idea right now. By just by reading the discussion mentioned above, I guess maybe
removing all contents in string / comment, including the quotes / comment prefix
would be a better solution? Because indentation rule generally will not depends on the content inside string / comment? 🤔Using the examples above:
if s == [[then]]
=> becomesif s ==
=> no more false positive ✅if s == --[[then]] then
=> becomesif s == then
=> again just check for\bthen\b\s*$
is enough to match this ✅The text was updated successfully, but these errors were encountered: