rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.38k stars 1.62k forks source link

Support `#[doc = include_str!(...)]` in hover documentation #11137

Open MaulingMonkey opened 2 years ago

MaulingMonkey commented 2 years ago

It'd be nice if external documentation could show up in hover tooltips

#[doc = include_str!("MyThing.md")] // doesn't work
#[doc = "this works fine already" ]
struct MyThing;

#[doc = include_str!(...)] attributes are currently thrown away here: https://github.com/rust-analyzer/rust-analyzer/blob/355a4bdb883ee9edc1c32b553fcf91c302b3df19/crates/hir_def/src/attr.rs#L696-L707

Hacking together a new branch is easy enough:

        } else if let Some(ast::Expr::MacroCall(call)) = ast.expr() {
            if call.path()?.segment()?.name_ref()?.text() == "include_str" {
                Some(Interned::new(AttrInput::Literal(SmolStr::new(format!("include_str!(...): {:?}", call)))))
            } else {
                None
            }

But I'm having difficulty figuring out how to plumb my way into hir_expand::builtin_fn_macro::include_str_expand, or to figure out the path of the current AST, or figuring out if this is even the right place to be handling this.

bjorn3 commented 2 years ago

Just checking the name is not enough. A different macro called include_str!() can shadow it I think. You can also re-export it with a different name. There probably is some logic for other builtin macros you can use.

Veykril commented 2 years ago

cc https://github.com/rust-analyzer/rust-analyzer/issues/8092

lnicola commented 2 years ago

We'll also have trouble finding the file, right? CC #10178

ModProg commented 2 years ago

Another addition here, I'm generating some documentation using concat! in a macro. It is probably thrown away for the same reason.

MasterTemple commented 3 weeks ago

I have created the lsp_doc crate that provides a workaround for the LSP hover/preview, and it is compatible with the cargo doc output.

Installation

cargo add lsp_doc

Usage

/// Here is a table:
#[lsp_doc("table.md")]
/// Here is some long piece of text:
#[lsp_doc("lorem.md")]
fn anything() {}

Neovim

VSCode

Hope that helps in the mean time!