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

added lua regular expression support for Lua.doc.<scope>Name #2753

Merged
merged 4 commits into from
Jul 15, 2024

Conversation

NeOzay
Copy link
Contributor

@NeOzay NeOzay commented Jul 12, 2024

hi,
this PR add support for lua regular expressions for parameters:

  • Lua.doc.privateName
  • Lua.doc.packageName
  • Lua.doc.protectedName

it also adds a new Lua.doc.regengine (the name can be changed) parameter, which can be set to glob(default) or lua

{
  "Lua.doc.protectedName": [
	  "^_[%w_]*_$"
  ],
  "Lua.doc.privateName": [
	  "^_[%w_]*%w$"
  ],
  "Lua.doc.regengine": "lua",
}
---@class A
local A = {}
A._id_ = 0 --  is protected
A._user = "bob" -- is private

---@type A
local t
print(t._id_) -- warning
print(t._user) -- warning

---@class B: A
local t2
print(t2._id_) 
print(t2._user) -- warning

@tomlau10
Copy link
Contributor

tomlau10 commented Jul 13, 2024

I don't think there is a need to add support for lua regex for Lua.doc.<scope> 😕
Currently it is using a custom syntax defined by LPeg.
Though I am not familiar with it, with a bit of testing, your proposed examples can be expressed by:

{
    "doc.protectedName": [
        "_*[a-zA-Z0-9]*_"   // prefix with at least 1 _, contains at least 1 char, then end with _
    ],
    "doc.privateName": [
        "_*",   // prefix with at least 1 _, followed by anything
        "!*_"   // but must not end with _
    ]
}
---@class A
local A = {}
A._id_ = 0      -- protected
A._id_2_ = 0    -- protected
A.__a_ = 0      -- protected
A._user = "bob" -- private
A._id_3 = 1     -- private
A.__b = 0       -- private
A.test = 1      -- normal
A._ = 1         -- normal
A.__ = 1        -- normal

---@type A
local t = {}
print(t._id_)   -- warning protected
print(t._id_2_) -- warning protected
print(t.__a_)   -- warning protected
print(t._user)  -- warning private
print(t._id_3)  -- warning private
print(t.__b)    -- warning private
print(t.test)   -- ok
print(t._)      -- ok
print(t.__)     -- ok

---@class B: A
local t2 = {}
print(t2._id_)  -- ok
print(t2._id_2_)-- ok
print(t2.__a_)  -- ok
print(t2._user) -- warning private
print(t2._id_3) -- warning private
print(t.__b)    -- warning private
print(t2.test)  -- ok
print(t2._)     -- ok
print(t2.__)    -- ok

I found that you have asked this question before in #2324, but seems that the reply doesn't actually answer your question directly 😂

@tomlau10
Copy link
Contributor

tomlau10 commented Jul 13, 2024

(off topic)
I just found a bug (?) related to the glob pattern for single range char set matching.
eg _[a-z] works but _[aeiou] is not working 😕
I have opened an issue #2754 for that.


edit: With more testing of the custom built-in glob pattern, its function seems to be somewhat limited and buggy 😕
So this justifies the need of a lua regular expression support 👍

Comment on lines 45 to 56
local regengine = config.get(uri, 'Lua.doc.regengine')
local function match(patterns)
if regengine == "glob" then
return glob.glob(patterns)(fieldName)
else
for i = 1, #patterns do
if string.find(fieldName, patterns[i]) then
return true
end
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should be declared in the outer scope. Otherwise each call to getVisibleType() will create an unnecessary closure function object, and adds burden to garbage collection.

Maybe something like this:

  • outer scope
local function globMatch(patterns, fieldName)
    return glob.glob(patterns)(fieldName)
end

local function luaMatch(patterns, fieldName)
    for i = 1, #patterns do
        if string.find(fieldName, patterns[i]) then
            return true
        end
    end
    return false
end
  • here
        local regengine = config.get(uri, 'Lua.doc.regengine')
        local match = regengine  == "glob" and globMatch or luaMatch
        ...
        if #privateNames > 0 and match(privateNames, fieldName) then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the advice. :)

@sumneko sumneko merged commit 3d88f33 into LuaLS:master Jul 15, 2024
10 of 11 checks passed
@sumneko
Copy link
Collaborator

sumneko commented Jul 15, 2024

Thank you!

@NeOzay NeOzay deleted the luaReg branch July 15, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants