Feat/refactor picker refresh (#32)
* Refactor refreshing the picker after add/delete/rename project * Tidying and add some comments explaining functions
This commit is contained in:
@@ -5,6 +5,7 @@ if not has_telescope then
|
||||
end
|
||||
|
||||
local actions = require("telescope.actions")
|
||||
local action_state = require("telescope.actions.state")
|
||||
local finders = require("telescope.finders")
|
||||
local pickers = require("telescope.pickers")
|
||||
local conf = require("telescope.config").values
|
||||
@@ -15,6 +16,9 @@ local project_actions = require("telescope._extensions.project_actions")
|
||||
|
||||
local project_dirs_file = vim.fn.stdpath('data') .. '/telescope-projects.txt'
|
||||
|
||||
-- Checks if the file containing the list of project
|
||||
-- directories already exists.
|
||||
-- If it doesn't exist, it creates it.
|
||||
local function check_for_project_dirs_file()
|
||||
local f = io.open(project_dirs_file, "r")
|
||||
if f ~= nil then
|
||||
@@ -27,7 +31,9 @@ local function check_for_project_dirs_file()
|
||||
end
|
||||
end
|
||||
|
||||
local select_project = function(opts, projects)
|
||||
-- Creates a Telescope `finder` based on the given options
|
||||
-- and list of projects
|
||||
local create_finder = function(opts, projects)
|
||||
local display_type = opts.display_type
|
||||
local widths = {
|
||||
title = 0,
|
||||
@@ -61,10 +67,7 @@ local select_project = function(opts, projects)
|
||||
}
|
||||
end
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt_title = 'Select a project',
|
||||
results_title = 'Projects',
|
||||
finder = finders.new_table {
|
||||
return finders.new_table {
|
||||
results = projects,
|
||||
entry_maker = function(entry)
|
||||
entry.value = entry.path
|
||||
@@ -72,9 +75,66 @@ local select_project = function(opts, projects)
|
||||
entry.display = make_display
|
||||
return entry
|
||||
end,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
local get_last_accessed_time = function(path)
|
||||
local expanded_path = vim.fn.expand(path)
|
||||
local fs_stat = vim.loop.fs_stat(expanded_path)
|
||||
if fs_stat then
|
||||
return fs_stat.atime.sec
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
-- Get information on all of the projects in the
|
||||
-- `project_dirs_file` and output it as a list
|
||||
local get_projects = function()
|
||||
check_for_project_dirs_file()
|
||||
local projects = {}
|
||||
|
||||
for line in io.lines(project_dirs_file) do
|
||||
local title, path = line:match("^(.-)=(.-)$")
|
||||
local last_accessed = get_last_accessed_time(path)
|
||||
table.insert(projects, {
|
||||
title = title,
|
||||
path = path,
|
||||
last_accessed = last_accessed
|
||||
})
|
||||
end
|
||||
|
||||
table.sort(projects, function(a,b)
|
||||
return a.last_accessed > b.last_accessed
|
||||
end)
|
||||
|
||||
return projects
|
||||
end
|
||||
|
||||
-- The main function.
|
||||
-- This creates a picker with a list of all of the projects,
|
||||
-- and attaches the appropriate mappings for associated
|
||||
-- actions.
|
||||
local project = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local projects = get_projects()
|
||||
local new_finder = create_finder(opts, projects)
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt_title = 'Select a project',
|
||||
results_title = 'Projects',
|
||||
finder = new_finder,
|
||||
sorter = conf.file_sorter(opts),
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
local refresh_projects = function()
|
||||
local picker = action_state.get_current_picker(prompt_bufnr)
|
||||
picker:refresh(create_finder(opts,get_projects()), {reset_prompt=true})
|
||||
end
|
||||
project_actions.add_project:enhance({ post = refresh_projects })
|
||||
project_actions.delete_project:enhance({ post = refresh_projects })
|
||||
project_actions.rename_project:enhance({ post = refresh_projects })
|
||||
|
||||
map('n', 'd', project_actions.delete_project)
|
||||
map('n', 'r', project_actions.rename_project)
|
||||
map('n', 'c', project_actions.add_project)
|
||||
@@ -92,37 +152,4 @@ local select_project = function(opts, projects)
|
||||
}):find()
|
||||
end
|
||||
|
||||
local get_last_accessed_time = function(path)
|
||||
local expanded_path = vim.fn.expand(path)
|
||||
local fs_stat = vim.loop.fs_stat(expanded_path)
|
||||
if fs_stat then
|
||||
return fs_stat.atime.sec
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
local project = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
check_for_project_dirs_file()
|
||||
local projects = {}
|
||||
|
||||
for line in io.lines(project_dirs_file) do
|
||||
local title, path = line:match("^(.-)=(.-)$")
|
||||
local last_accessed = get_last_accessed_time(path)
|
||||
table.insert(projects, {
|
||||
title = title,
|
||||
path = path,
|
||||
last_accessed = last_accessed
|
||||
})
|
||||
end
|
||||
|
||||
table.sort(projects, function(a,b)
|
||||
return a.last_accessed > b.last_accessed
|
||||
end)
|
||||
|
||||
select_project(opts, projects)
|
||||
end
|
||||
|
||||
return telescope.register_extension {exports = {project = project}}
|
||||
|
||||
@@ -10,6 +10,7 @@ function string.starts(String,Start)
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
end
|
||||
|
||||
-- Create a new project and add it to the list in the `project_dirs_file`
|
||||
project_actions.add_project = function(prompt_bufnr)
|
||||
local git_root = vim.fn.systemlist("git -C " .. vim.loop.cwd() .. " rev-parse --show-toplevel")[
|
||||
1
|
||||
@@ -44,10 +45,10 @@ project_actions.add_project = function(prompt_bufnr)
|
||||
print('project added: ' .. project_title)
|
||||
end
|
||||
io.close(file)
|
||||
actions.close(prompt_bufnr)
|
||||
require 'telescope'.extensions.project.project()
|
||||
end
|
||||
|
||||
-- Rename the selected project within the `project_dirs_file`.
|
||||
-- Uses a name provided by the user.
|
||||
project_actions.rename_project = function(prompt_bufnr)
|
||||
local oldName = actions.get_selected_entry(prompt_bufnr).ordinal
|
||||
local newName = vim.fn.input('Rename ' ..oldName.. ' to: ', oldName)
|
||||
@@ -67,10 +68,9 @@ project_actions.rename_project = function(prompt_bufnr)
|
||||
file:write(newLines)
|
||||
file:close()
|
||||
print('Project renamed: ' .. actions.get_selected_entry(prompt_bufnr).ordinal .. ' -> ' .. newName)
|
||||
actions.close(prompt_bufnr)
|
||||
require 'telescope'.extensions.project.project()
|
||||
end
|
||||
|
||||
-- Delete the selected project from the `project_dirs_file`
|
||||
project_actions.delete_project = function(prompt_bufnr)
|
||||
local newLines = ""
|
||||
for line in io.lines(project_dirs_file) do
|
||||
@@ -86,10 +86,10 @@ project_actions.delete_project = function(prompt_bufnr)
|
||||
file:write(newLines)
|
||||
file:close()
|
||||
print('Project deleted: ' .. actions.get_selected_entry(prompt_bufnr).ordinal)
|
||||
actions.close(prompt_bufnr)
|
||||
require 'telescope'.extensions.project.project()
|
||||
end
|
||||
|
||||
-- Find files within the selected project using the
|
||||
-- Telescope builtin `find_files`.
|
||||
project_actions.find_project_files = function(prompt_bufnr)
|
||||
local dir = actions.get_selected_entry(prompt_bufnr).value
|
||||
actions._close(prompt_bufnr, true)
|
||||
@@ -97,6 +97,8 @@ project_actions.find_project_files = function(prompt_bufnr)
|
||||
builtin.find_files({cwd = dir})
|
||||
end
|
||||
|
||||
-- Browse through files within the selected project using
|
||||
-- the Telescope builtin `file_browser`.
|
||||
project_actions.browse_project_files = function(prompt_bufnr)
|
||||
local dir = actions.get_selected_entry(prompt_bufnr).value
|
||||
actions._close(prompt_bufnr, true)
|
||||
@@ -104,6 +106,8 @@ project_actions.browse_project_files = function(prompt_bufnr)
|
||||
builtin.file_browser({cwd = dir})
|
||||
end
|
||||
|
||||
-- Search within files in the selected project using
|
||||
-- the Telescope builtin `live_grep`.
|
||||
project_actions.search_in_project_files = function(prompt_bufnr)
|
||||
local dir = actions.get_selected_entry(prompt_bufnr).value
|
||||
actions._close(prompt_bufnr, true)
|
||||
@@ -111,6 +115,8 @@ project_actions.search_in_project_files = function(prompt_bufnr)
|
||||
builtin.live_grep({cwd = dir})
|
||||
end
|
||||
|
||||
-- Search the recently used files within the selected project
|
||||
-- using the Telescope builtin `oldfiles`.
|
||||
project_actions.recent_project_files = function(prompt_bufnr)
|
||||
local dir = actions.get_selected_entry(prompt_bufnr).value
|
||||
actions._close(prompt_bufnr, true)
|
||||
@@ -118,6 +124,7 @@ project_actions.recent_project_files = function(prompt_bufnr)
|
||||
builtin.oldfiles({cwd_only = true})
|
||||
end
|
||||
|
||||
-- Change working directory to the selected project and close the picker.
|
||||
project_actions.change_working_directory = function(prompt_bufnr)
|
||||
local dir = actions.get_selected_entry(prompt_bufnr).value
|
||||
actions.close(prompt_bufnr)
|
||||
|
||||
Reference in New Issue
Block a user