luckasRanarison / nvim-devdocs

Neovim DevDocs integration
MIT License
269 stars 19 forks source link

Feature: Open Devdocs by buffers filetype #12

Closed Scarcy closed 1 year ago

Scarcy commented 1 year ago

Added functionality that takes the filetype for the current active nvim window ( local filetype = vim.bo.filetype ) and calles open_picker with that filetype. Added a table with aliases, like "js" = "javascript" for the popular filetypes I remembered. If the current filetype isnt in the table, the filetype is used instead.

The code:

M.aliases = {
  ["cs"] = "csharp",
  ["go"] = "go",
  ["js"] = "javascript",
  ["jsx"] = "javascript",
  ["md"] = "markdown",
  ["py"] = "python",
  ["rb"] = "ruby",
  ["rs"] = "rust",
  ["sass"] = "sass",
  ["scss"] = "sass",
  ["sh"] = "bash",
  ["ts"] = "typescript",
  ["tsx"] = "typescript",
  ["yml"] = "yaml",
}
M.open_doc_current_file = function()
  local filetype = vim.bo.filetype
  local alias = M.aliases[filetype] or filetype
  pickers.open_picker(alias, false)
end

cmd("DevdocsOpenCurrentFile", M.open_doc_current_file, {})
emmanueltouzery commented 1 year ago

I'm just a random watcher, but If I'm understanding this right, maybe a filetype could be made to match multiple doc types? For instance js->[JavaScript, html, lodash]

Assuming I'm understanding this right and the maintainer wants this feature in at all.

Scarcy commented 1 year ago

I'm just a random watcher, but If I'm understanding this right, maybe a filetype could be made to match multiple doc types? For instance js->[JavaScript, html, lodash]

Assuming I'm understanding this right and the maintainer wants this feature in at all.

That sounds like a good idea, but that's out of my depth at my current skill level

luckasRanarison commented 1 year ago

This is really a cool idea, I just realize it now. But I think some filetypes are wrong, filetype for javascript is javascript and not js, if you want to check a buffer filetype run echo &filetype. This still need some work but I'll help.

