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

fix(filter): correctly use uv.cwd when opts.cwd=true #757

Closed
wants to merge 2 commits into from

Conversation

dpetka2001
Copy link
Contributor

@dpetka2001 dpetka2001 commented Jan 26, 2025

Description

When opts.cwd=true the value of uv.cwd should be used to set self.cwd.

Related Issue(s)

Fixes #754

Screenshots

@folke
Copy link
Owner

folke commented Jan 28, 2025

opts.cwd should be a string?.
But I'll add support for true in config and will look into that original bug.

@dpetka2001
Copy link
Contributor Author

dpetka2001 commented Jan 28, 2025

Oh then maybe it should be self.cwd = type(cwd) == "string" and cwd or type(cwd) == "boolean" and uv.cwd() or opts.cwd or "."??

PS: I was confused about opts.cwd being the filter.cwd, but that's definitely not the case. Tried it locally and seems to also work and opts.cwd can still be a string in the picker opts like you correctly identified.

@folke
Copy link
Owner

folke commented Jan 28, 2025

The actual issue was because of truncpath. Fixed in 1069d78

@dpetka2001
Copy link
Contributor Author

dpetka2001 commented Jan 28, 2025

I don't think that fixed it. I just pulled and can still see the LICENCE file when I'm in ~/.config/nvim/lua/plugins directory, when I should not see it. Also the other items that are in the cwd are shown with paths relative to ~/.config/nvim directory. My fix shows them with no relative path to the roor dir.

@folke
Copy link
Owner

folke commented Jan 28, 2025

Right, I only fixed the path issue. Will fix the filtering

@dpetka2001
Copy link
Contributor Author

dpetka2001 commented Jan 28, 2025

I think it should be like self.cwd = type(cwd) == "string" and cwd or type(cwd) == "boolean" and uv.cwd() or opts.cwd or "." no? Not the initial I had in this PR.

folke added a commit that referenced this pull request Jan 28, 2025
@folke
Copy link
Owner

folke commented Jan 28, 2025

Filter check also fixed now

@folke
Copy link
Owner

folke commented Jan 28, 2025

Can you check if this fixes it for what you were testing?

@dpetka2001
Copy link
Contributor Author

No, it doesn't. Is what I suggested above self.cwd = type(cwd) == "string" and cwd or type(cwd) == "boolean" and uv.cwd() or opts.cwd or "." wrong?

@folke
Copy link
Owner

folke commented Jan 28, 2025

What is your setup? What options do you set and what do you want to achieve / is wrong?
And for which picker?

@dpetka2001
Copy link
Contributor Author

Ok @folke I think this all might as well not have been needed in the end. I reverted to 3a3e795 before you made all these changes, because I wanted to check something.

The problem lies in the LazyVim configuration i think. When you type <leader>fR that calls LazyVim.pick("oldfiles", { filter = { cwd = true }}) then the problem is there. But if I do :lua Snacks.picker.recent({filter = {cwd = true}}) then it shows correctly the the recent files as I would expect. It seems that somewhere in the LazyVim.pick command the opts get consumed? Or not get passed correctly?

Sorry about this, should have done more diligent investigation.

@folke
Copy link
Owner

folke commented Jan 28, 2025

No worries, but so with the latest changes in snacks, that LazyVim keymap also works as expected right?
At least it does for me.

@dpetka2001
Copy link
Contributor Author

Yes, even the LazyVim keymap works as expected now. Thank you 😄

@dpetka2001
Copy link
Contributor Author

I'm sorry again. But it doesn't. I had changed the mapping to { "<leader>fR", function() Snacks.picker.recent({filter = {cwd = true}}) end, desc = "Recent (cwd)" } and that was working. LazyVim.pick still doesn't...

Really sorry about my hastiness...

@dpetka2001
Copy link
Contributor Author

And just for completeness this is my whole picker personal configuration

