Skip to content

Commit 51e5b61

Browse files
authored
Auto-format imports using Module granularity (#702)
Part of #155 This is going to be a fun one 😆 I've pushed two preview commits: the first one imports using the `Crate` granularity, the other one with `Module` granularity. Disclaimer: I'm biased 😃 I've used this style since I started using Rust and it's also being used by vast majority of projects in the ecosystem that I've used or looked at; it's also used by the Rust compiler, Cargo and the tools like Rustfmt or Clippy. Here's a summary of the possible options: rust-lang/rustfmt#3362. Here is an RFC discussion about setting on a variant: rust-lang/style-team#140, I highly recommend reading it. To give a brief summary, it seems that people stand behind two options: - `Module` - good trade-off between readability and conciseness - `Item` - minimizes conflicts when many people edit the same files often To bring back some of the arguments raised in favor of the `Module` and explain some of my reasoning as well: - it naturally settles on the module as the, well, module/boundary from which items can be imported... - thus, helps build the intuition how to use and structure Rust crates - less verbose than Java-style (`Item`) imports, which can often explode and take unreasonable amount of space (and we don't really benefit enough from minimized conflicts as we probably won't be a team of 50 or the like)... - but repeats enough information to quickly trace a module path rather than having to reconstruct the paths from the `crate`-style use import tree... - and already often takes less space in our case, line-wise; - quite good `grep`pability
1 parent 1e3ef7a commit 51e5b61

File tree

180 files changed

+665
-800
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+665
-800
lines changed

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
33

44
group_imports = "StdExternalCrate"
5+
imports_granularity = "Module"

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"rust-analyzer.check.allTargets": true,
1010
"rust-analyzer.check.command": "clippy",
1111
"rust-analyzer.check.features": "all",
12+
"rust-analyzer.imports.granularity.enforce": true,
13+
"rust-analyzer.imports.granularity.group": "module",
14+
"rust-analyzer.imports.prefix": "crate",
1215
"rust-analyzer.rustfmt.extraArgs": [
1316
"+nightly-2023-12-01" // Keep in sync with other "RUST_NIGHTLY_VERSION" references
1417
],

crates/codegen/ebnf/src/precedence_parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use codegen_schema::types::{OperatorModel, PrecedenceParserRef};
22

3-
use crate::{nodes::EbnfNode, EbnfSerializer};
3+
use crate::nodes::EbnfNode;
4+
use crate::EbnfSerializer;
45

56
impl EbnfNode {
67
pub fn from_precedence_parser(

crates/codegen/grammar/src/grammar.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::collections::{BTreeSet, HashMap};
22

33
use semver::Version;
44

5-
use super::{
6-
GrammarVisitor, ParserDefinitionRef, PrecedenceParserDefinitionRef, ScannerDefinitionRef,
7-
TriviaParserDefinitionRef, Visitable,
8-
};
5+
use crate::parser_definition::{ParserDefinitionRef, TriviaParserDefinitionRef};
6+
use crate::visitor::{GrammarVisitor, Visitable};
7+
use crate::{PrecedenceParserDefinitionRef, ScannerDefinitionRef};
98

109
pub struct Grammar {
1110
pub name: String,

crates/codegen/grammar/src/parser_definition.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::fmt::Debug;
22
use std::rc::Rc;
33

4-
use super::{
5-
GrammarVisitor, PrecedenceParserDefinitionRef, ScannerDefinitionRef, VersionQualityRange,
6-
Visitable,
7-
};
4+
use crate::visitor::{GrammarVisitor, Visitable};
5+
use crate::{PrecedenceParserDefinitionRef, ScannerDefinitionRef, VersionQualityRange};
86

97
pub trait ParserDefinition: Debug {
108
fn name(&self) -> &'static str;

crates/codegen/grammar/src/precedence_parser_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22
use std::rc::Rc;
33

4-
use super::{GrammarVisitor, ParserDefinitionNode, Visitable};
4+
use crate::{GrammarVisitor, ParserDefinitionNode, Visitable};
55

66
pub trait PrecedenceParserDefinition: Debug {
77
fn name(&self) -> &'static str;

crates/codegen/grammar/src/scanner_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22
use std::rc::Rc;
33

4-
use super::{GrammarVisitor, VersionQualityRange, Visitable};
4+
use crate::{GrammarVisitor, VersionQualityRange, Visitable};
55

66
pub trait ScannerDefinition: Debug {
77
fn name(&self) -> &'static str;

crates/codegen/grammar/src/visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{
1+
use crate::{
22
Grammar, ParserDefinitionNode, ParserDefinitionRef, PrecedenceParserDefinitionNode,
33
PrecedenceParserDefinitionRef, ScannerDefinitionNode, ScannerDefinitionRef,
44
TriviaParserDefinitionRef,

crates/codegen/language/definition/src/compiler/analysis/definitions.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use std::collections::HashSet;
22

3-
use crate::{
4-
compiler::{
5-
analysis::{Analysis, ItemMetadata},
6-
version_set::VersionSet,
7-
},
8-
internals::Spanned,
9-
model::{Identifier, SpannedItem, SpannedVersionSpecifier},
10-
};
3+
use crate::compiler::analysis::{Analysis, ItemMetadata};
4+
use crate::compiler::version_set::VersionSet;
5+
use crate::internals::Spanned;
6+
use crate::model::{Identifier, SpannedItem, SpannedVersionSpecifier};
117

128
pub(crate) fn analyze_definitions(analysis: &mut Analysis) {
139
collect_top_level_items(analysis);

crates/codegen/language/definition/src/compiler/analysis/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ use std::rc::Rc;
88
use indexmap::IndexMap;
99
use proc_macro2::Span;
1010

11-
use crate::{
12-
compiler::analysis::{
13-
definitions::analyze_definitions, reachability::analyze_reachability,
14-
references::analyze_references,
15-
},
16-
compiler::version_set::VersionSet,
17-
internals::{ErrorsCollection, ParseOutput, Spanned},
18-
model::{Identifier, SpannedItem, SpannedLanguage},
19-
};
11+
use crate::compiler::analysis::definitions::analyze_definitions;
12+
use crate::compiler::analysis::reachability::analyze_reachability;
13+
use crate::compiler::analysis::references::analyze_references;
14+
use crate::compiler::version_set::VersionSet;
15+
use crate::internals::{ErrorsCollection, ParseOutput, Spanned};
16+
use crate::model::{Identifier, SpannedItem, SpannedLanguage};
2017

2118
pub(crate) struct Analysis {
2219
pub errors: ErrorsCollection,

0 commit comments

Comments
 (0)