EDIT: Oh, it works even though vim.bo.filetype output javascript lol (Ah it's because of the or)

tts2k commented 1 year ago

I think it would be nice to have an option that let people to define which docs they want to be opened for each filetype. The docs can be a string or a list of docs. In the case of list, it will be shown in a form of a picker.

Here is how I'm currently doing it. showNuiMenu creates a nui.nvim menu, but I guess you can do it in telescope too if you want to avoid having an additional dependency.

local docsByFt = {
  javascript = { 'javascript', 'node-18_lts' },
  typescript = { 'typescript', 'node-18_lts' },
  html = { 'html', 'tailwindcss' },
  svelte = { 'svelte', 'tailwindcss' },
  lua = 'lua-5.4',
  sql = 'postgresql-15'
}

local showDocsMenu = function(docs)
  local ui = require('util.ui')
  local items = {}

  for i = 1, #docs do
    table.insert(items, { label = docs[i] })
  end

  ui.nui.showNuiMenu(items, {
    on_submit = function(item)
      vim.cmd('DevdocsOpen ' .. item.text)
    end
  }, {
    border = {
      text = {
        top = "Choose a doc",
      }
    }
  })
end

local openDocsCurrentFt = function()
  local ft = vim.bo.ft
  local docs = docsByFt[ft]

  -- Custom name
  if type(docs) == 'string' then
    vim.cmd('DevdocsOpen ' .. docs)
    return
  end

  -- Custom set of docs
  if type(docs) == 'table' then
    showDocsMenu(docs)
    return
  end

  -- No custom ft
  vim.cmd('DevdocsOpen ' .. ft)
end

M.keys = {
  {
    "<Leader>dd",
    function() openDocsCurrentFt() end,
    desc = "Open documentation of current buffer filetype"
  },
 }

return M
luckasRanarison commented 1 year ago

I think it would be nice to have an option that let people to define which docs they want to be opened for each filetype. The docs can be a string or a list of docs. In the case of list, it will be shown in a form of a picker.

Here is how I'm currently doing it. showNuiMenu creates a nui.nvim menu, but I guess you can do it in telescope too if you want to avoid having an additional dependency.

local docsByFt = {
  javascript = { 'javascript', 'node-18_lts' },
  typescript = { 'typescript', 'node-18_lts' },
  html = { 'html', 'tailwindcss' },
  svelte = { 'svelte', 'tailwindcss' },
  lua = 'lua-5.4',
  sql = 'postgresql-15'
}

local showDocsMenu = function(docs)
  local ui = require('util.ui')
  local items = {}

  for i = 1, #docs do
    table.insert(items, { label = docs[i] })
  end

  ui.nui.showNuiMenu(items, {
    on_submit = function(item)
      vim.cmd('DevdocsOpen ' .. item.text)
    end
  }, {
    border = {
      text = {
        top = "Choose a doc",
      }
    }
  })
end

local openDocsCurrentFt = function()
  local ft = vim.bo.ft
  local docs = docsByFt[ft]

  -- Custom name
  if type(docs) == 'string' then
    vim.cmd('DevdocsOpen ' .. docs)
    return
  end

  -- Custom set of docs
  if type(docs) == 'table' then
    showDocsMenu(docs)
    return
  end

  -- No custom ft
  vim.cmd('DevdocsOpen ' .. ft)
end

M.keys = {
  {
    "<Leader>dd",
    function() openDocsCurrentFt() end,
    desc = "Open documentation of current buffer filetype"
  },
 }

return M

That's also a good idea! But I think it would be more useful to display the actual entries like when using DevdocsOpen without args but limit the results to the selected docs

luckasRanarison commented 1 year ago

It'dd also be a nice feature to search selected words but limit the result to docs matching the filetype

Scarcy commented 1 year ago

This is really a cool idea, I just realize it now. But I think some filetypes are wrong, filetype for javascript is javascript and not js, if you want to check a buffer filetype run echo &filetype. This still need some work but I'll help.

EDIT: Oh, it works even though vim.bo.filetype output javascript lol (Ah it's because of the or)

Appreciate the reply! I must admit that the mappings table is kinda sloppy, but the alias functionality works for me. Do you need me to improve something before it's merged? :) I'm just getting into the open source community and this is my first ever PR to something I don't own myself, so I'm happy to take pointers

luckasRanarison commented 1 year ago

Appreciate the reply! I must admit that the mappings table is kinda sloppy, but the alias functionality works for me. Do you need me to improve something before it's merged? :) I'm just getting into the open source community and this is my first ever PR to something I don't own myself, so I'm happy to take pointers

I'm also new to this tbh, I haven't merged it yet because of the README, I've made changes to the README after you did, maybe you could fix this. I'm not on my computer at the moment, also I added the argument float to your command you should mention it too

Scarcy commented 1 year ago

I'm also new to this tbh, I haven't merged it yet because of the README, I've made changes to the README after you did, maybe you could fix this. I'm not on my computer at the moment, also I added the argument float to your command you should mention it too

The README should now be the same version. I was uncertain on how I could best describe the float argument, so I just specified that there is 0 or 1 args like the other commands did.

Scarcy commented 1 year ago

It'dd also be a nice feature to search selected words but limit the result to docs matching the filetype

That's the functionality I originally had in mind when I started tinkering with this, but I realized it was too complicated for my skills now. It would be amazing to hover over a keyword and with one keymap get the detailed documentation from devdocs on that keyword.

luckasRanarison commented 1 year ago

Thinking about it now adding an argument is not really intuitive but adding float to the name would make it really long xD, :DevdocsOpenCurrentFileFloat, some other suggestions?

Scarcy commented 1 year ago

Thinking about it now adding an argument is not really intuitive but adding float to the name would make it really long xD, :DevdocsOpenCurrentFileFloat, some other suggestions?

I guess most people use tab-complete when entering commands so I don't think the length is necessarily an issue. Would also be nice to have :DevdocsOpenCurrentFile float=true, but afaik the only way to make that possible would be to pass a table {float = true}. Not sure if I like that

luckasRanarison commented 1 year ago

I've decided to use two different cmd and use a shorter name