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.
This commit is contained in:
Leiser Fernández Gallo 2024-05-17 17:22:18 +02:00 committed by GitHub
parent 78296bfabf
commit 03c607c2bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 12 deletions

View file

@ -1,4 +1,4 @@
*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 16
*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 17
==============================================================================
Table of Contents *luasnip-table-of-contents*

View file

@ -1,3 +1,16 @@
local function fts_from_ts_lang(lang)
local fts = {}
-- In case of someone using nvim <= 0.9
if vim.treesitter.language and vim.treesitter.language.get_filetypes then
fts = vim.treesitter.language.get_filetypes(lang)
end
-- Keep lang as part of the result, for backward compatibility
if not vim.list_contains(fts, lang) then
table.insert(fts, lang)
end
return fts
end
local function from_cursor_pos()
-- get_parser errors if parser not present (no grammar for language).
local has_parser, parser = pcall(vim.treesitter.get_parser)
@ -5,23 +18,22 @@ local function from_cursor_pos()
if has_parser then
local cursor = require("luasnip.util.util").get_cursor_0ind()
-- assumption: languagetree uses 0-indexed byte-ranges.
return {
parser
:language_for_range({
cursor[1],
cursor[2],
cursor[1],
cursor[2],
})
:lang(),
}
local lang = parser
:language_for_range({
cursor[1],
cursor[2],
cursor[1],
cursor[2],
})
:lang()
return fts_from_ts_lang(lang)
else
return {}
end
end
local function from_filetype()
return vim.split(vim.bo.filetype, ".", true)
return vim.split(vim.bo.filetype, ".", { plain = true, trimemtpy = false })
end
-- NOTE: Beware that the resulting filetypes may differ from the ones in `vim.bo.filetype`. (for