picker = {
        win = {
          input = {
            keys = {
              ["<Esc>"] = { "close", mode = { "n", "i" } },
            },
          },
        },
        formatters = {
          file = {
            filename_first = true,
          },
        },
        sources = {
          smart = {
            filter = {
              paths = {
                [vim.fn.stdpath("data")] = false,
                [vim.fn.stdpath("cache")] = false,
                [vim.fn.stdpath("state")] = false,
              },
            },
          },
          projects = {
            win = {
              input = {
                keys = {
                  ["<c-g>"] = { "live_grep", mode = { "i", "n" } },
                },
              },
            },
            actions = {
              live_grep = function(_, item)
                Snacks.picker.pick("grep", {
                  cwd = item.file,
                })
              end,
            },
            confirm = function(_, item)
              Snacks.picker.pick("files", {
                cwd = item.dir,
              })
            end,
          },
          -- Credit https://github.com/folke/snacks.nvim/issues/532#issuecomment-2609303872
          files_with_symbols = {
            multi = { "files", "lsp_symbols" },
            filter = {
              ---@param p snacks.Picker
              ---@param filter snacks.picker.Filter
              transform = function(p, filter)
                local symbol_pattern = filter.pattern:match("^.-@(.*)$")
                local line_nr_pattern = filter.pattern:match("^.-:(%d*)$")
                local search_pattern = filter.pattern:match("^.-#(.*)$")
                local pattern = symbol_pattern or line_nr_pattern or search_pattern

                if pattern then
                  local item = p:current()
                  if item and item.file then
                    filter.meta.buf = vim.fn.bufadd(item.file)
                  end
                end

                if not filter.meta.buf then
                  filter.source_id = 1
                  return
                end

                if symbol_pattern then
                  filter.pattern = symbol_pattern
                  filter.current_buf = filter.meta.buf
                  filter.source_id = 2
                  return
                end

                if line_nr_pattern then
                  filter.pattern = filter.pattern:gsub(":%d*$", "")
                  filter.current_buf = filter.meta.buf
                  filter.source_id = 1
                  local item = p:current()
                  if item then
                    item.pos = { tonumber(line_nr_pattern) or 1, 0 }
                    p.preview:loc()
                  end
                  return
                end

                if search_pattern then
                  filter.pattern = filter.pattern:gsub("#.*$", "")
                  filter.current_buf = filter.meta.buf
                  filter.source_id = 1
                  if search_pattern == "" then
                    return
                  end
                  local item = p:current()
                  vim.api.nvim_buf_call(p.preview.win.buf, function()
                    vim.api.nvim_win_set_cursor(0, { 1, 0 })
                    local search = vim.fn.searchpos(search_pattern, "cw")
                    if search[1] > 0 then
                      vim.cmd("/" .. search_pattern)
                      vim.api.nvim_win_set_cursor(0, { search[1], search[2] })
                      item.pos = { search[1], search[2] }
                    end
                  end)
                  return
                end
              end,
            },
            win = {
              input = {
                keys = {
                  ["<c-n>"] = { "next_result", mode = { "i", "n" } },
                  ["<esc>"] = { "close", mode = { "i", "n" } },
                },
              },
            },
            actions = {
              close = function(p, _)
                vim.cmd("noh")
                p:close()
              end,
              next_result = function(p, _)
                local filter = p:filter()
                local pattern = filter.pattern
                local search_pattern = pattern:match("^.-#(.*)$")

                if search_pattern and search_pattern ~= "" then
                  local item = p:current()

                  vim.api.nvim_buf_call(p.preview.win.buf, function()
                    local search = vim.fn.searchpos(search_pattern, "cw")
                    if search[1] > 0 then
                      vim.cmd("/" .. search_pattern)

                      vim.api.nvim_win_set_cursor(0, { search[1], search[2] })
                      item.pos = { search[1], search[2] }
                    end
                  end)
                  return
                end
              end,
            },
          },
          lsp_symbols = {
            layout = {
              preview = "main",
              preset = "ivy",
            },
          },
        },
      },

But I don't see anything that could cause such behavior.

@folke
Copy link
Owner

folke commented Jan 28, 2025

Right, that's because of root dir being passed as cwd.
So should probably be changed to Snacks.pick("oldfiles", {cwd = true, ...

@dpetka2001
Copy link
Contributor Author

dpetka2001 commented Jan 28, 2025

Ohhh, I see. That's why when I dd(filter) in function oldfiles i see the cwd is the root dir and Snacks.picker.recent prints the correct cwd. So, we could just pass root = false to LazyVim.pick? That should do the same right?

PS: I tested locally and it works, but root = false being in opts that will get passed to Snacks.picker would that maybe cause any conflict? Because that option doesn't exist in Snacks.picker. Would it just be ignored? Or is it safer to change the whole mapping to Snacks.picker like you suggested?

@folke
Copy link
Owner

folke commented Jan 28, 2025

Probbaly better to use the correct Snacks picker. No need to go through LazyVim.pick for this I guess?

@folke
Copy link
Owner

folke commented Jan 28, 2025

Feel free to create a PR :)

@dpetka2001 dpetka2001 deleted the fix/filter_cwd branch January 28, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(picker): wrong detect cwd with same prefix root
2 participants