summaryrefslogtreecommitdiff
path: root/script/core/rename.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
commit4b085b8aea5f33ec114baa31d2b9d72341383c32 (patch)
treefe35a326408e762711a31d3e803464f0c1a8468d /script/core/rename.lua
parent0c8c6bbf23082d0b858646846a47a3001f718ae2 (diff)
parent35ce57976db3b4c42193279dd55972ea013fecad (diff)
downloadlua-language-server-4b085b8aea5f33ec114baa31d2b9d72341383c32.zip
Merge branch 'newparser'
Diffstat (limited to 'script/core/rename.lua')
-rw-r--r--script/core/rename.lua25
1 files changed, 15 insertions, 10 deletions
diff --git a/script/core/rename.lua b/script/core/rename.lua
index 0ab7a055..0c48dbc4 100644
--- a/script/core/rename.lua
+++ b/script/core/rename.lua
@@ -36,12 +36,12 @@ local function isValidFunctionName(str)
if isValidGlobal(str) then
return true
end
- local pos = str:find(':', 1, true)
- if not pos then
+ local offset = str:find(':', 1, true)
+ if not offset then
return false
end
- return isValidGlobal(trim(str:sub(1, pos-1)))
- and isValidName(trim(str:sub(pos+1)))
+ return isValidGlobal(trim(str:sub(1, offset-1)))
+ and isValidName(trim(str:sub(offset+1)))
end
local function isFunctionGlobalName(source)
@@ -81,21 +81,26 @@ local function renameField(source, newname, callback)
elseif parent.type == 'getmethod' then
callback(source, source.start, source.finish, newname)
elseif parent.type == 'setmethod' then
- local uri = guide.getUri(source)
- local text = files.getText(uri)
+ local uri = guide.getUri(source)
+ local text = files.getText(uri)
+ local state = files.getState(uri)
local func = parent.value
-- function mt:name () end --> mt['newname'] = function (self) end
+ local startOffset = guide.positionToOffset(state, parent.start) + 1
+ local finishOffset = guide.positionToOffset(state, parent.node.finish)
local newstr = string.format('%s[%s] = function '
- , text:sub(parent.start, parent.node.finish)
+ , text:sub(startOffset, finishOffset)
, util.viewString(newname)
)
callback(source, func.start, parent.finish, newstr)
- local pl = text:find('(', parent.finish, true)
+ local finishOffset = guide.positionToOffset(state, parent.finish)
+ local pl = text:find('(', finishOffset, true)
if pl then
+ local insertPos = guide.offsetToPosition(state, pl)
if text:find('^%s*%)', pl + 1) then
- callback(source, pl + 1, pl, 'self')
+ callback(source, insertPos, insertPos, 'self')
else
- callback(source, pl + 1, pl, 'self, ')
+ callback(source, insertPos, insertPos, 'self, ')
end
end
end