diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-06-24 15:14:09 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-06-24 15:14:09 +0800 |
commit | c648e23cdfc73d7a0ac95141d10325acd08737e7 (patch) | |
tree | fa939095f70537f11dc179da2b040b3339940719 /server | |
parent | 68e04f690d1accf41f8a59b497bb8fb2222ecc02 (diff) | |
download | lua-language-server-c648e23cdfc73d7a0ac95141d10325acd08737e7.zip |
hover优化转义字符串
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/hover/hover.lua | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/server/src/core/hover/hover.lua b/server/src/core/hover/hover.lua index 48f91796..eb82d4cc 100644 --- a/server/src/core/hover/hover.lua +++ b/server/src/core/hover/hover.lua @@ -17,9 +17,40 @@ local OriginTypes = { ['function'] = true, } +local function longString(str) + for i = 0, 10 do + local finish = ']' .. ('='):rep(i) .. ']' + if not str:find(finish, 1, true) then + return ('[%s[\n%s%s'):format(('='):rep(i), str, finish) + end + end + return ('%q'):format(str) +end + +local function formatString(str) + if #str > 1000 then + str = str:sub(1000) + end + if str:find('[\r\n]') then + return longString(str) + else + local single = str:find("'", 1, true) + local double = str:find('"', 1, true) + if single and double then + return longString(str) + elseif double then + return ("'%s'"):format(str) + else + return ('"%s"'):format(str) + end + end +end + local function formatLiteral(v) if math.type(v) == 'float' then return ('%.10f'):format(v):gsub('[0]*$', ''):gsub('%.$', '.0') + elseif type(v) == 'string' then + return formatString(v) else return ('%q'):format(v) end @@ -88,7 +119,7 @@ local function formatKey(key) key = ('[%03d]'):format(key) elseif kType == 'string' then if key:find '^%d' or key:find '[^%w_]' then - key = ('[%q]'):format(key) + key = ('[%s]'):format(formatString(key)) end elseif key == '' then key = '[*any]' |