From be4b8a5a488355ade4d7f9a470a911c0f844ec18 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Thu, 16 May 2024 17:02:15 +0200 Subject: [PATCH 1/5] Use filetypes instead of langs from treesitter --- lua/luasnip/extras/filetype_functions.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/luasnip/extras/filetype_functions.lua b/lua/luasnip/extras/filetype_functions.lua index c3bc0bf44..e0bec30fe 100644 --- a/lua/luasnip/extras/filetype_functions.lua +++ b/lua/luasnip/extras/filetype_functions.lua @@ -6,14 +6,14 @@ local function from_cursor_pos() local cursor = require("luasnip.util.util").get_cursor_0ind() -- assumption: languagetree uses 0-indexed byte-ranges. return { - parser + vim.treesitter.language.get_filetypes(parser :language_for_range({ cursor[1], cursor[2], cursor[1], cursor[2], }) - :lang(), + :lang()), } else return {} From c48698f4aa48a37e730f447e427c66105037a500 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Thu, 16 May 2024 15:03:28 +0000 Subject: [PATCH 2/5] Auto generate docs --- doc/luasnip.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/luasnip.txt b/doc/luasnip.txt index 96365061d..32f7cac2c 100644 --- a/doc/luasnip.txt +++ b/doc/luasnip.txt @@ -1,4 +1,4 @@ -*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 14 +*luasnip.txt* For NVIM v0.8.0 Last change: 2024 May 16 ============================================================================== Table of Contents *luasnip-table-of-contents* From 871ab54b58b3282d7bf971c1e1304d45dee107a6 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Fri, 17 May 2024 09:02:43 +0200 Subject: [PATCH 3/5] Clean implementation --- lua/luasnip/extras/filetype_functions.lua | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lua/luasnip/extras/filetype_functions.lua b/lua/luasnip/extras/filetype_functions.lua index e0bec30fe..cb2fc4fbb 100644 --- a/lua/luasnip/extras/filetype_functions.lua +++ b/lua/luasnip/extras/filetype_functions.lua @@ -1,3 +1,11 @@ +local function fts_from_ts_lang(lang) + -- In case of someone using nvim <= 0.9 + if vim.treesitter.language and vim.treesitter.language.get_filetypes then + return vim.treesitter.language.get_filetypes(lang) + end + return { lang } +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 +13,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 { - vim.treesitter.language.get_filetypes(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 From 160fc83d7c476ff5ef54ab0c9a5cfdea648a6a78 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Fri, 17 May 2024 07:03:45 +0000 Subject: [PATCH 4/5] Auto generate docs --- doc/luasnip.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/luasnip.txt b/doc/luasnip.txt index 32f7cac2c..a766619b4 100644 --- a/doc/luasnip.txt +++ b/doc/luasnip.txt @@ -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* From 9963a780c84febdf541984e1c7db274f182970e7 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Fri, 17 May 2024 13:23:22 +0200 Subject: [PATCH 5/5] Add lang as one of the fts for compatibility --- lua/luasnip/extras/filetype_functions.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lua/luasnip/extras/filetype_functions.lua b/lua/luasnip/extras/filetype_functions.lua index cb2fc4fbb..2753f42ad 100644 --- a/lua/luasnip/extras/filetype_functions.lua +++ b/lua/luasnip/extras/filetype_functions.lua @@ -1,9 +1,14 @@ 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 - return vim.treesitter.language.get_filetypes(lang) + fts = vim.treesitter.language.get_filetypes(lang) end - return { lang } + -- 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()