From d72f73feebb1741e9acbd10ee23b3c1e0d5da5fa Mon Sep 17 00:00:00 2001 From: fffed <47778360+fffed@users.noreply.github.com> Date: Thu, 31 Dec 2020 00:24:45 +0300 Subject: [PATCH] fix: add ts_parsers.ft_to_lang to detect lang (#373) Add detection of a proper buffer language by `ts_parsers.ft_to_lang` as `ts_parsers.has_parser` works with `language` but not with `filetype` https://github.com/nvim-treesitter/nvim-treesitter/issues/796 --- lua/telescope/previewers/utils.lua | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lua/telescope/previewers/utils.lua b/lua/telescope/previewers/utils.lua index 475083a..ca41af5 100644 --- a/lua/telescope/previewers/utils.lua +++ b/lua/telescope/previewers/utils.lua @@ -49,31 +49,40 @@ utils.job_maker = function(cmd, bufnr, opts) end end +local function has_filetype(ft) + return ft and ft ~= '' +end + --- Attach default highlighter which will choose between regex and ts utils.highlighter = function(bufnr, ft) - if ft and ft ~= '' then - if has_ts and ts_parsers.has_parser(ft) then - ts_highlight.attach(bufnr, ft) - else - vim.cmd(':ownsyntax ' .. ft) - end + if not(utils.ts_highlighter(bufnr, ft)) then + utils.regex_highlighter(bufnr, ft) end end --- Attach regex highlighter utils.regex_highlighter = function(_, ft) - if ft and ft ~= '' then + if has_filetype(ft) then vim.cmd(':ownsyntax ' .. ft) + + return true end + + return false end -- Attach ts highlighter utils.ts_highlighter = function(bufnr, ft) - if ft and ft ~= '' then - if has_ts and ts_parsers.has_parser(ft) then - ts_highlight.attach(bufnr, ft) + if has_filetype(ft) then + local lang = ts_parsers.ft_to_lang(ft); + if has_ts and ts_parsers.has_parser(lang) then + ts_highlight.attach(bufnr, lang) + + return true end end + + return false end return utils