Skip to content

feat: add Sourcegraph provider #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A simple plugin to simplify searching for code on [Github Code Search](https://cs.github.com).
A simple plugin to quickly search for code on [Github Code Search](https://cs.github.com) or [Sourcegraph](https://sourcegraph.com/search)

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

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

csgithub.open(url)
Expand Down
6 changes: 4 additions & 2 deletions lua/csgithub/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
local M = {}

-- Return search url
---@param args table|nil
---@param args SearchArgs|nil
M.search = function(args)
---@type SearchArgs
local default_args = {
includeFilename = false,
includeExtension = true,
betaSearch = true,
provider = "github",
}

local merged_args = vim.tbl_extend("force", default_args, args or {})
Expand All @@ -16,7 +18,7 @@ M.search = function(args)
if q == nil then
return nil
end
local url = query.construct_url(q)
local url = query.construct_url(q, merged_args)

return url
end
Expand Down
42 changes: 42 additions & 0 deletions lua/csgithub/providers/github.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---@type SearchProvider
local M = {}

--- @param args SearchArgs
local construct_search_field = function(args)
if not args.betaSearch and args.includeFilename then
return "filename:"
elseif not args.betaSearch and args.includeExtension then
return "extension:"
else
return "path:"
end
end

--- @param args SearchArgs
local construct_query_path = function(args)
local ext = vim.fn.expand("%:e")

local onlyExtension = args.includeExtension and not args.includeFilename

if onlyExtension and args.betaSearch then
return "*." .. ext
elseif onlyExtension then
return "." .. ext
else
return vim.fn.expand("%:t")
end
end

M.construct_query_options = function(args)
local path = construct_query_path(args)
local search_field = construct_search_field(args)
return { search_field .. path }
end

M.construct_url = function(encoded_query)
local base = "https://github.com/search?type=code&q="
local url = base .. encoded_query
return url
end

return M
29 changes: 29 additions & 0 deletions lua/csgithub/providers/sourcegraph.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---@type SearchProvider
local M = {}

--- @param args SearchArgs
local construct_query_path = function(args)
local ext = vim.fn.expand("%:e")

local onlyExtension = args.includeExtension and not args.includeFilename

if onlyExtension then
return ".*\\." .. ext -- file:.*\.txt
else
return "(^|.*\\/)rc\\.lua$" -- file:(^|.*\/)rc\.lua$
end
end

M.construct_query_options = function(args)
local path = construct_query_path(args)
return { "file:" .. path }
end

-- Example: https://sourcegraph.com/search?q=file:.*\.txt+term
M.construct_url = function(encoded_query)
local base = "https://sourcegraph.com/search?q="
local url = base .. encoded_query
return url
end

return M
68 changes: 28 additions & 40 deletions lua/csgithub/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,6 @@ local function trim_string(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end

M.construct_query_path = function(args)
local ext = vim.fn.expand("%:e")

local onlyExtension = args.includeExtension and not args.includeFilename

if onlyExtension and args.betaSearch then
return "*." .. ext
elseif onlyExtension then
return "." .. ext
else
return vim.fn.expand("%:t")
end
end

M.construct_search_field = function(args)
if not args.betaSearch and args.includeFilename then
return "filename:"
elseif not args.betaSearch and args.includeExtension then
return "extension:"
else
return "path:"
end
end

M.construct_query_text = function()
local utils = require("csgithub.utils")
local text = ""
Expand All @@ -46,34 +22,46 @@ M.construct_query_text = function()
end

-- Return search url
---@param args table
---@param args SearchArgs
M.construct_query = function(args)
local query_parts = {}

local query_text = M.construct_query_text()
if query_text == "" then
return nil
end
-- path:
---@type SearchProvider
local provider = M.get_provider(args)

-- query options (eg. path:myfile.txt, extension:txt, etc.)
if args.includeFilename or args.includeExtension then
local path = M.construct_query_path(args)
local search_field = M.construct_search_field(args)
table.insert(query_parts, search_field .. path)
query_parts = provider.construct_query_options(args)
end

-- text
local query_text = M.construct_query_text()
if query_text == "" then
return nil
end
table.insert(query_parts, query_text)

return table.concat(query_parts, " ")
local query = table.concat(query_parts, " ")
return query
end

---@param args SearchArgs
---@return SearchProvider
M.get_provider = function(args)
if args.provider == "sourcegraph" then
return require("csgithub.providers.sourcegraph")
else
return require("csgithub.providers.github")
end
end

---@param query string
M.construct_url = function(query)
---@param url_query string
---@param args SearchArgs
M.construct_url = function(url_query, args)
local utils = require("csgithub.utils")
local encoded_query = utils.url_encode(query)
local base = "https://github.com/search?type=code&q="
local url = base .. encoded_query
return url
local encoded_query = utils.url_encode(url_query)
local provider = M.get_provider(args)
return provider.construct_url(encoded_query)
end

return M
15 changes: 15 additions & 0 deletions lua/csgithub/types/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---@alias SearchProviderEnum
---| '"github"'
---| '"sourcegraph"'

---@class SearchArgs
---@field includeFilename boolean
---@field includeExtension boolean
---@field betaSearch boolean Whether to use the newer "Github Code Search" (https://github.com/features/code-search)
---@field provider SearchProviderEnum

---@alias ConstructQueryOptions fun(args: SearchArgs): table<string>

---@class SearchProvider
---@field construct_query_options ConstructQueryOptions
---@field construct_url fun(encoded_query: string): string