From a396c287d4132f46a92d9d4c854a1162e5ad5b96 Mon Sep 17 00:00:00 2001 From: KitchenTable99 Date: Wed, 19 Apr 2023 00:08:42 -0400 Subject: [PATCH 1/3] add support for non-beta search users Without this change, users who haven't opted into the beta-search are met with a syntax error when launching their browser. I added an additional flag to modify what goes into the query. Tested this by trying every combination locally and it worked as expected. --- README.md | 1 + lua/csgithub/init.lua | 43 +++++++++++------------ lua/csgithub/query.lua | 77 +++++++++++++++++++++++++----------------- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index d4e112b..38c3cef 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ https://user-images.githubusercontent.com/33713262/226383032-113b4db8-27a3-4b8f- local url = csgithub.search({ includeFilename = false, includeExtension = true, + betaSearch = false, -- if you have opted in to the beta search on github, toggle this to true }) csgithub.open(url) diff --git a/lua/csgithub/init.lua b/lua/csgithub/init.lua index ba3a012..c1314bb 100644 --- a/lua/csgithub/init.lua +++ b/lua/csgithub/init.lua @@ -3,34 +3,35 @@ local M = {} -- Return search url ---@param args table|nil M.search = function(args) - local default_args = { - includeFilename = false, - includeExtension = true, - } + local default_args = { + includeFilename = false, + includeExtension = true, + betaSearch = false, + } - local merged_args = vim.tbl_extend("force", default_args, args or {}) + local merged_args = vim.tbl_extend("force", default_args, args or {}) - local query = require("csgithub.query") - local q = query.construct_query(merged_args) - local url = query.construct_url(q) + local query = require("csgithub.query") + local q = query.construct_query(merged_args) + local url = query.construct_url(q) - return url + return url end function M.open(url) - -- return if url is empty - if url == nil or url == "" then - print("Error: url is empty!") - return - end + -- return if url is empty + if url == nil or url == "" then + print("Error: url is empty!") + return + end - if vim.fn.has("mac") == 1 then - vim.fn.jobstart({ "open", url }, { detach = true }) - elseif vim.fn.has("unix") == 1 then - vim.fn.jobstart({ "xdg-open", url }, { detach = true }) - else - print("Error: unknown open command on this OS!") - end + if vim.fn.has("mac") == 1 then + vim.fn.jobstart({ "open", url }, { detach = true }) + elseif vim.fn.has("unix") == 1 then + vim.fn.jobstart({ "xdg-open", url }, { detach = true }) + else + print("Error: unknown open command on this OS!") + end end return M diff --git a/lua/csgithub/query.lua b/lua/csgithub/query.lua index cc119d4..6f5e356 100644 --- a/lua/csgithub/query.lua +++ b/lua/csgithub/query.lua @@ -1,53 +1,68 @@ local M = {} M.construct_query_path = function(args) - local ext = vim.fn.expand("%:e") + local ext = vim.fn.expand("%:e") - if args.includeExtension and not args.includeFilename then - return "*." .. ext - else - return vim.fn.expand("%:t") - end + 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 = "" - if vim.fn.mode() == "v" then - -- visual mode - text = utils.get_visual_selection() - else - -- normal mode - text = vim.fn.expand("") - end - - return text + local utils = require("csgithub.utils") + local text = "" + if vim.fn.mode() == "v" then + -- visual mode + text = utils.get_visual_selection() + else + -- normal mode + text = vim.fn.expand("") + end + + return text end -- Return search url ---@param args table M.construct_query = function(args) - local query_parts = {} + local query_parts = {} - -- path: - if args.includeFilename or args.includeExtension then - local path = M.construct_query_path(args) - table.insert(query_parts, "path:" .. path) - end + -- path: + 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) + end - -- text - table.insert(query_parts, M.construct_query_text()) + -- text + table.insert(query_parts, M.construct_query_text()) - return table.concat(query_parts, " ") + return table.concat(query_parts, " ") end ---@param query string M.construct_url = function(query) - 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 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 end return M From 47560f5ad22e5fb4f3c103f2eedb1c3d34b19da3 Mon Sep 17 00:00:00 2001 From: nbe Date: Wed, 19 Apr 2023 08:30:24 +0300 Subject: [PATCH 2/3] format --- lua/csgithub/init.lua | 44 +++++++++++------------ lua/csgithub/query.lua | 82 +++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/lua/csgithub/init.lua b/lua/csgithub/init.lua index c1314bb..c33ce3c 100644 --- a/lua/csgithub/init.lua +++ b/lua/csgithub/init.lua @@ -3,35 +3,35 @@ local M = {} -- Return search url ---@param args table|nil M.search = function(args) - local default_args = { - includeFilename = false, - includeExtension = true, - betaSearch = false, - } + local default_args = { + includeFilename = false, + includeExtension = true, + betaSearch = false, + } - local merged_args = vim.tbl_extend("force", default_args, args or {}) + local merged_args = vim.tbl_extend("force", default_args, args or {}) - local query = require("csgithub.query") - local q = query.construct_query(merged_args) - local url = query.construct_url(q) + local query = require("csgithub.query") + local q = query.construct_query(merged_args) + local url = query.construct_url(q) - return url + return url end function M.open(url) - -- return if url is empty - if url == nil or url == "" then - print("Error: url is empty!") - return - end + -- return if url is empty + if url == nil or url == "" then + print("Error: url is empty!") + return + end - if vim.fn.has("mac") == 1 then - vim.fn.jobstart({ "open", url }, { detach = true }) - elseif vim.fn.has("unix") == 1 then - vim.fn.jobstart({ "xdg-open", url }, { detach = true }) - else - print("Error: unknown open command on this OS!") - end + if vim.fn.has("mac") == 1 then + vim.fn.jobstart({ "open", url }, { detach = true }) + elseif vim.fn.has("unix") == 1 then + vim.fn.jobstart({ "xdg-open", url }, { detach = true }) + else + print("Error: unknown open command on this OS!") + end end return M diff --git a/lua/csgithub/query.lua b/lua/csgithub/query.lua index 6f5e356..10fffec 100644 --- a/lua/csgithub/query.lua +++ b/lua/csgithub/query.lua @@ -1,68 +1,68 @@ local M = {} M.construct_query_path = function(args) - local ext = vim.fn.expand("%:e") + local ext = vim.fn.expand("%:e") - local onlyExtension = args.includeExtension and not args.includeFilename + 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 + 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 + 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 = "" - if vim.fn.mode() == "v" then - -- visual mode - text = utils.get_visual_selection() - else - -- normal mode - text = vim.fn.expand("") - end + local utils = require("csgithub.utils") + local text = "" + if vim.fn.mode() == "v" then + -- visual mode + text = utils.get_visual_selection() + else + -- normal mode + text = vim.fn.expand("") + end - return text + return text end -- Return search url ---@param args table M.construct_query = function(args) - local query_parts = {} + local query_parts = {} - -- path: - 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) - end + -- path: + 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) + end - -- text - table.insert(query_parts, M.construct_query_text()) + -- text + table.insert(query_parts, M.construct_query_text()) - return table.concat(query_parts, " ") + return table.concat(query_parts, " ") end ---@param query string M.construct_url = function(query) - 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 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 end return M From 0a28b82795b2f716f8b0a72caca10728da2a1135 Mon Sep 17 00:00:00 2001 From: nbe Date: Wed, 19 Apr 2023 08:49:19 +0300 Subject: [PATCH 3/3] set default betaSearch value to true --- README.md | 2 +- lua/csgithub/init.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 38c3cef..2473173 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ https://user-images.githubusercontent.com/33713262/226383032-113b4db8-27a3-4b8f- local url = csgithub.search({ includeFilename = false, includeExtension = true, - betaSearch = false, -- if you have opted in to the beta search on github, toggle this to true + betaSearch = true, -- set to false if you haven't opted in to GitHub Code Search (beta) }) csgithub.open(url) diff --git a/lua/csgithub/init.lua b/lua/csgithub/init.lua index c33ce3c..11043ce 100644 --- a/lua/csgithub/init.lua +++ b/lua/csgithub/init.lua @@ -6,7 +6,7 @@ M.search = function(args) local default_args = { includeFilename = false, includeExtension = true, - betaSearch = false, + betaSearch = true, } local merged_args = vim.tbl_extend("force", default_args, args or {})