LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

feat(API): add emmylua annotations

Open kylo252 opened this issue 3 years ago • 10 comments

I think it might be better to let the API documentation live inside the code, and it also benefits from LSP capabilities

image


TODO

  • [ ] I've only added a few so far
  • [ ] autocompletion seems busted, I have a feeling it's something to do with some internal cache invalidation

kylo252 avatar Apr 09 '22 08:04 kylo252

I agree, it makes sense to have the documentation next to the code, should prevent it becoming outdated.

It would be really cool to automatically generate the entries in DOC.md (because I'd like to keep those as well) from these annotations, do you know if there are ways to do that?

L3MON4D3 avatar Apr 09 '22 09:04 L3MON4D3

I agree, it makes sense to have the documentation next to the code, should prevent it becoming outdated.

It would be really cool to automatically generate the entries in DOC.md (because I'd like to keep those as well) from these annotations, do you know if there are ways to do that?

I was looking earlier at ldoc which is able to generate html, and in theory markdown as well, but I could never get it to work with markdown. I think it's missing a template or something.

There's also this script from neovim-core, https://github.com/neovim/neovim/blob/8055f9857b8e384634d457533dfdb08618fc36f0/scripts/gen_vimdoc.py#L2

Lastly, I had an idea to leverage LSP for this, but it's a bit fiddley (redirect hover request output)

  local write_tmpfile = function(data)
    local tmpname = os.tmpname()
    local file = io.open(tmpname, "w")
    if type(data) == "table" then
      data = table.concat(data, "\n")
    end
    file:write(data)
    file:close()
    return tmpname
  end
  local util = require "vim.lsp.util"
  vim.lsp.handlers["textDocument/hover"] = function(_, result, ctx, config)
    config = config or {}
    config.focus_id = ctx.method
    if not (result and result.contents) then
      -- return { 'No information available' }
      return
    end
    local markdown_lines = util.convert_input_to_markdown_lines(result.contents)
    markdown_lines = util.trim_empty_lines(markdown_lines)
    if vim.tbl_isempty(markdown_lines) then
      -- return { 'No information available' }
      return
    end
    local tmpfile = write_tmpfile(markdown_lines) -- can also just copy the lines into some register instead
    print(tmpfile)
    vim.cmd([[edit ]] .. tmpfile)
    return tmpfile
  end

kylo252 avatar Apr 09 '22 10:04 kylo252

LDoc looks promising, but I'm surprised that there is no tool that straightforwardly generates markdown The LSP approach is interesting, is there a way to automate it?

L3MON4D3 avatar Apr 09 '22 19:04 L3MON4D3

is there a way to automate it?

Here's my script so far, just give it a valid bufnr (check :LspInfo) https://gist.github.com/kylo252/7b3edb23b55197316a33916fd79d942f

Here are the results, which as you can see need better formatting, but that's why I included the raw response as well https://gist.github.com/kylo252/1dcc3f9da60e42786d7088caa8441f67

I'm also counting on the fact that you have more experience with string manipulation 😃

kylo252 avatar Apr 11 '22 12:04 kylo252

Oh wow, that looks pretty good already :+1::+1: I won't be able to really get into helping you with it for some time though :/

L3MON4D3 avatar Apr 11 '22 14:04 L3MON4D3

There's also this treesitter module available for generating documentation thorough Emmy

saadparwaiz1 avatar May 03 '22 05:05 saadparwaiz1

Interesting, but it only generates vimdoc (afaict) :/

L3MON4D3 avatar May 03 '22 06:05 L3MON4D3

seems like this does the job? spinx generates rst by default but that works on github. if markdown is required this extension can be used?

saadparwaiz1 avatar May 04 '22 04:05 saadparwaiz1

Wonder why this one wasn't merged. Nice PR. Many projects are underdocumented.

hinell avatar Mar 29 '23 09:03 hinell

Hey @kylo252, are you fine with merging this as-is (or, more-or-less as-is, after looking into how up to date it still is)?

L3MON4D3 avatar Oct 04 '23 06:10 L3MON4D3