diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-02-15 15:08:51 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-02-15 15:09:15 +0800 |
commit | 08a8a5ddb05610e282b3d4bed438aa779a0789f2 (patch) | |
tree | 7380248f928dd0bb8505bbe99b66d0276987a22d | |
parent | 30a3c34fdc23bd8b6d08e0782971d53004f8c25e (diff) | |
download | lua-language-server-08a8a5ddb05610e282b3d4bed438aa779a0789f2.zip |
should normalize path in filewatch
-rw-r--r-- | script/cli/doc.lua | 4 | ||||
-rw-r--r-- | script/core/command/exportDocument.lua | 5 | ||||
-rw-r--r-- | script/files.lua | 35 | ||||
-rw-r--r-- | script/filewatch.lua | 2 | ||||
-rw-r--r-- | script/workspace/require-path.lua | 10 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 52 |
6 files changed, 56 insertions, 52 deletions
diff --git a/script/cli/doc.lua b/script/cli/doc.lua index cbbad6b0..d07375e4 100644 --- a/script/cli/doc.lua +++ b/script/cli/doc.lua @@ -310,8 +310,8 @@ function export.runCLI() local mdPath = doc2md.buildMD(LOGPATH) print(lang.script('CLI_DOC_DONE' - , ('[%s](%s)'):format(ws.normalize(docPath), furi.encode(docPath)) - , ('[%s](%s)'):format(ws.normalize(mdPath), furi.encode(mdPath)) + , ('[%s](%s)'):format(files.normalize(docPath), furi.encode(docPath)) + , ('[%s](%s)'):format(files.normalize(mdPath), furi.encode(mdPath)) )) end diff --git a/script/core/command/exportDocument.lua b/script/core/command/exportDocument.lua index c3544829..39832856 100644 --- a/script/core/command/exportDocument.lua +++ b/script/core/command/exportDocument.lua @@ -3,13 +3,14 @@ local client = require 'client' local furi = require 'file-uri' local lang = require 'language' local ws = require 'workspace' +local files = require 'files' ---@async return function (args) local outputPath = args[1] and furi.decode(args[1]) or LOGPATH local docPath, mdPath = doc.makeDoc(outputPath) client.showMessage('Info', lang.script('CLI_DOC_DONE' - , ('[%s](%s)'):format(ws.normalize(docPath), furi.encode(docPath)) - , ('[%s](%s)'):format(ws.normalize(mdPath), furi.encode(mdPath)) + , ('[%s](%s)'):format(files.normalize(docPath), furi.encode(docPath)) + , ('[%s](%s)'):format(files.normalize(mdPath), furi.encode(mdPath)) )) end diff --git a/script/files.lua b/script/files.lua index ece7f3a9..16b85068 100644 --- a/script/files.lua +++ b/script/files.lua @@ -887,6 +887,41 @@ function m.countStates() return n end +---@param path string +---@return string +function m.normalize(path) + path = path:gsub('%$%{(.-)%}', function (key) + if key == '3rd' then + return (ROOT / 'meta' / '3rd'):string() + end + if key:sub(1, 4) == 'env:' then + local env = os.getenv(key:sub(5)) + return env + end + end) + path = util.expandPath(path) + path = path:gsub('^%.[/\\]+', '') + for _ = 1, 1000 do + if path:sub(1, 2) == '..' then + break + end + local count + path, count = path:gsub('[^/\\]+[/\\]+%.%.[/\\]', '/', 1) + if count == 0 then + break + end + end + if platform.OS == 'Windows' then + path = path:gsub('[/\\]+', '\\') + :gsub('[/\\]+$', '') + :gsub('^(%a:)$', '%1\\') + else + path = path:gsub('[/\\]+', '/') + :gsub('[/\\]+$', '') + end + return path +end + --- 注册事件 ---@param callback async fun(ev: string, uri: uri) function m.watch(callback) diff --git a/script/filewatch.lua b/script/filewatch.lua index addf3496..a8fa925f 100644 --- a/script/filewatch.lua +++ b/script/filewatch.lua @@ -2,6 +2,7 @@ local fw = require 'bee.filewatch' local fs = require 'bee.filesystem' local plat = require 'bee.platform' local await = require 'await' +local files = require 'files' local MODIFY = 1 << 0 local RENAME = 1 << 1 @@ -93,6 +94,7 @@ function m.update() if not ev then break end + path = files.normalize(path) log.debug('filewatch:', ev, path) if not collect then collect = {} diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua index e51d1219..c319cbad 100644 --- a/script/workspace/require-path.lua +++ b/script/workspace/require-path.lua @@ -81,7 +81,7 @@ function mt:getRequireResultByPath(path) for _, searcher in ipairs(searchers) do local isAbsolute = searcher:match '^[/\\]' or searcher:match '^%a+%:' - searcher = workspace.normalize(searcher) + searcher = files.normalize(searcher) if searcher:sub(1, 1) == '.' then strict = true end @@ -158,7 +158,7 @@ function mt:getVisiblePath(path) and not self.scp:isLinkedUri(uri) then return {} end - path = workspace.normalize(path) + path = files.normalize(path) local result = self.visibleCache[path] if not result then result = self:getRequireResultByPath(path) @@ -196,7 +196,7 @@ function mt:searchUrisByRequireName(name) for _, searcher in ipairs(searchers) do local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%'))) - fspath = workspace.normalize(fspath) + fspath = files.normalize(fspath) local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '') for uri in files.eachFile(self.scp.uri) do if not searcherMap[uri] @@ -212,7 +212,7 @@ function mt:searchUrisByRequireName(name) or relative == '/' or relative == '' then results[#results+1] = uri - searcherMap[uri] = workspace.normalize(relative .. searcher) + searcherMap[uri] = files.normalize(relative .. searcher) end end end @@ -294,7 +294,7 @@ function m.isMatchedUri(suri, uri, name) for _, searcher in ipairs(searchers) do local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%'))) - fspath = workspace.normalize(fspath) + fspath = files.normalize(fspath) local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '') if util.stringEndWith(uri, tail) then local parentUri = files.getLibraryUri(suri, uri) or uri diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 9d701167..3e85e0fc 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -217,14 +217,14 @@ function m.getLibraryMatchers(scp) for _, path in ipairs(config.get(scp.uri, 'Lua.workspace.library')) do path = m.getAbsolutePath(scp.uri, path) if path then - librarys[m.normalize(path)] = true + librarys[files.normalize(path)] = true end end local metaPaths = scp:get 'metaPaths' log.debug('meta path:', inspect(metaPaths)) if metaPaths then for _, metaPath in ipairs(metaPaths) do - librarys[m.normalize(metaPath)] = true + librarys[files.normalize(metaPath)] = true end end @@ -326,7 +326,7 @@ function m.awaitPreload(scp) if scp.uri and not scp:get('bad root') then log.info('Scan files at:', scp:getName()) - scp:gc(fw.watch(m.normalize(furi.decode(scp.uri)), true, function (path) + scp:gc(fw.watch(files.normalize(furi.decode(scp.uri)), true, function (path) local rpath = m.getRelativePath(path) if native(rpath) then return false @@ -401,52 +401,18 @@ function m.findUrisByFilePath(path) return results end ----@param path string ----@return string -function m.normalize(path) - path = path:gsub('%$%{(.-)%}', function (key) - if key == '3rd' then - return (ROOT / 'meta' / '3rd'):string() - end - if key:sub(1, 4) == 'env:' then - local env = os.getenv(key:sub(5)) - return env - end - end) - path = util.expandPath(path) - path = path:gsub('^%.[/\\]+', '') - for _ = 1, 1000 do - if path:sub(1, 2) == '..' then - break - end - local count - path, count = path:gsub('[^/\\]+[/\\]+%.%.[/\\]', '/', 1) - if count == 0 then - break - end - end - if platform.OS == 'Windows' then - path = path:gsub('[/\\]+', '\\') - :gsub('[/\\]+$', '') - :gsub('^(%a:)$', '%1\\') - else - path = path:gsub('[/\\]+', '/') - :gsub('[/\\]+$', '') - end - return path -end ---@param folderUri? uri ---@param path string ---@return string? function m.getAbsolutePath(folderUri, path) - path = m.normalize(path) + path = files.normalize(path) if fs.path(path):is_relative() then if not folderUri then return nil end local folderPath = furi.decode(folderUri) - path = m.normalize(folderPath .. '/' .. path) + path = files.normalize(folderPath .. '/' .. path) end return path end @@ -465,14 +431,14 @@ function m.getRelativePath(uriOrPath) end local scp = scope.getScope(uri) if not scp.uri then - local relative = m.normalize(path) + local relative = files.normalize(path) return relative:gsub('^[/\\]+', ''), false end - local _, pos = m.normalize(path):find(furi.decode(scp.uri), 1, true) + local _, pos = files.normalize(path):find(furi.decode(scp.uri), 1, true) if pos then - return m.normalize(path:sub(pos + 1)):gsub('^[/\\]+', ''), true + return files.normalize(path:sub(pos + 1)):gsub('^[/\\]+', ''), true else - return m.normalize(path):gsub('^[/\\]+', ''), false + return files.normalize(path):gsub('^[/\\]+', ''), false end end |