summaryrefslogtreecommitdiff
path: root/script/workspace/require-path.lua
blob: c319cbad6ff4d126283ec0839ade1007a76e3be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
local platform  = require 'bee.platform'
local files     = require 'files'
local furi      = require 'file-uri'
local workspace = require "workspace"
local config    = require 'config'
local scope     = require 'workspace.scope'
local util      = require 'utility'

---@class require-path
local m = {}

---@class require-manager
---@field scp scope
---@field nameMap table<string, string>
---@field visibleCache table<string, require-manager.visibleResult[]>
---@field requireCache table<string, table>
local mt = {}
mt.__index = mt

---@alias require-manager.visibleResult { searcher: string, name: string }

---@param scp scope
---@return require-manager
local function createRequireManager(scp)
    return setmetatable({
        scp = scp,
        nameMap = {},
        visibleCache = {},
        requireCache = {},
    }, mt)
end

--- `aaa/bbb/ccc.lua` 与 `?.lua` 将返回 `aaa.bbb.cccc`
---@param path string
---@param searcher string
---@return string?
function mt:getRequireNameByPath(path, searcher)
    local separator    = config.get(self.scp.uri, 'Lua.completion.requireSeparator')
    local stemPath     = path
                        : gsub('%.[^%.]+$', '')
                        : gsub('[/\\%.]+', separator)
    local stemSearcher = searcher
                        : gsub('%.[^%.]+$', '')
                        : gsub('[/\\]+', separator)
    local start        = stemSearcher:match '()%?' or 1
    if stemPath:sub(1, start - 1) ~= stemSearcher:sub(1, start - 1) then
        return nil
    end
    for pos = #stemPath, start, -1 do
        local word = stemPath:sub(start, pos)
        local newSearcher = stemSearcher:gsub('%?', (word:gsub('%%', '%%%%')))
        if newSearcher == stemPath then
            return word
        end
    end
    return nil
end

