summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-20 10:54:19 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-20 10:54:19 +0800
commit2c90cf1b7b1fce9095f95bc54ed33680c848bfbe (patch)
tree67ec829fee457a0ddd117ab243ce35b19acb0ed5
parentd3872c08dc4b1003652073f2f2c9032ade608f18 (diff)
downloadlua-language-server-2c90cf1b7b1fce9095f95bc54ed33680c848bfbe.zip
更新 utility
-rw-r--r--server-beta/src/core/rename.lua44
-rw-r--r--server-beta/src/utility.lua49
2 files changed, 51 insertions, 42 deletions
diff --git a/server-beta/src/core/rename.lua b/server-beta/src/core/rename.lua
index e457a803..6f4f26e6 100644
--- a/server-beta/src/core/rename.lua
+++ b/server-beta/src/core/rename.lua
@@ -3,7 +3,7 @@ local searcher = require 'searcher'
local guide = require 'parser.guide'
local proto = require 'proto'
local define = require 'proto.define'
-local ws = require 'workspace'
+local util = require 'utility'
local Forcing
@@ -118,38 +118,6 @@ local function ofLocal(source, newname, callback)
end
end
-local esc = {
- ["'"] = [[\']],
- ['"'] = [[\"]],
- ['\r'] = [[\r]],
- ['\n'] = [[\n]],
-}
-
-local function toString(quo, newstr)
- if quo == "'" then
- return quo .. newstr:gsub([=[['\r\n]]=], esc) .. quo
- elseif quo == '"' then
- return quo .. newstr:gsub([=[["\r\n]]=], esc) .. quo
- else
- if newstr:find([[\r]], 1, true) then
- return toString('"', newstr)
- end
- local eqnum = #quo - 2
- local fsymb = ']' .. ('='):rep(eqnum) .. ']'
- if not newstr:find(fsymb, 1, true) then
- return quo .. newstr .. fsymb
- end
- for i = 0, 100 do
- local fsymb = ']' .. ('='):rep(i) .. ']'
- if not newstr:find(fsymb, 1, true) then
- local ssymb = '[' .. ('='):rep(i) .. '['
- return ssymb .. newstr .. fsymb
- end
- end
- return toString('"', newstr)
- end
-end
-
local function renameField(source, newname, callback)
if isValidName(newname) then
callback(source, source.start, source.finish, newname)
@@ -159,10 +127,10 @@ local function renameField(source, newname, callback)
if parent.type == 'setfield'
or parent.type == 'getfield' then
local dot = parent.dot
- local newstr = '[' .. toString('"', newname) .. ']'
+ local newstr = '[' .. util.vieString('"', newname) .. ']'
callback(source, dot.start, source.finish, newstr)
elseif parent.type == 'tablefield' then
- local newstr = '[' .. toString('"', newname) .. ']'
+ local newstr = '[' .. util.vieString('"', newname) .. ']'
callback(source, source.start, source.finish, newstr)
elseif parent.type == 'getmethod' then
if not askForcing(newname) then
@@ -176,7 +144,7 @@ local function renameField(source, newname, callback)
-- function mt:name () end --> mt['newname'] = function (self) end
local newstr = string.format('%s[%s] = function '
, text:sub(parent.start, parent.node.finish)
- , toString('"', newname)
+ , util.vieString('"', newname)
)
callback(source, func.start, parent.finish, newstr)
local pl = text:find('(', parent.finish, true)
@@ -196,7 +164,7 @@ local function renameGlobal(source, newname, callback)
callback(source, source.start, source.finish, newname)
return true
end
- local newstr = '_ENV[' .. toString('"', newname) .. ']'
+ local newstr = '_ENV[' .. util.vieString('"', newname) .. ']'
-- function name () end --> _ENV['newname'] = function () end
if source.value and source.value.type == 'function'
and source.value.start < source.start then
@@ -224,7 +192,7 @@ local function ofField(source, newname, callback)
end
if src.type == 'string' then
local quo = src[2]
- local text = toString(quo, newname)
+ local text = util.vieString(quo, newname)
callback(src, src.start, src.finish, text)
return
elseif src.type == 'field'
diff --git a/server-beta/src/utility.lua b/server-beta/src/utility.lua
index d624bcd2..055b35c6 100644
--- a/server-beta/src/utility.lua
+++ b/server-beta/src/utility.lua
@@ -135,9 +135,11 @@ function m.dump(tbl, option)
end
local value = tbl[key]
local tp = type(value)
- if tp == 'table' then
+ if option['format'] and option['format'][key] then
+ lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, option['format'][key](value, unpack, tab+1))
+ elseif tp == 'table' then
if mark[value] and mark[value] > 0 then
- lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, option.loop or '"<Loop>"')
+ lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, option['loop'] or '"<Loop>"')
else
lines[#lines+1] = ('%s%s{'):format(TAB[tab+1], keyWord)
unpack(value, tab+1)
@@ -146,7 +148,7 @@ function m.dump(tbl, option)
elseif tp == 'string' then
lines[#lines+1] = ('%s%s%q,'):format(TAB[tab+1], keyWord, value)
elseif tp == 'number' then
- lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, (option.number or formatNumber)(value))
+ lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, (option['number'] or formatNumber)(value))
elseif tp == 'nil' then
else
lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, tostring(value))
@@ -286,7 +288,7 @@ function m.counter(init, step)
if not step then
step = 1
end
- local current = init and (init - 1) or -1
+ local current = init and (init - 1) or 0
return function ()
current = current + step
return current
@@ -390,4 +392,43 @@ function m.defer(callback)
return setmetatable({ callback }, deferMT)
end
+local esc = {
+ ["'"] = [[\']],
+ ['"'] = [[\"]],
+ ['\r'] = [[\r]],
+ ['\n'] = '\\\n',
+}
+
+function m.viewString(str, quo)
+ if not quo then
+ if not str:find("'", 1, true) and str:find('"', 1, true) then
+ quo = "'"
+ else
+ quo = '"'
+ end
+ end
+ if quo == "'" then
+ return quo .. str:gsub([=[['\r\n]]=], esc) .. quo
+ elseif quo == '"' then
+ return quo .. str:gsub([=[["\r\n]]=], esc) .. quo
+ else
+ if str:find '\r' then
+ return m.viewString(str)
+ end
+ local eqnum = #quo - 2
+ local fsymb = ']' .. ('='):rep(eqnum) .. ']'
+ if not str:find(fsymb, 1, true) then
+ return quo .. str .. fsymb
+ end
+ for i = 0, 10 do
+ local fsymb = ']' .. ('='):rep(i) .. ']'
+ if not str:find(fsymb, 1, true) then
+ local ssymb = '[' .. ('='):rep(i) .. '['
+ return ssymb .. str .. fsymb
+ end
+ end
+ return m.viewString(str)
+ end
+end
+
return m