Skip to content

Commit

Permalink
Merge pull request #194 from defr0std/master
Browse files Browse the repository at this point in the history
Add support for search_paths option
  • Loading branch information
EpsilonKu authored Feb 5, 2024
2 parents 4b09141 + 0d67488 commit 81cb399
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,35 @@ require('spectre.actions').get_state()
-- write your custom open function
require('spectre').open({
is_insert_mode = true,
-- the directory where the search tool will be started in
cwd = "~/.config/nvim",
search_text="test",
replace_text="test",
-- the pattern of files to consider for searching
path="lua/**/*.lua",
-- the directories or files to search in
search_paths = {"lua/", "plugin/"},
is_close = false, -- close an exists instance of spectre and open new
})
-- you can use all variables above on command line
-- for example: Spectre % is_insert_mode=true cwd=~/.config/nvim
-- in this example `%` will expand to current file.

```

### Search paths

By default, searching is performed in the current working directory, which can
also be customized using the `cwd` option in the example above.

The `path` option limits the search only to the files matching the provided
pattern. Note, however, that even if you provide the `path`, all files in the
`cwd` still need to be listed, and this could be quite slow if `cwd` is a large
directory.

To limit the search paths further, you can also provide the `search_paths`
option. This is the list of directories or files to search in, regardless of the
`cwd`.

## Replace Method

There are two replace methods `sed` and `oxi`.
Expand Down
11 changes: 11 additions & 0 deletions lua/spectre/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ local open_file = function(filename, lnum, col, winid)
api.nvim_win_set_cursor(0, { lnum, col })
end

local is_absolute = function(filename)
if vim.loop.os_uname().sysname == "Windows_NT" then
return string.find(filename, "%a:\\") == 1
end
return string.sub(filename, 1, 1) == "/"
end

local get_file_path = function(filename)
-- if the path is absolute, return as is
if is_absolute(filename) then
return filename
end
-- use default current working directory if state.cwd is nil or empty string
--
if state.cwd == nil or state.cwd == "" then
Expand Down
4 changes: 3 additions & 1 deletion lua/spectre/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ M.open = function(opts)


state.cwd = opts.cwd
state.search_paths = opts.search_paths
M.change_view("reset")
ui.render_search_ui()

Expand Down Expand Up @@ -641,7 +642,8 @@ M.search = function(opts)
state.finder_instance:search({
cwd = state.cwd,
search_text = state.query.search_query,
path = state.query.path
path = state.query.path,
search_paths = state.search_paths,
})
M.init_regex()
end
Expand Down
12 changes: 9 additions & 3 deletions lua/spectre/search/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ base.search = function(self, query)
query.cwd = nil
end
table.insert(args, query.search_text)
if query.search_paths then
for _, dir in ipairs(query.search_paths) do
table.insert(args, dir)
end
else
-- https://github.com/nvim-telescope/telescope.nvim/issues/907
-- ripgrep issue
table.insert(args, '.')
end

-- https://github.com/nvim-telescope/telescope.nvim/issues/907
-- ripgrep issue
table.insert(args, '.')

log.debug("search cwd " .. (query.cwd or ''))
log.debug("search: " .. self.state.cmd .. ' ' .. table.concat(args, ' '))
Expand Down

0 comments on commit 81cb399

Please sign in to comment.