From e87c917cb68ddace71629a62450449e29888adfb Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Wed, 14 May 2025 00:46:55 -0500 Subject: [PATCH] use nightly rustfmt and sort imports (#145) --- .github/workflows/lint.yml | 6 ++++++ .pre-commit-config.yaml | 2 +- .rustfmt.toml | 5 +++++ crates/djls-conf/src/lib.rs | 13 ++++++++++--- crates/djls-project/src/lib.rs | 16 ++++++++++------ crates/djls-project/src/python.rs | 21 +++++++++++++++------ crates/djls-project/src/system.rs | 13 ++++++++++--- crates/djls-project/src/templatetags.rs | 6 ++++-- crates/djls-server/src/documents.rs | 6 ++++-- crates/djls-server/src/queue.rs | 16 +++++++++++----- crates/djls-server/src/server.rs | 9 ++++++--- crates/djls-server/src/session.rs | 3 ++- crates/djls-server/src/workspace.rs | 6 ++++-- crates/djls-templates/src/ast.rs | 5 ++++- crates/djls-templates/src/error.rs | 8 +++++--- crates/djls-templates/src/lexer.rs | 5 ++++- crates/djls-templates/src/lib.rs | 8 +++++--- crates/djls-templates/src/parser.rs | 12 +++++++++--- crates/djls-templates/src/tagspecs.rs | 9 ++++++--- crates/djls-templates/src/tokens.rs | 4 +++- crates/djls/src/cli.rs | 8 +++++--- crates/djls/src/commands.rs | 5 +++-- crates/djls/src/commands/serve.rs | 11 +++++++---- crates/djls/src/exit.rs | 3 ++- crates/djls/src/lib.rs | 3 ++- 25 files changed, 143 insertions(+), 60 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 15b1e1b..5623667 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -26,6 +26,12 @@ jobs: with: persist-credentials: false + - name: Install nightly toolchain for rustfmt + uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b + with: + toolchain: nightly + components: rustfmt + - name: Install uv uses: astral-sh/setup-uv@6b9c6063abd6010835644d4c2e1bef4cf5cd0fca # v6.0.1 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 369a4b9..ad071da 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,7 +29,7 @@ repos: types: - rust language: rust - entry: cargo fmt + entry: cargo +nightly fmt args: - -- - id: check diff --git a/.rustfmt.toml b/.rustfmt.toml index e69de29..4d84987 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -0,0 +1,5 @@ +group_imports = "StdExternalCrate" +imports_granularity = "Item" +imports_layout = "Vertical" +reorder_imports = true +unstable_features = true diff --git a/crates/djls-conf/src/lib.rs b/crates/djls-conf/src/lib.rs index 4e4157b..909da89 100644 --- a/crates/djls-conf/src/lib.rs +++ b/crates/djls-conf/src/lib.rs @@ -1,7 +1,12 @@ -use config::{Config, ConfigError as ExternalConfigError, File, FileFormat}; +use std::fs; +use std::path::Path; + +use config::Config; +use config::ConfigError as ExternalConfigError; +use config::File; +use config::FileFormat; use directories::ProjectDirs; use serde::Deserialize; -use std::{fs, path::Path}; use thiserror::Error; #[derive(Error, Debug)] @@ -85,10 +90,12 @@ impl Settings { #[cfg(test)] mod tests { - use super::*; use std::fs; + use tempfile::tempdir; + use super::*; + mod defaults { use super::*; diff --git a/crates/djls-project/src/lib.rs b/crates/djls-project/src/lib.rs index 18e2ef7..362ea0e 100644 --- a/crates/djls-project/src/lib.rs +++ b/crates/djls-project/src/lib.rs @@ -4,14 +4,16 @@ mod python; mod system; mod templatetags; +use std::fmt; +use std::path::Path; +use std::path::PathBuf; + use db::ProjectDatabase; use meta::ProjectMetadata; -use python::{find_python_environment, PythonEnvironment}; -pub use templatetags::TemplateTags; - use pyo3::prelude::*; -use std::fmt; -use std::path::{Path, PathBuf}; +use python::find_python_environment; +use python::PythonEnvironment; +pub use templatetags::TemplateTags; #[derive(Debug)] pub struct DjangoProject { @@ -90,10 +92,12 @@ impl fmt::Display for DjangoProject { #[cfg(test)] mod tests { - use super::*; use std::fs; + use tempfile::tempdir; + use super::*; + fn create_mock_django_project(dir: &Path) -> PathBuf { let project_path = dir.to_path_buf(); fs::create_dir_all(&project_path).unwrap(); diff --git a/crates/djls-project/src/python.rs b/crates/djls-project/src/python.rs index 83e7d37..4659296 100644 --- a/crates/djls-project/src/python.rs +++ b/crates/djls-project/src/python.rs @@ -1,8 +1,11 @@ +use std::fmt; +use std::path::Path; +use std::path::PathBuf; + +use pyo3::prelude::*; + use crate::db::Db; use crate::system; -use pyo3::prelude::*; -use std::fmt; -use std::path::{Path, PathBuf}; #[salsa::tracked] pub fn find_python_environment(db: &dyn Db) -> Option { @@ -152,12 +155,14 @@ impl fmt::Display for PythonEnvironment { #[cfg(test)] mod tests { - use super::*; use std::fs; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; + use tempfile::tempdir; + use super::*; + fn create_mock_venv(dir: &Path, version: Option<&str>) -> PathBuf { let prefix = dir.to_path_buf(); @@ -200,10 +205,14 @@ mod tests { } mod env_discovery { - use super::*; - use crate::system::mock::{self as sys_mock, MockGuard}; use which::Error as WhichError; + use super::*; + use crate::system::mock::MockGuard; + use crate::system::mock::{ + self as sys_mock, + }; + #[test] fn test_explicit_venv_path_found() { let project_dir = tempdir().unwrap(); diff --git a/crates/djls-project/src/system.rs b/crates/djls-project/src/system.rs index 3c94f59..167e51c 100644 --- a/crates/djls-project/src/system.rs +++ b/crates/djls-project/src/system.rs @@ -1,5 +1,6 @@ use std::env::VarError; use std::path::PathBuf; + use which::Error as WhichError; pub fn find_executable(name: &str) -> Result { @@ -26,11 +27,12 @@ pub fn env_var(key: &str) -> Result { #[cfg(test)] pub mod mock { - use super::*; use std::cell::RefCell; use std::collections::HashMap; use std::thread_local; + use super::*; + thread_local! { static MOCK_EXEC_RESULTS: RefCell>> = RefCell::new(HashMap::new()); static MOCK_ENV_RESULTS: RefCell>> = RefCell::new(HashMap::new()); @@ -95,12 +97,17 @@ pub mod mock { #[cfg(test)] mod tests { - use super::mock::{self as sys_mock, MockGuard}; - use super::*; use std::env::VarError; use std::path::PathBuf; + use which::Error as WhichError; + use super::mock::MockGuard; + use super::mock::{ + self as sys_mock, + }; + use super::*; + #[test] fn test_exec_mock_path_retrieval() { let _guard = MockGuard; diff --git a/crates/djls-project/src/templatetags.rs b/crates/djls-project/src/templatetags.rs index 6fe0e1e..12cdd81 100644 --- a/crates/djls-project/src/templatetags.rs +++ b/crates/djls-project/src/templatetags.rs @@ -1,7 +1,9 @@ -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyList}; use std::ops::Deref; +use pyo3::prelude::*; +use pyo3::types::PyDict; +use pyo3::types::PyList; + #[derive(Debug, Default, Clone)] pub struct TemplateTags(Vec); diff --git a/crates/djls-server/src/documents.rs b/crates/djls-server/src/documents.rs index 2408720..97a2439 100644 --- a/crates/djls-server/src/documents.rs +++ b/crates/djls-server/src/documents.rs @@ -1,6 +1,8 @@ -use anyhow::{anyhow, Result}; -use djls_project::TemplateTags; use std::collections::HashMap; + +use anyhow::anyhow; +use anyhow::Result; +use djls_project::TemplateTags; use tower_lsp_server::lsp_types::*; #[derive(Debug, Default)] diff --git a/crates/djls-server/src/queue.rs b/crates/djls-server/src/queue.rs index 7149a99..4c5338b 100644 --- a/crates/djls-server/src/queue.rs +++ b/crates/djls-server/src/queue.rs @@ -1,8 +1,11 @@ -use anyhow::{anyhow, Result}; use std::future::Future; use std::pin::Pin; use std::sync::Arc; -use tokio::sync::{mpsc, oneshot}; + +use anyhow::anyhow; +use anyhow::Result; +use tokio::sync::mpsc; +use tokio::sync::oneshot; /// Type alias for a type-erased, pinned, heap-allocated, Send-able future /// that resolves to `Result<()>`. @@ -186,12 +189,15 @@ impl Drop for QueueInner { #[cfg(test)] mod tests { - use super::*; - use anyhow::anyhow; - use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering; use std::time::Duration; + + use anyhow::anyhow; use tokio::time::sleep; + use super::*; + #[tokio::test] async fn test_submit_and_process() { let queue = Queue::new(); diff --git a/crates/djls-server/src/server.rs b/crates/djls-server/src/server.rs index 1924331..98bf8a0 100644 --- a/crates/djls-server/src/server.rs +++ b/crates/djls-server/src/server.rs @@ -1,10 +1,13 @@ -use crate::queue::Queue; -use crate::session::Session; use std::sync::Arc; + use tokio::sync::RwLock; use tower_lsp_server::jsonrpc::Result as LspResult; use tower_lsp_server::lsp_types::*; -use tower_lsp_server::{Client, LanguageServer}; +use tower_lsp_server::Client; +use tower_lsp_server::LanguageServer; + +use crate::queue::Queue; +use crate::session::Session; const SERVER_NAME: &str = "Django Language Server"; const SERVER_VERSION: &str = "0.1.0"; diff --git a/crates/djls-server/src/session.rs b/crates/djls-server/src/session.rs index 3e86974..4d3e33b 100644 --- a/crates/djls-server/src/session.rs +++ b/crates/djls-server/src/session.rs @@ -1,8 +1,9 @@ -use crate::documents::Store; use djls_conf::Settings; use djls_project::DjangoProject; use tower_lsp_server::lsp_types::ClientCapabilities; +use crate::documents::Store; + #[derive(Debug, Default)] pub struct Session { client_capabilities: Option, diff --git a/crates/djls-server/src/workspace.rs b/crates/djls-server/src/workspace.rs index 4b46590..08a40ba 100644 --- a/crates/djls-server/src/workspace.rs +++ b/crates/djls-server/src/workspace.rs @@ -1,6 +1,8 @@ -use percent_encoding::percent_decode_str; use std::path::PathBuf; -use tower_lsp_server::lsp_types::{InitializeParams, Uri}; + +use percent_encoding::percent_decode_str; +use tower_lsp_server::lsp_types::InitializeParams; +use tower_lsp_server::lsp_types::Uri; /// Determines the project root path from initialization parameters. /// diff --git a/crates/djls-templates/src/ast.rs b/crates/djls-templates/src/ast.rs index e520162..95aa40c 100644 --- a/crates/djls-templates/src/ast.rs +++ b/crates/djls-templates/src/ast.rs @@ -1,7 +1,10 @@ -use crate::tokens::{Token, TokenStream, TokenType}; use serde::Serialize; use thiserror::Error; +use crate::tokens::Token; +use crate::tokens::TokenStream; +use crate::tokens::TokenType; + #[derive(Clone, Debug, Default, Serialize)] pub struct Ast { nodelist: Vec, diff --git a/crates/djls-templates/src/error.rs b/crates/djls-templates/src/error.rs index dee782e..3e85477 100644 --- a/crates/djls-templates/src/error.rs +++ b/crates/djls-templates/src/error.rs @@ -1,10 +1,12 @@ -use crate::ast::{AstError, Span}; -use crate::lexer::LexerError; -use crate::parser::ParserError; use serde::Serialize; use thiserror::Error; use tower_lsp_server::lsp_types; +use crate::ast::AstError; +use crate::ast::Span; +use crate::lexer::LexerError; +use crate::parser::ParserError; + #[derive(Debug, Error, Serialize)] pub enum TemplateError { #[error("Lexer error: {0}")] diff --git a/crates/djls-templates/src/lexer.rs b/crates/djls-templates/src/lexer.rs index d24b0cd..f2a9107 100644 --- a/crates/djls-templates/src/lexer.rs +++ b/crates/djls-templates/src/lexer.rs @@ -1,6 +1,9 @@ -use crate::tokens::{Token, TokenStream, TokenType}; use thiserror::Error; +use crate::tokens::Token; +use crate::tokens::TokenStream; +use crate::tokens::TokenType; + pub struct Lexer { source: String, chars: Vec, diff --git a/crates/djls-templates/src/lib.rs b/crates/djls-templates/src/lib.rs index d26ab8d..9651a0f 100644 --- a/crates/djls-templates/src/lib.rs +++ b/crates/djls-templates/src/lib.rs @@ -6,10 +6,12 @@ mod tagspecs; mod tokens; use ast::Ast; -pub use error::{to_lsp_diagnostic, QuickFix, TemplateError}; - +pub use error::to_lsp_diagnostic; +pub use error::QuickFix; +pub use error::TemplateError; use lexer::Lexer; -pub use parser::{Parser, ParserError}; +pub use parser::Parser; +pub use parser::ParserError; /// Parses a Django template and returns the AST and any parsing errors. /// diff --git a/crates/djls-templates/src/parser.rs b/crates/djls-templates/src/parser.rs index 4133f24..2e26ced 100644 --- a/crates/djls-templates/src/parser.rs +++ b/crates/djls-templates/src/parser.rs @@ -1,8 +1,14 @@ -use crate::ast::{Ast, AstError, Node, Span}; -use crate::lexer::LexerError; -use crate::tokens::{Token, TokenStream, TokenType}; use thiserror::Error; +use crate::ast::Ast; +use crate::ast::AstError; +use crate::ast::Node; +use crate::ast::Span; +use crate::lexer::LexerError; +use crate::tokens::Token; +use crate::tokens::TokenStream; +use crate::tokens::TokenType; + pub struct Parser { tokens: TokenStream, current: usize, diff --git a/crates/djls-templates/src/tagspecs.rs b/crates/djls-templates/src/tagspecs.rs index 4c6ea58..2e64867 100644 --- a/crates/djls-templates/src/tagspecs.rs +++ b/crates/djls-templates/src/tagspecs.rs @@ -1,8 +1,10 @@ -use anyhow::Result; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs; use std::path::Path; + +use anyhow::Result; +use serde::Deserialize; +use serde::Serialize; use thiserror::Error; use toml::Value; @@ -184,9 +186,10 @@ pub struct EndTag { #[cfg(test)] mod tests { - use super::*; use std::fs; + use super::*; + #[test] fn test_can_load_builtins() -> Result<(), anyhow::Error> { let specs = TagSpecs::load_builtin_specs()?; diff --git a/crates/djls-templates/src/tokens.rs b/crates/djls-templates/src/tokens.rs index 88f8b3f..aacffb2 100644 --- a/crates/djls-templates/src/tokens.rs +++ b/crates/djls-templates/src/tokens.rs @@ -1,5 +1,7 @@ +use std::ops::Deref; +use std::ops::DerefMut; + use serde::Serialize; -use std::ops::{Deref, DerefMut}; #[derive(Clone, Debug, Serialize, PartialEq)] pub enum TokenType { diff --git a/crates/djls/src/cli.rs b/crates/djls/src/cli.rs index b35fe4c..4dc20a3 100644 --- a/crates/djls/src/cli.rs +++ b/crates/djls/src/cli.rs @@ -1,9 +1,11 @@ -use crate::args::Args; -use crate::commands::{Command, DjlsCommand}; -use crate::exit::Exit; use anyhow::Result; use clap::Parser; +use crate::args::Args; +use crate::commands::Command; +use crate::commands::DjlsCommand; +use crate::exit::Exit; + /// Main CLI structure that defines the command-line interface #[derive(Parser)] #[command(name = "djls")] diff --git a/crates/djls/src/commands.rs b/crates/djls/src/commands.rs index b30c7ea..9adef3b 100644 --- a/crates/djls/src/commands.rs +++ b/crates/djls/src/commands.rs @@ -1,9 +1,10 @@ mod serve; +use anyhow::Result; +use clap::Subcommand; + use crate::args::Args; use crate::exit::Exit; -use anyhow::Result; -use clap::Subcommand; pub trait Command { fn execute(&self, args: &Args) -> Result; diff --git a/crates/djls/src/commands/serve.rs b/crates/djls/src/commands/serve.rs index b4f5435..ca88ed0 100644 --- a/crates/djls/src/commands/serve.rs +++ b/crates/djls/src/commands/serve.rs @@ -1,10 +1,13 @@ +use anyhow::Result; +use clap::Parser; +use clap::ValueEnum; +use djls_server::DjangoLanguageServer; +use tower_lsp_server::LspService; +use tower_lsp_server::Server; + use crate::args::Args; use crate::commands::Command; use crate::exit::Exit; -use anyhow::Result; -use clap::{Parser, ValueEnum}; -use djls_server::DjangoLanguageServer; -use tower_lsp_server::{LspService, Server}; #[derive(Debug, Parser)] pub struct Serve { diff --git a/crates/djls/src/exit.rs b/crates/djls/src/exit.rs index bd91b32..6ff80b2 100644 --- a/crates/djls/src/exit.rs +++ b/crates/djls/src/exit.rs @@ -1,7 +1,8 @@ -use anyhow::Result; use std::error::Error; use std::fmt; +use anyhow::Result; + type ExitMessage = Option; #[derive(Debug)] diff --git a/crates/djls/src/lib.rs b/crates/djls/src/lib.rs index 79c7505..e144324 100644 --- a/crates/djls/src/lib.rs +++ b/crates/djls/src/lib.rs @@ -8,9 +8,10 @@ mod cli; mod commands; mod exit; -use pyo3::prelude::*; use std::env; +use pyo3::prelude::*; + #[pyfunction] /// Entry point called by Python when the CLI is invoked. /// This function handles argument parsing from Python and routes to the Rust CLI logic.