Skip to content

Commit 3996e19

Browse files
committed
1 parent 8c5dacd commit 3996e19

File tree

7 files changed

+98
-15
lines changed

7 files changed

+98
-15
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# changelog
22

33
## 3.1.0
4+
* `NEW` support find definition in method
45
* `CHG` hint: move to LSP. Its font is now controlled by the client.
56
* `CHG` hover: split `local` into `local` / `parameter` / `upvalue` / `self`.
67
* `CHG` hover: added parentheses to some words, such as `global` / `field` / `class`.

script/vm/global-manager.lua

+50
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local guide = require 'parser.guide'
33
local globalBuilder = require 'vm.global'
44
local signMgr = require 'vm.sign'
55
local genericMgr = require 'vm.generic'
6+
local localID = require 'vm.local-id'
67
---@class vm
78
local vm = require 'vm.vm'
89

@@ -317,6 +318,43 @@ function m.compileObject(source)
317318
compilerGlobalSwitch(source.type, source)
318319
end
319320

321+
---@param source parser.object
322+
function m.compileSelf(source)
323+
if source.parent.type ~= 'funcargs' then
324+
return
325+
end
326+
---@type parser.object
327+
local node = source.parent.parent and source.parent.parent.parent and source.parent.parent.parent.node
328+
if not node then
329+
return
330+
end
331+
local fields = localID.getFields(source)
332+
if not fields then
333+
return
334+
end
335+
local nodeLocalID = localID.getID(node)
336+
local globalNode = node._globalNode
337+
if not nodeLocalID and not globalNode then
338+
return
339+
end
340+
for _, field in ipairs(fields) do
341+
if field.type == 'setfield' then
342+
local key = guide.getKeyName(field)
343+
if key then
344+
if nodeLocalID then
345+
local myID = nodeLocalID .. vm.ID_SPLITE .. key
346+
localID.insertLocalID(myID, field)
347+
end
348+
if globalNode then
349+
local myID = globalNode:getName() .. vm.ID_SPLITE .. key
350+
local myGlobal = m.declareGlobal('variable', myID, guide.getUri(node))
351+
myGlobal:addSet(guide.getUri(node), field)
352+
end
353+
end
354+
end
355+
end
356+
end
357+
320358
---@param source parser.object
321359
function m.compileAst(source)
322360
local env = guide.getENV(source)
@@ -335,6 +373,18 @@ function m.compileAst(source)
335373
}, function (src)
336374
m.compileObject(src)
337375
end)
376+
377+
--[[
378+
local mt
379+
function mt:xxx()
380+
self.a = 1
381+
end
382+
383+
mt.a --> find this definition
384+
]]
385+
guide.eachSourceType(source, 'self', function (src)
386+
m.compileSelf(src)
387+
end)
338388
end
339389

340390
---@return vm.global

script/vm/global.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
local util = require 'utility'
2-
local scope= require 'workspace.scope'
1+
local util = require 'utility'
2+
local scope = require 'workspace.scope'
3+
local vm = require 'vm.vm'
34

45
---@class vm.global.link
56
---@field gets parser.object[]
@@ -15,8 +16,6 @@ mt.__index = mt
1516
mt.type = 'global'
1617
mt.name = ''
1718

18-
local ID_SPLITE = '\x1F'
19-
2019
---@param uri uri
2120
---@param source parser.object
2221
function mt:addSet(uri, source)
@@ -106,7 +105,7 @@ end
106105

107106
---@return string
108107
function mt:getKeyName()
109-
return self.name:match('[^' .. ID_SPLITE .. ']+$')
108+
return self.name:match('[^' .. vm.ID_SPLITE .. ']+$')
110109
end
111110

112111
---@return boolean

script/vm/local-id.lua

+15-7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ local leftSwitch = util.switch()
9898
return source.node
9999
end)
100100
: case 'local'
101+
: case 'self'
101102
: call(function (source)
102103
return source
103104
end)
@@ -108,6 +109,17 @@ function m.getLocal(source)
108109
return leftSwitch(source.type, source)
109110
end
110111

112+
---@param id string
113+
---@param source parser.object
114+
function m.insertLocalID(id, source)
115+
local root = guide.getRoot(source)
116+
if not root._localIDs then
117+
root._localIDs = util.multiTable(2)
118+
end
119+
local sources = root._localIDs[id]
120+
sources[#sources+1] = source
121+
end
122+
111123
function m.compileLocalID(source)
112124
if not source then
113125
return
@@ -117,15 +129,11 @@ function m.compileLocalID(source)
117129
return
118130
end
119131
compileSwitch(source.type, source)
120-
if not source._localID then
132+
local id = source._localID
133+
if not id then
121134
return
122135
end
123-
local root = guide.getRoot(source)
124-
if not root._localIDs then
125-
root._localIDs = util.multiTable(2)
126-
end
127-
local sources = root._localIDs[source._localID]
128-
sources[#sources+1] = source
136+
m.insertLocalID(id, source)
129137
end
130138

131139
---@param source parser.object

script/vm/node.lua

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local files = require 'files'
2-
local localMgr = require 'vm.local-manager'
32
---@class vm
43
local vm = require 'vm.vm'
54
local ws = require 'workspace.workspace'

test/definition/method.lua

+26
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,29 @@ end
2929
function mt:<!m4!>()
3030
end
3131
]]
32+
33+
TEST [[
34+
local mt
35+
36+
function mt:f()
37+
self.<!x!> = 1
38+
end
39+
40+
mt.<?x?>
41+
]]
42+
43+
TEST [[
44+
function G:f()
45+
self.<!x!> = 1
46+
end
47+
48+
G.<?x?>
49+
]]
50+
51+
TEST [[
52+
function G.H:f()
53+
self.<!x!> = 1
54+
end
55+
56+
G.H.<?x?>
57+
]]

test/diagnostics/common.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ local mt2 = {}
792792
---@type Foo
793793
local v
794794
print(v.field1 + 1)
795-
print(v.<!field2!> + 1)
795+
print(v.field2 + 1)
796796
print(v.<!field3!> + 1)
797797
print(v:method1())
798798
print(v.method2())
@@ -801,7 +801,7 @@ print(v:<!method3!>())
801801
---@type Bar
802802
local v2
803803
print(v2.field1 + 1)
804-
print(v2.<!field2!> + 1)
804+
print(v2.field2 + 1)
805805
print(v2.<!field3!> + 1)
806806
print(v2.field4 + 1)
807807
print(v2:method1())

0 commit comments

Comments
 (0)