Code Monkey home page Code Monkey logo

ssr.nvim's Introduction

ssr.nvim

Structural search and replace for Neovim.

Screen.Recording.2022-11-04.at.2.02.07.PM.mov

Installation

with packer.nvim:

use {
  "cshuaimin/ssr.nvim",
  module = "ssr",
  -- Calling setup is optional.
  config = function()
    require("ssr").setup {
      border = "rounded",
      min_width = 50,
      min_height = 5,
      max_width = 120,
      max_height = 25,
      keymaps = {
        close = "q",
        next_match = "n",
        prev_match = "N",
        replace_confirm = "<cr>",
        replace_all = "<leader><cr>",
      },
    }
  end
}

then add a mapping to open SSR:

vim.keymap.set({ "n", "x" }, "<leader>sr", function() require("ssr").open() end)

Usage

First put your cursor on the structure you want to search and replace (if you are not sure, select a region instead), then open SSR by pressing <leader>sr.

In the SSR float window you can see the placeholder search code, you can replace part of it with wildcards. A wildcard is an identifier starts with $, like $name. A $name wildcard in the search pattern will match any AST node and $name will reference it in the replacement.

Press <leader><cr> to replace all matches in current buffer, or <cr> to choose which match to replace.

The context

When opening SSR, the cursor position is important, you need to put the cursor on the structure you want to search. If the placeholder code is not correct, exit SSR with q and select the region instead.

ssr.nvim parses your search pattern to syntax trees to perform structural searching. However directly parsing code without context is not accurate, for example TypeScript function argument foo: number will be parsed as a label without context, so ssr.nvim parses pattern in it's original context.

Limitations

ssr.nvim performs searching and replacing at AST level and doesn't understand code as much as LSP servers do. Use LSP server's ssr implementation if possible.

ssr.nvim's People

Contributors

a-lamia avatar akinsho avatar cshuaimin avatar rmpr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.