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

proposal: git_files() which considers cwd #459

Closed
acro5piano opened this issue Jun 24, 2022 · 5 comments
Closed

proposal: git_files() which considers cwd #459

acro5piano opened this issue Jun 24, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@acro5piano
Copy link

acro5piano commented Jun 24, 2022

Hi, thank you for the great library. Your work changed my Neovim life.

Description

I love the feature git_files(). My proposal is to consider current working directory. This is especially useful for those who are working on a large monorepo project.

I've wrote code something like this and it's working - If it's useful to other people I'm happy to create a PR for this. so my question is that it's okay to create a PR?

local M = {}

M.regexEscape = function(str)
	return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
end

M.get_working_path_from_git_root = function()
	local handle = io.popen("git rev-parse --show-toplevel")
	local root = handle:read("*a"):gsub("\n", "")
	handle:close()
	current_dir = os.getenv("PWD")
	return current_dir:gsub(M.regexEscape(root), ""):gsub("^/", "")
end

M.git_files_cwd_aware = function()
	local relative_path = M.get_working_path_from_git_root()
	if relative_path == "" then
		require("fzf-lua").git_files()
	else
		require("fzf-lua").git_files({ fzf_opts = { ["--query"] = relative_path } })
	end
end

return M

out

Best regards,
Kay

@ibhagwan
Copy link
Owner

ibhagwan commented Jun 24, 2022

Hi, thank you for the great library. Your work changed my Neovim life.

Tysm for your kind words @acro5piano!

I love the feature git_files(). My proposal is to consider current working directory. This is especially useful for those who are working on a large mono-repo project.
I've wrote code something like this and it's working - If it's useful to other people I'm happy to create a PR for this. so my question is that it's okay to create a PR?

This is great functionality indeed, but given this can be so easily achieved with a simple customization and also very subjective (some users might like it, some probably won't), it would require yet another configuration option to control (which we have too many already) so we can just keep it documented in this thread.

If it turns out there's lots of demand to add this to the defaults (or as a new provider) we can reconsider.

In the meantime allow me to offer a slight improvement to your code reusing already exisitng code within fzf-lua:

-- The reason I added  'opts' as a paraameter is so you can
-- call this function with your own parameters / customizations
-- for example: 'git_files_cwd_aware({ cwd = <another git repo> })'
function M.git_files_cwd_aware(opts)
  opts = opts or {}
  local fzf_lua = require('fzf-lua')
  -- git_root() will warn us if we're not inside a git repo
  -- so we don't have to add another warning here, if
  -- you want to avoid the error message change it to:
  -- local git_root = fzf_lua.path.git_root(opts, true)
  local git_root = fzf_lua.path.git_root(opts)
  if not git_root then return end
  local relative = fzf_lua.path.relative(vim.loop.cwd(), git_root)
  opts.fzf_opts = { ['--query'] = git_root ~= relative and relative or nil }
  return fzf_lua.git_files(opts)
end

@ibhagwan ibhagwan added the enhancement New feature or request label Jun 24, 2022
acro5piano added a commit to acro5piano/dotfiles that referenced this issue Jun 25, 2022
@acro5piano
Copy link
Author

Thank you for sharing your thoughts and improved code! Yes the functionality can be here to keep the library maintainable. I'll use the improved code.

acro5piano added a commit to acro5piano/dotfiles that referenced this issue Jun 25, 2022
@nyngwang
Copy link
Contributor

nyngwang commented Oct 3, 2022

@acro5piano Awesome idea! Thanks for your sharing! and thanks @ibhagwan for helping with the simplification and future-proof comments! Let me put a link to this thread on the Wiki.

update: Done. Link to the page. (btw, I forgot to add the change summary...)

@nyngwang
Copy link
Contributor

nyngwang commented Oct 3, 2022

I just realized that this cwd-aware git_files() is achieved by pre-filling the prompt with the name of the current directory. But then I got the following result, which seems to be a bug, since the folder Repo doesn't contain the only item in the result list:

Would it be better to change the cwd(as shown in the image) to the current working directory instead of pre-filling?

update: OK, I just realized that what i need is simply git_files({ cwd = vim.fn.getcwd() }). I will update the corresponding Wiki for clarification :)

@ibhagwan
Copy link
Owner

ibhagwan commented Oct 3, 2022

Ty @nyngwang for updating the wiki :)

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

No branches or pull requests

3 participants