From d068c782329b54195a5e3f9b77bba0d322079110 Mon Sep 17 00:00:00 2001 From: Ahmed Kamal Date: Fri, 28 Jun 2024 22:18:43 +1000 Subject: [PATCH] feat(extra): add elixirls code actions --- lua/lazyvim/plugins/extras/lang/elixir.lua | 68 +++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/lua/lazyvim/plugins/extras/lang/elixir.lua b/lua/lazyvim/plugins/extras/lang/elixir.lua index d82e988e..a2c490c0 100644 --- a/lua/lazyvim/plugins/extras/lang/elixir.lua +++ b/lua/lazyvim/plugins/extras/lang/elixir.lua @@ -9,7 +9,73 @@ return { "neovim/nvim-lspconfig", opts = { servers = { - elixirls = {}, + elixirls = { + keys = { + { "cp", ":ElixirToPipe", desc = "To Pipe" }, + { "cP", ":ElixirFromPipe", desc = "From Pipe" }, + { "ce", mode = { "v" }, ":ElixirExpandMacro", desc = "Expand Macro" }, + }, + on_attach = function(client, bufnr) + local manipulate_pipes = function(direction) + local get_cursor_position = function() + local rowcol = vim.api.nvim_win_get_cursor(0) + local row = rowcol[1] - 1 + local col = rowcol[2] + return row, col + end + + local row, col = get_cursor_position() + client.request_sync("workspace/executeCommand", { + command = "manipulatePipes:serverid", + arguments = { direction, "file://" .. vim.api.nvim_buf_get_name(0), row, col }, + }, nil, 0) + end + + vim.api.nvim_buf_create_user_command(bufnr, "ElixirFromPipe", function() + manipulate_pipes("fromPipe") + end, {}) + vim.api.nvim_buf_create_user_command(bufnr, "ElixirToPipe", function() + manipulate_pipes("toPipe") + end, {}) + + vim.api.nvim_buf_create_user_command(bufnr, "ElixirExpandMacro", function() + local params = vim.lsp.util.make_given_range_params() + + local text = vim.api.nvim_buf_get_text( + 0, + params.range.start.line, + params.range.start.character, + params.range["end"].line, + params.range["end"].character, + {} + ) + + local resp = client.request_sync("workspace/executeCommand", { + command = "expandMacro:serverid", + arguments = { params.textDocument.uri, vim.fn.join(text, "\n"), params.range.start.line }, + }, nil, 0) + + local content = {} + if resp["result"] then + for k, v in pairs(resp.result) do + vim.list_extend(content, { "# " .. k, "" }) + vim.list_extend(content, vim.split(v, "\n")) + end + else + table.insert(content, "Error") + end + + vim.schedule(function() + local bufnr_floating, _ = vim.lsp.util.open_floating_preview( + vim.lsp.util.trim_empty_lines(content), + "elixir", + { border = "single" } + ) + vim.api.nvim_buf_set_option(bufnr_floating, "filetype", "elixir") + end) + end, { range = true }) + end, + }, }, }, },