summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-10-25 03:11:49 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-10-25 03:11:49 +0800
commit28dc2d3adce8729c90cf8abdaf5eb46efd39f2f1 (patch)
tree46bf83aa9f001f006e3ee42872838bea5a9a0660
parent8f2dac8ba32271e45061128070063e0ddd0d73c4 (diff)
downloadlua-language-server-28dc2d3adce8729c90cf8abdaf5eb46efd39f2f1.zip
`---@private` and `---@protected`
#1316
-rw-r--r--locale/zh-cn/script.lua2
-rw-r--r--script/parser/luadoc.lua22
-rw-r--r--script/vm/visible.lua27
-rw-r--r--test/hover/init.lua100
4 files changed, 146 insertions, 5 deletions
diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua
index a0384e8f..02511e3b 100644
--- a/locale/zh-cn/script.lua
+++ b/locale/zh-cn/script.lua
@@ -147,7 +147,7 @@ DIAG_UNREACHABLE_CODE =
DIAG_INVISIBLE_PRIVATE =
'字段 `{field}` 是私有的,只能在 `{class}` 类中才能访问。'
DIAG_INVISIBLE_PROTECTED =
-'字段 `{field}` 收到保护,只能在 `{class}` 类极其子类中才能访问。'
+'字段 `{field}` 受到保护,只能在 `{class}` 类极其子类中才能访问。'
MWS_NOT_SUPPORT =
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 71579fc4..bd631318 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -149,7 +149,7 @@ Symbol <- ({} {
---@field names? parser.object[]
---@field path? string
---@field bindComments? parser.object[]
----@field visible? 'public' | 'protected' | 'private'
+---@field visible? parser.visibleType
local function parseTokens(text, offset)
Ci = 0
@@ -1445,6 +1445,22 @@ local docSwitch = util.switch()
name.parent = result
return result
end)
+ : case 'private'
+ : call(function ()
+ return {
+ type = 'doc.private',
+ start = getFinish(),
+ finish = getFinish(),
+ }
+ end)
+ : case 'protected'
+ : call(function ()
+ return {
+ type = 'doc.protected',
+ start = getFinish(),
+ finish = getFinish(),
+ }
+ end)
local function convertTokens(doc)
local tp, text = nextToken()
@@ -1660,7 +1676,9 @@ local function bindDoc(source, binded)
or doc.type == 'doc.deprecated'
or doc.type == 'doc.version'
or doc.type == 'doc.module'
- or doc.type == 'doc.source' then
+ or doc.type == 'doc.source'
+ or doc.type == 'doc.private'
+ or doc.type == 'doc.protected' then
if source.type == 'function'
or isParam then
goto CONTINUE
diff --git a/script/vm/visible.lua b/script/vm/visible.lua
index 362bc568..f0b30a22 100644
--- a/script/vm/visible.lua
+++ b/script/vm/visible.lua
@@ -4,11 +4,13 @@ local guide = require 'parser.guide'
local config = require 'config'
local glob = require 'glob'
+---@alias parser.visibleType 'public' | 'protected' | 'private'
+
---@class parser.object
----@field _visibleType? 'public' | 'protected' | 'private'
+---@field _visibleType? parser.visibleType
---@param source parser.object
----@return 'public' | 'protected' | 'private'
+---@return parser.visibleType
function vm.getVisibleType(source)
if source._visibleType then
return source._visibleType
@@ -19,6 +21,20 @@ function vm.getVisibleType(source)
return source.visible
end
end
+
+ if source.bindDocs then
+ for _, doc in ipairs(source.bindDocs) do
+ if doc.type == 'doc.private' then
+ source._visibleType = 'private'
+ return 'private'
+ end
+ if doc.type == 'doc.protected' then
+ source._visibleType = 'protected'
+ return 'protected'
+ end
+ end
+ end
+
local fieldName = guide.getKeyName(source)
local uri = guide.getUri(source)
@@ -44,6 +60,13 @@ function vm.getParentClass(source)
if source.type == 'doc.field' then
return vm.getGlobalNode(source.class)
end
+ if source.type == 'setfield'
+ or source.type == 'setindex'
+ or source.type == 'setmethod'
+ or source.type == 'tablefield'
+ or source.type == 'tableindex' then
+ return vm.getDefinedClass(guide.getUri(source), source.node)
+ end
return nil
end
diff --git a/test/hover/init.lua b/test/hover/init.lua
index 47204e3a..165f62c5 100644
--- a/test/hover/init.lua
+++ b/test/hover/init.lua
@@ -2280,3 +2280,103 @@ local t: B {
z: number,
}
]]
+
+TEST [[
+---@class A
+local mt = {}
+
+---@private
+function mt:init()
+end
+
+---@protected
+function mt:update()
+end
+
+function mt:get()
+end
+
+print(<?mt?>)
+]]
+[[
+local mt: A {
+ get: function,
+ init: function,
+ update: function,
+}
+]]
+
+TEST [[
+---@class A
+local mt = {}
+
+---@private
+function mt:init()
+end
+
+---@protected
+function mt:update()
+end
+
+function mt:get()
+end
+
+---@type A
+local <?obj?>
+]]
+[[
+local obj: A {
+ get: function,
+}
+]]
+
+TEST [[
+---@class A
+local mt = {}
+
+---@private
+function mt:init()
+end
+
+---@protected
+function mt:update()
+end
+
+function mt:get()
+end
+
+---@class B: A
+local <?obj?>
+]]
+[[
+local obj: B {
+ get: function,
+ update: function,
+}
+]]
+
+TEST [[
+---@class A
+local mt = {}
+
+---@private
+function mt:init()
+end
+
+---@protected
+function mt:update()
+end
+
+function mt:get()
+end
+
+---@class B: A
+
+---@type B
+local <?obj?>
+]]
+[[
+local obj: B {
+ get: function,
+}
+]]