-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Code block continuation in documentation #2894
Conversation
I've agree that setup for doc tests creates friction that reduces doc test quality, but we clearly Ideally one might close the code block, but then reopen it after some regular text.
|
|
In principle you could even |
I don't see the value of having the order or the code not following the reading order. We are talking about a documention, so something that shouldn't have too much complexity to be readable. I can see the value of being to pause, and restart multiple time from the same point (like pausing after an initial set-up), but I think it is easy enough to just
Can be easily re-written as
Even if the number of variable created in the set-up, I don't expect that the majority of them need to be mutated by each example. In the example that I gave with the graph, neither part of the set-up (once finished) had to be mutable. |
Agreed, you can even hide code block symbols to simulate those saved states, which looks more readable to rust devs, even if more verbose:
|
Usually this is done by putting a macro in the docs that extracts out the common bits
|
This approach have several drawback
For example if you want to explain how your state machine works, how to you express this?
|
Are those objections really true? Would this work?
|
You will still write 2 times the content of the macro 2 times. Once in the macro, once in the doc. Witch is as bad as not creating the macro. |
It seems worth taking some inspiration from "literate programming" concepts here, before independently reinventing our own. |
I exposed the internals of the macro in the I suspect the macro solution breaks the playground link though, well maybe only |
I didn't know the name “literate programming”. I think that |
If you're interested in Literate Programming and Rust, you may be interested in the Tango crate: https://github.com/pnkfelix/tango |
@burdges I apologise, I did not understood what you said. So I added /// # Part 1
/// ``` rust
/// # macro_rules! p1 { () => {
/// let x = 1;
/// # } }
/// ```
/// # Part 2
/// ```
/// # p1!();
/// let y = x;
/// ```
So it seems that it suffer exactly the same issue that a simple
Whatever is declared in a given block isn't visible in the next one. So you need to create the macro outside of the documentation block, and thus repeat the content of the macro in the first code-block. If I am wrong, please correct me. |
To sum-up the current discussion:
Do I have everything right? |
If my macros do not work then how would the macros suggested by @KrishnaSannasi work? You put them in |
This is exactly what as said in the next phrase :) !
As far as I know, yes. I didn't tested. |
Hmm, how does the |
The html generation wouldn't change (except ignoring the keywords |
Sorry, what I meant was that I don't know if the markdown parser that we currently use gets confused if the end-ticks for a code block are not exactly three backticks and nothing else on that line. If it got confused by that, that'd pose a problem. As for |
Of course it doesn't work! warning: could not parse code block as Rust code
--> src/foo.rs:100:5
|
100 | /// ``` rust
| _____^
101 | | /// let x = 1;
102 | | /// ``` pause
103 | | ///
104 | | /// ``` resume
105 | | /// let y = 2;
106 | | /// ```
| |_______^
|
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: ` Is this a real issue? I knew I was going to have to modify it to support the
Maybe I'm wrong, but I thought that you can have only one tag ( If my hypothesis is wrong (you can have multiple tags), then they shouldn't automatically propagate (unlike what I said), and the exact interaction need indeed to be examined. I also forget to explicitly say that a This means that you can have |
Yes, although as it has been noted, this may lead to some duplication. |
You can't have spaces between the backticks and the keyword, so that explains why that particular example doesn't work. I think in general in markdown though, the ending code fence isn't expected to have keywords. I don't know how hard it would be to fix that.
You should (in theory) not have to change the markdown parser to add new code fence keywords/tags. It just passes the string it finds after the opening fence to you, and then leaves it up to you to derive semantics from it. Making it look for keywords in closing fences as well might require changing the actual Markdown standard and parser syntax though, which is likely a bigger (though not insurmountable) task.
Nope, you can have multiple separated by comma, such as
|
Interesting. Then I think that I need to take a closer look to the possible interaction between fences. First of all fences shouldn't propagate automatically unlike what I said initially. This also means that if adding fences at the end is complicated, we can add the |
It'd breaks playground links if you did macros in |
Speaking of playground links, is it an option, something currently being implemented, a RFC, a wish? I can't find it anywhere. |
1e3f9dc
to
b8206ca
Compare
- Rename the feature name - Use `merge_next`/`merge_previous` instead of documentation in documentation - Do not propagate tags anymore - Clarify the interaction with the various tags (like `compile_fail`, or `text`) - Add the use of macro as possible alternative
(Sorry for the spam, I forgot that the PR were updated automatically when I pushed a draft to my personal clone). So I updated my proposition to include all the changes that were suggested:
In the end, I think that all commits should be squashed, but for the moment it may be easier to keep them separated for an easier review. |
Yes, the standard doesn't currently allow keywords following the closing fence:
I personally don't see much utility in tagging the earlier block, it seems like a marker at the beginning to merge onto the previous block would be enough. |
If you don't tag the previous block, you can't insert other (unrelated) code block in the middle. |
One block could start with:
...then you could have some other code-blocks, and eventually a block that starts with:
All the blocks with |
We've no need for We've discussed restarting multiple times for identical initialization code, so our blocks expand into a tree. I'm confident developers would exploit trees like this, unlike |
I and @arifd have analyzed the discussion and would like to summarize and provide our input. Here's the current RFC's goal description:
We would phrase it this way, instead: Long code examples often call for explanatory text interspersed among them.
These three means seem like workarounds and lacking in elegance.
Notice that this re-definition is not about reusing code between examples and therefore does not attempt to provide solutions for that purpose. This is mentioned regarding suggestions in that direction, such as #2894 (comment). Two major design ideas have been identified:
Documentation is pages that are consumed usually top to bottom. Adding code blocks that continue one another in some tree structure may seem like a good idea from the perspective of the developer/author but would probably not actually be nice for the reader. The "linear" design seems like the best choice here, because it's simple and provides a feature that is easy to understand and use both for the author and for the reader. Note that we would recommend some additional UI component that indicates which blocks belong to a continuation. Something like "block x of n" at the corner of each such block. Regarding the additional possible "interleaved" feature, we suggest not implementing interleaving of multiple multi-part examples at this stage. However, there seems to be no reason to disallow single-block rust examples between the parts of a multi-part example, even though that should probably be used sparingly and with care. So while we don't see a real use case for interleaved multiple multi-part examples nor for tree structures, we may be wrong about any of those.
We would also suggest the following title for this RFC:
|
Whaoo. Your understanding of my initial idea is much better than mine! I really love how you phrased it. I don’t think I will have time in the short or mid-term future to work on this. @mightyiam if you think you have the time, and want to make this issue move forward, feel free to do so. If it’s easier to open a new PR, do it, ping me, and I will close this one. |
A new PR is in the works. I'd keep this one open for now. |
@robinmoussu and all interested: new PR: #3081 |
Rendered EDIT: update rendered link
This is my first RFC, and I am still extremely new in rust world. I expect to have changes to make, and would welcome suggestion. I would like to implement it, but a bit of help (where to look mostly) would be welcome as well.