nvim-telescope / telescope-frecency.nvim

A telescope.nvim extension that offers intelligent prioritization when selecting files from your editing history.
MIT License
688 stars 35 forks source link

[Feature] how about add a feature for search content in the frecency files? #216

Closed cstsunfu closed 2 days ago

cstsunfu commented 4 days ago

Thanks for the good job. I have used this plugin long time and this is a pretty good plugin to find the recent files.

But sometimes I am not remember the file name but a string in the content. It is better for me to search the content in the recent files. I am not good at telescope, so I only write a ugly code to make it work at git commit 'a3e818d0', but when the plugin is update it does not work anymore. So could you provide this as a builtin feature? Thanks

     _G.plugin_telescope__frecency_content = function(top_n)
        local actions = require("telescope.actions")
        local pickers = require("telescope.pickers")
        local make_entry = require("telescope.make_entry")
        local finders = require("telescope.finders")
        local conf = require("telescope.config").values
        local vimgrep_arguments = conf.vimgrep_arguments
        local args = vim.tbl_flatten({ vimgrep_arguments })

        local frecency = require("frecency.frecency").new({})
        local frecency_picker =
            require("frecency.picker").new(frecency.database, frecency.entry_maker, frecency.fs, frecency.recency, {
                default_workspace_tag = frecency.config.default_workspace,
                editing_bufnr = vim.api.nvim_get_current_buf(),
                filter_delimiter = frecency.config.filter_delimiter,
                initial_workspace_tag = nil,
                show_unindexed = frecency.config.show_unindexed,
                workspaces = frecency.config.workspaces,
            })
        local filepath_formatter = frecency_picker.filepath_formatter(frecency_picker, {})
        local entry_maker = frecency_picker.entry_maker:create(
            filepath_formatter,
            frecency_picker.workspace,
            frecency_picker.workspace_tag
        )
        local need_scandir = not not (frecency_picker.workspace and frecency_picker.config.show_unindexed)
        local find = require("frecency.finder").new(
            frecency_picker.database,
            entry_maker,
            frecency_picker.fs,
            need_scandir,
            frecency_picker.workspace,
            frecency_picker.recency,
            frecency_picker.state
        )
        local frecency_file_path = {}
        for _, result in ipairs(find.get_results(find, find.workspace)) do
            table.insert(frecency_file_path, result.path)
        end
        local live_grepper = finders.new_job(function(prompt)
            if not prompt or prompt == "" then
                return nil
            end

            return vim.tbl_flatten({ args, "--", prompt, frecency_file_path })
        end, make_entry.gen_from_vimgrep({}), 1000, "/")

        pickers
            .new({}, {
                prompt_title = "Live Grep Frecency",
                finder = live_grepper,
                previewer = conf.grep_previewer({}),
                -- TODO: It would be cool to use `--json` output for this
                -- and then we could get the highlight positions directly.
                sorter = require("telescope.sorters").highlighter_only({}),
                attach_mappings = function(_, map)
                    map("i", "<c-space>", actions.to_fuzzy_refine)
                    return true
                end,
            })
            :find()
    end
delphinus commented 4 days ago

Almost the same idea is issued on #125. I'm thinking an idea that I will introduce a “query” function to get entries from the DB.

local files = require("frecency").query {
  limit = 100,
  order_by = "score",
}

and this contains such as……

[
  "/path/to/foo.txt",
  "/more/path/to/bar.md",

  ……
  -- up to 100 files
]

Then your code can be shorten like this.

local frecency_file_path = require("frecency").query { …… }
local live_grepper = finders.new_job(function(prompt)
  if not prompt or prompt == "" then
    return nil
  end

  return vim.tbl_flatten({ args, "--", prompt, frecency_file_path })
end, make_entry.gen_from_vimgrep({}), 1000, "/")

pickers.new(……):find()

I see some people want to output entries from DB to use outside this plugin (#157), so this function can also be a solution for it.

How do you think this? Can this satisfy your purpose?

cstsunfu commented 4 days ago

Almost the same idea is issued on #125. I'm thinking an idea that I will introduce a “query” function to get entries from the DB.

local files = require("frecency").query {
  limit = 100,
  order_by = "score",
}

and this contains such as……

[
  "/path/to/foo.txt",
  "/more/path/to/bar.md",

  ……
  -- up to 100 files
]

Then your code can be shorten like this.

local frecency_file_path = require("frecency").query { …… }
local live_grepper = finders.new_job(function(prompt)
  if not prompt or prompt == "" then
    return nil
  end

  return vim.tbl_flatten({ args, "--", prompt, frecency_file_path })
end, make_entry.gen_from_vimgrep({}), 1000, "/")

pickers.new(……):find()

I see some people want to output entries from DB to use outside this plugin (#157), so this function can also be a solution for it.

How do you think this? Can this satisfy your purpose?

Yes, I think this is a great solution. This is very helpful for my issue, and this will used on many other purpose.

delphinus commented 3 days ago

I am still implementing #217 experimentally, but I can see it achieves your purpose with simple code.

(And I also found telescope's builtin live_grep picker accepts filenames to search.)

vim.keymap.set("n", "<Leader>tg", function()
  local frecency = require("telescope").extensions.frecency
  require("telescope.builtin").live_grep {
    -- HACK: `search_dirs` can accept files to grep nevertheless its name
    search_dirs = frecency.query {},
  }
end)
delphinus commented 2 days ago

I added query() to get entries from DB. Check it.

cstsunfu commented 21 hours ago

It works. Thanks for your awesome job.