-
Notifications
You must be signed in to change notification settings - Fork 342
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
Enforce formatting of code examples in Bevy book #279
Comments
This can be accomplished with the unstable Perhaps this could be linted in CI, but not required to merge? |
Yeah, I'd be happy to try that out. We'd want to add advice on how to run that command locally too. |
That was the initial solution proposed in #281 but was proven not viable (at least at the time, maybe stuff has changed). It only worked if the code was already inside a Rust file and not brought in via stuff like |
Other potential solutions I've considered, and are still viable in my mind, are:
I think that was all of them, tho there may have been more I've forgotten. Overall has been a little complex issue to design a fix for I've been mulling over for a while. |
This is the sort of direction I'm leaning towards as well. It's not perfect, but it seems a lot less fragile. |
I know |
Hmm, would single example per file, where we explicitly hide lines, be desirable, or would allowing multiple examples per file, and explicitly defining what code is used per an example, be better. I'm leaning more towards the define example over what to hide since it seems less likely to break if we format code. Single Example per File// HIDE
use bevy::prelude::*;
fn main() {
App::new().run();
} And used like: {% example(file="book/app/basic_app.rs") %} Multiple Examples per Fileuse bevy::prelude::*;
// EXAMPLE: basic_app
fn main() {
App::new().run();
}
// END_EXAMPLE And used like: {% example(file="book/app/basic_app.rs", example="basic_app") %} or instead {% example(file="book/app/basic_app.rs:basic_app") %} (This whole format, and the last usage API, are heavily inspired by how Note Working on a PR for this issue. |
Also another design question / consideration; how should "versioned" examples be handled? For example in the ECS section of Quick Start's Getting Started where throughout the chapter it changes a singular function to show progress. Like should we have |
I like the multiple examples per file approach, since it reduces the total amount of files required. (I think it would be nice to also have the ability to hide lines of code, so that we could also run For versioned, you could do: use bevy::prelude::*;
mod v1 {
// EXAMPLE: basic_app_v1
fn main() {
App::new().run();
}
// END_EXAMPLE
}
mod v2 {
// EXAMPLE: basic_app_v2
fn main() -> AppExit {
App::new().run()
}
// END_EXAMPLE
} Or some other form. |
What do you mean? We can still run |
What I meant is that sometimes code examples look like this: commands.spawn(Planet::new()); But that won't compile by itself, so you need to add some extra hidden code: # use bevy::prelude::*;
# use project::Planet;
#
# fn spawn_planet(mut commands: Commands) {
commands.spawn(Planet::new());
# } I want to make sure that you can still have this hidden code so that we can verify it using |
It can be quite challenging to maintain an appropriately consistent style when writing code snippets for examples. Indentation errors, offset parentheses and so on reduce the perception of quality to new readers, and distract from the learning experience.
Fortunately,
cargo fmt
is very good. We should ensure that submitted code passescargo fmt
. This is very closely related to #83, and we should ensure that the solution we select for that can be extended to fit this.Ideally, this process is also easy for authors to use. We probably won't be able to use
cargo fmt
directly, as the Rust files are embedded inside markdown, but a script that applies the formatting to all code blocks and can be run on command would be massively appreciated.The text was updated successfully, but these errors were encountered: