Skip to content

Commit

Permalink
Add a .scopes command to the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Apr 17, 2020
1 parent 8b83a28 commit fa0892f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
26 changes: 18 additions & 8 deletions lua/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ local ns_breakpoints = 'dap_breakpoints'
local ns_pos = 'dap_pos'
local Session = {}
local session = nil
local threads = nil


vim.fn.sign_define('DapBreakpoint', {text='B', texthl='', linehl='', numhl=''})
Expand Down Expand Up @@ -96,7 +95,8 @@ function Session:event_stopped(stopped)
print('Error retrieving threads: ' .. err0.message)
return
end
threads = {}
local threads = {}
self.threads = threads
for _, thread in pairs(threads_resp.threads) do
threads[thread.id] = thread
end
Expand Down Expand Up @@ -155,7 +155,6 @@ end

function Session:event_terminated()
self:close()
threads = nil
session = nil
ui.threads_clear()
end
Expand Down Expand Up @@ -216,6 +215,7 @@ function Session:connect(config)
config = config;
seq = 0;
stopped_thread_id = nil;
threads = {};
}
client:connect('127.0.0.1', port, function(err)
if (err) then print(err) end
Expand Down Expand Up @@ -250,7 +250,7 @@ end

function Session:close()
vim.fn.sign_unplace(ns_pos)

self.threads = {}
self.message_callbacks = nil
self.client:shutdown()
self.client:close()
Expand Down Expand Up @@ -287,30 +287,39 @@ function Session:evaluate(expression, fn)
self:request('evaluate', {
expression = expression;
context = 'repl';
frameId = ((threads or {}).current_frame or {}).id;
frameId = (self.threads.current_frame or {}).id;
}, fn)
end


function Session:_reset_stopped()
local thread_id = self.stopped_thread_id
self.stopped_thread_id = nil
vim.fn.sign_unplace(ns_pos)
return thread_id
end


function Session:continue()
if not self.stopped_thread_id then
print('No stopped thread. Cannot continue')
return
end
self:request('continue', { threadId = self.stopped_thread_id; }, function(err0, _)
local thread_id = self:_reset_stopped()
self:request('continue', { threadId = thread_id; }, function(err0, _)
if err0 then
print("Error continueing: " .. err0.message)
end
end)
end


function Session:next()
if not self.stopped_thread_id then
print('No stopped thread. Cannot move')
return
end
session:request('next', { threadId = session.stopped_thread_id })
local thread_id = self:_reset_stopped()
session:request('next', { threadId = thread_id; })
end


Expand Down Expand Up @@ -402,6 +411,7 @@ function M.attach(config)
session.capabilities = result
session:attach(config)
end)
return session
end


Expand Down
12 changes: 11 additions & 1 deletion lua/dap/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ function M.execute(text)
append('No active debug session')
return
end
local lnum = vim.fn.line('$') - 1
if text == '.continue' or text == '.c' then
session:continue()
elseif text == '.next' or text == '.n' then
session:next()
elseif text == '.scopes' then
local frame = session.threads.current_frame
if frame then
for _, scope in pairs(frame.scopes) do
append(scope.name)
for _, variable in pairs(scope.variables) do
append(string.rep(' ', 2) .. variable.name .. ': ' .. variable.value)
end
end
end
else
local lnum = vim.fn.line('$') - 1
session:evaluate(text, function(err, resp)
if err then
append(err.message, lnum)
Expand Down

0 comments on commit fa0892f

Please sign in to comment.