Skip to content

Commit 3ff7b4a

Browse files
committed
fix: wrong parsing with leading spaces
1 parent b4b1b7e commit 3ff7b4a

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

lua/fix/document.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ end
151151

152152
---@param buf number
153153
---@param lnum number 0-based
154+
---@param line_text string
154155
---@return TSNode|nil node, boolean covered -- covered=false: the tree does not span lnum (stale/in-flight parse)
155-
local function message_node_at(buf, lnum)
156+
local function message_node_at(buf, lnum, line_text)
156157
local ok, parser = pcall(vim.treesitter.get_parser, buf, "fix")
157158
if not ok or not parser then
158159
error("No FIX parser for buffer " .. buf)
@@ -162,7 +163,9 @@ local function message_node_at(buf, lnum)
162163
if lnum > end_row or (lnum == end_row and end_col == 0) then
163164
return nil, false
164165
end
165-
local node = root:descendant_for_range(lnum, 0, lnum, 0)
166+
local first_nonblank = line_text:find("%S")
167+
local col = first_nonblank and first_nonblank - 1 or 0
168+
local node = root:descendant_for_range(lnum, col, lnum, col)
166169
while node and node:type() ~= "message" do
167170
node = node:parent()
168171
end
@@ -187,7 +190,7 @@ function M.build_line(buf, lnum, line_text, key)
187190

188191
local semantic = Cache.get_semantic(key)
189192
if semantic == nil then
190-
local node, covered = message_node_at(buf, lnum)
193+
local node, covered = message_node_at(buf, lnum, line_text)
191194
if node then
192195
semantic = semantic_from_node(buf, node)
193196
Cache.put_semantic(key, semantic)

lua/fix/init.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
-- TODO: yank in picker
77
-- TODO: highlight values based on Type
88

9-
-- testing:
10-
-- TODO: test with spaces at the start
11-
129
---@class FixOpts
1310
---@field ft? table
1411
---@field ft.extensions? string[]

tests/integration/test_edge_cases.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,47 @@ T["duplicate tags render both extmarks"] = function()
5959
MiniTest.expect.equality(H.inline_label_count(nvim(), "Symbol"), 2)
6060
end
6161

62+
T["message with leading spaces is annotated"] = function()
63+
nvim().cmd("enew")
64+
nvim().lua([[
65+
vim.api.nvim_buf_set_lines(0, 0, -1, false, {
66+
" 8=FIX.4.4|9=20|35=D|49=A|56=B|10=000|",
67+
})
68+
]])
69+
nvim().cmd("set filetype=fix")
70+
H.wait_annotated(nvim())
71+
72+
H.expect_inline_label(nvim(), "BeginString")
73+
H.expect_inline_label(nvim(), "NewOrderSingle")
74+
75+
local decoded = nvim().lua_get([[
76+
(function()
77+
local message = require("fix.document").build_line(0, 0)
78+
if message == nil then
79+
return nil
80+
end
81+
local first = message:list_fields()[1]
82+
return {
83+
version = message.version,
84+
tag = first.tag,
85+
tag_start = first.tag_start,
86+
tag_end = first.tag_end,
87+
value_start = first.value_start,
88+
value_end = first.value_end,
89+
}
90+
end)()
91+
]])
92+
MiniTest.expect.equality(decoded, {
93+
version = "FIX.4.4",
94+
tag = 8,
95+
tag_start = 3,
96+
tag_end = 4,
97+
value_start = 5,
98+
value_end = 12,
99+
})
100+
H.expect_no_error_notifications(nvim())
101+
end
102+
62103
T["empty buffer produces no extmarks and no errors"] = function()
63104
H.load_fixture(nvim(), "empty.fix", { expect_extmarks = false, timeout_ms = 100 })
64105
MiniTest.expect.equality(#H.get_extmarks(nvim()), 0)

0 commit comments

Comments
 (0)