Skip to content

Commit

Permalink
Auto-format imports using Module granularity (#702)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Xanewok authored Dec 13, 2023
1 parent 1e3ef7a commit 51e5b61
Show file tree
Hide file tree
Showing 180 changed files with 665 additions and 800 deletions.
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md

group_imports = "StdExternalCrate"
imports_granularity = "Module"
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"rust-analyzer.check.allTargets": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.features": "all",
"rust-analyzer.imports.granularity.enforce": true,
"rust-analyzer.imports.granularity.group": "module",
"rust-analyzer.imports.prefix": "crate",
"rust-analyzer.rustfmt.extraArgs": [
"+nightly-2023-12-01" // Keep in sync with other "RUST_NIGHTLY_VERSION" references
],
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/ebnf/src/precedence_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use codegen_schema::types::{OperatorModel, PrecedenceParserRef};

use crate::{nodes::EbnfNode, EbnfSerializer};
use crate::nodes::EbnfNode;
use crate::EbnfSerializer;

impl EbnfNode {
pub fn from_precedence_parser(
Expand Down
7 changes: 3 additions & 4 deletions crates/codegen/grammar/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::collections::{BTreeSet, HashMap};

use semver::Version;

use super::{
GrammarVisitor, ParserDefinitionRef, PrecedenceParserDefinitionRef, ScannerDefinitionRef,
TriviaParserDefinitionRef, Visitable,
};
use crate::parser_definition::{ParserDefinitionRef, TriviaParserDefinitionRef};
use crate::visitor::{GrammarVisitor, Visitable};
use crate::{PrecedenceParserDefinitionRef, ScannerDefinitionRef};

pub struct Grammar {
pub name: String,
Expand Down
6 changes: 2 additions & 4 deletions crates/codegen/grammar/src/parser_definition.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::fmt::Debug;
use std::rc::Rc;

use super::{
GrammarVisitor, PrecedenceParserDefinitionRef, ScannerDefinitionRef, VersionQualityRange,
Visitable,
};
use crate::visitor::{GrammarVisitor, Visitable};
use crate::{PrecedenceParserDefinitionRef, ScannerDefinitionRef, VersionQualityRange};

pub trait ParserDefinition: Debug {
fn name(&self) -> &'static str;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/grammar/src/precedence_parser_definition.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::rc::Rc;

use super::{GrammarVisitor, ParserDefinitionNode, Visitable};
use crate::{GrammarVisitor, ParserDefinitionNode, Visitable};

pub trait PrecedenceParserDefinition: Debug {
fn name(&self) -> &'static str;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/grammar/src/scanner_definition.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::rc::Rc;

use super::{GrammarVisitor, VersionQualityRange, Visitable};
use crate::{GrammarVisitor, VersionQualityRange, Visitable};

pub trait ScannerDefinition: Debug {
fn name(&self) -> &'static str;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/grammar/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{
use crate::{
Grammar, ParserDefinitionNode, ParserDefinitionRef, PrecedenceParserDefinitionNode,
PrecedenceParserDefinitionRef, ScannerDefinitionNode, ScannerDefinitionRef,
TriviaParserDefinitionRef,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::collections::HashSet;

use crate::{
compiler::{
analysis::{Analysis, ItemMetadata},
version_set::VersionSet,
},
internals::Spanned,
model::{Identifier, SpannedItem, SpannedVersionSpecifier},
};
use crate::compiler::analysis::{Analysis, ItemMetadata};
use crate::compiler::version_set::VersionSet;
use crate::internals::Spanned;
use crate::model::{Identifier, SpannedItem, SpannedVersionSpecifier};

pub(crate) fn analyze_definitions(analysis: &mut Analysis) {
collect_top_level_items(analysis);
Expand Down
15 changes: 6 additions & 9 deletions crates/codegen/language/definition/src/compiler/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ use std::rc::Rc;
use indexmap::IndexMap;
use proc_macro2::Span;

use crate::{
compiler::analysis::{
definitions::analyze_definitions, reachability::analyze_reachability,
references::analyze_references,
},
compiler::version_set::VersionSet,
internals::{ErrorsCollection, ParseOutput, Spanned},
model::{Identifier, SpannedItem, SpannedLanguage},
};
use crate::compiler::analysis::definitions::analyze_definitions;
use crate::compiler::analysis::reachability::analyze_reachability;
use crate::compiler::analysis::references::analyze_references;
use crate::compiler::version_set::VersionSet;
use crate::internals::{ErrorsCollection, ParseOutput, Spanned};
use crate::model::{Identifier, SpannedItem, SpannedLanguage};

pub(crate) struct Analysis {
pub errors: ErrorsCollection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::HashSet;

use crate::{
compiler::{analysis::Analysis, version_set::VersionSet},
model::{Identifier, SpannedTriviaParser},
};
use crate::compiler::analysis::Analysis;
use crate::compiler::version_set::VersionSet;
use crate::model::{Identifier, SpannedTriviaParser};

pub(crate) fn analyze_reachability(analysis: &mut Analysis) {
check_unreachabable_items(analysis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ use std::fmt::Debug;
use indexmap::IndexMap;
use semver::Version;

use crate::{
compiler::{analysis::Analysis, version_set::VersionSet},
internals::Spanned,
model::{
Identifier, SpannedEnumItem, SpannedEnumVariant, SpannedField, SpannedFragmentItem,
SpannedItem,
SpannedItemDiscriminants::{
self, Enum, Fragment, Keyword, Precedence, Repeated, Separated, Struct, Token, Trivia,
},
SpannedKeywordDefinition, SpannedKeywordItem, SpannedPrecedenceExpression,
SpannedPrecedenceItem, SpannedPrecedenceOperator, SpannedPrimaryExpression,
SpannedRepeatedItem, SpannedScanner, SpannedSeparatedItem, SpannedStructItem,
SpannedTokenDefinition, SpannedTokenItem, SpannedTriviaItem, SpannedTriviaParser,
SpannedVersionSpecifier,
},
use crate::compiler::analysis::Analysis;
use crate::compiler::version_set::VersionSet;
use crate::internals::Spanned;
use crate::model::SpannedItemDiscriminants::{
self, Enum, Fragment, Keyword, Precedence, Repeated, Separated, Struct, Token, Trivia,
};
use crate::model::{
Identifier, SpannedEnumItem, SpannedEnumVariant, SpannedField, SpannedFragmentItem,
SpannedItem, SpannedKeywordDefinition, SpannedKeywordItem, SpannedPrecedenceExpression,
SpannedPrecedenceItem, SpannedPrecedenceOperator, SpannedPrimaryExpression,
SpannedRepeatedItem, SpannedScanner, SpannedSeparatedItem, SpannedStructItem,
SpannedTokenDefinition, SpannedTokenItem, SpannedTriviaItem, SpannedTriviaParser,
SpannedVersionSpecifier,
};

pub(crate) fn analyze_references(analysis: &mut Analysis) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
compiler::{analysis::Analysis, version_set::VersionSet},
model::SpannedVersionSpecifier,
};
use crate::compiler::analysis::Analysis;
use crate::compiler::version_set::VersionSet;
use crate::model::SpannedVersionSpecifier;

impl Analysis {
pub fn add_specifier(&self, set: &mut VersionSet, specifier: &SpannedVersionSpecifier) {
Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/language/definition/src/compiler/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::{compiler::analysis::Analysis, internals::WriteOutputTokens};
use crate::compiler::analysis::Analysis;
use crate::internals::WriteOutputTokens;

pub(crate) struct LanguageEmitter;

Expand Down
7 changes: 3 additions & 4 deletions crates/codegen/language/definition/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ mod version_set;

use proc_macro2::TokenStream;

use crate::{
compiler::{analysis::Analysis, emitter::LanguageEmitter},
internals::ParseAdapter,
};
use crate::compiler::analysis::Analysis;
use crate::compiler::emitter::LanguageEmitter;
use crate::internals::ParseAdapter;

pub struct LanguageCompiler;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{fmt::Display, mem::swap, ops::Range};
use std::fmt::Display;
use std::mem::swap;
use std::ops::Range;

use semver::Version;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use proc_macro2::TokenStream;
use syn::parse::{Parse, ParseStream};

use crate::{
internals::{ErrorsCollection, ParseInputTokens, Result},
model::SpannedLanguage,
};
use crate::internals::{ErrorsCollection, ParseInputTokens, Result};
use crate::model::SpannedLanguage;

pub(crate) struct ParseAdapter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{fmt::Debug, rc::Rc};
use std::fmt::Debug;
use std::rc::Rc;

use indexmap::{IndexMap, IndexSet};
use proc_macro2::Ident;
use semver::Version;
use syn::{parse::ParseStream, LitBool, LitChar, LitStr};
use syn::parse::ParseStream;
use syn::{LitBool, LitChar, LitStr};

use crate::internals::{
parse_input_tokens::ParseHelpers, ErrorsCollection, ParseInputTokens, Result, Spanned,
};
use crate::internals::parse_input_tokens::ParseHelpers;
use crate::internals::{ErrorsCollection, ParseInputTokens, Result, Spanned};

impl ParseInputTokens for bool {
fn parse_value(input: ParseStream<'_>, _: &mut ErrorsCollection) -> Result<Self> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt::Debug;

use indexmap::IndexMap;
use proc_macro2::{extra::DelimSpan, Delimiter, Ident, TokenStream};
use syn::{braced, bracketed, parenthesized, parse::ParseStream, Token};
use proc_macro2::extra::DelimSpan;
use proc_macro2::{Delimiter, Ident, TokenStream};
use syn::parse::ParseStream;
use syn::{braced, bracketed, parenthesized, Token};

use crate::internals::{Error, ErrorsCollection, ParseInputTokens, Result, Spanned};

Expand Down
3 changes: 2 additions & 1 deletion crates/codegen/language/definition/src/model/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::BTreeSet, rc::Rc};
use std::collections::BTreeSet;
use std::rc::Rc;

use codegen_language_internal_macros::{derive_spanned_type, ParseInputTokens, WriteOutputTokens};
use indexmap::IndexSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::ops::Deref;
use proc_macro2::{Literal, TokenStream};
use quote::quote;
use serde::{Deserialize, Serialize};
use syn::{parse::ParseStream, Ident};
use syn::parse::ParseStream;
use syn::Ident;

use crate::internals::{
ErrorsCollection, ParseHelpers, ParseInputTokens, Result, WriteOutputTokens,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{fold::Fold, parse_quote, Error, GenericArgument, Type};
use syn::fold::Fold;
use syn::{parse_quote, Error, GenericArgument, Type};

use crate::input_model::{add_spanned_prefix, InputField, InputItem, InputVariant};

Expand Down
6 changes: 2 additions & 4 deletions crates/codegen/language/internal_macros/src/input_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::fmt::Display;

use itertools::Itertools;
use quote::ToTokens;
use syn::{
parse::{Parse, ParseStream},
Data, DeriveInput, Error, Fields, FieldsNamed, Ident, Result, Type, Variant,
};
use syn::parse::{Parse, ParseStream};
use syn::{Data, DeriveInput, Error, Fields, FieldsNamed, Ident, Result, Type, Variant};

pub enum InputItem {
Struct {
Expand Down
20 changes: 9 additions & 11 deletions crates/codegen/parser/generator/src/code_generator.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
use std::{
collections::{BTreeMap, BTreeSet},
mem,
path::Path,
};
use std::collections::{BTreeMap, BTreeSet};
use std::mem;
use std::path::Path;

use anyhow::Result;
use codegen_grammar::{
Grammar, GrammarVisitor, ParserDefinitionNode, ParserDefinitionRef,
PrecedenceParserDefinitionRef, ScannerDefinitionNode, ScannerDefinitionRef,
TriviaParserDefinitionRef,
};
use infra_utils::{cargo::CargoWorkspace, codegen::Codegen};
use infra_utils::cargo::CargoWorkspace;
use infra_utils::codegen::Codegen;
use quote::{format_ident, quote};
use semver::Version;
use serde::Serialize;

use super::{
parser_definition::ParserDefinitionExtensions,
precedence_parser_definition::PrecedenceParserDefinitionExtensions,
scanner_definition::ScannerDefinitionExtensions, trie::Trie,
};
use crate::parser_definition::ParserDefinitionExtensions;
use crate::precedence_parser_definition::PrecedenceParserDefinitionExtensions;
use crate::scanner_definition::ScannerDefinitionExtensions;
use crate::trie::Trie;

#[derive(Default, Serialize)]
pub struct CodeGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use inflector::Inflector;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};

use super::parser_definition::ParserDefinitionNodeExtensions;
use crate::parser_definition::ParserDefinitionNodeExtensions;

pub trait PrecedenceParserDefinitionExtensions {
fn to_parser_code(&self) -> TokenStream;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/parser/generator/src/scanner_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use super::parser_definition::VersionQualityRangeVecExtensions;
use crate::parser_definition::VersionQualityRangeVecExtensions;

pub trait ScannerDefinitionExtensions {
fn to_scanner_code(&self) -> TokenStream;
Expand Down
5 changes: 3 additions & 2 deletions crates/codegen/parser/generator/src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{collections::BTreeMap, fmt::Debug};
use std::collections::BTreeMap;
use std::fmt::Debug;

use codegen_grammar::{ScannerDefinitionNode, ScannerDefinitionRef};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use super::parser_definition::VersionQualityRangeVecExtensions;
use crate::parser_definition::VersionQualityRangeVecExtensions;

#[derive(Clone, Debug, Default)]
pub struct Trie {
Expand Down
8 changes: 3 additions & 5 deletions crates/codegen/parser/runtime/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use std::rc::Rc;

use serde::Serialize;

use super::{
cursor::Cursor,
kinds::{RuleKind, TokenKind},
text_index::TextIndex,
};
use crate::cursor::Cursor;
use crate::kinds::{RuleKind, TokenKind};
use crate::text_index::TextIndex;

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct RuleNode {
Expand Down
8 changes: 3 additions & 5 deletions crates/codegen/parser/runtime/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
use std::rc::Rc;

use super::{
cst::{Node, RuleNode},
kinds::{RuleKind, TokenKind},
text_index::{TextIndex, TextRange},
};
use crate::cst::{Node, RuleNode};
use crate::kinds::{RuleKind, TokenKind};
use crate::text_index::{TextIndex, TextRange};

/// A [`PathNode`] that points to a [`RuleNode`].
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
8 changes: 3 additions & 5 deletions crates/codegen/parser/runtime/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
cst,
kinds::{IsLexicalContext, TokenKind},
support::{ParserContext, ParserResult},
};
use crate::cst;
use crate::kinds::{IsLexicalContext, TokenKind};
use crate::support::{ParserContext, ParserResult};

pub trait Lexer {
// Generated by the templating engine
Expand Down
Loading

0 comments on commit 51e5b61

Please sign in to comment.