diff --git a/README.md b/README.md index eefc5ea..ffcc2b9 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ An extension for [telescope.nvim](https://github.com/nvim-telescope/telescope.nv ## Supported File Type - Markdown, including `vimwiki`, `vim-pandoc-syntax`, and `vim-gfm-syntax`. -- AsciiDoc (experimental) -- LaTeX (experimental) -- OrgMode (experimental) -- ReStructuredText (experimental) -- Vim Help (experimental) +- AsciiDoc +- LaTeX +- OrgMode +- ReStructuredText +- Vim Help ## Setup @@ -30,11 +30,33 @@ You can setup the extension by adding the following to your config: require('telescope').load_extension('heading') ``` -Use treesitter (which only supports Markdown/ReStructuredText) to query headings. Setup with `telescope.setup` **before** `load_extension`: +### Tree-sitter Support + +telescope-heading supports Tree-sitter for parsing documents and finding headings. + +File types with Tree-sitter supports: +- Markdown +- ReStructuredText +- Vim Help + ```lua +-- add nvim-treesitter +use('nvim-treesitter/nvim-treesitter') + +-- make sure you have already installed treesitter modules +require('nvim-treesitter.configs').setup({ + ensure_installed = { + -- .. + 'markdown', + 'rst', + -- .. + }, +}) + +-- enable treesitter parsing local telescope = require('telescope') telescope.setup({ - -- other setups + -- ... extensions = { heading = { treesitter = true, @@ -42,8 +64,12 @@ telescope.setup({ }, }) +-- `load_extension` must be after `telescope.setup` +telescope.load_extension('heading') ``` +If nvim-treesitter is not correctly loaded, it will fallback to normal parsing. + ## Usage ```viml diff --git a/lua/telescope/_extensions/heading/format/help.lua b/lua/telescope/_extensions/heading/format/help.lua index f1eecad..b30feb5 100644 --- a/lua/telescope/_extensions/heading/format/help.lua +++ b/lua/telescope/_extensions/heading/format/help.lua @@ -43,4 +43,27 @@ function Help.get_headings(filepath, start, total) return headings end +function Help.ts_get_headings(filepath, bufnr) + local ts = vim.treesitter + local query = [[ + (headline (word)) @headline_title + ]] + local parsed_query = ts.parse_query('help', query) + local parser = ts.get_parser(bufnr, 'help') + local root = parser:parse()[1]:root() + local start_row, _, end_row, _ = root:range() + + local headings = {} + for _, node in parsed_query:iter_captures(root, bufnr, start_row, end_row) do + local row, _ = node:range() + local line = vim.fn.getline(row + 2) + table.insert(headings, { + heading = vim.trim(line), + line = row + 2, + path = filepath, + }) + end + return headings +end + return Help