save and load breaks

This commit is contained in:
ray-x 2022-05-14 01:07:44 +10:00
parent e54ed67325
commit f3e1da9968
3 changed files with 60 additions and 42 deletions

View File

@ -198,6 +198,9 @@ function go.setup(cfg)
vim.cmd(
[[command! -nargs=* -complete=custom,v:lua.package.loaded.go.dbg_complete GoDebug lua require"go.dap".run(<f-args>)]]
)
vim.cmd([[command! GoBreakSave lua require"go.dap".save_brks()]])
vim.cmd([[command! GoBreakLoad lua require"go.dap".load_brks()]])
vim.cmd([[command! GoDebugConfig lua require"go.launch".config()]])
vim.cmd([[command! GoBreakToggle lua require"go.dap".breakpt()]])
vim.cmd([[command! BreakCondition lua require"dap".set_breakpoint(vim.fn.input("Breakpoint condition: "))]])

View File

@ -123,6 +123,43 @@ M.breakpt = function()
require("dap").toggle_breakpoint()
end
M.save_bks = function()
local bks = require("dap.breakpoints").get()
local all_bks = {}
if bks and next(bks) then
local cfg, fld = require("go.project_setup").setup_project()
for bufnr, bk in pairs(bks) do
local uri = vim.uri_from_bufnr(bufnr)
all_bks[uri] = bk
end
local bkfile = fld .. utils.sep() .. "breakpoints.lua"
local writeStr = "return " .. vim.inspect(all_bks)
local writeLst = vim.split(writeStr, "\n")
vim.fn.writefile(writeLst, bkfile, "b")
end
end
M.load_bks = function()
utils.load_plugin("nvim-dap", "dap")
local _, brkfile = require("go.project_setup").project_existed()
if vim.fn.filereadable(brkfile) == 0 then
return
end
local f = assert(loadfile(brkfile))
local brks = f()
for uri, brk in pairs(brks) do
local bufnr = vim.uri_to_bufnr(uri)
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
for index, lnum in ipairs(brk) do
require("dap.breakpoints").set({}, bufnr, lnum.line)
end
end
end
local stdout, stderr, handle
M.run = function(...)
local args = { ... }

View File

@ -9,48 +9,12 @@ return {
max_line_len = 120,
tag_transform = false,
test_dir = "",
comment_placeholder = "",
icons = { breakpoint = "🧘", currentpos = "🏃" }, -- set to false to disable icons setup
verbose = false,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
lsp_cfg = false, -- false: do nothing
-- true: apply non-default gopls setup defined in go/lsp.lua
-- if lsp_cfg is a table, merge table with with non-default gopls setup in go/lsp.lua, e.g.
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
lsp_on_attach = nil, -- nil: use on_attach function defined in go/lsp.lua for gopls,
-- when lsp_cfg is true
-- if lsp_on_attach is a function: use this function as on_attach function for gopls,
-- when lsp_cfg is true
lsp_format_on_save = 1,
lsp_document_formatting = true,
-- set to true: use gopls to format
-- false if you want to use other formatter tool(e.g. efm, nulls)
lsp_keymaps = true, -- true: use default keymaps defined in go/lsp.lua
lsp_codelens = true,
lsp_diag_hdlr = true, -- hook lsp diag handler
-- virtual text setup
lsp_diag_virtual_text = { space = 0, prefix = "" },
lsp_diag_signs = true,
lsp_diag_update_in_insert = false,
go_boilplater_url = "https://github.com/thockin/go-build-template.git",
gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" }
gopls_remote_auto = true,
gocoverage_sign = "",
gocoverage_sign_priority = 5,
launch_json = nil, -- the launch.json file path, default to .vscode/launch.json
-- launch_json = vim.fn.getcwd() .. "/.vscode/launch.json",
dap_debug = true,
dap_debug_gui = true,
dap_debug_keymap = true, -- true: use keymap for debugger defined in go/dap.lua
-- false: do not use keymap in go/dap.lua. you must define your own.
dap_vt = true, -- false, true and 'all frames'
dap_port = 38697, -- can be set to a number or `-1` so go.nvim will pickup a random port
build_tags = "", --- you can provide extra build tags for tests or debugger
textobjects = true, -- treesitter binding for text objects
test_runner = "go", -- richgo, go test, richgo, dlv, ginkgo
verbose_tests = true, -- set to add verbose flag to tests
run_in_floaterm = false, -- set to true to run in float window.
}
]]
@ -62,17 +26,31 @@ local log = util.log
local M = {}
local sep = require("go.utils").sep()
function M.project_existed()
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
local gocfgfd = workfolder .. sep .. ".gonvim"
local gocfgbrks = gocfgfd .. sep .. "breakpoints.lua"
local gocfg = gocfgfd .. sep .. "init.lua"
if vim.fn.filereadable(gocfg) == 1 or vim.fn.filereadable(gocfgbrks) == 1 then
log("projects existed", gocfg, gocfgbrks)
return gocfg, gocfgbrks
end
end
function M.setup_project()
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
local gocfg = workfolder .. sep .. ".gonvim"
local gocfgfd = workfolder .. sep .. ".gonvim"
local gocfg = gocfgfd .. sep .. "init.lua"
if vim.fn.filereadable(gocfg) == 1 then
return gocfg
else
if vim.fn.isdirectory(gocfgfd) == 0 then
vim.fn.mkdir(gocfgfd)
end
if vim.fn.filereadable(gocfg) == 0 then
local f = io.open(gocfg, "w")
f:write("return {}")
f:close()
end
return gocfg, gocfgfd
end
function M.load_project()