Skip to content

Commit 1452946

Browse files
committed
feat: add Sourcegraph provider
1 parent 9df3744 commit 1452946

File tree

6 files changed

+120
-43
lines changed

6 files changed

+120
-43
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A simple plugin to simplify searching for code on [Github Code Search](https://cs.github.com).
1+
A simple plugin to quickly search for code on [Github Code Search](https://cs.github.com) or [Sourcegraph](https://sourcegraph.com/search)
22

33
In `normal` mode, searches for the word under the cursor.
44

@@ -24,6 +24,7 @@ https://user-images.githubusercontent.com/33713262/226383032-113b4db8-27a3-4b8f-
2424
includeFilename = false,
2525
includeExtension = true,
2626
betaSearch = true, -- set to false if you haven't opted in to GitHub Code Search (beta)
27+
provider = "github", -- "github" | "sourcegraph"
2728
})
2829

2930
csgithub.open(url)

lua/csgithub/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
local M = {}
22

33
-- Return search url
4-
---@param args table|nil
4+
---@param args SearchArgs|nil
55
M.search = function(args)
6+
---@type SearchArgs
67
local default_args = {
78
includeFilename = false,
89
includeExtension = true,
910
betaSearch = true,
11+
provider = "github",
1012
}
1113

1214
local merged_args = vim.tbl_extend("force", default_args, args or {})
@@ -16,7 +18,7 @@ M.search = function(args)
1618
if q == nil then
1719
return nil
1820
end
19-
local url = query.construct_url(q)
21+
local url = query.construct_url(q, merged_args)
2022

2123
return url
2224
end

lua/csgithub/providers/github.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---@type SearchProvider
2+
local M = {}
3+
4+
--- @param args SearchArgs
5+
local construct_search_field = function(args)
6+
if not args.betaSearch and args.includeFilename then
7+
return "filename:"
8+
elseif not args.betaSearch and args.includeExtension then
9+
return "extension:"
10+
else
11+
return "path:"
12+
end
13+
end
14+
15+
--- @param args SearchArgs
16+
local construct_query_path = function(args)
17+
local ext = vim.fn.expand("%:e")
18+
19+
local onlyExtension = args.includeExtension and not args.includeFilename
20+
21+
if onlyExtension and args.betaSearch then
22+
return "*." .. ext
23+
elseif onlyExtension then
24+
return "." .. ext
25+
else
26+
return vim.fn.expand("%:t")
27+
end
28+
end
29+
30+
M.construct_query_options = function(args)
31+
local path = construct_query_path(args)
32+
local search_field = construct_search_field(args)
33+
return { search_field .. path }
34+
end
35+
36+
M.construct_url = function(encoded_query)
37+
local base = "https://github.com/search?type=code&q="
38+
local url = base .. encoded_query
39+
return url
40+
end
41+
42+
return M
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---@type SearchProvider
2+
local M = {}
3+
4+
--- @param args SearchArgs
5+
local construct_query_path = function(args)
6+
local ext = vim.fn.expand("%:e")
7+
8+
local onlyExtension = args.includeExtension and not args.includeFilename
9+
10+
if onlyExtension then
11+
return ".*\\." .. ext -- file:.*\.txt
12+
else
13+
return "(^|.*\\/)rc\\.lua$" -- file:(^|.*\/)rc\.lua$
14+
end
15+
end
16+
17+
M.construct_query_options = function(args)
18+
local path = construct_query_path(args)
19+
return { "file:" .. path }
20+
end
21+
22+
-- Example: https://sourcegraph.com/search?q=file:.*\.txt+term
23+
M.construct_url = function(encoded_query)
24+
local base = "https://sourcegraph.com/search?q="
25+
local url = base .. encoded_query
26+
return url
27+
end
28+
29+
return M

lua/csgithub/query.lua

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,6 @@ local function trim_string(s)
77
return (s:gsub("^%s*(.-)%s*$", "%1"))
88
end
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-
3410
M.construct_query_text = function()
3511
local utils = require("csgithub.utils")
3612
local text = ""
@@ -46,34 +22,46 @@ M.construct_query_text = function()
4622
end
4723

4824
-- Return search url
49-
---@param args table
25+
---@param args SearchArgs
5026
M.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
6856
end
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)
7765
end
7866

7967
return M

lua/csgithub/types/init.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---@alias SearchProviderEnum
2+
---| '"github"'
3+
---| '"sourcegraph"'
4+
5+
---@class SearchArgs
6+
---@field includeFilename boolean
7+
---@field includeExtension boolean
8+
---@field betaSearch boolean Whether to use the newer "Github Code Search" (https://github.com/features/code-search)
9+
---@field provider SearchProviderEnum
10+
11+
---@alias ConstructQueryOptions fun(args: SearchArgs): table<string>
12+
13+
---@class SearchProvider
14+
---@field construct_query_options ConstructQueryOptions
15+
---@field construct_url fun(encoded_query: string): string

0 commit comments

Comments
 (0)