Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssr): support visual selection range #160

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.21.0] - 2024-04-01

### Added

- LSP: Support structural search and replace (SSR)
just for the selected range.

## [4.20.0] - 2024-04-01

### Added
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ by setting the rust-analyzer
<summary>
<b>Structural search replace</b>
</summary>

- Searches the entire buffer in normal mode.
- Searches the selection in visual mode.

```vim
:RustLsp ssr {query}
Expand Down
2 changes: 2 additions & 0 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ It accepts the following subcommands:
supports filtering (with a # suffix to the query) or searching dependencies.
'joinLines' - Join adjacent lines.
'ssr {query}' - Structural search and replace.
Searches the entire buffer in normal mode.
Searches the selected region in visual mode.
'crateGraph {backend}' - Create and view a crate graph with graphviz.
'syntaxTree' - View the syntax tree.
'view {mir|hir}' - View MIR or HIR.
Expand Down
9 changes: 6 additions & 3 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ local rustlsp_command_tbl = {
},
joinLines = {
impl = function(_, opts)
local visual_mode = opts.range ~= 0
---@cast opts vim.api.keyset.user_command
local visual_mode = opts.range and opts.range ~= 0 or false
require('rustaceanvim.commands.join_lines')(visual_mode)
end,
},
Expand Down Expand Up @@ -174,9 +175,11 @@ local rustlsp_command_tbl = {
end,
},
ssr = {
impl = function(args)
impl = function(args, opts)
---@cast opts vim.api.keyset.user_command
local visual_mode = opts.range and opts.range > 0 or false
local query = args and #args > 0 and table.concat(args, ' ') or nil
require('rustaceanvim.commands.ssr')(query)
require('rustaceanvim.commands.ssr')(query, visual_mode)
end,
},
reloadWorkspace = {
Expand Down
14 changes: 9 additions & 5 deletions lua/rustaceanvim/commands/ssr.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
local M = {}

local function get_opts(query)
---@param query string
---@param visual_mode boolean
local function get_opts(query, visual_mode)
local opts = vim.lsp.util.make_position_params()
local range = (visual_mode and vim.lsp.util.make_given_range_params() or vim.lsp.util.make_range_params()).range
opts.query = query
opts.parseOnly = false
opts.selections = { vim.lsp.util.make_range_params().range }
opts.selections = { range }
return opts
end

Expand All @@ -22,15 +25,16 @@ end

local rl = require('rustaceanvim.rust_analyzer')

function M.ssr(query)
---@param query string | nil
---@param visual_mode boolean
function M.ssr(query, visual_mode)
if not query then
vim.ui.input({ prompt = 'Enter query: ' }, function(input)
query = input
end)
end

if query then
rl.buf_request(0, 'experimental/ssr', get_opts(query), handler)
rl.buf_request(0, 'experimental/ssr', get_opts(query, visual_mode), handler)
end
end

Expand Down
2 changes: 2 additions & 0 deletions lua/rustaceanvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
--- supports filtering (with a # suffix to the query) or searching dependencies.
--- 'joinLines' - Join adjacent lines.
--- 'ssr {query}' - Structural search and replace.
--- Searches the entire buffer in normal mode.
--- Searches the selected region in visual mode.
--- 'crateGraph {backend}' - Create and view a crate graph with graphviz.
--- 'syntaxTree' - View the syntax tree.
--- 'view {mir|hir}' - View MIR or HIR.
Expand Down
Loading