From db3b91b2f85b48be6df8b89db3348fbdf874c000 Mon Sep 17 00:00:00 2001 From: Benjamin Christie Date: Wed, 19 Apr 2023 14:41:45 -0400 Subject: [PATCH 1/3] added support for searching in visual line mode --- lua/csgithub/query.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lua/csgithub/query.lua b/lua/csgithub/query.lua index 10fffec..762848b 100644 --- a/lua/csgithub/query.lua +++ b/lua/csgithub/query.lua @@ -1,5 +1,12 @@ local M = {} +--- useful for visual line mode when extra whitespace is +--- included in search query +--- @param s string +local function trim_string(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + M.construct_query_path = function(args) local ext = vim.fn.expand("%:e") @@ -27,14 +34,14 @@ 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() + local mode = vim.api.nvim_get_mode().mode + if mode == "v" or mode == "V" then + -- visual mode, visual line mode + text = trim_string(utils.get_visual_selection()) else -- normal mode text = vim.fn.expand("") end - return text end From fe75e14824cb817ce5165e60a65f4d120e727937 Mon Sep 17 00:00:00 2001 From: Benjamin Christie Date: Wed, 19 Apr 2023 15:20:14 -0400 Subject: [PATCH 2/3] no-op when no text selected --- lua/csgithub/init.lua | 3 +++ lua/csgithub/query.lua | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lua/csgithub/init.lua b/lua/csgithub/init.lua index 11043ce..545d36b 100644 --- a/lua/csgithub/init.lua +++ b/lua/csgithub/init.lua @@ -13,6 +13,9 @@ M.search = function(args) local query = require("csgithub.query") local q = query.construct_query(merged_args) + if q == nil then + return nil + end local url = query.construct_url(q) return url diff --git a/lua/csgithub/query.lua b/lua/csgithub/query.lua index 762848b..092b66a 100644 --- a/lua/csgithub/query.lua +++ b/lua/csgithub/query.lua @@ -50,6 +50,10 @@ end M.construct_query = function(args) local query_parts = {} + local query_text = M.construct_query_text() + if query_text == "" then + return nil + end -- path: if args.includeFilename or args.includeExtension then local path = M.construct_query_path(args) @@ -58,7 +62,7 @@ M.construct_query = function(args) end -- text - table.insert(query_parts, M.construct_query_text()) + table.insert(query_parts, query_text) return table.concat(query_parts, " ") end From d9b6e08ed7b85acd2870b3c6910af02f1be70662 Mon Sep 17 00:00:00 2001 From: nbe Date: Thu, 20 Apr 2023 14:08:46 +0300 Subject: [PATCH 3/3] trim whitespace --- lua/csgithub/query.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/csgithub/query.lua b/lua/csgithub/query.lua index 092b66a..cff2d38 100644 --- a/lua/csgithub/query.lua +++ b/lua/csgithub/query.lua @@ -1,10 +1,10 @@ local M = {} ---- useful for visual line mode when extra whitespace is +--- useful for visual line mode when extra whitespace is --- included in search query --- @param s string local function trim_string(s) - return (s:gsub("^%s*(.-)%s*$", "%1")) + return (s:gsub("^%s*(.-)%s*$", "%1")) end M.construct_query_path = function(args)