11local utils = require (" cd-project.utils" )
22local success , _ = pcall (require , " telescope.pickers" )
33if not success then
4- utils .log_error (" telescope not installed" )
5- return
4+ utils .log_error (" telescope not installed" )
5+ return
66end
77local pickers = require (" telescope.pickers" )
88local finders = require (" telescope.finders" )
@@ -16,136 +16,174 @@ local api = require("cd-project.api")
1616--- @param callback fun ( project : CdProject.Project ): nil
1717--- @param opts ? table
1818local function project_picker (callback , opts )
19- opts = opts or {}
20- -- TODO: a format function
21- local projects = repo .get_projects ()
22- local maxLength = 0
23- for _ , project in ipairs (projects ) do
24- if # project .name > maxLength then
25- maxLength = # project .name
26- end
27- end
19+ opts = opts or {}
20+ -- TODO: a format function
21+ local projects = repo .get_projects ()
22+ local maxLength = 0
23+ for _ , project in ipairs (projects ) do
24+ if # project .name > maxLength then
25+ maxLength = # project .name
26+ end
27+ end
2828
29- pickers
30- .new (opts , {
31- attach_mappings = function (prompt_bufnr )
32- actions .select_default :replace (function ()
33- actions .close (prompt_bufnr )
34- --- @type CdProject.Project
35- local selected_project = action_state .get_selected_entry ().value
36- callback (selected_project )
37- end )
38- return true
39- end ,
40- prompt_title = " [CdProject]" ,
41- finder = finders .new_table ({
42- results = projects ,
43- --- @param project CdProject.Project
44- entry_maker = function (project )
45- return {
46- value = project ,
47- display = utils .format_entry (project , maxLength ),
48- ordinal = project .path ,
49- }
50- end ,
51- }),
52- sorter = conf .generic_sorter (opts ),
53- })
54- :find ()
29+ pickers
30+ .new (opts , {
31+ attach_mappings = function (prompt_bufnr )
32+ actions .select_default :replace (function ()
33+ actions .close (prompt_bufnr )
34+ --- @type CdProject.Project
35+ local selected_project = action_state .get_selected_entry ().value
36+ callback (selected_project )
37+ end )
38+ return true
39+ end ,
40+ prompt_title = " [CdProject]" ,
41+ finder = finders .new_table ({
42+ results = projects ,
43+ --- @param project CdProject.Project
44+ entry_maker = function (project )
45+ return {
46+ value = project ,
47+ display = utils .format_entry (project , maxLength ),
48+ ordinal = project .path ,
49+ }
50+ end ,
51+ }),
52+ sorter = conf .generic_sorter (opts ),
53+ })
54+ :find ()
5555end
5656
5757--- @param opts ? table
5858local cd_project = function (opts )
59- opts = opts or {}
60- local projects = repo .get_projects ()
61- local maxLength = 0
62- for _ , project in ipairs (projects ) do
63- if # project .name > maxLength then
64- maxLength = # project .name
65- end
66- end
59+ opts = opts or {}
60+ local projects = repo .get_projects ()
61+ local maxLength = 0
62+ for _ , project in ipairs (projects ) do
63+ if # project .name > maxLength then
64+ maxLength = # project .name
65+ end
66+ end
6767
68- pickers
69- .new (opts , {
70- attach_mappings = function (prompt_bufnr )
71- actions .select_default :replace (function ()
72- actions .close (prompt_bufnr )
73- --- @type CdProject.Project
74- local selected_project = action_state .get_selected_entry ().value
75- api .cd_project (selected_project .path )
76- end )
77- return true
78- end ,
79- prompt_title = " cd to project" ,
80- finder = finders .new_table ({
81- results = projects ,
82- --- @param project CdProject.Project
83- entry_maker = function (project )
84- return {
85- value = project ,
86- display = utils .format_entry (project , maxLength ),
87- ordinal = project .path ,
88- }
89- end ,
90- }),
91- sorter = conf .generic_sorter (opts ),
92- })
93- :find ()
68+ pickers
69+ .new (opts , {
70+ attach_mappings = function (prompt_bufnr )
71+ actions .select_default :replace (function ()
72+ actions .close (prompt_bufnr )
73+ --- @type CdProject.Project
74+ local selected_project = action_state .get_selected_entry ().value
75+ api .cd_project (selected_project .path )
76+ end )
77+ return true
78+ end ,
79+ prompt_title = " cd to project" ,
80+ finder = finders .new_table ({
81+ results = projects ,
82+ --- @param project CdProject.Project
83+ entry_maker = function (project )
84+ return {
85+ value = project ,
86+ display = utils .format_entry (project , maxLength ),
87+ ordinal = project .path ,
88+ }
89+ end ,
90+ }),
91+ sorter = conf .generic_sorter (opts ),
92+ })
93+ :find ()
94+ end
95+
96+ --- @param opts ? table
97+ local cd_project_in_tab = function (opts )
98+ opts = opts or {}
99+ local projects = repo .get_projects ()
100+ local maxLength = 0
101+ for _ , project in ipairs (projects ) do
102+ if # project .name > maxLength then
103+ maxLength = # project .name
104+ end
105+ end
106+
107+ pickers
108+ .new (opts , {
109+ attach_mappings = function (prompt_bufnr )
110+ actions .select_default :replace (function ()
111+ actions .close (prompt_bufnr )
112+ --- @type CdProject.Project
113+ local selected_project = action_state .get_selected_entry ().value
114+ api .cd_project_in_tab (selected_project .path )
115+ end )
116+ return true
117+ end ,
118+ prompt_title = " tcd to project" ,
119+ finder = finders .new_table ({
120+ results = projects ,
121+ --- @param project CdProject.Project
122+ entry_maker = function (project )
123+ return {
124+ value = project ,
125+ display = utils .format_entry (project , maxLength ),
126+ ordinal = project .path ,
127+ }
128+ end ,
129+ }),
130+ sorter = conf .generic_sorter (opts ),
131+ })
132+ :find ()
94133end
95134
96135local search_and_add = function (opts )
97- opts = opts or {}
136+ opts = opts or {}
98137
99- local find_command = utils .check_for_find_cmd ()
100- if not find_command then
101- utils .log_error (" You need to install fd or find." )
102- return
103- end
138+ local find_command = utils .check_for_find_cmd ()
139+ if not find_command then
140+ utils .log_error (" You need to install fd or find." )
141+ return
142+ end
104143
105- vim .fn .jobstart (find_command , {
106- stdout_buffered = true ,
107- on_stdout = function (_ , data )
108- pickers
109- .new (opts , {
110- attach_mappings = function (prompt_bufnr )
111- actions .select_default :replace (function ()
112- actions .close (prompt_bufnr )
113- local selected_dir = action_state .get_selected_entry ().value
144+ vim .fn .jobstart (find_command , {
145+ stdout_buffered = true ,
146+ on_stdout = function (_ , data )
147+ pickers
148+ .new (opts , {
149+ attach_mappings = function (prompt_bufnr )
150+ actions .select_default :replace (function ()
151+ actions .close (prompt_bufnr )
152+ local selected_dir = action_state .get_selected_entry ().value
114153
115- vim .ui .input ({ prompt = " Add a project name: " }, function (name )
116- if not name or name == " " then
117- vim .notify (
118- ' No name given, using "' .. utils .get_tail_of_path (selected_dir ) .. ' " instead'
119- )
120- local project = api .build_project_obj (selected_dir )
121- if not project then
122- return
123- end
124- return api .add_project (project )
125- end
154+ vim .ui .input ({ prompt = " Add a project name: " }, function (name )
155+ if not name or name == " " then
156+ vim .notify (' No name given, using "' .. utils .get_tail_of_path (selected_dir ) .. ' " instead' )
157+ local project = api .build_project_obj (selected_dir )
158+ if not project then
159+ return
160+ end
161+ return api .add_project (project )
162+ end
126163
127- local project = api .build_project_obj (selected_dir , name )
128- if not project then
129- return
130- end
131- return api .add_project (project )
132- end )
133- end )
134- return true
135- end ,
136- prompt_title = " Select dir to add" ,
137- finder = finders .new_table ({
138- results = data ,
139- }),
140- sorter = conf .file_sorter (opts ),
141- })
142- :find ()
143- end ,
144- })
164+ local project = api .build_project_obj (selected_dir , name )
165+ if not project then
166+ return
167+ end
168+ return api .add_project (project )
169+ end )
170+ end )
171+ return true
172+ end ,
173+ prompt_title = " Select dir to add" ,
174+ finder = finders .new_table ({
175+ results = data ,
176+ }),
177+ sorter = conf .file_sorter (opts ),
178+ })
179+ :find ()
180+ end ,
181+ })
145182end
146183
147184return {
148- cd_project = cd_project ,
149- search_and_add = search_and_add ,
185+ cd_project = cd_project ,
186+ cd_project_in_tab = cd_project_in_tab ,
187+ search_and_add = search_and_add ,
150188 project_picker = project_picker ,
151189}
0 commit comments