@@ -7,30 +7,6 @@ local function trim_string(s)
77 return (s :gsub (" ^%s*(.-)%s*$" , " %1" ))
88end
99
10- M .construct_query_path = function (args )
11- local ext = vim .fn .expand (" %:e" )
12-
13- local onlyExtension = args .includeExtension and not args .includeFilename
14-
15- if onlyExtension and args .betaSearch then
16- return " *." .. ext
17- elseif onlyExtension then
18- return " ." .. ext
19- else
20- return vim .fn .expand (" %:t" )
21- end
22- end
23-
24- M .construct_search_field = function (args )
25- if not args .betaSearch and args .includeFilename then
26- return " filename:"
27- elseif not args .betaSearch and args .includeExtension then
28- return " extension:"
29- else
30- return " path:"
31- end
32- end
33-
3410M .construct_query_text = function ()
3511 local utils = require (" csgithub.utils" )
3612 local text = " "
@@ -46,34 +22,46 @@ M.construct_query_text = function()
4622end
4723
4824-- Return search url
49- --- @param args table
25+ --- @param args SearchArgs
5026M .construct_query = function (args )
5127 local query_parts = {}
5228
53- local query_text = M .construct_query_text ()
54- if query_text == " " then
55- return nil
56- end
57- -- path:
29+ --- @type SearchProvider
30+ local provider = M .get_provider (args )
31+
32+ -- query options (eg. path:myfile.txt, extension:txt, etc.)
5833 if args .includeFilename or args .includeExtension then
59- local path = M .construct_query_path (args )
60- local search_field = M .construct_search_field (args )
61- table.insert (query_parts , search_field .. path )
34+ query_parts = provider .construct_query_options (args )
6235 end
6336
6437 -- text
38+ local query_text = M .construct_query_text ()
39+ if query_text == " " then
40+ return nil
41+ end
6542 table.insert (query_parts , query_text )
6643
67- return table.concat (query_parts , " " )
44+ local query = table.concat (query_parts , " " )
45+ return query
46+ end
47+
48+ --- @param args SearchArgs
49+ --- @return SearchProvider
50+ M .get_provider = function (args )
51+ if args .provider == " sourcegraph" then
52+ return require (" csgithub.providers.sourcegraph" )
53+ else
54+ return require (" csgithub.providers.github" )
55+ end
6856end
6957
70- --- @param query string
71- M .construct_url = function (query )
58+ --- @param url_query string
59+ --- @param args SearchArgs
60+ M .construct_url = function (url_query , args )
7261 local utils = require (" csgithub.utils" )
73- local encoded_query = utils .url_encode (query )
74- local base = " https://github.com/search?type=code&q="
75- local url = base .. encoded_query
76- return url
62+ local encoded_query = utils .url_encode (url_query )
63+ local provider = M .get_provider (args )
64+ return provider .construct_url (encoded_query )
7765end
7866
7967return M
0 commit comments