Skip to content

Commit 03c607c

Browse files
authored
ft_func: translate treesitter-language to filetypes (close #1174).
Sometimes the treesitter-language-name coincides with the filetype (like for cpp), but in other cases, these are wholly different (lang: latex, filetype: tex/sty/cls). So, to be more accomodating and forego the need on the users part to define the correct `ls.filetype_extend`, we resolve language to filetypes in here.
1 parent 78296bf commit 03c607c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

doc/luasnip.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 16
1+
*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 17
22

33
==============================================================================
44
Table of Contents *luasnip-table-of-contents*

lua/luasnip/extras/filetype_functions.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
1+
local function fts_from_ts_lang(lang)
2+
local fts = {}
3+
-- In case of someone using nvim <= 0.9
4+
if vim.treesitter.language and vim.treesitter.language.get_filetypes then
5+
fts = vim.treesitter.language.get_filetypes(lang)
6+
end
7+
-- Keep lang as part of the result, for backward compatibility
8+
if not vim.list_contains(fts, lang) then
9+
table.insert(fts, lang)
10+
end
11+
return fts
12+
end
13+
114
local function from_cursor_pos()
215
-- get_parser errors if parser not present (no grammar for language).
316
local has_parser, parser = pcall(vim.treesitter.get_parser)
417

518
if has_parser then
619
local cursor = require("luasnip.util.util").get_cursor_0ind()
720
-- assumption: languagetree uses 0-indexed byte-ranges.
8-
return {
9-
parser
10-
:language_for_range({
11-
cursor[1],
12-
cursor[2],
13-
cursor[1],
14-
cursor[2],
15-
})
16-
:lang(),
17-
}
21+
local lang = parser
22+
:language_for_range({
23+
cursor[1],
24+
cursor[2],
25+
cursor[1],
26+
cursor[2],
27+
})
28+
:lang()
29+
return fts_from_ts_lang(lang)
1830
else
1931
return {}
2032
end
2133
end
2234

2335
local function from_filetype()
24-
return vim.split(vim.bo.filetype, ".", true)
36+
return vim.split(vim.bo.filetype, ".", { plain = true, trimemtpy = false })
2537
end
2638

2739
-- NOTE: Beware that the resulting filetypes may differ from the ones in `vim.bo.filetype`. (for

0 commit comments

Comments
 (0)