Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some luals warnings and polish some code #1251

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lua/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ end


local signs = {
DapBreakpoint = { text = "B", texthl = "", linehl = "", numhl = "" },
DapBreakpointCondition = { text = "C", texthl = "", linehl = "", numhl = "" },
DapBreakpointRejected = { text = 'R', texthl = '', linehl = '', numhl = '' },
DapLogPoint = { text = 'L', texthl = '', linehl = '', numhl = '' },
DapStopped = { text = '→', texthl = '', linehl = 'debugPC', numhl = '' },
DapBreakpoint = { text = "B", texthl = "SignColumn", linehl = "", numhl = "" },
DapBreakpointCondition = { text = "C", texthl = "SignColumn", linehl = "", numhl = "" },
DapBreakpointRejected = { text = 'R', texthl = "SignColumn", linehl = '', numhl = '' },
DapLogPoint = { text = 'L', texthl = "SignColumn", linehl = '', numhl = '' },
DapStopped = { text = '→', texthl = "SignColumn", linehl = 'debugPC', numhl = '' },
}

local function sign_try_define(name)
Expand Down
35 changes: 14 additions & 21 deletions lua/dap/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ function variable.is_lazy(var)
end


---@alias dap.entity.hl [string, integer, integer][]


---@param var dap.Variable|dap.EvaluateResponse
---@result string, dap.entity.hl[]
function variable.render_parent(var)
if var.name then
return variable.render_child(var, 0)
return variable.render_child(var --[[@as dap.Variable]], 0)
end
local syntax_group = var.type and syntax_mapping[var.type]
if syntax_group then
Expand All @@ -38,6 +42,9 @@ function variable.render_parent(var)
return var.result
end

