Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: viewHir command #107

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [[#14](https://github.com/mrcjkb/rustaceanvim/issues/14)].

## [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