From 5641c17faaed10409a476558dde8e15f4f91f9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 5 Jan 2021 15:40:25 +0800 Subject: guess `self` --- changelog.md | 1 + script/parser/guide.lua | 25 +++++++++++++++++++++++++ test/definition/special.lua | 10 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/changelog.md b/changelog.md index 4a290acf..03774edd 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## 1.11.0 * `NEW` `Lua.runtime.plugin` +* `NEW` intelli-scense: improved `m.f = function (self) end` from `self` to `m` * `CHG` performance optimization * `CHG` completion: improve performance of workspace words * `FIX` hover: tail comments may be cutted diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 354a5eed..6ba7e903 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -1506,6 +1506,29 @@ function m.checkSameSimpleInSpecialBranch(status, obj, start, pushQueue) end end +function m.checkSameSimpleInParamSelf(status, obj, start, pushQueue) + if obj.type ~= 'getlocal' or obj[1] ~= 'self' then + return + end + local node = obj.node + if node.tag == 'self' then + return + end + if node.parent.type ~= 'funcargs' then + return + end + local func = node.parent.parent + if func.type ~= 'function' or func.parent.type ~= 'setfield' then + return + end + local fieldNode = func.parent.node + local newStatus = m.status(status) + m.searchRefs(newStatus, fieldNode, 'ref') + for _, ref in ipairs(newStatus.results) do + pushQueue(ref, start, true) + end +end + local function appendValidGenericType(results, status, typeName, obj) if typeName.parent.type == 'doc.type.typeliteral' then if obj.type == 'string' and status.interface.docType then @@ -2415,6 +2438,8 @@ function m.checkSameSimple(status, simple, ref, start, force, mode, pushQueue) m.checkSameSimpleInValueOfCallMetaTable(status, ref, i, pushQueue) -- 检查自己是特殊变量的分支的情况 m.checkSameSimpleInSpecialBranch(status, ref, i, pushQueue) + -- self 的特殊处理 + m.checkSameSimpleInParamSelf(status, ref, i, pushQueue) if cmode == 'ref' then -- 检查形如 { a = f } 的情况 m.checkSameSimpleAsTableField(status, ref, i, pushQueue) diff --git a/test/definition/special.lua b/test/definition/special.lua index 4e3728e3..3f0d076a 100644 --- a/test/definition/special.lua +++ b/test/definition/special.lua @@ -124,3 +124,13 @@ end obj:() ]] + +TEST [[ +local mt = {} + +mt. = 1 + +mt.yy = function (self) + print(self.) +end +]] -- cgit v1.2.3