Another stream
This commit is contained in:
84
scratch/file_finder.lua
Normal file
84
scratch/file_finder.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
local telescope = require('telescope')
|
||||
|
||||
-- Goals:
|
||||
-- 1. You pick a directory
|
||||
-- 2. We `git ls-files` in that directory ONCE and ONLY ONCE to get the results.
|
||||
-- 3. You can fuzzy find those results w/ fzf
|
||||
-- 4. Select one and go to file.
|
||||
|
||||
|
||||
--[[
|
||||
ls_files_job.start()
|
||||
fzf_job.stdin = ls_files_job.stdout
|
||||
|
||||
self.stdin = vim.loop.new_pipe(false)
|
||||
-> self.stdin = finder.stdout
|
||||
|
||||
|
||||
-- Finder:
|
||||
intermediary_pipe = self.stdout
|
||||
|
||||
-- repeat send this pipe when we want to get new filtering
|
||||
self.stdin = intermediary_pipe
|
||||
|
||||
|
||||
-- Filter + Sort
|
||||
ok, we could do scoring + cutoff and have that always be the case.
|
||||
|
||||
OR
|
||||
|
||||
filter takes a function, signature (prompt: str, line: str): number
|
||||
|
||||
=> echo $line | fzf --filter "prompt"
|
||||
return stdout != ""
|
||||
|
||||
=> lua_fuzzy_finder(prompt, line) return true if good enough
|
||||
|
||||
TODO: Rename everything to be more clear like the name below.
|
||||
IFilterSorterAbstractFactoryGeneratorv1ProtoBeta
|
||||
|
||||
--]]
|
||||
|
||||
local string_distance = require('telescope.algos.string_distance')
|
||||
|
||||
local file_finder = telescope.finders.new {
|
||||
fn_command = function(self, prompt)
|
||||
-- todo figure out how to cache this later
|
||||
if false then
|
||||
if self[prompt] == nil then
|
||||
self[prompt] = nil
|
||||
end
|
||||
|
||||
return self[prompt]
|
||||
else
|
||||
return 'git ls-files'
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
local file_sorter = telescope.sorters.new {
|
||||
scoring_function = function(self, prompt, line)
|
||||
if prompt == '' then return 0 end
|
||||
if not line then return -1 end
|
||||
|
||||
local dist = string_distance(prompt, line)
|
||||
-- if dist > (0.75 * #line) and #prompt > 3 then
|
||||
-- return -1
|
||||
-- end
|
||||
|
||||
return dist
|
||||
end
|
||||
}
|
||||
|
||||
local file_previewer = telescope.previewers.vim_buffer
|
||||
|
||||
local file_picker = telescope.pickers.new {
|
||||
previewer = file_previewer
|
||||
}
|
||||
|
||||
file_picker:find {
|
||||
prompt = 'Find File',
|
||||
finder = file_finder,
|
||||
sorter = file_sorter,
|
||||
}
|
||||
|
||||
@@ -5,37 +5,19 @@ local telescope = require('telescope')
|
||||
-- When updating the table, we should call filter on those items
|
||||
-- and then only display ones that pass the filter
|
||||
local rg_finder = telescope.finders.new {
|
||||
fn_command = function(prompt)
|
||||
fn_command = function(self, prompt)
|
||||
return string.format('rg --vimgrep %s', prompt)
|
||||
end,
|
||||
|
||||
responsive = false
|
||||
}
|
||||
|
||||
|
||||
local p = telescope.pickers.new {
|
||||
previewer = telescope.previewers.new(function(preview_win, preview_bufnr, results_bufnr, row)
|
||||
assert(preview_bufnr)
|
||||
|
||||
local line = vim.api.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1]
|
||||
local file_name = vim.split(line, ":")[1]
|
||||
|
||||
-- print(file_name)
|
||||
-- vim.fn.termopen(
|
||||
-- string.format("bat --color=always --style=grid %s"),
|
||||
-- vim.fn.fnamemodify(file_name, ":p")
|
||||
local bufnr = vim.fn.bufadd(file_name)
|
||||
vim.fn.bufload(bufnr)
|
||||
|
||||
-- TODO: We should probably call something like this because we're not always getting highlight and all that stuff.
|
||||
-- api.nvim_command('doautocmd filetypedetect BufRead ' .. vim.fn.fnameescape(filename))
|
||||
vim.api.nvim_win_set_buf(preview_win, bufnr)
|
||||
vim.api.nvim_win_set_option(preview_win, 'wrap', false)
|
||||
vim.api.nvim_win_set_option(preview_win, 'winhl', 'Normal:Normal')
|
||||
vim.api.nvim_win_set_option(preview_win, 'winblend', 20)
|
||||
vim.api.nvim_win_set_option(preview_win, 'signcolumn', 'no')
|
||||
vim.api.nvim_win_set_option(preview_win, 'foldlevel', 100)
|
||||
end)
|
||||
previewer = telescope.previewers.vim_buffer
|
||||
}
|
||||
p:find {
|
||||
prompt = 'grep',
|
||||
finder = rg_finder
|
||||
}
|
||||
p:find(rg_finder)
|
||||
|
||||
|
||||
|
||||
@@ -4,3 +4,7 @@ sleep 1
|
||||
echo "cool"
|
||||
sleep 1
|
||||
echo "world"
|
||||
sleep 1
|
||||
echo "x"
|
||||
sleep 1
|
||||
echo "y"
|
||||
|
||||
7
scratch/string_distance_stuff.lua
Normal file
7
scratch/string_distance_stuff.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
local string_distance = require('telescope.algos.string_distance')
|
||||
|
||||
print(string_distance("hello", "help"))
|
||||
print(string_distance("hello", "hello"))
|
||||
print(string_distance("hello", "asdf"))
|
||||
|
||||
Reference in New Issue
Block a user