From 81de981f263ec32ac977ecfd7519c4a6e39b4935 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Fri, 22 Dec 2023 17:34:28 +0100 Subject: [PATCH] feat: `viewHir` command Closes #15. --- CHANGELOG.md | 6 ++++ README.md | 17 ++++++++++++ lua/rustaceanvim/commands/hir.lua | 44 ++++++++++++++++++++++++++++++ lua/rustaceanvim/commands/init.lua | 3 ++ 4 files changed, 70 insertions(+) create mode 100644 lua/rustaceanvim/commands/hir.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index e5c5b4a6..aa239e7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ 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). +## [Unreleased] + +### Added + +- LSP: `viewHir` command [[#14](https://github.com/mrcjkb/rustaceanvim/issues/14)]. + ## [3.10.5] - 2023-12-22 ### Fixed diff --git a/README.md b/README.md index c0e80693..7031047f 100644 --- a/README.md +++ b/README.md @@ -386,6 +386,23 @@ for more configuration options. +
+ + View Hir + + + Opens a buffer with a textual representation of the HIR + of the function containing the cursor. + Useful for debugging or when working on rust-analyzer itself. + + ```vimscript + :RustLsp viewHir + ``` + ```lua + vim.cmd.RustLsp('viewHir') + ``` +
+ ## Advanced configuration diff --git a/lua/rustaceanvim/commands/hir.lua b/lua/rustaceanvim/commands/hir.lua new file mode 100644 index 00000000..674e24a2 --- /dev/null +++ b/lua/rustaceanvim/commands/hir.lua @@ -0,0 +1,44 @@ +local M = {} + +local rl = require('rustaceanvim.rust_analyzer') +local ui = require('rustaceanvim.ui') + +---@type integer | nil +local latest_buf_id = nil + +local function handler(err, result) + if err then + vim.notify('viewHir failed' .. result and ': ' .. result or '', vim.log.levels.ERROR) + return + end + if result and result:match('Not inside a function body') then + vim.notify('viewHir failed: Not inside a function body', vim.log.levels.ERROR) + return + elseif type(result) ~= 'string' then + vim.notify('viewHir failed: ' .. vim.inspect(result), vim.log.levels.ERROR) + end + + -- check if a buffer with the latest id is already open, if it is then + -- delete it and continue + ui.delete_buf(latest_buf_id) + + -- create a new buffer + latest_buf_id = vim.api.nvim_create_buf(false, true) -- not listed and scratch + + -- split the window to create a new buffer and set it to our window + ui.split(true, latest_buf_id) + + local lines = vim.split(result, '\n') + + -- set filetype to rust for syntax highlighting + vim.bo[latest_buf_id].filetype = 'rust' + -- write the expansion content to the buffer + vim.api.nvim_buf_set_lines(latest_buf_id, 0, 0, false, lines) +end + +function M.hir() + local position_params = vim.lsp.util.make_position_params(0, nil) + rl.buf_request(0, 'rust-analyzer/viewHir', position_params, handler) +end + +return M.hir diff --git a/lua/rustaceanvim/commands/init.lua b/lua/rustaceanvim/commands/init.lua index fb5a7db9..675ffa95 100644 --- a/lua/rustaceanvim/commands/init.lua +++ b/lua/rustaceanvim/commands/init.lua @@ -102,6 +102,9 @@ local command_tbl = { local cmd = args[1] or 'run' require('rustaceanvim.commands.fly_check')(cmd) end, + viewHir = function() + require('rustaceanvim.commands.hir')() + end, logFile = function() vim.cmd.e(config.server.logfile) end,