Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 16, 2022
1 parent 8c5dacd commit 3996e19
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# changelog

## 3.1.0
* `NEW` support find definition in method
* `CHG` hint: move to LSP. Its font is now controlled by the client.
* `CHG` hover: split `local` into `local` / `parameter` / `upvalue` / `self`.
* `CHG` hover: added parentheses to some words, such as `global` / `field` / `class`.
Expand Down
50 changes: 50 additions & 0 deletions script/vm/global-manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local guide = require 'parser.guide'
local globalBuilder = require 'vm.global'
local signMgr = require 'vm.sign'
local genericMgr = require 'vm.generic'
local localID = require 'vm.local-id'
---@class vm
local vm = require 'vm.vm'

Expand Down Expand Up @@ -317,6 +318,43 @@ function m.compileObject(source)
compilerGlobalSwitch(source.type, source)
end

---@param source parser.object
function m.compileSelf(source)
if source.parent.type ~= 'funcargs' then
return
end
---@type parser.object
local node = source.parent.parent and source.parent.parent.parent and source.parent.parent.parent.node
if not node then
return
end
local fields = localID.getFields(source)
if not fields then
return
end
local nodeLocalID = localID.getID(node)
local globalNode = node._globalNode
if not nodeLocalID and not globalNode then
return
end
for _, field in ipairs(fields) do
if field.type == 'setfield' then
local key = guide.getKeyName(field)
if key then
if nodeLocalID then
local myID = nodeLocalID .. vm.ID_SPLITE .. key
localID.insertLocalID(myID, field)
end
if globalNode then
local myID = globalNode:getName() .. vm.ID_SPLITE .. key
local myGlobal = m.declareGlobal('variable', myID, guide.getUri(node))
myGlobal:addSet(guide.getUri(node), field)
end
end
end
end
end

---@param source parser.object
function m.compileAst(source)
local env = guide.getENV(source)
Expand All @@ -335,6 +373,18 @@ function m.compileAst(source)
}, function (src)
m.compileObject(src)
end)

--[[
local mt
function mt:xxx()
self.a = 1
end
mt.a --> find this definition
]]
guide.eachSourceType(source, 'self', function (src)
m.compileSelf(src)
end)
end

---@return vm.global
Expand Down
9 changes: 4 additions & 5 deletions script/vm/global.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local util = require 'utility'
local scope= require 'workspace.scope'
local util = require 'utility'
local scope = require 'workspace.scope'
local vm = require 'vm.vm'

---@class vm.global.link
---@field gets parser.object[]
Expand All @@ -15,8 +16,6 @@ mt.__index = mt
mt.type = 'global'
mt.name = ''

local ID_SPLITE = '\x1F'

---@param uri uri
---@param source parser.object
function mt:addSet(uri, source)
Expand Down Expand Up @@ -106,7 +105,7 @@ end

---@return string
function mt:getKeyName()
return self.name:match('[^' .. ID_SPLITE .. ']+$')
return self.name:match('[^' .. vm.ID_SPLITE .. ']+$')
end

---@return boolean
Expand Down
22 changes: 15 additions & 7 deletions script/vm/local-id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ local leftSwitch = util.switch()
return source.node
end)
: case 'local'
: case 'self'
: call(function (source)
return source
end)
Expand All @@ -108,6 +109,17 @@ function m.getLocal(source)
return leftSwitch(source.type, source)
end

---@param id string
---@param source parser.object
function m.insertLocalID(id, source)
local root = guide.getRoot(source)
if not root._localIDs then
root._localIDs = util.multiTable(2)
end
local sources = root._localIDs[id]
sources[#sources+1] = source
end

function m.compileLocalID(source)
if not source then
return
Expand All @@ -117,15 +129,11 @@ function m.compileLocalID(source)
return
end
compileSwitch(source.type, source)
if not source._localID then
local id = source._localID
if not id then
return
end
local root = guide.getRoot(source)
if not root._localIDs then
root._localIDs = util.multiTable(2)
end
local sources = root._localIDs[source._localID]
sources[#sources+1] = source
m.insertLocalID(id, source)
end

---@param source parser.object
Expand Down
1 change: 0 additions & 1 deletion script/vm/node.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local files = require 'files'
local localMgr = require 'vm.local-manager'
---@class vm
local vm = require 'vm.vm'
local ws = require 'workspace.workspace'
Expand Down
26 changes: 26 additions & 0 deletions test/definition/method.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ end
function mt:<!m4!>()
end
]]

TEST [[
local mt
function mt:f()
self.<!x!> = 1
end
mt.<?x?>
]]

TEST [[
function G:f()
self.<!x!> = 1
end
G.<?x?>
]]

TEST [[
function G.H:f()
self.<!x!> = 1
end
G.H.<?x?>
]]
4 changes: 2 additions & 2 deletions test/diagnostics/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ local mt2 = {}
---@type Foo
local v
print(v.field1 + 1)
print(v.<!field2!> + 1)
print(v.field2 + 1)
print(v.<!field3!> + 1)
print(v:method1())
print(v.method2())
Expand All @@ -801,7 +801,7 @@ print(v:<!method3!>())
---@type Bar
local v2
print(v2.field1 + 1)
print(v2.<!field2!> + 1)
print(v2.field2 + 1)
print(v2.<!field3!> + 1)
print(v2.field4 + 1)
print(v2:method1())
Expand Down

0 comments on commit 3996e19

Please sign in to comment.