diff options
author | sumneko <sumneko@hotmail.com> | 2019-05-15 19:24:48 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-05-15 19:24:48 +0800 |
commit | 632b98d4ddeadadc0c362391020e96ae2736accd (patch) | |
tree | 9712c5b1154cb6a45dbc7068914bb4e9bb6d3cd6 /server | |
parent | 935faea26c5f148651d1371e6979f016a14c3aa0 (diff) | |
download | lua-language-server-632b98d4ddeadadc0c362391020e96ae2736accd.zip |
整理代码
Diffstat (limited to 'server')
-rw-r--r-- | server/src/method/workspace/didChangeWatchedFiles.lua | 3 | ||||
-rw-r--r-- | server/src/uri.lua | 54 | ||||
-rw-r--r-- | server/src/workspace.lua | 63 | ||||
-rw-r--r-- | server/test/crossfile/completion.lua | 5 | ||||
-rw-r--r-- | server/test/crossfile/definition.lua | 5 | ||||
-rw-r--r-- | server/test/crossfile/document_symbol.lua | 9 | ||||
-rw-r--r-- | server/test/crossfile/hover.lua | 11 | ||||
-rw-r--r-- | server/test/crossfile/references.lua | 7 |
8 files changed, 85 insertions, 72 deletions
diff --git a/server/src/method/workspace/didChangeWatchedFiles.lua b/server/src/method/workspace/didChangeWatchedFiles.lua index 06d840e1..dcca48f1 100644 --- a/server/src/method/workspace/didChangeWatchedFiles.lua +++ b/server/src/method/workspace/didChangeWatchedFiles.lua @@ -1,4 +1,5 @@ local fs = require 'bee.filesystem' +local uric = require 'uri' local FileChangeType = { Created = 1, @@ -12,7 +13,7 @@ return function (lsp, params) end local needReset for _, change in ipairs(params.changes) do - local path = lsp.workspace:uriDecode(change.uri) + local path = uric.decode(change.uri) if change.type == FileChangeType.Created then lsp.workspace:addFile(path) if lsp:getVM(change.uri) then diff --git a/server/src/uri.lua b/server/src/uri.lua new file mode 100644 index 00000000..b41bc1db --- /dev/null +++ b/server/src/uri.lua @@ -0,0 +1,54 @@ +local fs = require 'bee.filesystem' + +local function decode(uri) + -- Unix-like系统根是/ + if uri:sub(1, 9) == 'file:////' then + return fs.path(uri:sub(9)) + end + if uri:sub(1, 8) ~= 'file:///' then + log.error('uri decode failed: ', uri) + return nil + end + local names = {} + for name in uri:sub(9):gmatch '[^%/]+' do + names[#names+1] = name:gsub('%%([0-9a-fA-F][0-9a-fA-F])', function (hex) + return string.char(tonumber(hex, 16)) + end) + end + if #names == 0 then + log.error('uri decode failed: ', uri) + return nil + end + -- 盘符后面加个斜杠 + local path = fs.path(names[1] .. '\\') + for i = 2, #names do + path = path / names[i] + end + return fs.absolute(path) +end + +local function encode(path) + local names = {} + local cur = fs.absolute(path) + while true do + local name = cur:filename():string() + if name == '' then + -- 盘符,去掉一个斜杠 + name = cur:string():sub(1, -2) + end + name = name:gsub([=[[^%w%-%_%.%~]]=], function (char) + return ('%%%02X'):format(string.byte(char)) + end) + table.insert(names, 1, name) + if cur == cur:parent_path() then + break + end + cur = cur:parent_path() + end + return 'file:///' .. table.concat(names, '/') +end + +return { + encode = encode, + decode = decode, +} diff --git a/server/src/workspace.lua b/server/src/workspace.lua index ee467a4b..4962e7e1 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -4,6 +4,7 @@ local config = require 'config' local ll = require 'lpeglabel' local platform = require 'bee.platform' local glob = require 'glob' +local uric = require 'uri' local TrueName = {} @@ -52,59 +53,11 @@ function mt:fileNameEq(a, b) end end -function mt:uriDecode(uri) - -- Unix-like系统根是/ - if uri:sub(1, 9) == 'file:////' then - return fs.path(uri:sub(9)) - end - if uri:sub(1, 8) ~= 'file:///' then - log.error('uri decode failed: ', uri) - return nil - end - local names = {} - for name in uri:sub(9):gmatch '[^%/]+' do - names[#names+1] = name:gsub('%%([0-9a-fA-F][0-9a-fA-F])', function (hex) - return string.char(tonumber(hex, 16)) - end) - end - if #names == 0 then - log.error('uri decode failed: ', uri) - return nil - end - -- 盘符后面加个斜杠 - local path = fs.path(names[1] .. '\\') - for i = 2, #names do - path = path / names[i] - end - return fs.absolute(path) -end - -function mt:uriEncode(path) - local names = {} - local cur = fs.absolute(path) - while true do - local name = cur:filename():string() - if name == '' then - -- 盘符,去掉一个斜杠 - name = cur:string():sub(1, -2) - end - name = name:gsub([=[[^%w%-%_%.%~]]=], function (char) - return ('%%%02X'):format(string.byte(char)) - end) - table.insert(names, 1, name) - if cur == cur:parent_path() then - break - end - cur = cur:parent_path() - end - return 'file:///' .. table.concat(names, '/') -end - function mt:listenLoadFile() self._loadFileRequest = async.run('loadfile', nil, function (filename, buf) local path = fs.path(filename) local name = getFileName(path) - local uri = self:uriEncode(path) + local uri = uric.encode(path) self.files[name] = uri self.lsp:readText(uri, path, buf, self._currentScanCompiled) end) @@ -192,7 +145,7 @@ function mt:scanFiles() end function mt:init(rootUri) - self.root = self:uriDecode(rootUri) + self.root = uric.decode(rootUri) self.uri = rootUri if not self.root then return @@ -231,7 +184,7 @@ function mt:addFile(path) return end local name = getFileName(path) - local uri = self:uriEncode(path) + local uri = uric.encode(path) self.files[name] = uri self.lsp:readText(uri, path) end @@ -242,13 +195,13 @@ function mt:removeFile(path) return end self.files[name] = nil - local uri = self:uriEncode(path) + local uri = uric.encode(path) self.lsp:removeText(uri) end function mt:findPath(baseUri, searchers) local results = {} - local baseName = getFileName(self:uriDecode(baseUri)) + local baseName = getFileName(uric.decode(baseUri)) for filename, uri in pairs(self.files) do if filename ~= baseName then for _, searcher in ipairs(searchers) do @@ -381,7 +334,7 @@ function mt:matchPath(baseUri, input) if not first then return nil end - local baseName = getFileName(self:uriDecode(baseUri)) + local baseName = getFileName(uric.decode(baseUri)) local rootLen = #self.root:string() local map = {} for filename in pairs(self.files) do @@ -485,7 +438,7 @@ function mt:reset() end function mt:relativePathByUri(uri) - local path = self:uriDecode(uri) + local path = uric.decode(uri) local relate = fs.relative(path, self.root) return relate end diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index 4ee9508e..6fbc0541 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -2,6 +2,7 @@ local service = require 'service' local workspace = require 'workspace' local fs = require 'bee.filesystem' local core = require 'core' +local uric = require 'uri' rawset(_G, 'TEST', true) @@ -71,7 +72,7 @@ function TEST(data) local mainBuf local pos for _, info in ipairs(data) do - local uri = ws:uriEncode(fs.path(info.path)) + local uri = uric.encode(fs.path(info.path)) local script = info.content if info.main then pos = script:find('$', 1, true) - 1 @@ -80,7 +81,7 @@ function TEST(data) mainBuf = script end lsp:saveText(uri, 1, script) - ws:addFile(ws:uriDecode(uri)) + ws:addFile(uric.decode(uri)) while lsp._needCompile[1] do lsp:compileVM(lsp._needCompile[1]) diff --git a/server/test/crossfile/definition.lua b/server/test/crossfile/definition.lua index 90bf1651..73752ae6 100644 --- a/server/test/crossfile/definition.lua +++ b/server/test/crossfile/definition.lua @@ -2,6 +2,7 @@ local service = require 'service' local workspace = require 'workspace' local fs = require 'bee.filesystem' local core = require 'core' +local uric = require 'uri' rawset(_G, 'TEST', true) @@ -51,7 +52,7 @@ function TEST(datas) local sourceList, sourceUri for i, data in ipairs(datas) do - local uri = ws:uriEncode(fs.path(data.path)) + local uri = uric.encode(fs.path(data.path)) local new, list = catch_target(data.content, '!') if new ~= data.content or data.target then if data.target then @@ -79,7 +80,7 @@ function TEST(datas) data.content = new end lsp:saveText(uri, 1, data.content) - ws:addFile(ws:uriDecode(uri)) + ws:addFile(uric.decode(uri)) end while lsp._needCompile[1] do diff --git a/server/test/crossfile/document_symbol.lua b/server/test/crossfile/document_symbol.lua index e09ed5d1..997d42c5 100644 --- a/server/test/crossfile/document_symbol.lua +++ b/server/test/crossfile/document_symbol.lua @@ -2,6 +2,7 @@ local service = require 'service' local workspace = require 'workspace' local fs = require 'bee.filesystem' local core = require 'core' +local uric = require 'uri' local SymbolKind = { File = 1, @@ -67,13 +68,13 @@ function TEST(data) local ws = workspace(lsp, 'test') lsp.workspace = ws - local targetUri = ws:uriEncode(fs.path(data[1].path)) - local sourceUri = ws:uriEncode(fs.path(data[2].path)) + local targetUri = uric.encode(fs.path(data[1].path)) + local sourceUri = uric.encode(fs.path(data[2].path)) lsp:saveText(sourceUri, 1, data[2].content) - ws:addFile(ws:uriDecode(sourceUri)) + ws:addFile(uric.decode(sourceUri)) lsp:saveText(targetUri, 1, data[1].content) - ws:addFile(ws:uriDecode(targetUri)) + ws:addFile(uric.decode(targetUri)) while lsp._needCompile[1] do lsp:compileVM(lsp._needCompile[1]) end diff --git a/server/test/crossfile/hover.lua b/server/test/crossfile/hover.lua index fc5df36a..f2dcd443 100644 --- a/server/test/crossfile/hover.lua +++ b/server/test/crossfile/hover.lua @@ -2,6 +2,7 @@ local service = require 'service' local workspace = require 'workspace' local fs = require 'bee.filesystem' local core = require 'core' +local uric = require 'uri' rawset(_G, 'TEST', true) @@ -57,16 +58,16 @@ function TEST(data) ws.root = ROOT local targetScript = data[1].content - local targetUri = ws:uriEncode(fs.path(data[1].path)) + local targetUri = uric.encode(fs.path(data[1].path)) local sourceScript, sourceList = catch_target(data[2].content, '?') - local sourceUri = ws:uriEncode(fs.path(data[2].path)) + local sourceUri = uric.encode(fs.path(data[2].path)) lsp:saveText(targetUri, 1, targetScript) - ws:addFile(ws:uriDecode(targetUri)) + ws:addFile(uric.decode(targetUri)) lsp:compileVM(targetUri) lsp:saveText(sourceUri, 1, sourceScript) - ws:addFile(ws:uriDecode(sourceUri)) + ws:addFile(uric.decode(sourceUri)) lsp:compileVM(sourceUri) local sourceVM = lsp:loadVM(sourceUri) @@ -76,7 +77,7 @@ function TEST(data) local hover = core.hover(source, lsp) assert(hover) if data.hover.description then - local uriROOT = ws:uriEncode(ROOT):gsub('%%', '%%%%') + local uriROOT = uric.encode(ROOT):gsub('%%', '%%%%') data.hover.description = data.hover.description:gsub('%$ROOT%$', uriROOT) end if hover.label then diff --git a/server/test/crossfile/references.lua b/server/test/crossfile/references.lua index dd1958aa..9f81707c 100644 --- a/server/test/crossfile/references.lua +++ b/server/test/crossfile/references.lua @@ -2,6 +2,7 @@ local service = require 'service' local workspace = require 'workspace' local fs = require 'bee.filesystem' local core = require 'core' +local uric = require 'uri' rawset(_G, 'TEST', true) @@ -79,11 +80,11 @@ function TEST(data) local pos local expect = {} for _, info in ipairs(data) do - local uri = ws:uriEncode(fs.path(info.path)) - ws:addFile(ws:uriDecode(uri)) + local uri = uric.encode(fs.path(info.path)) + ws:addFile(uric.decode(uri)) end for _, info in ipairs(data) do - local uri = ws:uriEncode(fs.path(info.path)) + local uri = uric.encode(fs.path(info.path)) local script = info.content local list = catch_target(script) for _, location in ipairs(list) do |