---@param var dap.Variable
---@param indent integer
---@result string, dap.entity.hl[]
function variable.render_child(var, indent)
indent = indent or 0
local hl_regions = {
Expand All @@ -55,7 +62,7 @@ function variable.has_children(var)
return (var.variables and #var.variables > 0) or var.variablesReference ~= 0
end

---@param var dap.Variable
---@param var dap.Variable|dap.Scope
---@result dap.Variable[]
function variable.get_children(var)
return var.variables or {}
Expand All @@ -75,7 +82,8 @@ local function cmp_vars(a, b)
end


---@param var dap.Variable
---@param var dap.Variable|dap.Scope
---@param cb fun(variables: dap.Variable[])
function variable.fetch_children(var, cb)
local session = require('dap').session()
if var.variables then
Expand All @@ -100,6 +108,7 @@ function variable.fetch_children(var, cb)

table.sort(variables, cmp_vars)
for i, v in ipairs(variables) do
v.parent = var
if variable.is_lazy(v) then
variable.load_value(v, function(loaded_v)
variables[i] = loaded_v
Expand Down Expand Up @@ -150,23 +159,7 @@ function variable.load_value(var, cb)
end


local function get_parent(var, variables)
for _, v in pairs(variables) do
local children = variable.get_children(v)
if children then
if vim.tbl_contains(children, var) then
return v
end
local parent = get_parent(var, children)
if parent then
return parent
end
end
end
return nil
end


---@param item dap.Variable
local function set_variable(_, item, _, context)
local session = require('dap').session()
if not session then
Expand All @@ -177,7 +170,7 @@ local function set_variable(_, item, _, context)
utils.notify('Session has no active frame, cannot set variable')
return
end
local parent = get_parent(item, session.current_frame.scopes)
local parent = item.parent
if not parent then
utils.notify(string.format(
"Cannot set variable on %s, couldn't find its parent container",
Expand Down
8 changes: 5 additions & 3 deletions lua/dap/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ function M.create_logger(filename)
loggers[filename] = logger

local path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/"
local joinpath = vim.fs.joinpath or function(...)
local joinpath = (vim.fs or {}).joinpath or function(...)
---@diagnostic disable-next-line: deprecated
return table.concat(vim.tbl_flatten{...}, path_sep)
end
local logfilename = joinpath(vim.fn.stdpath('cache'), filename)
local cache_dir = vim.fn.stdpath('cache')
assert(type(cache_dir) == "string")
local logfilename = joinpath(cache_dir, filename)

local current_log_level = M.levels.INFO

Expand All @@ -42,7 +44,7 @@ function M.create_logger(filename)
return logfilename
end

vim.fn.mkdir(vim.fn.stdpath('cache'), "p")
vim.fn.mkdir(cache_dir, "p")
local logfile = assert(io.open(logfilename, "a+"))
for level, levelnr in pairs(M.levels) do
logger[level:lower()] = function(...)
Expand Down
6 changes: 6 additions & 0 deletions lua/dap/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
---@field indexedVariables? number
---@field memoryReference? string
---@field variables? dap.Variable[] resolved variablesReference. Not part of the spec; added by nvim-dap
---@field parent? dap.Variable|dap.Scope injected by nvim-dap

---@class dap.EvaluateArguments
---@field expression string
Expand Down Expand Up @@ -161,6 +162,11 @@
---@field adapterData nil|any


---@class dap.SourceResponse
---@field content string
---@field mimeType? string


---@class dap.Capabilities
---@field supportsConfigurationDoneRequest boolean|nil
---@field supportsFunctionBreakpoints boolean|nil
Expand Down
16 changes: 9 additions & 7 deletions lua/dap/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ local utils = require('dap.utils')
local M = {}

-- Copied from neovim rpc.lua
---@param header string
local function parse_headers(header)
if type(header) ~= 'string' then
return nil
end
local headers = {}
for line in vim.gsplit(header, '\r\n', true) do
for line in vim.gsplit(header, '\r\n', {plain = true}) do
if line == '' then
break
end
Expand All @@ -33,6 +31,12 @@ local function parse_chunk_loop()
local start, finish = buffer:find('\r\n\r\n', 1, true)
if start then
local buffer_start = buffer:find(header_start_pattern)
if not buffer_start then
error(string.format(
"Headers were expected but debug adapter sent: %s",
buffer
))
end
local headers = parse_headers(buffer:sub(buffer_start, start - 1))
local content_length = headers.content_length
local body_chunks = {buffer:sub(finish + 1)}
Expand Down Expand Up @@ -66,9 +70,7 @@ function M.create_read_loop(handle_body, on_no_chunk)
parse_chunk()
return function (err, chunk)
if err then
vim.schedule(function()
utils.notify(err, vim.log.levels.ERROR)
end)
utils.notify(err, vim.log.levels.ERROR)
return
end
if not chunk then
Expand Down
102 changes: 53 additions & 49 deletions lua/dap/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,22 @@ local function defaults(session)
return dap().defaults[session.config.type]
end

local function co_resume_schedule(co)
return function(...)
local args = {...}
vim.schedule(function()
coroutine.resume(co, unpack(args))
end)
end
end


local function co_resume(co)
local function coresume(co)
return function(...)
coroutine.resume(co, ...)
end
end


local function signal_err(err, cb)
if err then
if cb then
cb(err)
if coroutine.status(co) == "suspended" then
coroutine.resume(co, ...)
else
error(utils.fmt_error(err))
local args = {...}
vim.schedule(function()
assert(
coroutine.status(co) == "suspended",
"Incorrect use of coresume. Callee must have yielded"
)
coroutine.resume(co, unpack(args))
end)
end
return true
end
return false
end


Expand Down Expand Up @@ -533,7 +522,7 @@ local function frame_to_bufnr(session, frame)
end
local co = coroutine.running()
assert(co, 'Must run in coroutine')
session:source(source, co_resume(co))
session:source(source, coresume(co))
local _, bufnr = coroutine.yield()
return bufnr
end
Expand Down Expand Up @@ -573,7 +562,7 @@ end

--- Request a source
---@param source dap.Source
---@param cb fun(err, buf) the buffer will have the contents of the source
---@param cb fun(err: dap.ErrorResponse?, buf: integer?) the buffer will have the contents of the source
function Session:source(source, cb)
assert(source, 'source is required')
assert(source.sourceReference, 'sourceReference is required')
Expand All @@ -582,8 +571,12 @@ function Session:source(source, cb)
source = source,
sourceReference = source.sourceReference
}
self:request('source', params, function(err, response)
if signal_err(err, cb) then

---@param err dap.ErrorResponse
---@param response dap.SourceResponse
local function on_source(err, response)
if err then
cb(err, nil)
return
end
local buf = api.nvim_create_buf(false, true)
Expand All @@ -605,18 +598,25 @@ function Session:source(source, cb)
vim.bo[buf].filetype = filetype
end
end
if cb then
cb(nil, buf)
end
end)
cb(nil, buf)
end

self:request('source', params, on_source)
end


---@param cb fun(err: dap.ErrorResponse?)
function Session:update_threads(cb)
self:request('threads', nil, function(err, response)
if signal_err(err, cb) then return end

---@param err dap.ErrorResponse?
---@param response dap.ThreadResponse?
local on_threads = function(err, response)
if err then
cb(err)
return
end
local threads = {}
for _, thread in pairs(response.threads) do
for _, thread in ipairs((response or {}).threads) do
threads[thread.id] = thread
local old_thread = self.threads[thread.id]
if old_thread then
Expand All @@ -626,10 +626,10 @@ function Session:update_threads(cb)
end
self.threads = threads
self.dirty.threads = false
if cb then
cb(nil, threads)
end
end)
cb(nil)
end

self:request('threads', nil, on_threads)
end


Expand All @@ -652,7 +652,7 @@ function Session:event_stopped(stopped)
local co = coroutine.running()

if self.dirty.threads or (stopped.threadId and self.threads[stopped.threadId] == nil) then
self:update_threads(co_resume(co))
self:update_threads(coresume(co))
local err = coroutine.yield()
if err then
utils.notify('Error retrieving threads: ' .. utils.fmt_error(err), vim.log.levels.ERROR)
Expand Down Expand Up @@ -776,6 +776,9 @@ function Session:_request_scopes(current_frame)
---@param resp dap.VariableResponse?
local function on_variables(_, resp)
scope.variables = resp and resp.variables or nil
for _, v in ipairs(scope.variables or {}) do
v.parent = scope
end
end

local varparams = { variablesReference = scope.variablesReference }
Expand Down Expand Up @@ -1039,7 +1042,7 @@ local function start_debugging(self, request)
config.request = body.request

if type(adapter) == "function" then
adapter(co_resume_schedule(co), config, self)
adapter(coresume(co), config, self)
adapter = coroutine.yield()
end

Expand Down Expand Up @@ -1712,7 +1715,7 @@ function Session:request(command, arguments, callback)
if not callback then
co = coroutine.running()
if co then
callback = co_resume(co)
callback = coresume(co)
else
-- Assume missing callback is intentional.
-- Prevent error logging in Session:handle_body
Expand Down Expand Up @@ -1845,18 +1848,20 @@ function Session:_frame_delta(delta)
end
local frames = self.threads[self.stopped_thread_id].frames
assert(frames, 'Stopped thread must have frames')
local current_frame_index = index_of(frames, function(i) return i.id == self.current_frame.id end)
assert(current_frame_index, 'id of current frame must be present in frames')
local frameidx = index_of(frames, function(i)
return i.id == self.current_frame.id
end)
assert(frameidx, 'id of current frame must be present in frames')

current_frame_index = current_frame_index + delta
if current_frame_index < 1 then
current_frame_index = 1
frameidx = frameidx + delta
if frameidx < 1 then
frameidx = 1
utils.notify("Can't move past first frame", vim.log.levels.INFO)
elseif current_frame_index > #frames then
current_frame_index = #frames
elseif frameidx > #frames then
frameidx = #frames
utils.notify("Can't move past last frame", vim.log.levels.INFO)
end
self:_frame_set(frames[current_frame_index])
self:_frame_set(frames[frameidx])
end


Expand All @@ -1869,7 +1874,6 @@ end
function Session.event_process()
end


function Session.event_loadedSource()
end

Expand Down
Loading
Loading