77use std:: ffi:: { CString , c_char, c_void} ;
88use std:: ptr;
99
10+ use fff:: file_picker:: FilePicker ;
1011use fff:: git:: format_git_status;
1112use fff:: { FileItem , GrepMatch , GrepResult , Location , Score , SearchResult } ;
1213
13- // ---------------------------------------------------------------------------
14- // Helpers
15- // ---------------------------------------------------------------------------
16-
1714/// Allocate a heap CString from a `&str`, returning a raw pointer.
1815fn cstring_new ( s : & str ) -> * mut c_char {
1916 CString :: new ( s) . unwrap_or_default ( ) . into_raw ( )
@@ -64,7 +61,6 @@ unsafe fn free_cstring_array(arr: *mut *mut c_char, count: u32) {
6461/// Free the entire result with `fff_free_search_result`.
6562#[ repr( C ) ]
6663pub struct FffFileItem {
67- pub path : * mut c_char ,
6864 pub relative_path : * mut c_char ,
6965 pub file_name : * mut c_char ,
7066 pub git_status : * mut c_char ,
@@ -77,11 +73,11 @@ pub struct FffFileItem {
7773}
7874
7975impl FffFileItem {
80- pub fn from_item ( item : & FileItem , arena : * const u8 ) -> Self {
76+ pub fn from_item ( item : & FileItem , picker : & FilePicker ) -> Self {
77+ let rel_path = picker. relative_path ( item) ;
8178 FffFileItem {
82- path : cstring_new ( & item. relative_path ( arena) ) ,
83- relative_path : cstring_new ( & item. relative_path ( arena) ) ,
84- file_name : cstring_new ( & item. file_name ( arena) ) ,
79+ relative_path : cstring_new ( & rel_path) ,
80+ file_name : cstring_new ( & picker. file_name ( item) ) ,
8581 git_status : cstring_new ( format_git_status ( item. git_status ) ) ,
8682 size : item. size ,
8783 modified : item. modified ,
@@ -98,9 +94,6 @@ impl FffFileItem {
9894 /// All string pointers must have been allocated by `CString::into_raw`.
9995 pub unsafe fn free_strings ( & mut self ) {
10096 unsafe {
101- if !self . path . is_null ( ) {
102- drop ( CString :: from_raw ( self . path ) ) ;
103- }
10497 if !self . relative_path . is_null ( ) {
10598 drop ( CString :: from_raw ( self . relative_path ) ) ;
10699 }
@@ -232,11 +225,11 @@ pub struct FffSearchResult {
232225
233226impl FffSearchResult {
234227 /// Convert a core `SearchResult` into a heap-allocated `FffSearchResult`.
235- pub fn from_core ( result : & SearchResult , arena : * const u8 ) -> * mut Self {
228+ pub fn from_core ( result : & SearchResult , picker : & FilePicker ) -> * mut Self {
236229 let items: Vec < FffFileItem > = result
237230 . items
238231 . iter ( )
239- . map ( |i| FffFileItem :: from_item ( i, arena ) )
232+ . map ( |i| FffFileItem :: from_item ( i, picker ) )
240233 . collect ( ) ;
241234 let scores: Vec < FffScore > = result. scores . iter ( ) . map ( FffScore :: from) . collect ( ) ;
242235 let count = items. len ( ) as u32 ;
@@ -273,7 +266,6 @@ pub struct FffMatchRange {
273266#[ repr( C ) ]
274267pub struct FffGrepMatch {
275268 // -- pointers (8 bytes each) --
276- pub path : * mut c_char ,
277269 pub relative_path : * mut c_char ,
278270 pub file_name : * mut c_char ,
279271 pub git_status : * mut c_char ,
@@ -303,7 +295,7 @@ pub struct FffGrepMatch {
303295}
304296
305297impl FffGrepMatch {
306- fn from_core_with_file ( m : & GrepMatch , file : & FileItem , arena : * const u8 ) -> Self {
298+ fn from_core_with_file ( m : & GrepMatch , file : & FileItem , picker : & FilePicker ) -> Self {
307299 let ranges: Vec < FffMatchRange > = m
308300 . match_byte_offsets
309301 . iter ( )
@@ -317,10 +309,10 @@ impl FffGrepMatch {
317309 None => ( false , 0 ) ,
318310 } ;
319311
312+ let rel_path = picker. relative_path ( file) ;
320313 FffGrepMatch {
321- path : cstring_new ( & file. relative_path ( arena) ) ,
322- relative_path : cstring_new ( & file. relative_path ( arena) ) ,
323- file_name : cstring_new ( & file. file_name ( arena) ) ,
314+ relative_path : cstring_new ( & rel_path) ,
315+ file_name : cstring_new ( & picker. file_name ( file) ) ,
324316 git_status : cstring_new ( format_git_status ( file. git_status ) ) ,
325317 line_content : cstring_new ( & m. line_content ) ,
326318 match_ranges,
@@ -348,9 +340,6 @@ impl FffGrepMatch {
348340 /// All pointers must have been allocated by the corresponding `from_core`.
349341 pub unsafe fn free_fields ( & mut self ) {
350342 unsafe {
351- if !self . path . is_null ( ) {
352- drop ( CString :: from_raw ( self . path ) ) ;
353- }
354343 if !self . relative_path . is_null ( ) {
355344 drop ( CString :: from_raw ( self . relative_path ) ) ;
356345 }
@@ -401,13 +390,13 @@ pub struct FffGrepResult {
401390
402391impl FffGrepResult {
403392 /// Convert a core `GrepResult` into a heap-allocated `FffGrepResult`.
404- pub fn from_core ( result : & GrepResult , arena : * const u8 ) -> * mut Self {
393+ pub fn from_core ( result : & GrepResult , picker : & FilePicker ) -> * mut Self {
405394 let items: Vec < FffGrepMatch > = result
406395 . matches
407396 . iter ( )
408397 . map ( |m| {
409398 let file = result. files [ m. file_index ] ;
410- FffGrepMatch :: from_core_with_file ( m, file, arena )
399+ FffGrepMatch :: from_core_with_file ( m, file, picker )
411400 } )
412401 . collect ( ) ;
413402 let ( items_ptr, count) = vec_to_raw ( items) ;
0 commit comments