rust-lang / rust-analyzer

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

Treat doctests as rust code #4170

Open ordian opened 4 years ago

ordian commented 4 years ago

With syntax highlighting, completion, etc. See https://github.com/intellij-rust/intellij-rust/pull/3144.

matklad commented 4 years ago

The relevant bit of code to look at is https://github.com/rust-analyzer/rust-analyzer/blob/7a9ba1657daa9fd90c639dcd937da11b4f526675/crates/ra_ide/src/syntax_highlighting.rs#L473-L510

flodiebold commented 4 years ago

Well, that's the relevant bit of code just for syntax highlighting, right? For proper analysis, completion etc. this gets even more interesting/fun :smile:

(Conversely, completion in ra_fixture parameters would be cool, if not that useful... And a problem if the fixture is a test for a crash...)

matklad commented 4 years ago

That's another use case for hypothetical, -1 durability things in salsa btw.

flodiebold commented 4 years ago

Hmm doctests don't need to be hypothetical though, right? They could just be normal HIR files, or am I missing something? (Kind of similar to macros)

matklad commented 4 years ago

Hm, I guess we could make them real files, but I'd rather avoid that. Like, we don't want to bust caches when we add an impl to a doc test. More generally, they feel like they should be something which is processed only after everything else is fully analyzed.

flodiebold commented 4 years ago

Well, I guess this is rather hypothetical anyway currently :thinking:

petr-tik commented 4 years ago

looks like this can be closed?

ordian commented 4 years ago

Was completion, type resolution, etc implemented? As far as I see, only syntax highlighting was (which is great btw, thank you!).

frantisekhanzlikbl commented 3 years ago

was #4683 also supposed to work with inner doctests? e.g.: is missing syntax highlighting here intended? image

Veykril commented 3 years ago

That should ideally work but doesn't due to the way its currently implemented, see https://github.com/rust-analyzer/rust-analyzer/issues/6873.

frantisekhanzlikbl commented 3 years ago

I am not sure if I missed something, but to me, it seems like #6873 only discusses block comments, but line comments don't work either for me.

Veykril commented 3 years ago

Oh sorry, I misunderstood that. I'm a bit confused though because inner line doc comment highlighting works for me 🤔

image Do you have Semantic Highlighting enabled? Without that Doccomment highlighting injection won't work, that is your theme has to support semantic highlighting.

frantisekhanzlikbl commented 3 years ago

Oh, that's my fault - I didn't realise that whether it is enabled or not is by default dependant on the selected theme. After setting "editor.semanticHighlighting.enabled": true everything works as expected.

Wardenfar commented 2 years ago

Hello, Someone knows the steps (or where to look) for implementing completion in doctests ? Maybe just some hints. Thanks

Veykril commented 2 years ago

I don't think we have a clear view on how we want to achieve this yet

mohe2015 commented 1 year ago

I think a good workaround is implementing a proc macro that generates the doctests comment from source code. I did this in https://gist.github.com/mohe2015/84725cc7b60040afe2d44fe03a9d5eef. The advantage also is that you don't need to handle the weird /// at the start of every line and in my opinion it just looks nicer. Also line number information seems to be more correct in normal tests than in doctests.

Edit: I hope I have now improved it so it plays nicely with rust-analyzer.

IceTDrinker commented 11 months ago

I'm interested in this if it is planned to include refactoring, i.e. if I'm renaming a function/struct in my code base that the change also gets propagated to docstrings

Maybe by pretending a doctest is a file somehow ? I'm really not familiar with rust analyzer internals, would be curious to know more about this topic

bionicles commented 11 months ago

Would it be possible to implement this the same way normal code is treated? Rust-analyzer is so useful, it's hugely noticeable to not have on doctests (which are simultaneously awesome usage examples, unit tests, and enablers of compile_fail) ... i'd love to see rust-analyzer's feedback on doctest code, doctests are great, more doctest driven development could help the community a lot