Skip to content

Commit

Permalink
feat(ssr): support visual selection range
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 14, 2024
1 parent 1981968 commit 6f87a5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

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

### Fixed

- LSP: If inlay hints are enabled for a buffer, force Neovim
Expand Down
10 changes: 6 additions & 4 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local M = {}

local rust_lsp_cmd_name = 'RustLsp'

---@type { string: fun(args: string[]) }
---@type { string: fun(args: string[], opts: vim.api.keyset.user_command) }
local command_tbl = {
codeAction = function(_)
require('rustaceanvim.commands.code_action_group')()
Expand Down Expand Up @@ -87,9 +87,11 @@ local command_tbl = {
parentModule = function(_)
require('rustaceanvim.commands.parent_module')()
end,
ssr = function(args)
ssr = function(args, opts)
---@cast opts vim.api.keyset.user_command
local is_range = opts.range and opts.range > 0
local query = args and #args > 0 and table.concat(args, ' ') or nil
require('rustaceanvim.commands.ssr')(query)
require('rustaceanvim.commands.ssr')(query, is_range)
end,
reloadWorkspace = function()
require('rustaceanvim.commands.workspace_refresh')()
Expand Down Expand Up @@ -134,7 +136,7 @@ local function rust_lsp(opts)
vim.notify(rust_lsp_cmd_name .. ': Unknown subcommand: ' .. cmd, vim.log.levels.ERROR)
return
end
command(args)
command(args, opts)
end

---Create the `:RustLsp` command
Expand Down
26 changes: 18 additions & 8 deletions lua/rustaceanvim/commands/ssr.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
local M = {}

local function get_opts(query)
local opts = vim.lsp.util.make_position_params()
opts.query = query
opts.parseOnly = false
opts.selections = { vim.lsp.util.make_range_params().range }
return opts
local ui = require('rustaceanvim.ui')

---@param query string
---@param is_range boolean
local function get_opts(query, is_range)
local params = vim.lsp.util.make_position_params()
params.query = query
params.parseOnly = false
if is_range then
params.selections = { vim.print(ui.get_visual_selected_range()) }
else
params.selections = {}
end
return params
end

local function handler(err, result, ctx)
Expand All @@ -22,15 +30,17 @@ end

local rl = require('rustaceanvim.rust_analyzer')

function M.ssr(query)
---@param query string | nil
---@param is_range boolean
function M.ssr(query, is_range)
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, is_range), handler)
end
end

Expand Down

0 comments on commit 6f87a5a

Please sign in to comment.