Skip to content

Commit

Permalink
feat(lsp): join multiple visually selected lines (RustLsp joinLines) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Apr 1, 2024
1 parent d107d75 commit e9db3d5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- LSP: Join multiple visually selected lines with `:RustLsp joinLines`.

### Fixed

- Escape character inserted before `}` when applying code action
- LSP: Escape character inserted before `}` when applying code action
with `SnippetTextEdit` [[#303](https://github.com/mrcjkb/rustaceanvim/issues/303)].

## [4.18.2] - 2024-03-28
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,8 @@ by setting the rust-analyzer
<b>Join lines</b>
</summary>

Join selected lines into one,
smartly fixing up whitespace,
trailing commas, and braces.
Join selected lines into one, smartly fixing up whitespace, trailing commas, and braces.
Works with individual lines in normal mode and multiple lines in visual mode.

```vimscript
:RustLsp joinLines
Expand Down
5 changes: 3 additions & 2 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ local rustlsp_command_tbl = {
bang = true,
},
joinLines = {
impl = function(_)
require('rustaceanvim.commands.join_lines')()
impl = function(_, opts)
local visual_mode = opts.range ~= 0
require('rustaceanvim.commands.join_lines')(visual_mode)
end,
},
moveItem = {
Expand Down
10 changes: 6 additions & 4 deletions lua/rustaceanvim/commands/join_lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ local M = {}

---@alias lsp_join_lines_params { textDocument: lsp_text_document, ranges: lsp_range[] }

---@param visual_mode boolean
---@return lsp_join_lines_params
local function get_params()
local params = vim.lsp.util.make_range_params()
local function get_params(visual_mode)
local params = visual_mode and vim.lsp.util.make_given_range_params() or vim.lsp.util.make_range_params()
local range = params.range

params.range = nil
Expand All @@ -21,8 +22,9 @@ local rl = require('rustaceanvim.rust_analyzer')

--- Sends the request to rust-analyzer to get the TextEdits to join the lines
--- under the cursor and applies them
function M.join_lines()
rl.buf_request(0, 'experimental/joinLines', get_params(), handler)
---@param visual_mode boolean
function M.join_lines(visual_mode)
rl.buf_request(0, 'experimental/joinLines', get_params(visual_mode), handler)
end

return M.join_lines

0 comments on commit e9db3d5

Please sign in to comment.