@@ -35,105 +35,97 @@ pub fn calculate_distance_penalty(current_file: Option<&str>, candidate_path: &s
3535 . count ( ) ;
3636
3737 let current_depth_from_common = current_parts. len ( ) - common_len;
38- let candidate_depth_from_common = candidate_parts. len ( ) - common_len;
39- let total_distance = current_depth_from_common + candidate_depth_from_common;
4038
41- if total_distance == 0 {
42- return 0 ; // Same path
39+ if current_depth_from_common == 0 {
40+ return 0 ; // Current file is at the common ancestor level
4341 }
4442
45- let penalty = -( total_distance as i32 * 2 ) ;
43+ let penalty = -( current_depth_from_common as i32 ) ;
4644
4745 penalty. max ( -20 )
4846}
4947
5048#[ cfg( test) ]
5149mod tests {
5250 use super :: * ;
53- use std :: path :: Path ;
51+
5452 #[ test]
53+ #[ cfg( not( target_family = "windows" ) ) ]
5554 fn test_calculate_distance_penalty ( ) {
56- {
57- let other_path = Path :: new ( "path" ) . join ( "to" ) . join ( "file.txt" ) ;
58- assert_eq ! (
59- calculate_distance_penalty( None , other_path. to_str( ) . unwrap( ) ) ,
60- 0
61- ) ;
62- }
63- {
64- let base_path = Path :: new ( "path" ) . join ( "to" ) . join ( "current" ) ;
65- let current_path = base_path. join ( "file.txt" ) ;
66- let other_path = base_path. join ( "other.txt" ) ;
67- assert_eq ! (
68- calculate_distance_penalty(
69- Some ( current_path. to_str( ) . unwrap( ) ) ,
70- other_path. to_str( ) . unwrap( )
71- ) ,
72- 0
73- ) ;
74- }
75- {
76- let base_path = Path :: new ( "path" ) . join ( "to" ) ;
77- let current_path = base_path. join ( "current" ) . join ( "file.txt" ) ;
78- let other_path = base_path. join ( "file.txt" ) ;
79- assert_eq ! (
80- calculate_distance_penalty(
81- Some ( current_path. to_str( ) . unwrap( ) ) ,
82- other_path. to_str( ) . unwrap( )
83- ) ,
84- -2
85- ) ;
86- }
87- {
88- let base_path = Path :: new ( "path" ) . join ( "to" ) ;
89- let current_path = base_path. join ( "current" ) . join ( "file.txt" ) ;
90- let other_path = base_path. join ( "other" ) . join ( "file.txt" ) ;
91- assert_eq ! (
92- calculate_distance_penalty(
93- Some ( current_path. to_str( ) . unwrap( ) ) ,
94- other_path. to_str( ) . unwrap( )
95- ) ,
96- -4
97- ) ;
98- }
99- {
100- let base_path = Path :: new ( "path" ) . join ( "to" ) ;
101- let current_path = base_path. join ( "current" ) . join ( "file.txt" ) ;
102- let other_path = base_path. join ( "another" ) . join ( "dir" ) . join ( "file.txt" ) ;
103- assert_eq ! (
104- calculate_distance_penalty(
105- Some ( current_path. to_str( ) . unwrap( ) ) ,
106- other_path. to_str( ) . unwrap( )
107- ) ,
108- -6
109- ) ;
110- }
111- {
112- let current_path = Path :: new ( "a" )
113- . join ( "b" )
114- . join ( "c" )
115- . join ( "d" )
116- . join ( "file.txt" ) ;
117- let other_path = Path :: new ( "x" )
118- . join ( "y" )
119- . join ( "z" )
120- . join ( "w" )
121- . join ( "file.txt" ) ;
122- assert_eq ! (
123- calculate_distance_penalty(
124- Some ( current_path. to_str( ) . unwrap( ) ) ,
125- other_path. to_str( ) . unwrap( )
126- ) ,
127- -16
128- ) ;
129- }
130- {
131- let current_path = Path :: new ( "file1.txt" ) . to_str ( ) . unwrap ( ) ;
132- let other_path = Path :: new ( "file2.txt" ) . to_str ( ) . unwrap ( ) ;
133- assert_eq ! (
134- calculate_distance_penalty( Some ( current_path) , other_path) ,
135- 0
136- ) ;
137- }
55+ assert_eq ! (
56+ calculate_distance_penalty( None , "examples/user/test/mod.rs" ) ,
57+ 0
58+ ) ;
59+ // Same directory
60+ assert_eq ! (
61+ calculate_distance_penalty(
62+ Some ( "examples/user/test/main.rs" ) ,
63+ "examples/user/test/mod.rs"
64+ ) ,
65+ 0
66+ ) ;
67+ //
68+ // One level apart
69+ assert_eq ! (
70+ calculate_distance_penalty(
71+ Some ( "examples/user/test/subdir/file.rs" ) ,
72+ "examples/user/test/mod.rs"
73+ ) ,
74+ -1
75+ ) ;
76+ //
77+ // Different subdirectories (same parent)
78+ assert_eq ! (
79+ calculate_distance_penalty(
80+ Some ( "examples/user/test/dir1/file.rs" ) ,
81+ "examples/user/test/dir2/mod.rs"
82+ ) ,
83+ -1
84+ ) ;
85+
86+ assert_eq ! (
87+ calculate_distance_penalty(
88+ Some ( "examples/audio-announce/src/lib/audio-announce.rs" ) ,
89+ "examples/audio-announce/src/main.rs"
90+ ) ,
91+ -1
92+ ) ;
93+
94+ assert_eq ! (
95+ calculate_distance_penalty(
96+ Some ( "examples/audio-announce/src/audio-announce.rs" ) ,
97+ "examples/pixel/src/main.rs"
98+ ) ,
99+ -2
100+ ) ;
101+
102+ // Root level files
103+ assert_eq ! ( calculate_distance_penalty( Some ( "main.rs" ) , "lib.rs" ) , 0 ) ;
104+ }
105+
106+ #[ test]
107+ #[ cfg( target_family = "windows" ) ]
108+ fn distance_penalty_works_on_windows ( ) {
109+ assert_eq ! (
110+ calculate_distance_penalty( None , "examples\\ user\\ test\\ mod.rs" ) ,
111+ 0
112+ ) ;
113+ // Same directory
114+ assert_eq ! (
115+ calculate_distance_penalty(
116+ Some ( "examples\\ user\\ test\\ main.rs" ) ,
117+ "examples\\ user\\ test\\ mod.rs"
118+ ) ,
119+ 0
120+ ) ;
121+ //
122+ // One level apart
123+ assert_eq ! (
124+ calculate_distance_penalty(
125+ Some ( "examples\\ user\\ test\\ subdir\\ file.rs" ) ,
126+ "examples\\ user\\ test\\ mod.rs"
127+ ) ,
128+ -1
129+ ) ;
138130 }
139131}
0 commit comments