Giter Club home page Giter Club logo

Comments (5)

crispgm avatar crispgm commented on August 20, 2024

The idea is great. Actually this is my initial plan but I personally did not find sufficient docs or demos for that.

from telescope-heading.nvim.

maxRN avatar maxRN commented on August 20, 2024

I'm also really new to TreeSitter but I'm glad to hear your are open to the idea πŸ˜ƒ

I've found the official Neovim TreeSitter playground repository very helpful. (It says you need nightly Neovim, but I only have 0.6 and it works fine for me so far.)
After installing the plugin you can run :TSPlaygroundToggle to show the abstract syntax tree (AST) in a side view.
I've attached a screenshot of what the AST looks like for an example MarkDown document.

When you hover over the nodes in the tree, the corresponding part of the document is highlighted.
Screenshot 2022-02-27 at 08 50 15

As to queries, when you hit o while you are in the AST view, you open a third window in which you can edit your own queries. A very basic one I've tried is (atx_heading) @capture which highlights all headings (h1, h2, etc.) in the document. Note: To highlight them in the document you have to move your cursor on the the @capture part of the query.
Screenshot 2022-02-27 at 08 58 03

Other plugins that I know of that use TreeSitter are this refactoring plugin, and this tabout one. So I guess we could look there for some inspiration.

from telescope-heading.nvim.

crispgm avatar crispgm commented on August 20, 2024

@maxRN https://www.reddit.com/r/neovim/comments/kx1ceh/need_a_little_help_with_tree_sitter_query_matching/ I found this guide, which is basically the scenario we need. I would dive into this recently.

And now, tree-sitter only supports markdown and rst, not capable for every format. This is a cons.

from telescope-heading.nvim.

crispgm avatar crispgm commented on August 20, 2024
local function ts_get_headings(filepath)
    local headings = {}
    local query = [[
    (atx_heading) @heading
    ]]
    local parsed_query = ts.parse_query('markdown', query)
    local bufnr = vim.api.nvim_get_current_buf() or 0
    local parser = ts.get_parser(bufnr, 'markdown')
    local root = parser:parse()[1]:root()
    local start_row, _, end_row, _ = root:range()

    for _id, node in parsed_query:iter_captures(root, bufnr, start_row, end_row) do
        local row, _ = node:range()
        local line = vim.fn.getline(row + 1)
        table.insert(headings, {
            heading = vim.trim(line),
            line = row,
            path = filepath,
        })
    end
    return headings
end

I think this works good. All I have to finish this feature is to add an option to use tree sitter and some checks.

from telescope-heading.nvim.

crispgm avatar crispgm commented on August 20, 2024

@maxRN Treesitter parsing for markdown is merged. You may try it. Thanks for your idea.

from telescope-heading.nvim.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.