nvim: lsp: Add range formatting with motion

This commit is contained in:
Sanchayan Maity 2021-09-25 15:24:53 +05:30
parent eebffce98e
commit 8801b26fa9

View file

@ -6,6 +6,19 @@ local null_ls = require("null-ls")
local util = require("lspconfig/util") local util = require("lspconfig/util")
-- https://github.com/neovim/nvim-lspconfig/wiki/User-contributed-tips -- https://github.com/neovim/nvim-lspconfig/wiki/User-contributed-tips
function format_range_operator()
local old_func = vim.go.operatorfunc
_G.op_func_formatting = function()
local start = vim.api.nvim_buf_get_mark(0, '[')
local finish = vim.api.nvim_buf_get_mark(0, ']')
vim.lsp.buf.range_formatting({}, start, finish)
vim.go.operatorfunc = old_func
_G.op_func_formatting = nil
end
vim.go.operatorfunc = 'v:lua.op_func_formatting'
vim.api.nvim_feedkeys('g@', 'n', false)
end
local function preview_location_callback(_, result) local function preview_location_callback(_, result)
if result == nil or vim.tbl_isempty(result) then if result == nil or vim.tbl_isempty(result) then
return nil return nil
@ -58,7 +71,7 @@ local on_attach = function(client, bufnr)
if client.resolved_capabilities.document_formatting then if client.resolved_capabilities.document_formatting then
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
elseif client.resolved_capabilities.document_range_formatting then elseif client.resolved_capabilities.document_range_formatting then
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts) vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gq', '<cmd>lua format_range_operator()<CR>', opts)
end end
if client.resolved_capabilities.code_lens then if client.resolved_capabilities.code_lens then