Code style guide #6

Open
opened 2023-10-18 20:55:43 -04:00 by enot · 9 comments
Collaborator

Should we have some general style rules? Just looking at the repo and hit save for some reason, then realized my auto-format was not formatting the same way as the bit of code we have so far.

We can do this with rustfmt, which has integrations in most popular editors as well can be run from cmd line.

Note: I have set nothing fancy, so if no one has strong feelings we can just use the default which I think is just the official conventions.

REF Rust Style-Guide

Should we have some general style rules? Just looking at the repo and hit save for some reason, then realized my auto-format was not formatting the same way as the bit of code we have so far. We can do this with rustfmt, which has integrations in most popular editors as well can be run from cmd line. **Note:** I have set nothing fancy, so if no one has strong feelings we can just use the default which I _think_ is just the official conventions. REF [Rust Style-Guide](https://doc.rust-lang.org/nightly/style-guide/)
enot added the
help wanted
question
labels 2023-10-18 20:55:56 -04:00
enot self-assigned this 2023-10-18 20:56:06 -04:00
Owner

👍

Agree that we should just use rustfmt --edition 2021 as our default

👍 Agree that we should just use `rustfmt --edition 2021` as our default
Owner

Would love to have a pre-commit hook or a format on save config for Visual Studio Code that works, if anyone wants to knock that together.

Would love to have a pre-commit hook or a format on save config for Visual Studio Code that works, if anyone wants to knock that together.
Author
Collaborator

I can do both later today, but unfortunately the day job is calling. So if anyone wants to do it before then, please do.

I can do both later today, but unfortunately the day job is calling. So if anyone wants to do it before then, please do.
Collaborator

For Visual Studio Code the rust-analyzer extension has a knob for rustfmt on save and on paste. This conversation lays out some problems with performing formatting in a git hook and some suggestions that might be helpful. I'll poke at it deeper if I get a moment after dinner.

For Visual Studio Code the [rust-analyzer](https://code.visualstudio.com/docs/languages/rust) extension has a knob for `rustfmt` on save and on paste. [This conversation](https://users.rust-lang.org/t/git-hooks-for-rust-cargo/73847) lays out some problems with performing formatting in a git hook and some suggestions that might be helpful. I'll poke at it deeper if I get a moment after dinner.
Author
Collaborator

For Visual Studio Code the rust-analyzer extension has a knob for rustfmt on save and on paste.

Yep, and installing the Rust meta extension includes this. So we just need to make sure we are using the same 'version' of formatting, and then have some instructions for users who aren't familiar with how to set format on save for a language.

On the topic of editors and rust-analyzer, there are also integrations for he following editors and they have nice instructions already we can link to:

  • Emacs
  • Vim/Neo Vim
  • Sublime
  • Eclipse
  • many more

Each editor also has one or more plugins that make this possible so users can use whichever suits them. Documentation

This conversation lays out some problems with performing formatting in a git hook and some suggestions that might be helpful. I'll poke at it deeper if I get a moment after dinner.

Mostly that person is trying to do some magic, if we have a pre commit hook it will check if the code is formatted and then abort the commit when it isn't, telling the user to do so. Answer clarifying

The question I have is where should this information be? Contribution guide? A style-guide file?

> For Visual Studio Code the [rust-analyzer](https://code.visualstudio.com/docs/languages/rust) extension has a knob for `rustfmt` on save and on paste. Yep, and installing the Rust meta extension includes this. So we just need to make sure we are using the same 'version' of formatting, and then have some instructions for users who aren't familiar with how to set format on save for a language. On the topic of editors and rust-analyzer, there are also integrations for he following editors and they have nice instructions already we can link to: * Emacs * Vim/Neo Vim * Sublime * Eclipse * many more Each editor also has one or more plugins that make this possible so users can use whichever suits them. [Documentation](https://rust-analyzer.github.io/manual.html) >[This conversation](https://users.rust-lang.org/t/git-hooks-for-rust-cargo/73847) lays out some problems with performing formatting in a git hook and some suggestions that might be helpful. I'll poke at it deeper if I get a moment after dinner. Mostly that person is trying to do some magic, if we have a pre commit hook it will check if the code is formatted and then abort the commit when it isn't, telling the user to do so. [Answer clarifying](https://users.rust-lang.org/t/git-hooks-for-rust-cargo/73847/5) The question I have is where should this information be? Contribution guide? A style-guide file?
Author
Collaborator

Or maybe not, regarding the commit hook. I will test that out tonight and see what happens.

Or maybe not, regarding the commit hook. I will test that out tonight and see what happens.
Author
Collaborator

So cargo fmt --check works as expected and returns non-zero when code is not formatted correctly. It also work recursively through project directories. cargo fmt is using rustfmt and we can specify the edition in the Cargo.toml config file. If I am reading this issue correctly, then I believe that rust analyzer just runs rustfmt which also picks up the edition from the Cargo.toml. 😬

The last trick is how to share our pre commit hook. I hoped git templates would work but I'm not sure it works like I hoped.

EDIT: Maybe we can just offer the pre commit hook as some documentation, this way people can format with whichever method works for them but the option to catch it pre commit can remain with them. Then once we have CI we can enforce it there as well?

So `cargo fmt --check` works as expected and returns non-zero when code is not formatted correctly. It also work recursively through project directories. `cargo fmt` is using `rustfmt` and we can specify the `edition` in the Cargo.toml config file. If I am reading [this issue](https://github.com/rust-lang/rust-analyzer/issues/11419) correctly, then I _believe_ that rust analyzer just runs rustfmt which also picks up the edition from the Cargo.toml. 😬 The last trick is how to share our pre commit hook. I hoped git templates would work but I'm not sure it works like I hoped. EDIT: Maybe we can just offer the pre commit hook as some documentation, this way people can format with whichever method works for them but the option to catch it pre commit can remain with them. Then once we have CI we can enforce it there as well?
Collaborator

I like the idea of sharing any hooks we recommend for devs to use in an appropriate folder.

I like the idea of sharing any hooks we recommend for devs to use in an appropriate folder.
Author
Collaborator

I like the idea of sharing any hooks we recommend for devs to use in an appropriate folder.

Ya I agree, I was thinking that but wasn't sure the directory structure to go with. So the pull request is is changed to WIP so we can fine tune everything 😎

> I like the idea of sharing any hooks we recommend for devs to use in an appropriate folder. Ya I agree, I was thinking that but wasn't sure the directory structure to go with. So the pull request is is changed to WIP so we can fine tune everything 😎
Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: MountainTownTechnology/aural_isle#6
No description provided.