From 9c3e4bea8d36cb23082999494e3aea9f547c3c2a Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Sat, 12 Aug 2023 14:16:32 +0800 Subject: [PATCH] feat: neovim - find and search globally, refactoring --- .../desktop/neovim/astronvim_user/init.lua | 6 ++--- .../neovim/astronvim_user/mappings.lua | 27 +++++++++++++++++++ home/base/desktop/neovim/default.nix | 10 +++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/home/base/desktop/neovim/astronvim_user/init.lua b/home/base/desktop/neovim/astronvim_user/init.lua index c2506259..54f21b85 100644 --- a/home/base/desktop/neovim/astronvim_user/init.lua +++ b/home/base/desktop/neovim/astronvim_user/init.lua @@ -99,8 +99,7 @@ return { }, } }, - -- The plugin offers the `:Refactor` command to refactor code. - -- TODO not work yet + -- The plugin offers the alibity to refactor code. { "ThePrimeagen/refactoring.nvim", dependencies = { @@ -108,8 +107,7 @@ return { {"nvim-treesitter/nvim-treesitter"} } }, - -- The plugin offers the `:Spectre` command to search and replace. - -- TODO not work yet + -- The plugin offers the abilibty to search and replace. { "nvim-pack/nvim-spectre", dependencies = { diff --git a/home/base/desktop/neovim/astronvim_user/mappings.lua b/home/base/desktop/neovim/astronvim_user/mappings.lua index 139c1692..1af3ccf9 100644 --- a/home/base/desktop/neovim/astronvim_user/mappings.lua +++ b/home/base/desktop/neovim/astronvim_user/mappings.lua @@ -4,6 +4,7 @@ -- lower level configuration and more robust one. (which-key will -- automatically pick-up stored data by this setting.) local utils = require "astronvim.utils" +require("telescope").load_extension("refactoring") return { -- first key is the mode n = { @@ -13,6 +14,32 @@ return { -- quick save [""] = { ":w!", desc = "Save File" }, -- change description but the same command ["tp"] = { function() utils.toggle_term_cmd("ipython") end, desc = "ToggleTerm python" }, + + -- search and replace globally + ['ss'] = {'lua require("spectre").toggle()', desc = "Toggle Spectre" }, + ['sw'] = {'lua require("spectre").open_visual({select_word=true})', desc = "Search current word" }, + ['sp'] ={'lua require("spectre").open_file_search({select_word=true})', desc = "Search on current file" }, + + -- refactoring + ["ri"] = { function() require('refactoring').refactor('Inline Variable') end, desc = "Refactoring Inline Variable" }, + ["rb"] = { function() require('refactoring').refactor('Extract Block') end, desc = "Extract Block" }, + ["rbf"] = { function() require('refactoring').refactor('Extract Block To File') end, desc = "Extract Block To File" }, + ["rr"] = { function() require('telescope').extensions.refactoring.refactors() end, desc = "Prompt for a refactor to apply" }, + ["rp"] = { function() require('refactoring').debug.printf({below = false}) end, desc = "Print debug info" }, + ["rv"] = { function() require('refactoring').debug.print_var() end, desc = "Print debug var" }, + ["rc"] = { function() require('refactoring').debug.cleanup({}) end, desc = "Cleanup debugging" }, + }, + v = { + -- search and replace globally + ['sw'] = {'lua require("spectre").open_visual()', desc = "Search current word" }, + }, + x = { + -- refactoring + ["ri"] = { function() require('refactoring').refactor('Inline Variable') end, desc = "Refactoring Inline Variable" }, + ["re"] = { function() require('refactoring').refactor('Extract Function') end, desc = "Extract Function" }, + ["rf"] = { function() require('refactoring').refactor('Extract Function To File') end, desc = "Extract Function To File" }, + ["rv"] = { function() require('refactoring').refactor('Extract Variable') end, desc = "Extract Variable" }, + ["rr"] = { function() require('telescope').extensions.refactoring.refactors() end, desc = "Prompt for a refactor to apply" }, }, t = { -- setting a mapping to false will disable it diff --git a/home/base/desktop/neovim/default.nix b/home/base/desktop/neovim/default.nix index c44bf2f7..4acdef5d 100644 --- a/home/base/desktop/neovim/default.nix +++ b/home/base/desktop/neovim/default.nix @@ -85,6 +85,10 @@ # 2. `c` means ask for confirmation before replacing. # 3. `i` means ignore case. # +# Seach and replace globally: +# 1. Open Spectre search and replace panel: ` + ss` +# 2. search and replace in command line: `find -name "*.nix" | sad '' '' | delta` +# # Joining a Selection of Lines With Space: `:join` # Joining without spaces: `:join!` # @@ -94,12 +98,6 @@ # # Save the selected text to a file: `:w filename` (will show `:'<,'>w filename`) # -# Search key pattern and Replace in Multiple Files: -# sed -ri "s/pattern_str/replace_str/g" $(grep "key_pattern" 'path_pattern' -rl) -# -# Search file name pattern and Replace in Multiple Files: -# sed -ri "s/pattern_str/replace_str/g" $(find . -name "pattern") -# # ...... # See https://astronvim.com/Basic%20Usage/walkthrough #