From b98842964e5913b10104701e8c294832de465273 Mon Sep 17 00:00:00 2001 From: David Zhang Date: Wed, 13 Apr 2022 20:26:05 +0800 Subject: [PATCH] feat: check ts language installed --- lua/telescope/_extensions/heading.lua | 32 ++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lua/telescope/_extensions/heading.lua b/lua/telescope/_extensions/heading.lua index 82a4b26..ffcfafc 100644 --- a/lua/telescope/_extensions/heading.lua +++ b/lua/telescope/_extensions/heading.lua @@ -29,8 +29,14 @@ local function filetype() end local function support_treesitter(ft) - if ft == 'markdown' or ft == 'rst' then - return true + local ts_ft_maps = { + ['markdown'] = true, + ['rst'] = true, + ['help'] = true, + } + + if ts_ft_maps[ft] ~= nil then + return ts_ft_maps[ft] end return false end @@ -46,7 +52,7 @@ local function get_headings() vim.notify( 'The file type is not supported by telescope-heading', vim.log.levels.WARN, - { title = 'Telescope heading' } + { title = 'Telescope Heading' } ) return nil end @@ -54,13 +60,23 @@ local function get_headings() local index, total = 1, vim.fn.line('$') local bufnr = vim.api.nvim_get_current_buf() local filepath = vim.api.nvim_buf_get_name(bufnr) - local headings = {} -- luacheck: ignore if heading_config.treesitter and support_treesitter(ft) then - headings = mod.ts_get_headings(filepath, bufnr) - else - headings = mod.get_headings(filepath, index, total) + if vim._ts_has_language(ft) then + print('ts') + return mod.ts_get_headings(filepath, bufnr) + else + vim.notify( + string.format( + 'No treesitter language parser installed for [%s]. Fallback to normal parser.', + ft + ), + vim.log.levels.WARN, + { title = 'Telescope Heading' } + ) + end end - return headings + + return mod.get_headings(filepath, index, total) end local function pick_headings(opts)