---@param path string
---@return require-manager.visibleResult[]
function mt:getRequireResultByPath(path)
    local vm  = require 'vm'
    local uri = furi.encode(path)
    local result = {}
    if vm.isMetaFile(uri) then
        local metaName = vm.getMetaName(uri)
        if metaName then
            if vm.isMetaFileRequireable(uri) then
                result[#result+1] = {
                    name = metaName,
                    searcher = '[[meta]]',
                }
            end
            return result
        end
    end
    local searchers   = config.get(self.scp.uri, 'Lua.runtime.path')
    local strict      = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
    local libUri      = files.getLibraryUri(self.scp.uri, uri)
    local libraryPath = libUri and furi.decode(libUri)
    for _, searcher in ipairs(searchers) do
        local isAbsolute = searcher:match '^[/\\]'
                        or searcher:match '^%a+%:'
        searcher = files.normalize(searcher)
        if searcher:sub(1, 1) == '.' then
            strict = true
        end
        local cutedPath = path
        local currentPath = path
        local head
        local pos = 1
        if not isAbsolute then
            if libraryPath then
                currentPath = currentPath:sub(#libraryPath + 2)
            else
                currentPath = workspace.getRelativePath(uri)
            end
        end

        -- handle `../?.lua`
        local parentCount = 0
        for _ = 1, 1000 do
            if searcher:match '^%.%.[/\\]' then
                parentCount = parentCount + 1
                searcher = searcher:sub(4)
            else
                break
            end
        end
        if parentCount > 0 then
            local parentPath = libraryPath
                            or (self.scp.uri and furi.decode(self.scp.uri))
            if parentPath then
                local tail
                for _ = 1, parentCount do
                    parentPath, tail = parentPath:match '^(.+)[/\\]([^/\\]*)$'
                    currentPath = tail .. '/' .. currentPath
                end
            end
        end

        repeat
            cutedPath = currentPath:sub(pos)
            head = currentPath:sub(1, pos - 1)
            pos = currentPath:match('[/\\]+()', pos)
            if platform.OS == 'Windows' then
                searcher = searcher :gsub('[/\\]+', '\\')
            else
                searcher = searcher :gsub('[/\\]+', '/')
            end
            local name = self:getRequireNameByPath(cutedPath, searcher)
            if name then
                local mySearcher = searcher
                if head then
                    mySearcher = head .. searcher
                end
                result[#result+1] = {
                    name     = name,
                    searcher = mySearcher,
                }
            end
        until not pos or strict
    end
    return result
end

---@param name string
function mt:addName(name)
    local separator = config.get(self.scp.uri, 'Lua.completion.requireSeparator')
    local fsname = name:gsub('%' .. separator, '/')
    self.nameMap[fsname] = name
end

---@return require-manager.visibleResult[]
function mt:getVisiblePath(path)
    local uri = furi.encode(path)
    if  not self.scp:isChildUri(uri)
    and not self.scp:isLinkedUri(uri) then
        return {}
    end
    path = files.normalize(path)
    local result = self.visibleCache[path]
    if not result then
        result = self:getRequireResultByPath(path)
        self.visibleCache[path] = result
    end
    return result
end

--- 查找符合指定require name的所有uri
---@param name string
---@return uri[]
---@return table<uri, string>?
function mt:searchUrisByRequireName(name)
    local vm          = require 'vm'
    local searchers   = config.get(self.scp.uri, 'Lua.runtime.path')
    local strict      = config.get(self.scp.uri, 'Lua.runtime.pathStrict')
    local separator   = config.get(self.scp.uri, 'Lua.completion.requireSeparator')
    local path        = name:gsub('%' .. separator, '/')
    local results     = {}
    local searcherMap = {}
    local excludes    = {}

    for uri in files.eachFile(self.scp.uri) do
        if vm.isMetaFileRequireable(uri) then
            local metaName = vm.getMetaName(uri)
            if metaName == name then
                results[#results+1] = uri
                return results
            end
            if metaName then
                excludes[uri] = true
            end
        end
    end

    for _, searcher in ipairs(searchers) do
        local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%')))
        fspath = files.normalize(fspath)
        local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '')
        for uri in files.eachFile(self.scp.uri) do
            if  not searcherMap[uri]
            and not excludes[uri]
            and util.stringEndWith(uri, tail)
            and (not vm.isMetaFile(uri) or vm.isMetaFileRequireable(uri)) then
                local parentUri = files.getLibraryUri(self.scp.uri, uri) or self.scp.uri
                if parentUri == nil or parentUri == '' then
                    parentUri = furi.encode '/'
                end
                local relative = uri:sub(#parentUri + 1):sub(1, - #tail)
                if not strict
                or relative == '/'
                or relative == '' then
                    results[#results+1] = uri
                    searcherMap[uri] = files.normalize(relative .. searcher)
                end
            end
        end
    end

    for uri in files.eachDll() do
        local opens = files.getDllOpens(uri) or {}
        for _, open in ipairs(opens) do
            if open == path then
                results[#results+1] = uri
            end
        end
    end

    return results, searcherMap
end

--- 查找符合指定require name的所有uri,并排除当前文件
---@param suri uri
---@param name string
---@return uri[]
---@return table<uri, string>?
function mt:findUrisByRequireName(suri, name)
    if type(name) ~= 'string' then
        return {}
    end
    local cache = self.requireCache[name]
    if not cache then
        local results, searcherMap = self:searchUrisByRequireName(name)
        cache = {
            results = results,
            searcherMap = searcherMap,
        }
        self.requireCache[name] = cache
    end
    local results = {}
    local searcherMap = {}
    for _, uri in ipairs(cache.results) do
        if uri ~= suri then
            results[#results+1] = uri
            searcherMap[uri] = cache.searcherMap and cache.searcherMap[uri]
        end
    end
    return results, searcherMap
end

---@param uri uri
---@param path string
---@return require-manager.visibleResult[]
function m.getVisiblePath(uri, path)
    local scp = scope.getScope(uri)
    ---@type require-manager
    local mgr = scp:get 'requireManager'
             or scp:set('requireManager', createRequireManager(scp))
    return mgr:getVisiblePath(path)
end

---@param uri uri
---@param name string
---@return uri[]
---@return table<uri, string>?
function m.findUrisByRequireName(uri, name)
    local scp = scope.getScope(uri)
    ---@type require-manager
    local mgr = scp:get 'requireManager'
             or scp:set('requireManager', createRequireManager(scp))
    return mgr:findUrisByRequireName(uri, name)
end

---@param suri uri
---@param uri uri
---@param name string
---@return boolean
function m.isMatchedUri(suri, uri, name)
    local searchers   = config.get(suri, 'Lua.runtime.path')
    local strict      = config.get(suri, 'Lua.runtime.pathStrict')
    local separator   = config.get(suri, 'Lua.completion.requireSeparator')
    local path        = name:gsub('%' .. separator, '/')

    for _, searcher in ipairs(searchers) do
        local fspath = searcher:gsub('%?', (path:gsub('%%', '%%%%')))
        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
            if parentUri == nil or parentUri == '' then
                parentUri = furi.encode '/'
            end
            local relative = uri:sub(#parentUri + 1):sub(1, - #tail)
            if not strict
            or relative == '/'
            or relative == '' then
                return true
            end
        end
    end
    return false
end

files.watch(function (ev, uri)
    for _, scp in ipairs(workspace.folders) do
        scp:set('requireManager', nil)
    end
    scope.fallback:set('requireManager', nil)
end)

config.watch(function (uri, key, value, oldValue)
    if key == 'Lua.completion.requireSeparator'
    or key == 'Lua.runtime.path'
    or key == 'Lua.runtime.pathStrict' then
        local scp = scope.getScope(uri)
        scp:set('requireManager', nil)
    end
end)

return m