nvim-treesitter-context icon indicating copy to clipboard operation
nvim-treesitter-context copied to clipboard

In markdown only dispaly outermost title.

Open miraculin1 opened this issue 1 year ago • 9 comments

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. rwa

Actual behavior

This is overview of the example rwa

This is when title1 and title 2 are out of screen. problem

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

  1. :TSInstall markdown markdown_inline
  2. TSCotextEnable
  3. scroll down through markdown file

miraculin1 avatar Jun 14 '23 13:06 miraculin1

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.

Screen Shot 2023-06-28 at 18 44 02

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)

relaxdiego avatar Jun 28 '23 10:06 relaxdiego

This bug is caused by this change.

kit494way avatar Jul 04 '23 23:07 kit494way

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.

lewis6991 avatar Aug 27 '23 13:08 lewis6991

Upstream issue: https://github.com/tree-sitter/tree-sitter/issues/2580

lewis6991 avatar Aug 29 '23 11:08 lewis6991

Same issue here too.

gruvw avatar Sep 14 '23 11:09 gruvw

Looking into this a little further:

  1. Reverting the changes from the commit mentioned by @kit494way fixes the issue.
  2. Removing the check on local r before the return statement also fixes the issue (Line).
  3. 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?

SuchithSridhar avatar Nov 09 '23 03:11 SuchithSridhar

@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.

lewis6991 avatar Nov 09 '23 09:11 lewis6991

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.

SuchithSridhar avatar Nov 09 '23 15:11 SuchithSridhar

Should be fixed with https://github.com/tree-sitter/tree-sitter/pull/3191

lewis6991 avatar Mar 18 '24 17:03 lewis6991

Should be fixed with tree-sitter/tree-sitter#3191

My parsers are all updated but I still face this issue :/

ghostfish0 avatar Mar 20 '24 22:03 ghostfish0

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.

lewis6991 avatar Mar 20 '24 22:03 lewis6991

HOOK, any updates on the TS "marksman" "bug" fix?

MikeLemo1 avatar Mar 28 '24 15:03 MikeLemo1

I have no idea what you are referring to. Please comment on the appropriate thread.

lewis6991 avatar Mar 28 '24 22:03 lewis6991

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!

viktor-yakubiv avatar Apr 25 '24 08:04 viktor-yakubiv

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.

lewis6991 avatar Apr 25 '24 08:04 lewis6991