Skip to content

Commit

Permalink
feat(dap): auto-generate source map to allow stepping into std (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Actions <actions@github>
  • Loading branch information
mrcjkb and Github Actions authored Oct 30, 2023
1 parent 94787c8 commit f7415da
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.3.0] - 2023-10-30

### Added
- DAP: Auto-generate source map, to allow stepping into `std`.

## [3.2.1] - 2023-10-29

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ RustaceanDapOpts *RustaceanDapOpts*
@field rust-analyzer? table Options to send to rust-analyzer. See: https://rust-analyzer.github.io/manual.html#configuration

Fields: ~
{adapter?} (RustaceanDapAdapterOpts) Options for the debug adapter
{adapter?} (RustaceanDapAdapterOpts) Options for the debug adapter
{auto_generate_source_map} (fun():boolean|boolean) Whether to auto-generate a source map for the standard library.


RustaceanDapAdapterOpts *RustaceanDapAdapterOpts*
Expand Down
1 change: 1 addition & 0 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ vim.g.rustaceanvim = vim.g.rustaceanvim

---@class RustaceanDapOpts
---@field adapter? RustaceanDapAdapterOpts Options for the debug adapter
---@field auto_generate_source_map fun():boolean | boolean Whether to auto-generate a source map for the standard library.

---@class RustaceanDapAdapterOpts
---@field type? string The type of debug adapter (default: `"executable"`)
Expand Down
5 changes: 5 additions & 0 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ local RustaceanDefaultConfig = {
---@type string
name = 'lldb',
},
--- Whether to auto-generate a source map for the standard library.
---@type boolean | fun():boolean
auto_generate_source_map = function()
return vim.fn.executable('rustc') == 1
end,
},
}

Expand Down
55 changes: 52 additions & 3 deletions lua/rustaceanvim/dap.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config = require('rustaceanvim.config.internal')
local compat = require('rustaceanvim.compat')
local Types = require('rustaceanvim.types.internal')

local function scheduled_error(err)
vim.schedule(function()
Expand Down Expand Up @@ -57,12 +58,60 @@ local function get_cargo_args_from_runnables_args(runnable_args)
return cargo_args
end

---@param callback fun(commit_hash:string)
local function get_rustc_commit_hash(callback)
compat.system({ 'rustc', '--version', '--verbose' }, nil, function(sc)
---@cast sc vim.SystemCompleted
local result = sc.stdout
if sc.code ~= 0 or result == nil then
return
end
local commit_hash = result:match('commit%-hash:%s+([^\n]+)$')
if not commit_hash then
return
end
callback(commit_hash)
end)
end

local function get_rustc_sysroot(callback)
compat.system({ 'rustc', '--print', 'sysroot' }, nil, function(sc)
---@cast sc vim.SystemCompleted
local result = sc.stdout
if sc.code ~= 0 or result == nil then
return
end
callback(result)
end)
end

---@alias DapSourceMap {[string]: string}

---@type DapSourceMap
local source_map = {}

---See https://github.com/vadimcn/codelldb/issues/204
local function generate_source_map()
get_rustc_commit_hash(function(commit_hash)
get_rustc_sysroot(function(rustc_sysroot)
---@type DapSourceMap
local new_map = {
[compat.joinpath('/rustc/', commit_hash)] = compat.joinpath(rustc_sysroot, '/lib/rustlib/src/rust'),
}
vim.tbl_extend('force', source_map, { new_map })
end)
end)
end

---@param args RADebuggableArgs
function M.start(args)
local cargo_args = get_cargo_args_from_runnables_args(args)

vim.notify('Compiling a debug build for debugging. This might take some time...')

if Types.evaluate(config.dap.auto_generate_source_map) then
generate_source_map()
end

local cargo_args = get_cargo_args_from_runnables_args(args)
local cmd = vim.list_extend({ 'cargo' }, cargo_args)
compat.system(cmd, { cwd = args.workspaceRoot }, function(sc)
---@cast sc vim.SystemCompleted
Expand Down Expand Up @@ -135,7 +184,7 @@ function M.start(args)
runInTerminal = false,
}
-- start debugging
dap.run(dap_config)
dap.run(vim.tbl_deep_extend('force', dap_config, { sourceMap = source_map }))
end)
end)
end
Expand Down

0 comments on commit f7415da

Please sign in to comment.