davidgranstrom / scnvim

Neovim frontend for SuperCollider.
GNU General Public License v3.0
187 stars 25 forks source link

[FR] Add solution to be able to search through documentation #59

Open madskjeldgaard opened 4 years ago

madskjeldgaard commented 4 years ago

A feature request:

Add solution to be able to search through documentation. This is especially useful when searching for help files that are not for classes but things like references, guides or tutorials. An example could be the help file "nodeproxy roles".

As of now, I don't think there's a way to do this but I could be wrong.

davidgranstrom commented 4 years ago

@madskjeldgaard Yes, I agree. It would also be nice to let the end user decide on how to present the search results. For instance, piping the results to a utility such as fzf. (That applies for the current implementation as well..). I'm hoping to look into this once #58 is completed!

madskjeldgaard commented 4 years ago

That's a great idea. Maybe most of the work has already done in using fzf for example, unless you want to stay dependency-less :)

salkin-mada commented 4 years ago

fzf integration sounds very promising i would say

madskjeldgaard commented 3 years ago

Using fzf for this would still be awesome. Maybe something like running SCDoc.renderAll in SC first, and then (from within vim in a fzf buffer and using the scnvim help browser instead of w3m):

cd /home/$USER/.local/share/SuperCollider/Help && w3m $(fd .html | fzf)
davidgranstrom commented 3 years ago

I did some work towards better introspection for sclang classes here: https://github.com/davidgranstrom/scnvim/commit/5159f7edad91edd0647c1825fedb83d441d9790d although its not complete, I think something like this would be of great help to build different tools such as fuzzy finding for sclang classes/methods. It would not be necessary to generate all docs, since they could be rendered on the fly when accessing the associated help file.

madskjeldgaard commented 3 years ago

I messed around with this a bit today. For those on Linux with fzf vim installed, you can run this command:

:call fzf#run(fzf#wrap({'source':'fd .txt ~/.local/share/SuperCollider/Help -t f'}))

fzf schelp

madskjeldgaard commented 3 years ago

The above only works for classes though, FYI

madskjeldgaard commented 3 years ago

I have now made a solution for fuzzy finding of definitions and classes. The former opens the definition in a buffer and the latter in a scnvim help window. Works with both skim and fzf, which ma be set in the global varialble scnvim_fuzzy_command.

It requires nvim 0.5 to run.

Load this using the lua command like so: lua require('scnvim_fuzzy.lua').init() to register the commands :SCNvimFuzzyHelp and :SCNvimFuzzyDefinitions.

This can be done in your init.vim automatically:

autocmd filetype supercollider,scnvim,scdoc,supercollider.help lua require'scnvim_fuzzy'.init()

scnvim_fuzzy.lua:

--
-- Extra functions for SuperCollider / scnvim
-- by Mads Kjeldgaard , 2020
-- for Nvim => v0.5

local M = {}

-- Init function
-- Run this to register commands that interface with the functions here
function M.init()
    -- Use fuzzy finding (fzf / skim) to search for help and find definitions
    vim.cmd("command! SCNvimFuzzyHelp lua require('scnvim_extra').scnvim_fuzzy_help()")
    vim.cmd("command! SCNvimFuzzyDefinitions lua require('scnvim_extra').scnvim_fuzzy_definition()")
       -- Set command to use: fzf or skim
    vim.g.scnvim_fuzzy_command = vim.g.scnvim_fuzzy_command or "skim"

end

local function fzf(sources, sinkfunc, custom_options)
    local cmd = vim.g.scnvim_fuzzy_command;
    local fzf_run = vim.fn[cmd .. "#run"]
    local fzf_wrap = vim.fn[cmd .. "#wrap"]

    local wrapped = fzf_wrap("test", {
        source = sources,
        options = custom_options or {},
        -- don't set `sink` or `sink*` here
    })

    wrapped["sink*"] = nil   -- this line is required if you want to use `sink` only
    wrapped.sink = sinkfunc
    fzf_run(wrapped)
end

-- Unpack csv file with tags into lua table
local function scnvim_help_table()
    local root = vim.g.scnvim_root_dir
    local classes = root .. "/scnvim-data/tags"
    local tagsfile = io.open(classes)
    local help = {}

    for line in tagsfile:lines() do
        local tagname, tagpath, _, _= line:match("%s*(.-)\t%s*(.-)\t%s*(.-)\t%s*(.-)")
        help[tostring(tagname)] = tagpath
        -- print(tagname)
    end

    return help
end

function M.scnvim_fuzzy_definition()
    local help = scnvim_help_table()
    local help_keys = {};

    for k,_ in pairs(help) do
        table.insert(help_keys, k)
    end

    fzf(help_keys, function(class_name)
        local key = tostring(class_name)
        local lookup_path = help[key]
        vim.cmd("spl " .. lookup_path)
    end)
end

M.open_help = vim.fn["scnvim#help#open_help_for"]

function M.scnvim_fuzzy_help()
    local help = scnvim_help_table()
    local help_keys = {};

    for k,_ in pairs(help) do
        table.insert(help_keys, tostring(k))
    end

    fzf(help_keys, function(class_name)
        M.open_help(tostring(class_name))
    end)
end

return M
madskjeldgaard commented 3 years ago

The above has a small (weird) bug: it posts a warning about broken links in help files whenever you trigger it, but apart from that it works fine.

@davidgranstrom the above could either go in the wiki or a PR or stay here. It's up to you!

vitreo12 commented 3 years ago

Thanks for this snippet, @madskjeldgaard ! It's super useful, similar to ScIDE's Ctrl + I.

One quick thing,given the scnvim_fuzzy.lua file name, the two lines:

vim.cmd("command! SCNvimFuzzyHelp lua require('scnvim_extra').scnvim_fuzzy_help()")
vim.cmd("command! SCNvimFuzzyDefinitions lua require('scnvim_extra').scnvim_fuzzy_definition()")

should be

vim.cmd("command! SCNvimFuzzyHelp lua require('scnvim_fuzzy').scnvim_fuzzy_help()")
vim.cmd("command! SCNvimFuzzyDefinitions lua require('scnvim_fuzzy').scnvim_fuzzy_definition()")

for it to work

madskjeldgaard commented 3 years ago

I've solved this for myself for the time being by adding commands that call the QT help doc browser (ala scvim classic) to allow searching through non class documentation. I've added them here if anyone's interested: https://github.com/madskjeldgaard/supercollider-h4x-nvim