Skip to content

Commit 9df3744

Browse files
Support for searching in visual line mode (#2)
* added support for searching in visual line mode * no-op when no text selected * trim whitespace --------- Co-authored-by: nbe <[email protected]>
1 parent 259555d commit 9df3744

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lua/csgithub/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ M.search = function(args)
1313

1414
local query = require("csgithub.query")
1515
local q = query.construct_query(merged_args)
16+
if q == nil then
17+
return nil
18+
end
1619
local url = query.construct_url(q)
1720

1821
return url

lua/csgithub/query.lua

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

3+
--- useful for visual line mode when extra whitespace is
4+
--- included in search query
5+
--- @param s string
6+
local function trim_string(s)
7+
return (s:gsub("^%s*(.-)%s*$", "%1"))
8+
end
9+
310
M.construct_query_path = function(args)
411
local ext = vim.fn.expand("%:e")
512

@@ -27,14 +34,14 @@ end
2734
M.construct_query_text = function()
2835
local utils = require("csgithub.utils")
2936
local text = ""
30-
if vim.fn.mode() == "v" then
31-
-- visual mode
32-
text = utils.get_visual_selection()
37+
local mode = vim.api.nvim_get_mode().mode
38+
if mode == "v" or mode == "V" then
39+
-- visual mode, visual line mode
40+
text = trim_string(utils.get_visual_selection())
3341
else
3442
-- normal mode
3543
text = vim.fn.expand("<cword>")
3644
end
37-
3845
return text
3946
end
4047

@@ -43,6 +50,10 @@ end
4350
M.construct_query = function(args)
4451
local query_parts = {}
4552

53+
local query_text = M.construct_query_text()
54+
if query_text == "" then
55+
return nil
56+
end
4657
-- path:
4758
if args.includeFilename or args.includeExtension then
4859
local path = M.construct_query_path(args)
@@ -51,7 +62,7 @@ M.construct_query = function(args)
5162
end
5263

5364
-- text
54-
table.insert(query_parts, M.construct_query_text())
65+
table.insert(query_parts, query_text)
5566

5667
return table.concat(query_parts, " ")
5768
end

0 commit comments

Comments
 (0)