diff options
-rw-r--r-- | locale/en-us/setting.lua | 4 | ||||
-rw-r--r-- | locale/pt-br/setting.lua | 4 | ||||
-rw-r--r-- | locale/zh-cn/setting.lua | 4 | ||||
-rw-r--r-- | locale/zh-tw/setting.lua | 4 | ||||
-rw-r--r-- | script/config/template.lua | 4 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 5 | ||||
-rw-r--r-- | script/vm/visible.lua | 29 | ||||
-rw-r--r-- | test/diagnostics/common.lua | 44 |
8 files changed, 94 insertions, 4 deletions
diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua index 2c9cd19d..ea786117 100644 --- a/locale/en-us/setting.lua +++ b/locale/en-us/setting.lua @@ -295,6 +295,10 @@ When checking the type of union type, ignore the `nil` in it. When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. ]] +config.doc.privateName = +'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.' +config.doc.protectedName = +'Treat specific field names as protected, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are protected, witch can only be accessed in the class where the definition is located and its subclasses.' config.diagnostics['unused-local'] = 'Enable unused local variable diagnostics.' config.diagnostics['unused-function'] = diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua index 88f8bd0c..2844d646 100644 --- a/locale/pt-br/setting.lua +++ b/locale/pt-br/setting.lua @@ -295,6 +295,10 @@ When checking the type of union type, ignore the `nil` in it. When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. ]] +config.doc.privateName = -- TODO: need translate! +'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.' +config.doc.protectedName = -- TODO: need translate! +'Treat specific field names as protected, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are protected, witch can only be accessed in the class where the definition is located and its subclasses.' config.diagnostics['unused-local'] = -- TODO: need translate! '未使用的局部变量' config.diagnostics['unused-function'] = -- TODO: need translate! diff --git a/locale/zh-cn/setting.lua b/locale/zh-cn/setting.lua index 93bdc7ff..3f57f995 100644 --- a/locale/zh-cn/setting.lua +++ b/locale/zh-cn/setting.lua @@ -294,6 +294,10 @@ config.type.weakNilCheck = 此设置为 `false` 时,`numer|nil` 类型无法赋给 `number` 类型;为 `true` 是则可以。 ]] +config.doc.privateName = +'将特定名称的字段视为私有,例如 `m_*` 意味着 `XXX.m_id` 与 `XXX.m_type` 是私有字段,只能在定义所在的类中访问。' +config.doc.protectedName = +'将特定名称的字段视为受保护,例如 `m_*` 意味着 `XXX.m_id` 与 `XXX.m_type` 是受保护的字段,只能在定义所在的类极其子类中访问。' config.diagnostics['unused-local'] = '未使用的局部变量' config.diagnostics['unused-function'] = diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua index 5f6e564e..cf0c2a4c 100644 --- a/locale/zh-tw/setting.lua +++ b/locale/zh-tw/setting.lua @@ -294,6 +294,10 @@ When checking the type of union type, ignore the `nil` in it. When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. ]] +config.doc.privateName = -- TODO: need translate! +'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.' +config.doc.protectedName = -- TODO: need translate! +'Treat specific field names as protected, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are protected, witch can only be accessed in the class where the definition is located and its subclasses.' config.diagnostics['unused-local'] = '未使用的區域變數' config.diagnostics['unused-function'] = diff --git a/script/config/template.lua b/script/config/template.lua index c71faa10..dfdfab56 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -307,7 +307,7 @@ local template = { 'Opened', 'Disable', }, - ['Lua.diagnostics.unusedLocalExclude'] = Type.Array(Type.String), + ['Lua.diagnostics.unusedLocalExclude'] = Type.Array(Type.String), ['Lua.workspace.ignoreDir'] = Type.Array(Type.String) >> { '.vscode', }, @@ -390,6 +390,8 @@ local template = { ['Lua.type.castNumberToInteger'] = Type.Boolean >> true, ['Lua.type.weakUnionCheck'] = Type.Boolean >> false, ['Lua.type.weakNilCheck'] = Type.Boolean >> false, + ['Lua.doc.privateName'] = Type.Array(Type.String), + ['Lua.doc.protectedName'] = Type.Array(Type.String), -- VSCode ['files.associations'] = Type.Hash(Type.String, Type.String), diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index 0fac3e17..719ae12a 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -644,8 +644,9 @@ files.watch(function (ev, uri) ---@async end) config.watch(function (uri, key, value, oldValue) - if util.stringStartWith(key, 'Lua.diagnostics') - or util.stringStartWith(key, 'Lua.spell') then + if util.stringStartWith(key, 'Lua.diagnostics') + or util.stringStartWith(key, 'Lua.spell') + or util.stringStartWith(key, 'Lua.doc') then if value ~= oldValue then m.diagnosticsScope(uri) m.refreshClient() diff --git a/script/vm/visible.lua b/script/vm/visible.lua index ec538f24..362bc568 100644 --- a/script/vm/visible.lua +++ b/script/vm/visible.lua @@ -1,13 +1,40 @@ ---@class vm local vm = require 'vm.vm' local guide = require 'parser.guide' +local config = require 'config' +local glob = require 'glob' + +---@class parser.object +---@field _visibleType? 'public' | 'protected' | 'private' ---@param source parser.object ---@return 'public' | 'protected' | 'private' function vm.getVisibleType(source) + if source._visibleType then + return source._visibleType + end if source.type == 'doc.field' then - return source.visible or 'public' + if source.visible then + source._visibleType = source.visible + return source.visible + end + end + local fieldName = guide.getKeyName(source) + local uri = guide.getUri(source) + + local privateNames = config.get(uri, 'Lua.doc.privateName') + if #privateNames > 0 and glob.glob(privateNames)(fieldName) then + source._visibleType = 'private' + return 'private' + end + + local protectedNames = config.get(uri, 'Lua.doc.protectedName') + if #protectedNames > 0 and glob.glob(protectedNames)(fieldName) then + source._visibleType = 'protected' + return 'protected' end + + source._visibleType = 'public' return 'public' end diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index 0231cc86..2ff0cd5f 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -2142,3 +2142,47 @@ local t print(t.z) ]] + +TEST [[ +---@class A +---@field _id number + +---@type A +local t + +print(t._id) +]] + +config.set(nil, 'Lua.doc.privateName', { '_*' }) +TEST [[ +---@class A +---@field _id number + +---@type A +local t + +print(t.<!_id!>) + +---@class B: A +local t2 + +print(t2.<!_id!>) +]] +config.set(nil, 'Lua.doc.privateName', nil) + +config.set(nil, 'Lua.doc.protectedName', { '_*' }) +TEST [[ +---@class A +---@field _id number + +---@type A +local t + +print(t.<!_id!>) + +---@class B: A +local t2 + +print(t2._id) +]] +config.set(nil, 'Lua.doc.protectedName', nil) |