Skip to content

Commit

Permalink
feat: support help file
Browse files Browse the repository at this point in the history
  • Loading branch information
crispgm committed Apr 14, 2022
1 parent b988429 commit 4bb2547
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -30,20 +30,46 @@ 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,
},
},
})

-- `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
Expand Down
23 changes: 23 additions & 0 deletions lua/telescope/_extensions/heading/format/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4bb2547

Please sign in to comment.