Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Support for custom filetypes using header characters (or just .vimrc / init.vim) #19

Open
awillats opened this issue Aug 7, 2022 · 7 comments

Comments

@awillats
Copy link

awillats commented Aug 7, 2022

Hello,
thanks for the plugin!

I use mutliple comment symbols to define headings in several filetypes (including my .vimrc)

" this is a comment
"" Heading: where my settings go
let g:foo=bar
"" Heading: where my plugins go
Plug 'crispgm/telescope-heading.nvim'
""" this is a subheading

This is almost identical to how headings are managed in markdown for instance. Is it possible with the current plugin to extend heading search to work in my .vimrc? Or would it be possible to add configurable parameters for additional filetypes by specifying the characters used for headings?

@awillats
Copy link
Author

awillats commented Aug 7, 2022

maybe as simple as adding:

local Vimrc = {}

function Vimrc.get_headings(filepath, start, total)
    local headings = {}
    local index = start
    local matches = {
        '"" ',
        '""" ',
        '"""" ',
        '""""" ',
        '"""""" ',
    }
    while index <= total do
        local line = vim.fn.getline(index)
        -- match heading
        for _, pattern in pairs(matches) do
            if vim.startswith(line, pattern) then
                table.insert(headings, {
                    heading = vim.trim(line),
                    line = index,
                    path = filepath,
                })
                break
            end
        end

        ::next::
        index = index + 1
    end
    return headings
end

return Vimrc

to extensions/headings/ ?
I dropped the codeblock logic since that wouldnt apply to vimrc

@awillats
Copy link
Author

awillats commented Aug 7, 2022

Hacked something together over on
awillats/telescope-heading.nvim : feature-vimrc-support
and it works!
let me know if you think it would be appropriate to submit a pull request or whether there's more work to do

@crispgm
Copy link
Owner

crispgm commented Aug 8, 2022

I understand what you want. However, this is kinda a hack on comments instead of headings.

@awillats
Copy link
Author

awillats commented Aug 8, 2022

I might be missing something about the semantics of what constitutes a header... I guess part of the problem is if there isn't a single agreed upon syntax for section headings in vim config files then tree-sitter integration wouldn't work, and it would be tricky to support those styles?

But it seems like some sort of solution for section headings within a vim config file would be useful to lots of people. (for now my hack is sufficient for my own purposes)

@awillats
Copy link
Author

awillats commented Aug 8, 2022

More generally it would be nice for the plugin to be more extensible / customizable with options like: (please excuse what may be non-functional lua, I'm not super familiar with the language)

local mappings = {
  {ft = 'markdown', 
   heading_match_pattern = '^#{1,6}', 
   tree_sitter_supported=true},
  {ft = 'neorg', 
    heading_match_pattern='^*{1,6}', 
    tree_sitter_support=true},
  {ft = 'vim', 
    heading_match_pattern='^"{2,6}', 
    tree_sitter_support=false},
}

since so many languages use this multiple character pattern to define headings, this would also simply code which is duplicated between files in lua/telescope/_extension/heading/format/*.lua and lower the burden for supporting new file types

@crispgm
Copy link
Owner

crispgm commented Aug 23, 2022

I think "comment heading" is a better description of this feature. And this should be another function provided by this plugin, which is based on comment string rather than simple filetype.

But I have a question about this comment heading. Is there a common grammar or standard on how to define a heading inside a comment string?

If there was, we would have use tree-sitter to query comments and just search with the standard format. This sounds good.

@awillats
Copy link
Author

I think applying this "multiples of the same comment for header levels" beyond markdown (and neorg) is wishful thinking on my part. I tried to scan some .vimrc or init.lua files, and couldn't find anyone else that does it this way.

But generalizing the comment string to get headings for more filetypes still seems valuable.

MATLAB uses %% (double comment) for section breaks with header-like title text: https://www.mathworks.com/help/matlab/matlab_prog/create-and-run-sections.html
and this format (<comment> %% i.e. # %%) has been adopted by coding notebooks for several languages (python, Julia, R etc.): https://jupytext.readthedocs.io/en/latest/formats.html#the-percent-format
See also this popular plaintext code notebook format:
https://github.com/mwouts/jupytext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants