nvim-treesitter-context
nvim-treesitter-context copied to clipboard
In markdown only dispaly outermost title.
Description
Thank you for reading this. For example
# title 1
a lot things
## title 2
a lot things
### title3
cursor is here
In this case # title1
and ## title2
are both out of screen. I expected both of # title1
and ## title2
will be displayed but only title 1 got displayed.
Also if example be like:
## title 2
a lot things
### title3
a lot things
#### title4
cursor here
In this case ## title2
and ### title3
are both out of screen. I expected both of ## title2
and ### title3
will be displayed but only title2 got displayed.
I'm using MDeiml /tree-sitter-markdown as parser for markdown.
I'm new to treesitter, I'm not sure if this is due to my wrong config, or if this is cause by the parser I'm using or because this plugin. I'd appreciate any help.
If I'm doing anything wrong if possible please let me know what should I do, thanks. I'm sorry if I cause any trouble
Neovim version
NVIM v0.9.1 Build type: Release LuaJIT 2.1.0-beta3 system vimrc file: "$VIM/sysinit.vim" fall-back for $VIM: "/usr/share/nvim"
Expected behavior
In the example below, both title1 and title2 will be displayed by treesitter context, when cursor is under title3 and title1 and title are out of screen.
Actual behavior
This is overview of the example
This is when title1 and title 2 are out of screen.
Minimal config
require'nvim-treesitter.configs'.setup {
-- A list of parser names, or "all" (the four listed parsers should always be installed)
ensure_installed = { "c", "lua", "vim", "cpp", "markdown", "markdown_inline" },
-- Install parsers synchronously (only applied to `ensure_installed`)
sync_install = false,
-- Automatically install missing parsers when entering buffer
-- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
auto_install = false,
-- List of parsers to ignore installing (for "all")
ignore_install = { "javascript" },
---- If you need to change the installation directory of the parsers (see -> Advanced Setup)
-- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")!
highlight = {
-- `false` will disable the whole extension
enable = true,
-- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
-- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
-- the name of the parser)
-- list of language that will be disabled
disable = {},
-- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files
-- disable = function(lang, buf)
-- local max_filesize = 100 * 1024 -- 100 KB
-- local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
-- if ok and stats and stats.size > max_filesize then
-- return true
-- end
-- end,
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
}
require'treesitter-context'.setup{
enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
line_numbers = true,
multiline_threshold = 20, -- Maximum number of lines to collapse for a single context line
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = nil,
zindex = 20, -- The Z-index of the context window
on_attach = nil, -- (fun(buf: integer): boolean) return false to disable attaching
}
for name, url in pairs(plugins) do
local install_path = '/tmp/nvim/site/'..name
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
end
vim.o.runtimepath = install_path..','..vim.o.runtimepath
end
-- ADD INIT.LUA SETTINGS THAT IS _NECESSARY_ FOR REPRODUCING THE ISSUE
Steps to reproduce
-
:TSInstall markdown markdown_inline
-
TSCotextEnable
- scroll down through markdown file
I can confirm that I'm seeing the same behavior.
Using Treesitter Playground, I copied the context.scm
file for markdown and noticed that it captures more than one heading at a time.
I then tried to change it to the following, however it's producing a syntax error. Apologies as I'm still new to the query syntax:
((section) (_) (heading_content: (_) @context.end) @context)
This bug is caused by this change.
This looks like a bug in treesitter.
If you run:
vim.treesitter.query.parse('markdown', '(section) @context'):iter_matches(vim.treesitter.get_node():parent():parent(), 0, 0, -1, {max_start_depth=0})()
Over title1
and it gets a match. However over title2
it doesn't match. Even if you remove max_start_depth
, it still doesn't return the correct match.
Upstream issue: https://github.com/tree-sitter/tree-sitter/issues/2580
Same issue here too.
Looking into this a little further:
- Reverting the changes from the commit mentioned by @kit494way fixes the issue.
- Removing the check on
local r
before thereturn
statement also fixes the issue (Line). - It looks like (not 100% sure)
:iter_matches
only returns sections that follow the section if it's not a top-level heading section.
I would like to fix this issue but I'm unsure as to what the exact issue is, if someone could point me in that direction I'll try my best! Thanks! (I tried to look at https://github.com/tree-sitter/tree-sitter/issues/2580 but this was not helpful)
@lewis6991 Are the changes mentioned in 1 required? Would reverting be a possibility?
@lewis6991 Are the changes mentioned in 1 required? Would reverting be a possibility?
No. As mentioned this is an issue with the actual parser. It cannot be fixed here.
For a temporary workaround, you can refer to this solution. It's not ideal, but it's functional until a proper fix is implemented upstream.
Should be fixed with https://github.com/tree-sitter/tree-sitter/pull/3191
Should be fixed with tree-sitter/tree-sitter#3191
My parsers are all updated but I still face this issue :/
That PR isn't even merged yet, and even when it is we need to wait for a new treesitter release, and then Neovim needs to update to that release.
When all that happens, I'll close the issue.
In the meantime, please be more careful with drive by comments.
HOOK, any updates on the TS "marksman" "bug" fix?
I have no idea what you are referring to. Please comment on the appropriate thread.
I do not have setext headings shown in the context window while atx headings work just fine. This thread is the closest I could find in the tracker. I wonder if you have in mind that something might be misconfigured on my side (I use defaults) or not implemented, of this is something I should create a separate issue for. Thank you for your help in advance!
Might not fully implemented in the queries, but otherwise Markdown still has a known issue with upstream treesitter that is fixed in HEAD of treesitter.