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(rustc_unpretty): use rustc inspect mir #192

Merged
merged 6 commits into from
Jan 29, 2024

Conversation

saying121
Copy link
Contributor

Use rustc -Z unpretty=mir to inspect mir and other things,
and achieve an experience similar to Rust Playground.
The rust-analyzer view does not support viewing MIR for async blocks,
but rustc can do it.

Copy link
Contributor

Review Checklist

Does this PR follow the Contribution Guidelines? Following is a partial checklist:

Proper conventional commit scoping:

  • For example, fix(lsp): some lsp-related bugfix

  • Pull request title has the appropriate conventional commit prefix.

If applicable:

  • Tested
    • Tests have been added.
    • Tested manually (Steps to reproduce in PR description).
  • Updated documentation.
  • Updated CHANGELOG.md

@saying121 saying121 changed the title Rustc unpretty feat(rustc_unpretty): use rustc inspect mir Jan 28, 2024
@mrcjkb
Copy link
Owner

mrcjkb commented Jan 28, 2024

Hey 👋

This looks great! Thanks for the PR! 😄

I'll have a closer look at it later today.

Copy link
Owner

@mrcjkb mrcjkb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for this awesome PR! ❤️

I have left some thoughts and suggestions.
Let me know if you'd like to implement them. If not, I'm happy to merge this into a feature branch and implement them myself 😄

@@ -97,6 +97,27 @@ local command_tbl = {
local cmd = args[1] or 'run'
require('rustaceanvim.commands.fly_check')(cmd)
end,
rustcUnpretty = function(args)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: rust-analyzer isn't required for this command. Perhaps it would be better to create a separate Rustc command?

issue: The -Z option requires a nightly compiler.
So we should document that in the readme and lua docs (which I use to generate vimdoc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the code structure, so I'll pass on this task.

Comment on lines 34 to 38
-- Get a compatible vim range (1 index based) from a TS node range.
--
-- TS nodes start with 0 and the end col is ending exclusive.
-- They also treat a EOF/EOL char as a char ending in the first
-- col of the next row.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quibble: Doc comments should have a --- commentstring.
(just relevant for lua-language-server hovers here 😄)

Suggested change
-- Get a compatible vim range (1 index based) from a TS node range.
--
-- TS nodes start with 0 and the end col is ending exclusive.
-- They also treat a EOF/EOL char as a char ending in the first
-- col of the next row.
--- Get a compatible vim range (1 index based) from a TS node range.
---
--- TS nodes start with 0 and the end col is ending exclusive.
--- They also treat a EOF/EOL char as a char ending in the first
--- col of the next row.

pos = { temp[1] - 1, temp[2] }
end

local node = ts.get_node { pos = pos }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I tested this (on a line that doesn't contain fn), and got:

Error executing Lua callback: ...rapped-98a4ed0/share/nvim/runtime/lua/vim/treesitter.lua:392: Invalid position: row and col must be non-negative

with a stack trace that leads to this function call.

local node = ts.get_node { pos = pos }

if node == nil or node:type() ~= 'function_item' then
vim.notify('not found function or function is uncomplete', vim.log.levels.ERROR)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quibble

Suggested change
vim.notify('not found function or function is uncomplete', vim.log.levels.ERROR)
vim.notify('no function found or function is incomplete', vim.log.levels.ERROR)

---@param level rustcir_level
function M.rustc_unpretty(level)
if #api.nvim_get_runtime_file('parser/rust.so', true) == 0 then
vim.notify('please install rust parser in nvim-treesitter', vim.log.levels.ERROR)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Technically, nvim-treesitter is not required.

Suggested change
vim.notify('please install rust parser in nvim-treesitter', vim.log.levels.ERROR)
vim.notify('a treesitter parser for Rust is required for 'rustc unpretty'', vim.log.levels.ERROR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to obtain the function?
I'm not sure what to do.

Comment on lines +187 to +192
---settings for rustc -Z unpretty
---@type table
rustc_unpretty = {
---@type string
edition = '2021',
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This is the internal config.
There's another 'meta' module, rustaceanvim/config/init.lua that contains luacats documentation, from which I generate vimdoc in CI.

@mrcjkb mrcjkb force-pushed the master branch 2 times, most recently from 1bb620a to d0cec19 Compare January 28, 2024 20:46
@saying121 saying121 closed this Jan 29, 2024
@saying121 saying121 reopened this Jan 29, 2024
@saying121
Copy link
Contributor Author

I think I have done all I can do. 😄
Thanks for your work.

@mrcjkb mrcjkb changed the base branch from master to rustc-unpretty-continued January 29, 2024 11:00
@mrcjkb
Copy link
Owner

mrcjkb commented Jan 29, 2024

I think I have done all I can do. 😄 Thanks for your work.

Thanks 🙏. I'll merge it into the rustc-unpretty-continued branch and continue working on it from there.

@mrcjkb mrcjkb merged commit 07f5c2d into mrcjkb:rustc-unpretty-continued Jan 29, 2024
10 checks passed
@mrcjkb
Copy link
Owner

mrcjkb commented Jan 29, 2024

@saying121 I've just finished the touchup and pushed a 4.1.0 release. The command is now :Rustc unpretty, and the config field is tools.rustc.

Thanks again for the awesome contribution 🙏 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants