Skip to content

Commit bfce42e

Browse files
RoyLinRoyLin
authored andcommitted
chore: fix cargo fmt formatting issues
1 parent 02a4ab5 commit bfce42e

5 files changed

Lines changed: 110 additions & 49 deletions

File tree

core/src/agent_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ pub struct SessionOptions {
185185
///
186186
/// When set, enables custom document format support (PDF, Excel, Word, etc.)
187187
/// for the agentic_search tool. If not set, only plain text files are searched.
188-
pub document_parser_registry: Option<Arc<crate::tools::document_parser::DocumentParserRegistry>>,
188+
pub document_parser_registry:
189+
Option<Arc<crate::tools::document_parser::DocumentParserRegistry>>,
189190
}
190191

191192
impl std::fmt::Debug for SessionOptions {

core/src/context/ripgrep_provider.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ impl RipgrepContextProvider {
131131
}
132132

133133
/// Search files for pattern matches
134-
async fn search_files(&self, query: &str, max_results: usize) -> anyhow::Result<Vec<FileMatch>> {
134+
async fn search_files(
135+
&self,
136+
query: &str,
137+
max_results: usize,
138+
) -> anyhow::Result<Vec<FileMatch>> {
135139
let root = self.config.root_path.clone();
136140
let max_file_size = self.config.max_file_size;
137141
let include = self.config.include_patterns.clone();
@@ -202,10 +206,7 @@ impl RipgrepContextProvider {
202206
.map(|s| s.to_string())
203207
.collect()
204208
} else {
205-
lines[0..line_idx]
206-
.iter()
207-
.map(|s| s.to_string())
208-
.collect()
209+
lines[0..line_idx].iter().map(|s| s.to_string()).collect()
209210
};
210211

211212
let context_after = if line_idx + context_lines < lines.len() {
@@ -242,7 +243,11 @@ impl RipgrepContextProvider {
242243
}
243244

244245
// Sort by relevance (descending)
245-
file_matches.sort_by(|a, b| b.relevance.partial_cmp(&a.relevance).unwrap_or(std::cmp::Ordering::Equal));
246+
file_matches.sort_by(|a, b| {
247+
b.relevance
248+
.partial_cmp(&a.relevance)
249+
.unwrap_or(std::cmp::Ordering::Equal)
250+
});
246251
file_matches.truncate(max_results);
247252

248253
Ok::<_, anyhow::Error>(file_matches)
@@ -259,7 +264,11 @@ impl RipgrepContextProvider {
259264
match depth {
260265
crate::context::ContextDepth::Abstract => {
261266
// Just show file path and match count
262-
output.push_str(&format!("{}: {} matches\n", path_str, file_match.matches.len()));
267+
output.push_str(&format!(
268+
"{}: {} matches\n",
269+
path_str,
270+
file_match.matches.len()
271+
));
263272
}
264273
crate::context::ContextDepth::Overview => {
265274
// Show first few matches with limited context
@@ -272,7 +281,10 @@ impl RipgrepContextProvider {
272281
output.push_str(&format!(" {}\n", m.line_content));
273282
}
274283
if file_match.matches.len() > 3 {
275-
output.push_str(&format!(" ... and {} more matches\n", file_match.matches.len() - 3));
284+
output.push_str(&format!(
285+
" ... and {} more matches\n",
286+
file_match.matches.len() - 3
287+
));
276288
}
277289
}
278290
crate::context::ContextDepth::Full => {
@@ -443,7 +455,10 @@ mod tests {
443455
assert_eq!(result.provider, "ripgrep");
444456
assert!(!result.items.is_empty());
445457
// Should find "Rust" in README.md
446-
assert!(result.items.iter().any(|item| item.content.contains("Rust")));
458+
assert!(result
459+
.items
460+
.iter()
461+
.any(|item| item.content.contains("Rust")));
447462
}
448463

449464
#[tokio::test]
@@ -492,6 +507,10 @@ mod tests {
492507
fn test_matches_patterns_include() {
493508
let patterns = vec!["**/*.rs".to_string()];
494509
assert!(matches_patterns(Path::new("src/main.rs"), &patterns, false));
495-
assert!(!matches_patterns(Path::new("src/main.py"), &patterns, false));
510+
assert!(!matches_patterns(
511+
Path::new("src/main.py"),
512+
&patterns,
513+
false
514+
));
496515
}
497516
}

core/src/tools/builtin/agentic_search.rs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ impl FileType {
6767
fn from_path(path: &std::path::Path) -> Self {
6868
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
6969
match ext {
70-
"rs" | "py" | "ts" | "tsx" | "js" | "jsx" | "go" | "java" | "c" | "cpp" | "h" | "hpp" => Self::Code,
70+
"rs" | "py" | "ts" | "tsx" | "js" | "jsx" | "go" | "java" | "c" | "cpp" | "h"
71+
| "hpp" => Self::Code,
7172
"toml" | "yaml" | "yml" | "json" | "ini" | "conf" => Self::Config,
7273
"md" | "txt" | "rst" | "adoc" => Self::Documentation,
7374
_ => Self::Other,
@@ -216,7 +217,14 @@ impl AgenticSearchTool {
216217

217218
// Phase 1+2: Parallel search across all keywords
218219
let matches = tokio::task::spawn_blocking(move || {
219-
search_workspace(&workspace, &keywords, max_results, include.as_deref(), context_lines, registry.as_deref())
220+
search_workspace(
221+
&workspace,
222+
&keywords,
223+
max_results,
224+
include.as_deref(),
225+
context_lines,
226+
registry.as_deref(),
227+
)
220228
})
221229
.await
222230
.map_err(|e| anyhow::anyhow!("Search task failed: {}", e))??;
@@ -257,7 +265,14 @@ impl AgenticSearchTool {
257265

258266
// Phase 1: Initial broad search (2x max_results for sampling pool)
259267
let initial_matches = tokio::task::spawn_blocking(move || {
260-
search_workspace(&workspace, &keywords, max_results * 2, include.as_deref(), context_lines, registry.as_deref())
268+
search_workspace(
269+
&workspace,
270+
&keywords,
271+
max_results * 2,
272+
include.as_deref(),
273+
context_lines,
274+
registry.as_deref(),
275+
)
261276
})
262277
.await
263278
.map_err(|e| anyhow::anyhow!("Search task failed: {}", e))??;
@@ -331,14 +346,13 @@ impl AgenticSearchTool {
331346
/// - Individual words (for broad match)
332347
fn extract_keywords(query: &str) -> Vec<String> {
333348
const STOP_WORDS: &[&str] = &[
334-
"a", "an", "the", "is", "are", "was", "were", "be", "been", "being",
335-
"have", "has", "had", "do", "does", "did", "will", "would", "could",
336-
"should", "may", "might", "shall", "can", "need", "dare", "ought",
337-
"how", "what", "where", "when", "why", "who", "which", "that", "this",
338-
"these", "those", "it", "its", "in", "on", "at", "to", "for", "of",
339-
"with", "by", "from", "up", "about", "into", "through", "during",
340-
"and", "or", "but", "not", "no", "nor", "so", "yet", "both", "either",
341-
"work", "works", "working", "find", "get", "use", "make", "look",
349+
"a", "an", "the", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had",
350+
"do", "does", "did", "will", "would", "could", "should", "may", "might", "shall", "can",
351+
"need", "dare", "ought", "how", "what", "where", "when", "why", "who", "which", "that",
352+
"this", "these", "those", "it", "its", "in", "on", "at", "to", "for", "of", "with", "by",
353+
"from", "up", "about", "into", "through", "during", "and", "or", "but", "not", "no", "nor",
354+
"so", "yet", "both", "either", "work", "works", "working", "find", "get", "use", "make",
355+
"look",
342356
];
343357

344358
let mut keywords: Vec<String> = Vec::new();
@@ -397,9 +411,7 @@ fn search_workspace(
397411
// Build regex patterns for each keyword
398412
let patterns: Vec<Regex> = keywords
399413
.iter()
400-
.filter_map(|kw| {
401-
Regex::new(&format!("(?i){}", regex::escape(kw))).ok()
402-
})
414+
.filter_map(|kw| Regex::new(&format!("(?i){}", regex::escape(kw))).ok())
403415
.collect();
404416

405417
if patterns.is_empty() {
@@ -581,11 +593,7 @@ fn find_matching_files(
581593
// ============================================================================
582594

583595
fn format_results(matches: &[FileMatch], query: &str) -> String {
584-
let mut out = format!(
585-
"Found {} file(s) matching \"{}\"\n\n",
586-
matches.len(),
587-
query
588-
);
596+
let mut out = format!("Found {} file(s) matching \"{}\"\n\n", matches.len(), query);
589597

590598
for (i, fm) in matches.iter().enumerate() {
591599
let rel_path = fm.path.display();
@@ -598,9 +606,7 @@ fn format_results(matches: &[FileMatch], query: &str) -> String {
598606

599607
out.push_str(&format!(
600608
"─── {} ({}, relevance: {:.2}) ───\n",
601-
rel_path,
602-
type_label,
603-
fm.relevance
609+
rel_path, type_label, fm.relevance
604610
));
605611

606612
for m in fm.matches.iter().take(5) {
@@ -825,7 +831,11 @@ mod tests {
825831
writeln!(f, "pub struct Session {{\n pub user_id: String,\n pub token: String,\n}}\n\nimpl Session {{\n pub fn new(user_id: &str, token: &str) -> Self {{\n Self {{ user_id: user_id.to_string(), token: token.to_string() }}\n }}\n}}").unwrap();
826832

827833
let mut f = File::create(root.join("README.md")).unwrap();
828-
writeln!(f, "# Auth Service\n\nHandles JWT authentication and session management.").unwrap();
834+
writeln!(
835+
f,
836+
"# Auth Service\n\nHandles JWT authentication and session management."
837+
)
838+
.unwrap();
829839

830840
dir
831841
}
@@ -855,9 +865,18 @@ mod tests {
855865

856866
#[test]
857867
fn test_file_type_detection() {
858-
assert_eq!(FileType::from_path(std::path::Path::new("main.rs")), FileType::Code);
859-
assert_eq!(FileType::from_path(std::path::Path::new("config.toml")), FileType::Config);
860-
assert_eq!(FileType::from_path(std::path::Path::new("README.md")), FileType::Documentation);
868+
assert_eq!(
869+
FileType::from_path(std::path::Path::new("main.rs")),
870+
FileType::Code
871+
);
872+
assert_eq!(
873+
FileType::from_path(std::path::Path::new("config.toml")),
874+
FileType::Config
875+
);
876+
assert_eq!(
877+
FileType::from_path(std::path::Path::new("README.md")),
878+
FileType::Documentation
879+
);
861880
}
862881

863882
#[tokio::test]
@@ -916,7 +935,10 @@ mod tests {
916935

917936
assert!(output.success);
918937
if let Some(meta) = &output.metadata {
919-
let count = meta.get("result_count").and_then(|v| v.as_u64()).unwrap_or(0);
938+
let count = meta
939+
.get("result_count")
940+
.and_then(|v| v.as_u64())
941+
.unwrap_or(0);
920942
assert!(count <= 1);
921943
}
922944
}

core/src/tools/document_parser.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,17 @@ impl DocumentParser for PlainTextParser {
9393
fn supported_extensions(&self) -> &[&str] {
9494
&[
9595
// Code
96-
"rs", "py", "ts", "tsx", "js", "jsx", "go", "java", "c", "cpp", "h", "hpp",
97-
"cs", "rb", "php", "swift", "kt", "scala", "sh", "bash", "zsh",
98-
// Config
99-
"toml", "yaml", "yml", "json", "ini", "conf", "cfg", "env",
100-
// Documentation
101-
"md", "txt", "rst", "adoc", "org",
102-
// Web
103-
"html", "htm", "xml", "css", "scss", "sass", "less",
104-
// Data
96+
"rs", "py", "ts", "tsx", "js", "jsx", "go", "java", "c", "cpp", "h", "hpp", "cs", "rb",
97+
"php", "swift", "kt", "scala", "sh", "bash", "zsh", // Config
98+
"toml", "yaml", "yml", "json", "ini", "conf", "cfg", "env", // Documentation
99+
"md", "txt", "rst", "adoc", "org", // Web
100+
"html", "htm", "xml", "css", "scss", "sass", "less", // Data
105101
"csv", "tsv", "log",
106102
]
107103
}
108104

109105
fn parse(&self, path: &Path) -> Result<String> {
110-
std::fs::read_to_string(path)
111-
.map_err(|e| anyhow::anyhow!("Failed to read file: {}", e))
106+
std::fs::read_to_string(path).map_err(|e| anyhow::anyhow!("Failed to read file: {}", e))
112107
}
113108

114109
fn max_file_size(&self) -> u64 {

scripts/pre-commit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
# Pre-commit hook for a3s-code
3+
# Ensures cargo fmt passes before allowing commit
4+
#
5+
# Installation:
6+
# cp scripts/pre-commit .git/hooks/pre-commit
7+
# chmod +x .git/hooks/pre-commit
8+
9+
set -e
10+
11+
echo "🔍 Running cargo fmt check..."
12+
13+
# Run cargo fmt check
14+
if ! cargo fmt --all -- --check; then
15+
echo ""
16+
echo "❌ Formatting check failed!"
17+
echo ""
18+
echo "Please run 'cargo fmt --all' to format your code before committing."
19+
echo ""
20+
exit 1
21+
fi
22+
23+
echo "✅ Formatting check passed!"
24+
exit 0

0 commit comments

Comments
 (0)