mkdnflow.nvim icon indicating copy to clipboard operation
mkdnflow.nvim copied to clipboard

Is there a way to autocomplete links to headers?

Open krankur opened this issue 1 year ago • 2 comments

I often find myself linking to headers and it's hard to remember full text of the headers in some cases so I find myself jumping back and forth between files to copy the exact text. It would be great if we there's a way to get autocompletion suggestions for headers.

krankur avatar Sep 03 '22 15:09 krankur

Hi, thanks for the issue. I agree that completions for headers would be nice, although I'll have to do a bit of learning to figure out how to implement that. In the mean time, are you aware of the ya ("yank anchor") mapping built into the plug-in? With your cursor on the heading, it will add a formatted anchor link to the register, and you can then go and paste that link wherever you like with p. Then if you want to paste an anchor link for the subsequent heading, you can do '' and you'll be taken back to the header you last copied, where you can do ]] to go to the subsequent heading, do ya again, and so on and so forth.

jakewvincent avatar Sep 08 '22 16:09 jakewvincent

I wasn't aware of how "yank anchor" works. This is really useful. Thanks.

Meanwhile, I am working on adding auto-completion for anchors using ctags. I am half way through the implementation and I think it would work. But I may be wrong. Will share my findings here either way.

krankur avatar Sep 09 '22 06:09 krankur

@krankur you can use the telescope, but you will need to create a custom action after pressing Enter on whatever note you find to insert the link in the format you want

lumpsoid avatar Oct 18 '22 04:10 lumpsoid

@krankur in this code you can find any line you want from files in the directory where you call the function (if I understand in right), in your case, as I understand you need # something like this when you press Enter you will create link with anchor (and I'm sorry, because I don't actually know which syntax you need in your notes, so you can change it in selection variable)

local previewers = require("telescope.previewers")
local pickers = require("telescope.pickers")
local sorters = require("telescope.sorters")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local make_entry = require "telescope.make_entry"

local M = {}

function M.test()
  opts = opts or {}
  local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
  local search_dirs = opts.search_dirs
  local grep_open_files = opts.grep_open_files
  opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()

  local live_grepper = finders.new_job(function(prompt)
    -- TODO: Probably could add some options for smart case and whatever else rg offers.

    if not prompt or prompt == "" then
      return nil
    end

    local search_list = {}

    if grep_open_files then
      search_list = filelist
    elseif search_dirs then
      search_list = search_dirs
    end

    return flatten { vimgrep_arguments, additional_args, "--", prompt, search_list }
  end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, opts.cwd)

  pickers
    .new(opts, {
      prompt_title = "Live Grep",
      finder = live_grepper,
      previewer = conf.grep_previewer(opts),
      sorter = sorters.highlighter_only(opts),
      attach_mappings = function(prompt_bufnr, map)
        actions.select_default:replace(function()
          actions.close(prompt_bufnr)
          local selection = action_state.get_selected_entry()
          print(vim.inspect(selection)) -- for debugging and understanding what you get from picker
          -- you need to add transform to lowercase
          -- and regex spaces to dashes 
          -- and hastags to one
          -- and maybe promt to write link field
          selection = "[link](" .. selection.filename .. selection.text .. ")"
          vim.api.nvim_put({ selection }, "", false, true)
        end)
        return true
      end,
    })
    :find()
end

return M

lumpsoid avatar Oct 18 '22 21:10 lumpsoid