Skip to content

Commit b813797

Browse files
committed
fix: Accuracy finding embedded langs - closes #84
1 parent 2ba2dca commit b813797

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

lua/debugprint/utils.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ local get_node_at_cursor = function()
2929
end
3030

3131
M.get_effective_filetype = function()
32-
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]
33-
local current_line_col = vim.api.nvim_win_get_cursor(0)[2]
32+
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1] - 1
33+
-- Looking at the last column is more accurate because there are some
34+
-- embeddings (e.g. JS in HTML) where the Treesitter embedding doesn't begin
35+
-- until the first non-whitespace column
36+
local current_line_col = vim.fn.col('$')
3437

3538
local success, parser = pcall(vim.treesitter.get_parser, 0)
3639

3740
if success then
3841
-- For some reason I don't understand, this parse line is necessary to
3942
-- make embedded languages work
40-
parser:parse({ 0, current_line_nr })
43+
parser:parse(true)
4144

4245
return parser
4346
:language_for_range({

tests/debugprint.lua

+50
Original file line numberDiff line numberDiff line change
@@ -1481,5 +1481,55 @@ if vim.fn.has("nvim-0.9.0") == 1 then
14811481
"bar",
14821482
})
14831483
end)
1484+
1485+
it("lua in markdown above", function()
1486+
local filename = init_file({
1487+
"foo",
1488+
"```lua",
1489+
"x = 1",
1490+
"```",
1491+
"bar",
1492+
}, "markdown", 3, 0)
1493+
1494+
feedkeys("g?P")
1495+
1496+
check_lines({
1497+
"foo",
1498+
"```lua",
1499+
"print('DEBUGPRINT[1]: " .. filename .. ":3 (before x = 1)')",
1500+
"x = 1",
1501+
"```",
1502+
"bar",
1503+
})
1504+
end)
1505+
1506+
it("javascript in html", function()
1507+
local filename = init_file({
1508+
"<html>",
1509+
"<body>",
1510+
"<script>",
1511+
" let x = 3;",
1512+
"",
1513+
" console.log(x);",
1514+
"</script>",
1515+
"</body>",
1516+
"</html>",
1517+
}, "html", 6, 0)
1518+
1519+
feedkeys("g?p")
1520+
1521+
check_lines({
1522+
"<html>",
1523+
"<body>",
1524+
"<script>",
1525+
" let x = 3;",
1526+
"",
1527+
" console.log(x);",
1528+
" console.warn(\"DEBUGPRINT[1]: " .. filename .. ":6 (after console.log(x);)\")",
1529+
"</script>",
1530+
"</body>",
1531+
"</html>",
1532+
})
1533+
end)
14841534
end)
14851535
end

0 commit comments

Comments
 (0)