diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-10-22 18:29:44 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-10-22 18:29:44 +0800 |
commit | 591a154c735c4c9fcb64bd16e5b845c496fdd676 (patch) | |
tree | 6bfa3e8a860025bbf7f72c04bef30f0b67ad4bd3 | |
parent | 0c364067c6350e0c524e2f772f733b6ebb6f902c (diff) | |
download | lua-language-server-591a154c735c4c9fcb64bd16e5b845c496fdd676.zip |
stash
-rw-r--r-- | script/encoder/init.lua | 111 | ||||
-rw-r--r-- | script/files.lua | 5 | ||||
-rw-r--r-- | script/fs-utility.lua | 8 | ||||
-rw-r--r-- | script/library.lua | 4 | ||||
-rw-r--r-- | script/proto/converter.lua | 16 | ||||
-rw-r--r-- | script/text-merger.lua | 16 |
6 files changed, 137 insertions, 23 deletions
diff --git a/script/encoder/init.lua b/script/encoder/init.lua index 152193e7..f33f5a1d 100644 --- a/script/encoder/init.lua +++ b/script/encoder/init.lua @@ -1,5 +1,106 @@ -return { - ansi = require 'encoder.ansi', - utf16le = require 'encoder.utf16le', - utf16be = require 'encoder.utf16be' -} +local ansi = require 'encoder.ansi' +local utf16le = require 'encoder.utf16le' +local utf16be = require 'encoder.utf16be' + +---@alias encoder.encoding '"utf8"'|'"utf16"'|'"utf16le"'|'"utf16be"' + +local m = {} + +---@param encoding encoder.encoding +---@param s string +---@param i integer +---@param j integer +function m.len(encoding, s, i, j) + i = i or 1 + j = j or #s + if encoding == 'utf16' + or encoding == 'utf16' then + local us = utf16le.encode(s:sub(i, j)) + return #us // 2 + end + if encoding == 'utf16be' then + local us = utf16be.encode(s:sub(i, j)) + return #us // 2 + end + if encoding == 'utf8' then + return utf8.len(s, i, j, true) + end + log.error('Unsupport len encoding:', encoding) + return j - i + 1 +end + +---@param encoding encoder.encoding +---@param s string +---@param n integer +---@param i integer +function m.offset(encoding, s, n, i) + i = i or 1 + if encoding == 'utf16' + or encoding == 'utf16le' then + local line = s:match('[^\r\n]*', i) + if not line:find '[\x80-\xff]' then + return n + i - 1 + end + local us = utf16le.encode(line) + local os = utf16le.decode(us:sub(1, n * 2 - 2)) + return #os + i + end + if encoding == 'utf16be' then + local line = s:match('[^\r\n]*', i) + if not line:find '[\x80-\xff]' then + return n + i - 1 + end + local us = utf16be.encode(line) + local os = utf16be.decode(us:sub(1, n * 2 - 2)) + return #os + i + end + if encoding == 'utf8' then + return utf8.offset(s, n, i) + end + log.error('Unsupport offset encoding:', encoding) + return n + i - 1 +end + +---@param encoding encoder.encoding +---@param text string +---@return string +function m.encode(encoding, text) + if encoding == 'utf8' then + return text + end + if encoding == 'ansi' then + return ansi.encode(text) + end + if encoding == 'utf16' + or encoding == 'utf16le' then + return utf16le.encode(text) + end + if encoding == 'utf16be' then + return utf16be.encode(text) + end + log.error('Unsupport encode encoding:', encoding) + return text +end + +---@param encoding encoder.encoding +---@param text string +---@return string +function m.decode(encoding, text) + if encoding == 'utf8' then + return text + end + if encoding == 'ansi' then + return ansi.decode(text) + end + if encoding == 'utf16' + or encoding == 'utf16le' then + return utf16le.decode(text) + end + if encoding == 'utf16be' then + return utf16be.decode(text) + end + log.error('Unsupport encode encoding:', encoding) + return text +end + +return m diff --git a/script/files.lua b/script/files.lua index 566b012c..4d649b29 100644 --- a/script/files.lua +++ b/script/files.lua @@ -140,9 +140,8 @@ function m.setText(uri, text, isTrust, instance) return end if not isTrust then - if config.get 'Lua.runtime.fileEncoding' == 'ansi' then - text = encoder.ansi.decode(text) - end + local encoding = config.get 'Lua.runtime.fileEncoding' + text = encoder.decode(encoding, text) end if file.originText == text then return diff --git a/script/fs-utility.lua b/script/fs-utility.lua index 2f5d404e..f4e55716 100644 --- a/script/fs-utility.lua +++ b/script/fs-utility.lua @@ -579,4 +579,12 @@ function m.scanDirectory(dir, callback) end end +function m.listDirectory(dir) + if dir.type == 'dummy' then + return dir:listDirectory() + else + return fs.pairs(dir) + end +end + return m diff --git a/script/library.lua b/script/library.lua index 66b26b0d..0911750e 100644 --- a/script/library.lua +++ b/script/library.lua @@ -236,9 +236,7 @@ local function initBuiltIn() local metaDoc = compileSingleMetaDoc(fsu.loadFile(libPath), metaLang, status) if metaDoc then local outPath = metaPath / libName - if encoding == 'ansi' then - metaDoc = encoder.ansi.encode(metaDoc) - end + encoder.encode(encoding, metaDoc) out:saveFile(libName, metaDoc) m.metaPaths[#m.metaPaths+1] = outPath:string() end diff --git a/script/proto/converter.lua b/script/proto/converter.lua index 1c8e20fc..cf6331f1 100644 --- a/script/proto/converter.lua +++ b/script/proto/converter.lua @@ -1,5 +1,9 @@ -local guide = require 'parser.guide' -local files = require 'files' +local guide = require 'parser.guide' +local files = require 'files' +local encoder = require 'encoder' + +-- TODO +local offsetEncoding = 'utf16' local m = {} @@ -16,7 +20,7 @@ local function rawPackPosition(uri, pos) local start = lineOffset local finish = lineOffset + col - 1 if start <= #text and finish <= #text then - col = utf8.len(text, lineOffset, lineOffset + col - 1, true) + col = encoder.len(offsetEncoding, text, lineOffset, lineOffset + col - 1) end else col = 0 @@ -41,7 +45,7 @@ local function diffedPackPosition(uri, pos) if text then local lineOffset = originLines[row] local finalOffset = math.min(lineOffset + col - 1, #text + 1) - col = utf8.len(text, lineOffset, finalOffset, true) + col = encoder.len(offsetEncoding, text, lineOffset, finalOffset) end end return { @@ -68,7 +72,7 @@ local function rawUnpackPosition(uri, position) local text = files.getText(uri) if state and text then local lineOffset = state.lines[row] - local textOffset = utf8.offset(text, col + 1, lineOffset) + local textOffset = encoder.offset(offsetEncoding, text, col + 1, lineOffset) if textOffset and lineOffset then col = textOffset - lineOffset end @@ -85,7 +89,7 @@ local function diffedUnpackPosition(uri, position) local text = files.getOriginText(uri) if text then local lineOffset = originLines[row] - local textOffset = utf8.offset(text, col + 1, lineOffset) + local textOffset = encoder.offset(offsetEncoding, text, col + 1, lineOffset) if textOffset and lineOffset then col = textOffset - lineOffset end diff --git a/script/text-merger.lua b/script/text-merger.lua index ba7cff58..afd3343f 100644 --- a/script/text-merger.lua +++ b/script/text-merger.lua @@ -1,5 +1,9 @@ -local files = require 'files' -local util = require 'utility' +local files = require 'files' +local util = require 'utility' +local encoder = require 'encoder' + +-- TODO +local offsetEncoding = 'utf16' local function splitRows(text) local rows = {} @@ -14,14 +18,14 @@ local function getLeft(text, char) return '' end local left - local length = util.utf8Len(text) + local length = encoder.len(offsetEncoding, text) if char == 0 then left = '' elseif char >= length then left = text else - left = text:sub(1, utf8.offset(text, char + 1) - 1) + left = text:sub(1, encoder.offset(offsetEncoding, text, char + 1) - 1) end return left @@ -32,14 +36,14 @@ local function getRight(text, char) return '' end local right - local length = util.utf8Len(text) + local length = encoder.len(offsetEncoding, text) if char == 0 then right = text elseif char >= length then right = '' else - right = text:sub(utf8.offset(text, char + 1)) + right = text:sub(encoder.offset(offsetEncoding, text, char + 1)) end return right |