@@ -8,23 +8,32 @@ use std::path::PathBuf;
88use air_r_formatter:: context:: RFormatOptions ;
99use air_r_parser:: RParserOptions ;
1010use fs:: relativize_path;
11- use ignore:: DirEntry ;
1211use itertools:: Either ;
1312use itertools:: Itertools ;
14- use line_ending:: LineEnding ;
1513use thiserror:: Error ;
14+ use workspace:: resolve:: discover_r_file_paths;
15+ use workspace:: resolve:: SettingsResolver ;
16+ use workspace:: settings:: FormatSettings ;
17+ use workspace:: settings:: Settings ;
1618
1719use crate :: args:: FormatCommand ;
1820use crate :: ExitStatus ;
1921
2022pub ( crate ) fn format ( command : FormatCommand ) -> anyhow:: Result < ExitStatus > {
2123 let mode = FormatMode :: from_command ( & command) ;
22- let paths = resolve_paths ( & command. paths ) ;
24+
25+ let paths = discover_r_file_paths ( & command. paths ) ;
26+
27+ let mut resolver = SettingsResolver :: new ( Settings :: default ( ) ) ;
28+ resolver. load_from_paths ( & command. paths ) ?;
2329
2430 let ( actions, errors) : ( Vec < _ > , Vec < _ > ) = paths
2531 . into_iter ( )
2632 . map ( |path| match path {
27- Ok ( path) => format_file ( path, mode) ,
33+ Ok ( path) => {
34+ let settings = resolver. resolve_or_fallback ( & path) ;
35+ format_file ( path, mode, & settings. format )
36+ }
2837 Err ( err) => Err ( err. into ( ) ) ,
2938 } )
3039 . partition_map ( |result| match result {
@@ -99,62 +108,6 @@ fn write_changed(actions: &[FormatFileAction], f: &mut impl Write) -> io::Result
99108 Ok ( ( ) )
100109}
101110
102- fn resolve_paths ( paths : & [ PathBuf ] ) -> Vec < Result < PathBuf , ignore:: Error > > {
103- let paths: Vec < PathBuf > = paths. iter ( ) . map ( fs:: normalize_path) . collect ( ) ;
104-
105- let ( first_path, paths) = paths
106- . split_first ( )
107- . expect ( "Clap should ensure at least 1 path is supplied." ) ;
108-
109- // TODO: Parallel directory visitor
110- let mut builder = ignore:: WalkBuilder :: new ( first_path) ;
111-
112- for path in paths {
113- builder. add ( path) ;
114- }
115-
116- let mut out = Vec :: new ( ) ;
117-
118- for path in builder. build ( ) {
119- match path {
120- Ok ( entry) => {
121- if let Some ( path) = is_valid_path ( entry) {
122- out. push ( Ok ( path) ) ;
123- }
124- }
125- Err ( err) => {
126- out. push ( Err ( err) ) ;
127- }
128- }
129- }
130-
131- out
132- }
133-
134- // Decide whether or not to accept an `entry` based on include/exclude rules.
135- fn is_valid_path ( entry : DirEntry ) -> Option < PathBuf > {
136- // Ignore directories
137- if entry. file_type ( ) . map_or ( true , |ft| ft. is_dir ( ) ) {
138- return None ;
139- }
140-
141- // Accept all files that are passed-in directly, even non-R files
142- if entry. depth ( ) == 0 {
143- let path = entry. into_path ( ) ;
144- return Some ( path) ;
145- }
146-
147- // Otherwise check if we should accept this entry
148- // TODO: Many other checks based on user exclude/includes
149- let path = entry. into_path ( ) ;
150-
151- if !fs:: has_r_extension ( & path) {
152- return None ;
153- }
154-
155- Some ( path)
156- }
157-
158111pub ( crate ) enum FormatFileAction {
159112 Formatted ( PathBuf ) ,
160113 Unchanged ,
@@ -166,18 +119,15 @@ impl FormatFileAction {
166119 }
167120}
168121
169- // TODO: Take workspace `FormatOptions` that get resolved to `RFormatOptions`
170- // for the formatter here. Respect user specified `LineEnding` option too, and
171- // only use inferred endings when `FormatOptions::LineEnding::Auto` is used.
172- fn format_file ( path : PathBuf , mode : FormatMode ) -> Result < FormatFileAction , FormatCommandError > {
122+ fn format_file (
123+ path : PathBuf ,
124+ mode : FormatMode ,
125+ settings : & FormatSettings ,
126+ ) -> Result < FormatFileAction , FormatCommandError > {
173127 let source = std:: fs:: read_to_string ( & path)
174128 . map_err ( |err| FormatCommandError :: Read ( path. clone ( ) , err) ) ?;
175129
176- let line_ending = match line_ending:: infer ( & source) {
177- LineEnding :: Lf => biome_formatter:: LineEnding :: Lf ,
178- LineEnding :: Crlf => biome_formatter:: LineEnding :: Crlf ,
179- } ;
180- let options = RFormatOptions :: default ( ) . with_line_ending ( line_ending) ;
130+ let options = settings. to_format_options ( & source) ;
181131
182132 let formatted = match format_source ( source. as_str ( ) , options) {
183133 Ok ( formatted) => formatted,
0 commit comments