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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* `NEW` Add support for lambda style functions, `|paramList| expr` is syntactic sugar for `function(paramList) return expr end`
* `FIX` Respect `completion.showParams` config for local function completion
* `CHG` Improve performance of multithreaded `--check` and `undefined-field` diagnostic
* `NEW` added lua regular expression support for Lua.doc.<scope>Name [#2753](https://github.com/LuaLS/lua-language-server/pull/2753)


## 3.9.3
`2024-6-11`
Expand Down
5 changes: 4 additions & 1 deletion script/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ local template = {
['Lua.doc.privateName'] = Type.Array(Type.String),
['Lua.doc.protectedName'] = Type.Array(Type.String),
['Lua.doc.packageName'] = Type.Array(Type.String),

['Lua.doc.regengine'] = Type.String >> 'glob' << {
'glob',
'lua',
},
-- VSCode
["Lua.addonManager.enable"] = Type.Boolean >> true,
['files.associations'] = Type.Hash(Type.String, Type.String),
Expand Down
22 changes: 17 additions & 5 deletions script/vm/visible.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,33 @@ local function getVisibleType(source)

if type(fieldName) == 'string' then
local uri = guide.getUri(source)
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. :)


local privateNames = config.get(uri, 'Lua.doc.privateName')
if #privateNames > 0 and glob.glob(privateNames)(fieldName) then
if #privateNames > 0 and match(privateNames) then
source._visibleType = 'private'
return 'private'
end

local protectedNames = config.get(uri, 'Lua.doc.protectedName')
if #protectedNames > 0 and glob.glob(protectedNames)(fieldName) then
if #protectedNames > 0 and match(protectedNames) then
source._visibleType = 'protected'
return 'protected'
end

local packageNames = config.get(uri, 'Lua.doc.packageName')
if #packageNames > 0 and glob.glob(packageNames)(fieldName) then
if #packageNames > 0 and match(packageNames) then
source._visibleType = 'package'
return 'package'
end
Expand Down
22 changes: 22 additions & 0 deletions test/diagnostics/invisible.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ print(t2._id)
]]
config.set(nil, 'Lua.doc.protectedName', nil)

config.set(nil, 'Lua.doc.regengine', 'lua' )
config.set(nil, 'Lua.doc.privateName', { '^_[%w_]*%w$' })
config.set(nil, 'Lua.doc.protectedName', { '^_[%w_]*_$' })
TEST [[
---@class A
---@field _id_ number
---@field _user number

---@type A
local t
print(t.<!_id_!>)
print(t.<!_user!>)

---@class B: A
local t2
print(t2._id_)
print(t2.<!_user!>)
]]
config.set(nil, 'Lua.doc.privateName', nil)
config.set(nil, 'Lua.doc.protectedName', nil)
config.set(nil, 'Lua.doc.regengine', nil )

TEST [[
---@class A
---@field private x number
Expand Down
Loading