Skip to content

Commit 7ea95e8

Browse files
committed
暂存
1 parent 1924930 commit 7ea95e8

File tree

14 files changed

+1502
-227
lines changed

14 files changed

+1502
-227
lines changed

debugger.lua

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,37 @@
1-
if not DEVELOP then
2-
return
3-
end
4-
5-
local fs = require 'bee.filesystem'
6-
local luaDebugs = {}
1+
local INSIDERS = false
72

8-
local home = os.getenv 'USERPROFILE' or os.getenv 'HOME'
9-
if not home then
10-
log.error('Cannot find home directory')
11-
return
12-
end
13-
for _, vscodePath in ipairs { '.vscode', '.vscode-insiders', '.vscode-server-insiders' } do
14-
local extensionPath = fs.path(home) / vscodePath / 'extensions'
15-
log.debug('Search extensions at:', extensionPath:string())
16-
17-
if fs.exists(extensionPath) then
18-
for path in fs.pairs(extensionPath) do
19-
if fs.is_directory(path) then
20-
local name = path:filename():string()
21-
if name:find('actboy168.lua-debug-', 1, true) then
22-
luaDebugs[#luaDebugs+1] = path:string()
23-
end
24-
end
3+
local function searchDebugger(luaDebugs, tag)
4+
if INSIDERS then
5+
tag = tag .. "-insiders"
6+
end
7+
local isWindows = package.config:sub(1,1) == "\\"
8+
local extensionPath = (isWindows and os.getenv "USERPROFILE" or os.getenv "HOME") .. "/.vscode"..tag.."/extensions"
9+
local command = isWindows and ("dir /B " .. extensionPath:gsub("/", "\\") .. " 2>nul") or ("ls -1 " .. extensionPath .. " 2>/dev/null")
10+
for name in io.popen(command):lines() do
11+
local a, b, c = name:match "^actboy168%.lua%-debug%-(%d+)%.(%d+)%.(%d+)"
12+
if a then
13+
luaDebugs[#luaDebugs+1] = { a * 10000 + b * 100 + c, extensionPath .. "/" .. name }
2514
end
2615
end
2716
end
2817

29-
if #luaDebugs == 0 then
30-
log.debug('Cant find "actboy168.lua-debug"')
31-
return
32-
end
33-
34-
local function getVer(filename)
35-
local a, b, c = filename:match('actboy168%.lua%-debug%-(%d+)%.(%d+)%.(%d+)')
36-
if not a then
37-
return 0
18+
local function getLatestDebugger()
19+
local luaDebugs = {}
20+
searchDebugger(luaDebugs, "")
21+
searchDebugger(luaDebugs, "-server")
22+
if #luaDebugs == 0 then
23+
error "Cant find `actboy168.lua-debug`"
3824
end
39-
return a * 1000000 + b * 1000 + c
25+
table.sort(luaDebugs, function (a, b) return a[1] == b[1] and a[2] > b[2] or a[1] > b[1] end)
26+
return luaDebugs[1][2]
4027
end
4128

42-
table.sort(luaDebugs, function (a, b)
43-
return getVer(a) > getVer(b)
44-
end)
45-
46-
local debugPath = luaDebugs[1]
47-
local cpath = "/runtime/win64/lua54/?.dll;/runtime/win64/lua54/?.so"
48-
local path = "/script/?.lua"
49-
50-
local function tryDebugger()
51-
local entry = assert(package.searchpath('debugger', debugPath .. path))
52-
local root = debugPath
53-
local addr = ("127.0.0.1:%d"):format(DBGPORT)
54-
local dbg = loadfile(entry)(entry)
55-
dbg:start {
56-
address = addr,
57-
}
58-
log.debug('Debugger startup, listen port:', DBGPORT)
59-
log.debug('Debugger args:', addr, root, path, cpath)
60-
if DBGWAIT then
61-
dbg:event('wait')
62-
end
63-
return dbg
29+
local function dofile(filename)
30+
local f = assert(io.open(filename))
31+
local str = f:read "*a"
32+
f:close()
33+
return assert(load(str, "=(debugger.lua)"))(filename)
6434
end
6535

66-
xpcall(tryDebugger, log.debug)
36+
local path = getLatestDebugger()
37+
return dofile(path .. "/script/debugger.lua")

main.lua

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,4 @@
1-
local fs = require 'bee.filesystem'
2-
3-
---@class LuaLS
4-
luals = {}
5-
6-
luals.util = require 'tools.utility'
7-
luals.inspect = require 'tools.inspect'
8-
luals.json = require 'tools.json'
9-
package.loaded['json'] = luals.json
10-
require 'tools.json-beautify'
11-
require 'tools.jsonc'
12-
require 'tools.json-edit'
13-
14-
--语言服务器自身的状态
15-
---@class LuaLS.Runtime
16-
luals.runtime = require 'runtime.lua'
17-
18-
local currentPath = debug.getinfo(1, 'S').source:sub(2)
19-
local rootPath = currentPath:gsub('[/\\]*[^/\\]-$', '')
20-
rootPath = (rootPath == '' and '.' or rootPath)
21-
ROOT = fs.path(util.expandPath(rootPath))
22-
LOGPATH = LOGPATH and util.expandPath(LOGPATH) or (ROOT:string() .. '/log')
23-
METAPATH = METAPATH and util.expandPath(METAPATH) or (ROOT:string() .. '/meta')
24-
25-
---@diagnostic disable-next-line: deprecated
26-
debug.setcstacklimit(200)
271
collectgarbage('generational', 10, 50)
28-
--collectgarbage('incremental', 120, 120, 0)
29-
30-
---@diagnostic disable-next-line: lowercase-global
31-
log = require 'log'
32-
log.init(ROOT, fs.path(LOGPATH) / 'service.log')
33-
if LOGLEVEL then
34-
log.level = tostring(LOGLEVEL):lower()
35-
end
36-
37-
log.info('Lua Lsp startup, root: ', ROOT)
38-
log.info('ROOT:', ROOT:string())
39-
log.info('LOGPATH:', LOGPATH)
40-
log.info('METAPATH:', METAPATH)
41-
log.info('VERSION:', version.getVersion())
42-
43-
require 'tracy'
44-
45-
xpcall(dofile, log.debug, (ROOT / 'debugger.lua'):string())
46-
47-
require 'cli'
48-
49-
local _, service = xpcall(require, log.error, 'service')
502

51-
service.start()
3+
require 'luals'
4+
require 'master'

script/luals.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Class = require 'tools.class'.declare
2+
New = require 'tools.class'.new
3+
4+
---@class LuaLS
5+
luals = {}
6+
7+
luals.util = require 'tools.utility'
8+
9+
luals.inspect = require 'tools.inspect'
10+
11+
luals.json = require 'tools.json'
12+
package.loaded['json'] = luals.json
13+
require 'tools.json-beautify'
14+
require 'tools.jsonc'
15+
require 'tools.json-edit'
16+
17+
luals.uri = require 'tools.uri'
18+
19+
return luals

script/master/init.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
local time = require 'bee.time'
3+
4+
--语言服务器自身的状态
5+
---@class LuaLS.Runtime
6+
luals.runtime = require 'runtime.lua'
7+
8+
---@class Log
9+
log = Class 'Log' {
10+
clock = function ()
11+
return time.monotonic() / 1000.0
12+
end,
13+
time = function ()
14+
return time.time() // 1000
15+
end,
16+
path = luals.uri.decode(luals.runtime.logUri) .. '/service.log',
17+
}
18+
19+
log.info('Lua Lsp startup!')
20+
log.info('ROOT:', luals.runtime.rootUri)
21+
log.info('LOGPATH:', luals.runtime.logUri)
22+
log.info('METAPATH:', luals.runtime.metaUri)
23+
log.info('VERSION:', luals.runtime.version)
24+
25+
xpcall(function ()
26+
if not luals.runtime.args.DEVELOP then
27+
return
28+
end
29+
require 'debugger':start "127.0.0.1:12399"
30+
end, log.warn)
31+
32+
require 'cli'
33+
34+
local _, service = xpcall(require, log.error, 'service')
35+
36+
service.start()

script/meta/time.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---@meta bee.time
2+
3+
---@class Bee.Time
4+
local M = {}
5+
6+
---@return integer ms
7+
function M.time() end
8+
9+
---@return integer ms
10+
function M.monotonic() end
11+
12+
return M

script/runtime/argparser.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@class ArgParser
1+
---@class Runtime.ArgParser
22
local M = {}
33

44
---@alias ArgParser.Value string | number | boolean

script/runtime/init.lua

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11

22
local argparser = require 'runtime.argparser'
3+
local version = require 'runtime.version'
34

45
---@class LuaLS.Runtime
56
local M = {}
67

78
--启动时的命令行参数
8-
M.args = argparser.parse(arg, true)
9+
---@class LuaLS.Runtime.Args
10+
---@field DEVELOP? boolean # 是否为开发模式
11+
---@field DBGPORT? integer # 调试器端口号,默认为 11411
12+
---@field DBGADDRESS? string # 调试器地址,默认为 '127.0.0.1'
13+
M.args = {
14+
--指定日志输出目录,默认为 `./log`
15+
LOGPATH = '${LUALS}/log',
16+
--指定meta文件的生成目录,默认为 `./meta`
17+
METAPATH = '${LUALS}/meta',
18+
--是否为开发模式
19+
DEVELOP = false,
20+
--调试器端口号,默认为 11411
21+
DBGPORT = 11411,
22+
--调试器地址,默认为 '127.0.0.1'
23+
DBGADDRESS = '127.0.0.1',
24+
--等待调试器连接
25+
DBGWAIT = false,
26+
--显示语言
27+
LOCALE = 'en-us',
28+
--全局配置文件的路径
29+
CONFIGPATH = '',
30+
--在日志中记录RPC信息
31+
RPCLOG = false,
32+
--使用socket来连接客户端,指定端口号
33+
SOCKET = 0,
34+
--强制接受工作区。默认情况下会拒绝根目录与Home目录作为工作区
35+
FORCE_ACCEPT_WORKSPACE = false,
36+
37+
--命令行:诊断指定工作区
38+
CHECK = '',
39+
--诊断工作区时需要报告的诊断等级
40+
---@type 'Error' | 'Warning' | 'Information' | 'Hint'
41+
CHECKLEVEL = 'Warning',
42+
--诊断报告生成的文件路径(json),默认为 `$LOGPATH/check.json`
43+
CHECK_OUT_PATH = '${LOGPATH}/check.json',
44+
45+
--命令行:生成文档
46+
DOC = '',
47+
}
48+
luals.util.tableMerge(M.args, argparser.parse(arg, true))
49+
50+
--启动时的版本号
51+
M.version = version.getVersion()
952

1053
--路径相关
1154
---@class LuaLS.Path
1255
M.path = {}
1356

57+
---@return string
1458
local function findRoot()
1559
local lastPath
1660
for i = 1, 10 do
@@ -20,10 +64,15 @@ local function findRoot()
2064
end
2165
lastPath = currentPath:sub(2)
2266
end
23-
return lastPath
67+
assert(lastPath, 'Can not find root path')
68+
local rootPath = lastPath:gsub('[/\\]*[^/\\]-$', '')
69+
rootPath = (rootPath == '' and '.' or rootPath)
70+
return rootPath
2471
end
2572

2673
--语言服务器根路径
27-
M.path.root = findRoot()
74+
M.rootUri = luals.uri.encode(luals.util.expandPath(findRoot()))
75+
M.logUri = luals.uri.encode(luals.util.expandPath(M.args.LOGPATH))
76+
M.metaUri = luals.uri.encode(luals.util.expandPath(M.args.METAPATH))
2877

2978
return M

script/runtime/version.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local function loadVersion()
2+
local changelogUri = luals.uri.join(luals.runtime.rootUri, 'changelog.md')
3+
local changelog = luals.util.loadFile(luals.uri.decode(changelogUri))
4+
if not changelog then
5+
return
6+
end
7+
8+
local version, pos = changelog:match '%#%# (%d+%.%d+%.%d+)()'
9+
if not version then
10+
return
11+
end
12+
13+
if not changelog:find('^[\r\n]+`', pos) then
14+
version = version .. '-dev'
15+
end
16+
return version
17+
end
18+
19+
---@class Runtime.VersionParser
20+
local M = {}
21+
22+
---@return string
23+
function M.getVersion()
24+
if not M.version then
25+
M.version = loadVersion() or '<Unknown>'
26+
end
27+
28+
return M.version
29+
end
30+
31+
return M

0 commit comments

Comments
 (0)