Skip to content

Commit 59fca2a

Browse files
committed
add support for lines/columns start at 1 option
1 parent 08659cb commit 59fca2a

3 files changed

Lines changed: 84 additions & 12 deletions

File tree

‎src/moonbug.lua‎

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,52 @@ local function frame_setlocal(handle, ordinal, index, value)
14211421
end
14221422
end
14231423

1424+
---@param value integer?
1425+
---@param field "line"|"column"
1426+
---@return integer?
1427+
local function position_from_client(value, field)
1428+
if not value then
1429+
return nil
1430+
end
1431+
1432+
assert(field == "column" or field == "line")
1433+
1434+
if not session.client_args then
1435+
return value
1436+
end
1437+
1438+
local startsAt1 = field == "line" and session.client_args.linesStartAt1 or session.client_args.columnsStartAt1
1439+
1440+
if startsAt1 == false then
1441+
return value + 1
1442+
end
1443+
1444+
return value
1445+
end
1446+
1447+
---@param value integer?
1448+
---@param field "line"|"column"
1449+
---@return integer?
1450+
local function position_to_client(value, field)
1451+
if not value then
1452+
return nil
1453+
end
1454+
1455+
assert(field == "column" or field == "line")
1456+
1457+
if not session.client_args then
1458+
return value
1459+
end
1460+
1461+
local startsAt1 = field == "line" and session.client_args.linesStartAt1 or session.client_args.columnsStartAt1
1462+
1463+
if startsAt1 == false then
1464+
return value - 1
1465+
end
1466+
1467+
return value
1468+
end
1469+
14241470
---@param object moonbug.dap.ProtocolMessage
14251471
local function session_send_seq(object)
14261472
session.seq = session.seq + 1
@@ -1444,8 +1490,9 @@ end
14441490
---@param output string
14451491
---@param source? moonbug.dap.Source
14461492
---@param line? integer
1493+
---@param column? integer
14471494
---@return boolean
1448-
local function session_send_output(category, output, source, line)
1495+
local function session_send_output(category, output, source, line, column)
14491496
if not session.ready or not session.client then
14501497
return false
14511498
end
@@ -1454,7 +1501,8 @@ local function session_send_output(category, output, source, line)
14541501
category = category,
14551502
output = output,
14561503
source = source,
1457-
line = line,
1504+
line = position_to_client(line, "line"),
1505+
column = position_to_client(column, "column"),
14581506
})
14591507
return true
14601508
end
@@ -2254,7 +2302,7 @@ function RequestHandler.handle_set_breakpoints(req)
22542302
session.breakpoints[path] = {}
22552303

22562304
for _, bp in ipairs(args.breakpoints or {}) do
2257-
local line = bp.line
2305+
local line = position_from_client(bp.line, "line")
22582306
local ok = path ~= "" and line ~= nil
22592307

22602308
if ok then
@@ -2270,6 +2318,7 @@ function RequestHandler.handle_set_breakpoints(req)
22702318

22712319
log.debug(" set breakpoint: %s:%d%s", path, line, condition)
22722320

2321+
---@cast line integer
22732322
session.breakpoints[path][line] = {
22742323
condition = bp.condition,
22752324
hit_condition = bp.hitCondition,
@@ -2278,7 +2327,7 @@ function RequestHandler.handle_set_breakpoints(req)
22782327
}
22792328
end
22802329

2281-
table.insert(list, { line = line, verified = ok })
2330+
table.insert(list, { line = position_to_client(line, "line"), verified = ok })
22822331
end
22832332

22842333
session_send_response(req, true, { breakpoints = list })
@@ -2351,8 +2400,8 @@ function RequestHandler.handle_stack_trace(req)
23512400
table.insert(frames, {
23522401
id = session.next_frame_id,
23532402
name = name,
2354-
line = line,
2355-
column = 1,
2403+
line = position_to_client(line, "line"),
2404+
column = position_to_client(1, "column"),
23562405
source = {
23572406
path = path,
23582407
name = (info.short_src or ""):match "[^/\\]+$" or info.short_src,
@@ -2368,8 +2417,8 @@ function RequestHandler.handle_stack_trace(req)
23682417
table.insert(frames, {
23692418
id = session.next_frame_id,
23702419
name = "unable to access main thread while in a coroutine",
2371-
line = 0,
2372-
column = 1,
2420+
line = position_to_client(1, "line"),
2421+
column = position_to_client(1, "column"),
23732422
})
23742423

23752424
session.next_frame_id = session.next_frame_id + 1
@@ -2664,7 +2713,9 @@ end
26642713
function RequestHandler.handle_completions(req)
26652714
local args = req.arguments or {}
26662715
local text = args.text or ""
2667-
local column = args.column or 1
2716+
local column = position_from_client(args.column or 1, "column")
2717+
2718+
---@cast column integer
26682719
local base, prefix, separator, prefix_start = split_completion_input(text, column)
26692720

26702721
---@type moonbug.dap.CompletionItem[]
@@ -2673,12 +2724,15 @@ function RequestHandler.handle_completions(req)
26732724
if session.paused then
26742725
local frame_handle = nil
26752726
local depth = nil
2727+
26762728
if args.frameId then
26772729
frame_handle, depth = find_frame(args.frameId)
26782730
end
2731+
26792732
if base then
26802733
-- member completion, resolve the base expr and then enumerate keys
26812734
local base_value = nil
2735+
26822736
if depth then
26832737
if frame_handle == current_handle() then
26842738
local ok, res = evaluate_expr(depth, base, nil, "watch")
@@ -2689,6 +2743,7 @@ function RequestHandler.handle_completions(req)
26892743
elseif _G[base] ~= nil then
26902744
base_value = _G[base]
26912745
end
2746+
26922747
if type(base_value) == "table" then
26932748
targets = complete_fields(base_value, prefix, separator)
26942749
end
@@ -2710,7 +2765,7 @@ function RequestHandler.handle_completions(req)
27102765
end
27112766

27122767
for _, t in ipairs(targets) do
2713-
t.start = prefix_start
2768+
t.start = position_to_client(prefix_start, "column")
27142769
t.length = #prefix
27152770
end
27162771

‎tests/dap_session.lua‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ function M:configure(opts)
261261

262262
local initialize = opts.initialize or {}
263263
initialize.adapterID = initialize.adapterID or "moonbug-tests"
264-
initialize.linesStartAt1 = true
265-
initialize.columnsStartAt1 = true
266264

267265
local response = M.assert_success(self:request("initialize", initialize))
268266
self:wait_for_event "initialized"

‎tests/integration/test_01_simple.lua‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,22 @@ test("returns an error for unsupported commands", function()
109109
}))
110110
end)
111111
end)
112+
113+
test("supports zero based lines and columns", function()
114+
dap.with_session(program, function(session)
115+
local capabilities = session:configure {
116+
initialize = {
117+
adapterID = "moonbug-tests",
118+
linesStartAt1 = false,
119+
columnsStartAt1 = false,
120+
},
121+
breakpoints = {
122+
{ line = breakpoint_line - 1 },
123+
},
124+
}
125+
126+
local stop = session:wait_for_stop "breakpoint"
127+
expect.eq(breakpoint_line - 1, stop.frames[1].line)
128+
expect.eq(0, stop.frames[1].column)
129+
end)
130+
end)

0 commit comments

Comments
 (0)