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

Use frecency with git_files #49

Open
marekdedic opened this issue Oct 13, 2021 · 12 comments
Open

Use frecency with git_files #49

marekdedic opened this issue Oct 13, 2021 · 12 comments

Comments

@marekdedic
Copy link

Hi,
I would like to use this with git_files or ideally with https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#falling-back-to-find_files-if-git_files-cant-find-a-git-directory

Is there any way to do this? I think #13 and #4 are going this way of being able to use frecency with any picker, not just recent files, but the current workaround with the workspaces is not it.

Anyway, thanks for the great work!

@Conni2461
Copy link
Member

the current workaround with the workspaces is not it

Is it really a workaround if this feature was planed like for the beginning 🤔

git_files isnt that easy to integrate here, because we arnt shelling out to get the workspace files. We use plenary.scandir which uses uv.fs_scandir

@marekdedic
Copy link
Author

Hi,
as far as I understand the architecture of telescope (which is not that much I have to admit), the fundamental issue for this as well as the other mentioned issues is that frecency is a picker and not a sorter - if it were a sorter, it could work on find_files, git_files and any other files-based picker. But because it's a picker, it has to replicate the functionality of all these pickers if it is to be able to do that.

Am I correct?

@Conni2461
Copy link
Member

A sorter is nothing more than a function that gets a entry and the current prompt and returns a score.

You maybe can get a list, from find_files and ask sql for the score of the file. That way you could do a initial sorting. And if the prompt is not empty call a real fuzzy sorter?

But you still need a simple list all my frecency entries because the sorter cant add new things to a picker.

PR welcome

@Conni2461
Copy link
Member

I thought about it a little bit more. I havent this project, just havent seen the creator in months.

I think this might actually work. A additional frecency sorter you can set on top of find_files and git_files which might be a alternative to the tags idea. But i dont think the frecency picker itself will go away because sometimes you still need a look outside of your cwd.

@marekdedic
Copy link
Author

Yes, that is what I was thinking - a frecency sorter set on top of find_files or git_files which wouldn't add entries, just sort them based on their frecency... There is obviously a lot of things to think through like what to do with files which are not in the database, whether frecency should kick in with a non-empty prompt (honestly, why not?) etc.

I do think that this picker would be obsoleted by that - you could just use the frecency sorter with the builtin.oldfiles picker instead to achieve the same effect, right?

@tim-hilt
Copy link

Hey there! I'm seeing the exact same issue with this project: It's a picker, rather than a sorter.

This would also make sense for :Telescope buffers, to pre-select the most frecent buffer.

Are there any news on this issue?

@marekdedic
Copy link
Author

I think the news are that it will happen if there's someone willing to make it...

@nikhilkalige
Copy link

nikhilkalige commented Feb 3, 2022

This is what I am using

local ext = require("telescope._extensions")
local builtins = require("telescope.builtin")
local frecency_db = require("telescope._extensions.frecency.db_client")

local fzf = ext.manager.fzf

local function frecency_score(self, prompt, line, entry)
  if prompt == nil or prompt == "" then
    for _, file_entry in ipairs(self.state.frecency) do
      local filepath = entry.cwd .. "/" .. entry.value
      if file_entry.filename == filepath then
        return 9999 - file_entry.score
      end
    end

    return 9999
  end

  return self.default_scoring_function(self, prompt, line, entry)
end

local function frecency_start(self, prompt)
  self.default_start(self, prompt)

  if not self.state.frecency then
    self.state.frecency = frecency_db.get_file_scores()
  end
end

local frecency_sorter = function(opts)
  local fzf_sorter = fzf.native_fzf_sorter()

  fzf_sorter.default_scoring_function = fzf_sorter.scoring_function
  fzf_sorter.default_start = fzf_sorter.start

  fzf_sorter.scoring_function = frecency_score
  fzf_sorter.start = frecency_start

  return fzf_sorter
end

return {
  frecency_sorter = frecency_sorter,
}

---
telescope.setup({
      defaults = {
          file_sorter = mysorter.frecency_sorter,
      },

@danielo515
Copy link

I was about to ask the same thing, I would love to use this not only for files, but also for commands

@Innei
Copy link

Innei commented Aug 10, 2023

nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#falling-back-to-find_files-if-git_files-cant-find-a-git-directory

local frecency_db = require("telescope._extensions.frecency.db_client")

Is outdated, there are new way to handle this?

@delphinus
Copy link
Member

db_client.lua is removed by #93. So I think a similar method for getting entries from frecency is below.

    self.state.frecency = require("frecency").frecency().picker:fetch_results()

(I have not tried @nikhilkalige's snippets with this change.)

@FelipeLema
Copy link

FelipeLema commented Aug 18, 2023

should such sorter be added to telescope.defaults.file_sorter?

I used to have it under telescope.extensions.frecency.sorter but nvim is complaining about a nil sorter entry

¯\_(ツ)_/¯

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

No branches or pull requests

8 participants