aboutsummaryrefslogtreecommitdiffstats
path: root/lua/plugins/spectre.lua
blob: 4dcc782636058fd4557f804ae735cae54e3a73fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--- General configuration for spectre base on current git repo
---@param default_opts table | nil
---@return table
function _G.get_spectre_options(default_opts)
  local opts = default_opts or {}

  -- Check if we're in a git repository and get the root
  local git_root = vim.fn.system("git rev-parse --show-toplevel 2>/dev/null")
  if vim.v.shell_error == 0 then
    opts.cwd = vim.trim(git_root)
  end

  return opts
end

return {
  { "MagicDuck/grug-far.nvim", enabled = false },
  {
    -- Search and replace with pattern
    "nvim-pack/nvim-spectre",
    cmd = "Spectre",
    opts = { open_cmd = "noswapfile vnew" },
    keys = {
      {
        "<leader>sr",
        function()
          require("spectre").open()
        end,
        desc = "Replace in files",
      },
      {
        "<leader>sp",
        ":lua require('spectre').open(_G.get_spectre_options())<CR>",
        desc = "Replace in files (Root dir)",
      },
      -- Search current word
      {
        "<leader>sP",
        ":lua require('spectre').open_visual(_G.get_spectre_options({ select_word = true }))<CR>",
        desc = "Replace current word (Root dir)",
      },
      -- Open search with select word in visual mode
      {
        "<leader>sr",
        ":lua require('spectre').open_visual(_G.get_spectre_options())<CR>",
        mode = "v",
        silent = true,
        desc = "Replace current word (Root dir)",
      },
      -- Search on current file
      {
        "<leader>sf",
        ":lua require('spectre').open_file_search(_G.get_spectre_options({ select_word = true }))<CR>",
        desc = "Replace in current file",
      },
    },
  },
  {
    "folke/edgy.nvim",
    optional = true,
    opts = function(_, opts)
      opts.left = opts.left or {}
      table.insert(opts.left, {
        title = "Spectre",
        ft = "spectre_panel",
        size = { width = 0.3 },
      })
    end,
  },
}