Skip to content

Commit f3255f7

Browse files
committed
Move config file resolver to library
When using stylua as a library, the only formatting interface it provides are the `format_code` and `format_ast` functions, which both already expect a config file. To replicate the config file discovery that the stylua CLI implements, one would have to copy this. My motivation: For one of my projects I'm creating bindings for other languages to the stylua_lib. There it would be really useful, if I could use the config file discovery.
1 parent c2f8394 commit f3255f7

File tree

5 files changed

+347
-355
lines changed

5 files changed

+347
-355
lines changed

src/cli/config.rs

Lines changed: 0 additions & 347 deletions
This file was deleted.

src/cli/main.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ use serde_json::json;
77
use std::collections::HashSet;
88
use std::fs;
99
use std::io::{stderr, stdin, stdout, Read, Write};
10-
use std::path::Path;
10+
use std::path::{Path, PathBuf};
1111
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
1212
use std::sync::Arc;
1313
use std::time::Instant;
1414
use thiserror::Error;
1515
use threadpool::ThreadPool;
1616

17-
use stylua_lib::{format_code, Config, OutputVerification, Range};
17+
use stylua_lib::{config_resolver, format_code, Config, OutputVerification, Range};
1818

19-
use crate::config::find_ignore_file_path;
20-
21-
mod config;
2219
mod opt;
2320
mod output_diff;
2421

@@ -204,6 +201,19 @@ fn format_string(
204201
}
205202
}
206203

204+
fn find_ignore_file_path(mut directory: PathBuf, recursive: bool) -> Option<PathBuf> {
205+
debug!("config: looking for ignore file in {}", directory.display());
206+
let file_path = directory.join(".styluaignore");
207+
if file_path.is_file() {
208+
debug!("config: resolved ignore file at {}", file_path.display());
209+
Some(file_path)
210+
} else if recursive && directory.pop() {
211+
find_ignore_file_path(directory, recursive)
212+
} else {
213+
None
214+
}
215+
}
216+
207217
fn get_ignore(
208218
directory: &Path,
209219
search_parent_directories: bool,
@@ -281,7 +291,7 @@ fn format(opt: opt::Opt) -> Result<i32> {
281291

282292
// Load the configuration
283293
let opt_for_config_resolver = opt.clone();
284-
let mut config_resolver = config::ConfigResolver::new(&opt_for_config_resolver)?;
294+
let config_resolver = config_resolver::ConfigResolver::new(opt_for_config_resolver.into())?;
285295

286296
// Create range if provided
287297
let range = if opt.range_start.is_some() || opt.range_end.is_some() {

0 commit comments

Comments
 (0)