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]: Allow starting the finder from a custom root item group #390

Closed
mrjones2014 opened this issue Jul 6, 2023 Discussed in #389 · 6 comments · Fixed by #391
Closed

[FEATURE]: Allow starting the finder from a custom root item group #390

mrjones2014 opened this issue Jul 6, 2023 Discussed in #389 · 6 comments · Fixed by #391
Assignees
Labels
enhancement New feature or request

Comments

@mrjones2014
Copy link
Owner

Discussed in #389

Originally posted by towry July 6, 2023
I want to use legendary as folder action select, so I can select folder from telescope and use legendary.

So I write the script as following:

return function (cwd)
  legendary.funcs({
    itemgroup = 'Folder action',
    funcs = {},
})

legendary.find({
  select_prompt = 'Folder action under: ' .. cwd,
  filters = {
   --- how to write this??
  }
})
end

how to write the filters so that only everything under 'Folder action' item group shown?

I have looked over the docs, nothing found.

@mrjones2014 mrjones2014 added the enhancement New feature or request label Jul 6, 2023
@mrjones2014 mrjones2014 self-assigned this Jul 6, 2023
@mrjones2014
Copy link
Owner Author

I'll need to think about how to implement this exactly. When you add keymaps to legendary, there is a parse step, so that means that when you would pass items to the finder as a custom "root" we would need to figure out:

  • is this a list of unparsed item tables?
  • Is this a list of parsed legendary items?
  • something else?

@towry how would you feel about the API just accepting a group name as a string, and then legendary would look up the group by name?

So, you could do something like:

require('legendary').find({ itemgroup = 'My item group' })

@towry
Copy link

towry commented Jul 6, 2023

Yes, I had randomly tried this API, I think it is intuitive.

@mrjones2014
Copy link
Owner Author

@towry can you give #391 a test for me? 🙏

@towry
Copy link

towry commented Jul 7, 2023

@mrjones2014 It works.

截屏2023-07-07 09 03 18

截屏2023-07-07 08 46 57

截屏2023-07-07 09 03 03

@towry
Copy link

towry commented Jul 7, 2023

My current usage is a bit tedious because I have to manage many temporary variables. I am not sure if I am overusing the legendary functions API, but it seems to work well with various plugins as popup menus (or context menus). Otherwise, I would have to use other plugins to meet my needs, which would greatly improve my workflow in a monorepo codebase.

Thanks for the work, much appreciate.

local M = {}

local cwd = nil
local legendary_registered = false
local item_group_name = 'folder_actions'

function M.enter(new_cwd)
  cwd = new_cwd
  return M
end

local get_current_cwd = function() return cwd end

function M.then_folder_action()
  local lg = require('legendary')
  local select_prompt = 'Folder action under: ' .. require('userlib.runtime.path').home_to_tilde(cwd)

  if not cwd then return end

  if legendary_registered then
    lg.find({
      select_prompt = select_prompt,
      itemgroup = item_group_name,
    })
    return
  end
  legendary_registered = true

  lg.funcs({
    itemgroup = 'folder_actions',
    description = 'Folder actions',
    funcs = {
      {
        description = 'Open with mini.files',
        function()
        end,
      },
      {
        description = 'Open with NVimTree',
        function()
        end,
      },
      {
        description = 'Find files with telescope',
        function()
          require('userlib.telescope.pickers').project_files({
            cwd = get_current_cwd(),
            use_all_files = true,
          })
        end,
      },
      {
        description = 'Recent files',
        function()
          require('userlib.telescope.pickers').project_files({
            oldfiles = true,
            cwd_only = true,
            cwd = get_current_cwd(),
          })
        end,
      },
      {
        description = 'Search content',
        function()
          require('userlib.telescope.live_grep_call')({
            cwd = get_current_cwd(),
          })
        end,
      },
    }
  })

  lg.find({
    select_prompt = select_prompt,
    itemgroup = item_group_name,
  })
end

return M

@mrjones2014
Copy link
Owner Author

Instead of

function M.enter(new_cwd)
  cwd = new_cwd
  return M
end

local get_current_cwd = function() return cwd end

You can just use vim.cmd.cd(new_cwd) and vim.loop.cwd(). To replace $HOME with ~ you can just use vim.fn.fnamemodify(path, ':~:.').

As far as keeping track of legendary_registered, why don't you just register them via require('legendary').setup() wherever you set up the plugin? It doesn't look like they are actually dependent on the cwd. There is also autocmd DirChanged and autocmd DirChangedPre if that helps.

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

Successfully merging a pull request may close this issue.

2 participants