LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.23k stars 302 forks source link

The `set` pattern for `single range char` is not working in glob #2754

Open tomlau10 opened 1 month ago

tomlau10 commented 1 month ago

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

I am testing the glob like pattern used in Lua.doc.<scope>Name, and the set pattern seems not working for single character.

---@type A local t = {} print(t._A) -- warning print(t._Z) -- warning print(t._a) -- warning print(t._e) -- warning print(t._z) -- ok print(t._AA) -- ok print(t._aa) -- ok


### Actual Behaviour

```lua
---@type A
local t = {}
print(t._A)     -- warning
print(t._Z)     -- warning
print(t._a)     -- ok (false negative)
print(t._e)     -- ok (false negative)
print(t._z)     -- ok
print(t._AA)    -- ok
print(t._aa)    -- ok

Reproduction steps

Use the provided snippet

Additional Notes

I know that the glob pattern syntax in defined using LPeg: https://github.com/LuaLS/lua-language-server/blob/ddc96bd1ec0be95a985ab82531763578bf7eb793/script/glob/glob.lua#L19-L46 I am not familiar with LPeg, but by adding a print(#range, range[1], range[2]) inside mt:range() here, those single range word seems don't even get parsed. 😕 https://github.com/LuaLS/lua-language-server/blob/ddc96bd1ec0be95a985ab82531763578bf7eb793/script/glob/matcher.lua#L99-L100

With a bit of testing, the RangeUnit definition seems should be changed from:

    ['RangeUnit']   = m.Ct(m.C(m.V'RangeWord') * m.P'-' * m.C(m.V'RangeWord'))
                    + m.V'RangeWord',

to =>

    ['RangeUnit']   = m.Ct(m.C(m.V'RangeWord') * m.P'-' * m.C(m.V'RangeWord'))
                    + m.Ct(m.C(m.V'RangeWord')),

Then the set logic for single range character starts to work 🎉 But I don't know why it works this way 🙈


Can anyone comment on my above suggested change? If this is correct, I am going to open a PR. 🙂

Log File

No response

tomlau10 commented 1 month ago

More issues found 😕

{
    "doc.privateName": [
        "_*[a-zA-Z0-9]"     // prefix with at least 1 _, followed by anything, then end with a char
    ]
}

---@type A local t = {} print(t._a) -- warning print(t._a1) -- ok (false negative) print(t.a) -- warning print(t.a1) -- ok (false negative)



---
By testing the glob pattern here, all of the above should be matched:
https://www.digitalocean.com/community/tools/glob?comments=true&glob=_%2A%5Ba-zA-Z0-9%5D&matches=false&tests=_a&tests=_a1&tests=__a&tests=__a1
sumneko commented 1 week ago

Perhaps I should go and find a third-party library.