Skip to content

Commit

Permalink
feat: viewHir command
Browse files Browse the repository at this point in the history
Closes #15.
  • Loading branch information
mrcjkb committed Dec 22, 2023
1 parent 259f881 commit 40f0afe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [[#15](https://github.com/mrcjkb/rustaceanvim/issues/15)].

## [3.10.5] - 2023-12-22

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ for more configuration options.
</details>

<details>
<summary>
<b>View Hir</b>
</summary>

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')
```
</details>

<!-- markdownlint-restore -->

## Advanced configuration
Expand Down
44 changes: 44 additions & 0 deletions lua/rustaceanvim/commands/hir.lua
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 40f0afe

Please sign in to comment.