summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/highlight.lua40
-rw-r--r--script/core/rename.lua25
-rw-r--r--script/parser/guide.lua1
-rw-r--r--script/parser/newparser.lua16
-rw-r--r--test/rename/init.lua17
5 files changed, 56 insertions, 43 deletions
diff --git a/script/core/highlight.lua b/script/core/highlight.lua
index 47b482d5..02f3c07f 100644
--- a/script/core/highlight.lua
+++ b/script/core/highlight.lua
@@ -54,12 +54,12 @@ local function find(source, uri, callback)
end
end
-local function checkInIf(source, text, offset)
+local function checkInIf(state, source, text, position)
-- 检查 end
- local endA = source.finish - #'end' + 1
- local endB = source.finish
- if offset >= endA
- and offset <= endB
+ local endB = guide.positionToOffset(state, source.finish)
+ local endA = endB - #'end' + 1
+ if position >= source.finish - #'end'
+ and position <= source.finish
and text:sub(endA, endB) == 'end' then
return true
end
@@ -68,7 +68,7 @@ local function checkInIf(source, text, offset)
for i = 1, #block.keyword, 2 do
local start = block.keyword[i]
local finish = block.keyword[i+1]
- if offset >= start and offset <= finish then
+ if position >= start and position <= finish then
return true
end
end
@@ -76,12 +76,12 @@ local function checkInIf(source, text, offset)
return false
end
-local function makeIf(source, text, callback)
+local function makeIf(state, source, text, callback)
-- end
- local endA = source.finish - #'end' + 1
- local endB = source.finish
+ local endB = guide.positionToOffset(state, source.finish)
+ local endA = endB - #'end' + 1
if text:sub(endA, endB) == 'end' then
- callback(endA, endB)
+ callback(source.finish - #'end', source.finish)
end
-- 每个子模块
for _, block in ipairs(source) do
@@ -94,8 +94,8 @@ local function makeIf(source, text, callback)
return false
end
-local function findKeyWord(ast, text, offset, callback)
- guide.eachSourceContain(ast.ast, offset, function (source)
+local function findKeyWord(state, text, position, callback)
+ guide.eachSourceContain(state.ast, position, function (source)
if source.type == 'do'
or source.type == 'function'
or source.type == 'loop'
@@ -106,7 +106,7 @@ local function findKeyWord(ast, text, offset, callback)
for i = 1, #source.keyword, 2 do
local start = source.keyword[i]
local finish = source.keyword[i+1]
- if offset >= start and offset <= finish then
+ if position >= start and position <= finish then
ok = true
break
end
@@ -119,9 +119,9 @@ local function findKeyWord(ast, text, offset, callback)
end
end
elseif source.type == 'if' then
- local ok = checkInIf(source, text, offset)
+ local ok = checkInIf(state, source, text, position)
if ok then
- makeIf(source, text, callback)
+ makeIf(state, source, text, callback)
end
end
end)
@@ -238,15 +238,15 @@ local function isLiteralValue(source)
end
return function (uri, offset)
- local ast = files.getState(uri)
- if not ast then
+ local state = files.getState(uri)
+ if not state then
return nil
end
local text = files.getText(uri)
local results = {}
local mark = {}
- local source = findSource(ast, offset, accept)
+ local source = findSource(state, offset, accept)
if source then
local isGlobal = guide.isGlobal(source)
local isLiteral = isLiteralValue(source)
@@ -344,7 +344,7 @@ return function (uri, offset)
end)
end
- findKeyWord(ast, text, offset, function (start, finish)
+ findKeyWord(state, text, offset, function (start, finish)
results[#results+1] = {
start = start,
finish = finish,
@@ -352,7 +352,7 @@ return function (uri, offset)
}
end)
- checkRegion(ast, text, offset, function (start, finish)
+ checkRegion(state, text, offset, function (start, finish)
results[#results+1] = {
start = start,
finish = finish,
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
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index cea56be8..28a275cf 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -54,6 +54,7 @@ local type = type
---@field ref parser.guide.object[]
---@field returnIndex integer
---@field docs parser.guide.object[]
+---@field state table
---@field _root parser.guide.object
---@field _noders noders
---@field _mnode parser.guide.object
diff --git a/script/parser/newparser.lua b/script/parser/newparser.lua
index ba91cd10..957d98e8 100644
--- a/script/parser/newparser.lua
+++ b/script/parser/newparser.lua
@@ -416,11 +416,11 @@ local function parseLongString()
fastForwardToken(finish + 1)
local startPos = getPosition(start, 'left')
local finishMark = sgsub(mark, '%[', ']')
- local stringResult, finishOffset = resolveLongString(finishMark)
+ local stringResult, finishPos = resolveLongString(finishMark)
return {
type = 'string',
start = startPos,
- finish = getPosition(finishOffset, 'right'),
+ finish = finishPos,
[1] = stringResult,
[2] = mark,
}
@@ -1430,14 +1430,14 @@ local function parseExpList(mini)
end
local function parseIndex()
- local bstart = getPosition(Tokens[Index], 'left')
+ local start = getPosition(Tokens[Index], 'left')
Index = Index + 2
skipSpace()
local exp = parseExp()
local index = {
type = 'index',
- start = bstart,
- finish = exp and exp.finish or (bstart + 1),
+ start = start,
+ finish = exp and exp.finish or (start + 1),
index = exp
}
if exp then
@@ -1764,9 +1764,9 @@ local function parseSimple(node, funcName)
node.parent = call
node = call
else
- local index = parseIndex()
+ local index = parseIndex()
+ local bstart = index.start
index.type = 'getindex'
- index.bstart = index.start
index.start = node.start
index.node = node
node.next = index
@@ -1775,7 +1775,7 @@ local function parseSimple(node, funcName)
if funcName then
pushError {
type = 'INDEX_IN_FUNC_NAME',
- start = index.bstart,
+ start = bstart,
finish = index.finish,
}
end
diff --git a/test/rename/init.lua b/test/rename/init.lua
index 4b10756e..b20c0279 100644
--- a/test/rename/init.lua
+++ b/test/rename/init.lua
@@ -1,16 +1,21 @@
local core = require 'core.rename'
local files = require 'files'
+local catch = require 'catch'
+local guide = require 'parser.guide'
local function replace(text, positions)
+ local state = files.getState('')
local buf = {}
table.sort(positions, function (a, b)
return a.start < b.start
end)
local lastPos = 1
for _, info in ipairs(positions) do
- buf[#buf+1] = text:sub(lastPos, info.start - 1)
+ local start = guide.positionToOffset(state, info.start)
+ local finish = guide.positionToOffset(state, info.finish)
+ buf[#buf+1] = text:sub(lastPos, start)
buf[#buf+1] = info.text
- lastPos = info.finish + 1
+ lastPos = finish + 1
end
buf[#buf+1] = text:sub(lastPos)
return table.concat(buf)
@@ -21,10 +26,12 @@ function TEST(oldName, newName)
return function (expectScript)
files.removeAll()
files.setText('', oldScript)
- local pos = oldScript:find('[^%w_]'..oldName..'[^%w_]')
- assert(pos)
+ local state = files.getState('')
+ local offset = oldScript:find('[^%w_]'..oldName..'[^%w_]')
+ assert(offset)
+ local position = guide.offsetToPosition(state, offset)
- local positions = core.rename('', pos+1, newName)
+ local positions = core.rename('', position, newName)
local script = oldScript
if positions then
script = replace(script, positions)