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
|
local fs = require 'bee.filesystem'
local async = require 'async'
local config = require 'config'
local function split(str, sep)
local t = {}
for s in str:gmatch('[^' .. sep .. ']+') do
t[#t+1] = s
end
return t
end
local function similarity(a, b)
local ta = split(a, '/\\')
local tb = split(b, '/\\')
for i = 1, #ta do
if ta[i] ~= tb[i] then
return i - 1
end
end
return #ta
end
local mt = {}
mt.__index = mt
function mt:uriDecode(uri)
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:init(rootUri)
self.root = self:uriDecode(rootUri)
if not self.root then
return
end
log.info('Workspace inited, root: ', self.root)
local logPath = ROOT / 'log' / (rootUri:gsub('[/:]+', '_') .. '.log')
log.info('Log path: ', logPath)
log.init(ROOT, logPath)
async.run('scanfiles', self.root:string(), function (list)
log.info(('Found [%d] files'):format(#list))
local ignored = {}
for name in pairs(config.config.workspace.ignoreDir) do
local path = fs.absolute(self.root / name)
local str = path:string():lower()
ignored[#ignored+1] = str
end
for _, filename in ipairs(list) do
local path = fs.absolute(fs.path(filename))
local name = path:string():lower()
local ok = true
for _, ignore in ipairs(ignored) do
if name:sub(1, #ignore) == ignore then
ok = false
break
end
end
if ok then
local uri = self:uriEncode(path)
self.files[name] = uri
self.lsp:readText(uri, path)
end
end
self:reset()
self._complete = true
return true
end)
end
function mt:isComplete()
return not not self._complete
end
function mt:addFile(uri)
if uri:sub(-4) == '.lua' then
local path = self:uriDecode(uri)
local name = path:string():lower()
self.files[name] = uri
self.lsp:readText(uri, path)
end
end
function mt:removeFile(uri)
local name = self:uriDecode(uri):string():lower()
self.files[name] = nil
self.lsp:removeText(uri)
end
function mt:findPath(baseUri, searchers)
local results = {}
for filename, uri in pairs(self.files) do
for _, searcher in ipairs(searchers) do
if filename:sub(-#searcher) == searcher then
local sep = filename:sub(-#searcher-1, -#searcher-1)
if sep == '/' or sep == '\\' then
results[#results+1] = uri
end
end
end
end
if #results == 0 then
return nil
end
local uri
if #results == 1 then
uri = results[1]
else
table.sort(results, function (a, b)
return similarity(a, baseUri) > similarity(b, baseUri)
end)
uri = results[1]
end
self.lsp:readText(uri, self:uriDecode(uri))
return uri
end
function mt:compileLuaPath()
for i, luapath in ipairs(self.luapath) do
self.compiledpath[i] = '^' .. luapath:gsub('%.', '%%.'):gsub('%?', '(.-)') .. '$'
end
end
function mt:convertPathAsRequire(filename, start)
local list
for _, luapath in ipairs(self.compiledpath) do
local str = filename:match(luapath, start)
if str then
if not list then
list = {}
end
list[#list+1] = str:gsub('/', '.')
end
end
return list
end
function mt:matchPath(baseUri, input)
input = input:lower()
local first = input:match '[^%.]+'
if not first then
return nil
end
local baseName = self:uriDecode(baseUri):string():lower()
local rootLen = #self.root:string()
local map = {}
for filename in pairs(self.files) do
local start = filename:find('/' .. first, rootLen + 1, true)
if start then
local list = self:convertPathAsRequire(filename, start + 1)
if list then
for _, str in ipairs(list) do
if #str >= #input and str:sub(1, #input) == input then
if not map[str]
or similarity(filename, baseName) > similarity(map[str], baseName)
then
map[str] = filename
end
end
end
end
end
end
local list = {}
for str in pairs(map) do
list[#list+1] = str
end
if #list == 0 then
return nil
end
table.sort(list, function (a, b)
local sa = similarity(map[a], baseName)
local sb = similarity(map[b], baseName)
if sa == sb then
return a < b
else
return sa > sb
end
end)
return list
end
function mt:searchPath(baseUri, str)
if self.searched[str] then
return self.searched[str]
end
str = str:gsub('%.', '/')
local searchers = {}
for i, luapath in ipairs(self.luapath) do
searchers[i] = luapath:gsub('%?', str):lower()
end
local uri = self:findPath(baseUri, searchers)
if uri then
self.searched[str] = uri
end
return uri
end
function mt:loadPath(baseUri, str)
str = fs.relative(fs.absolute(self.root / str), self.root):string():lower()
if self.loaded[str] then
return self.loaded[str]
end
local searchers = { str }
local uri = self:findPath(baseUri, searchers)
if uri then
self.loaded[str] = uri
end
return uri
end
function mt:reset()
self.searched = {}
self.loaded = {}
self.lsp:reCompile()
end
function mt:relativePathByUri(uri)
local path = self:uriDecode(uri)
local relate = fs.relative(path, self.root)
return relate
end
return function (lsp, name)
local workspace = setmetatable({
lsp = lsp,
name = name,
files = {},
searched = {},
loaded = {},
luapath = {
'?.lua',
'?/init.lua',
'?/?.lua',
},
compiledpath = {}
}, mt)
workspace:compileLuaPath()
return workspace
end
|