|
| 1 | +--- 校验并补全调试配置 |
| 2 | +--- @param config table 原始配置表 |
| 3 | +--- @return table|nil config 校验后的配置表,失败时为 nil |
| 4 | +--- @return string|nil errmsg 错误信息,成功时为 nil |
| 5 | +local function resolve_config(config) |
| 6 | + -- 通用默认值填充 |
| 7 | + config.type = "lua" |
| 8 | + if config.request ~= "attach" then |
| 9 | + config.request = "launch" |
| 10 | + end |
| 11 | + if type(config.name) ~= "string" then |
| 12 | + config.name = "Not specified" |
| 13 | + end |
| 14 | + if type(config.cwd) ~= "string" then |
| 15 | + if type(config.workspaceFolder) == "string" then |
| 16 | + config.cwd = config.workspaceFolder |
| 17 | + end |
| 18 | + end |
| 19 | + if type(config.stopOnEntry) ~= "boolean" then |
| 20 | + config.stopOnEntry = true |
| 21 | + end |
| 22 | + if type(config.stopOnThreadEntry) ~= "boolean" then |
| 23 | + config.stopOnThreadEntry = false |
| 24 | + end |
| 25 | + if type(config.luaVersion) ~= "string" then |
| 26 | + config.luaVersion = "lua54" |
| 27 | + end |
| 28 | + if type(config.console) ~= "string" then |
| 29 | + config.console = "internalConsole" |
| 30 | + end |
| 31 | + if type(config.sourceCoding) ~= "string" then |
| 32 | + config.sourceCoding = "utf8" |
| 33 | + end |
| 34 | + if type(config.outputCapture) ~= "table" then |
| 35 | + if config.console == "internalConsole" then |
| 36 | + config.outputCapture = { |
| 37 | + "print", |
| 38 | + "io.write", |
| 39 | + "stdout", |
| 40 | + "stderr", |
| 41 | + } |
| 42 | + else |
| 43 | + config.outputCapture = {} |
| 44 | + end |
| 45 | + end |
| 46 | + if type(config.pathFormat) ~= "string" then |
| 47 | + if config.useWSL then |
| 48 | + config.pathFormat = "path" |
| 49 | + else |
| 50 | + ---@NOTICE 后端无法获取前端的操作系统,只能假设和后端是一致的 |
| 51 | + local platform = require 'bee.platform' |
| 52 | + if platform.os == "windows" or platform.os == "macos" then |
| 53 | + config.pathFormat = "path" |
| 54 | + else |
| 55 | + config.pathFormat = "linuxpath" |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + -- sourceMaps 校验 |
| 61 | + if type(config.sourceMaps) == "table" then |
| 62 | + for _, sourceMap in ipairs(config.sourceMaps) do |
| 63 | + if type(sourceMap) ~= "table" or #sourceMap ~= 2 then |
| 64 | + error "Invalid sourceMaps." |
| 65 | + end |
| 66 | + end |
| 67 | + else |
| 68 | + config.sourceMaps = nil |
| 69 | + end |
| 70 | + |
| 71 | + -- client 默认值 |
| 72 | + if type(config.address) == "string" and type(config.client) ~= "boolean" then |
| 73 | + config.client = true |
| 74 | + end |
| 75 | + |
| 76 | + -- configuration.variables 默认值(后端无法获取 VSCode settings,使用空表) |
| 77 | + if type(config.configuration) ~= "table" then |
| 78 | + config.configuration = { |
| 79 | + variables = {}, |
| 80 | + } |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +return resolve_config |
0 commit comments