Skip to content

Commit 3b21eba

Browse files
committed
fix: Mapping TS lang → filetype - closes #93
1 parent 83e3039 commit 3b21eba

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

lua/debugprint/init.lua

+16-15
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,19 @@ local debuginfo = function(opts)
7272
return line
7373
end
7474

75-
local filetype_configured = function()
76-
local effective_filetype = utils.get_effective_filetype()
75+
local get_filetype_config = function()
76+
local effective_filetypes = utils.get_effective_filetypes()
7777

78-
return vim.tbl_contains(
79-
vim.tbl_keys(global_opts.filetypes),
80-
effective_filetype
81-
)
82-
end
78+
for _, effective_filetype in ipairs(effective_filetypes) do
79+
if global_opts.filetypes[effective_filetype] ~= nil then
80+
return global_opts.filetypes[effective_filetype]
81+
end
82+
end
8383

84-
local construct_debugprint_line = function(opts, effective_filetype)
85-
local fileconfig = global_opts.filetypes[effective_filetype]
84+
return nil
85+
end
8686

87+
local construct_debugprint_line = function(opts, fileconfig)
8788
local line_to_insert
8889

8990
if opts.variable_name then
@@ -120,16 +121,16 @@ local construct_error_line = function(errormsg)
120121
end
121122

122123
local addline = function(opts)
123-
local effective_filetype = utils.get_effective_filetype()
124-
125124
local line_to_insert
126125

127-
if filetype_configured() then
128-
line_to_insert = construct_debugprint_line(opts, effective_filetype)
126+
local fileconfig = get_filetype_config()
127+
128+
if fileconfig ~= nil then
129+
line_to_insert = construct_debugprint_line(opts, fileconfig)
129130
else
130131
line_to_insert = construct_error_line(
131132
"No debugprint configuration for filetype "
132-
.. effective_filetype
133+
.. utils.get_effective_filetypes()[1]
133134
.. "; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes"
134135
)
135136
end
@@ -163,7 +164,7 @@ local cache_request = nil
163164

164165
M.debugprint_cache = function(opts)
165166
if opts and opts.prerepeat == true then
166-
if filetype_configured() and opts.variable == true then
167+
if get_filetype_config() and opts.variable == true then
167168
opts.variable_name = utils.get_variable_name(
168169
global_opts.ignore_treesitter,
169170
opts.ignore_treesitter

lua/debugprint/utils.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ M.set_callback = function(func_name)
9494
vim.go.operatorfunc = func_name
9595
end
9696

97-
M.get_effective_filetype = function()
97+
M.get_effective_filetypes = function()
9898
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1] - 1
9999
-- Looking at the last column is more accurate because there are some
100100
-- embeddings (e.g. JS in HTML) where the Treesitter embedding doesn't begin
@@ -108,16 +108,20 @@ M.get_effective_filetype = function()
108108
-- make embedded languages work
109109
parser:parse(true)
110110

111-
return parser
111+
local treesitter_lang = parser
112112
:language_for_range({
113113
current_line_nr,
114114
current_line_col,
115115
current_line_nr,
116116
current_line_col,
117117
})
118118
:lang()
119+
120+
local filetypes = vim.treesitter.language.get_filetypes(treesitter_lang)
121+
assert(vim.tbl_count(filetypes) > 0)
122+
return filetypes
119123
else
120-
return vim.api.nvim_get_option_value("filetype", { scope = "local" })
124+
return { vim.api.nvim_get_option_value("filetype", { scope = "local" }) }
121125
end
122126
end
123127

tests/debugprint.lua

+20
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ describe("can do various file types", function()
454454
})
455455
end)
456456

457+
it("can handle a file where ext != filetype", function()
458+
local filename = init_file({
459+
"const Foo: FunctionalComponent = () => {",
460+
" return <div>Hello World!</div>;",
461+
"};",
462+
}, "tsx", 1, 0)
463+
464+
vim.api.nvim_set_option_value("shiftwidth", 4, {})
465+
feedkeys("g?p")
466+
467+
check_lines({
468+
"const Foo: FunctionalComponent = () => {",
469+
' console.warn("DEBUGPRINT[1]: '
470+
.. filename
471+
.. ':1 (after const Foo: FunctionalComponent = () => )")',
472+
" return <div>Hello World!</div>;",
473+
"};",
474+
})
475+
end)
476+
457477
it("can gracefully handle unknown filetypes", function()
458478
init_file({
459479
"foo",

0 commit comments

Comments
 (0)