@ -113,11 +113,7 @@ pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<std::p
/// documents that use a top-level array of values like the `languages.toml`,
/// documents that use a top-level array of values like the `languages.toml`,
/// where one usually wants to override or add to the array instead of
/// where one usually wants to override or add to the array instead of
/// replacing it altogether.
/// replacing it altogether.
pub fn merge_toml_values (
pub fn merge_toml_values ( left : toml ::Value , right : toml ::Value , merge_depth : usize ) -> toml ::Value {
left : toml ::Value ,
right : toml ::Value ,
merge_toplevel_arrays : bool ,
) -> toml ::Value {
use toml ::Value ;
use toml ::Value ;
fn get_name ( v : & Value ) -> Option < & str > {
fn get_name ( v : & Value ) -> Option < & str > {
@ -131,7 +127,7 @@ pub fn merge_toml_values(
// that you can specify a sub-set of languages in an overriding
// that you can specify a sub-set of languages in an overriding
// `languages.toml` but that nested arrays like Language Server
// `languages.toml` but that nested arrays like Language Server
// arguments are replaced instead of merged.
// arguments are replaced instead of merged.
if merge_ toplevel_arrays {
if merge_ depth > 0 {
left_items . reserve ( right_items . len ( ) ) ;
left_items . reserve ( right_items . len ( ) ) ;
for rvalue in right_items {
for rvalue in right_items {
let lvalue = get_name ( & rvalue )
let lvalue = get_name ( & rvalue )
@ -140,7 +136,7 @@ pub fn merge_toml_values(
} )
} )
. map ( | lpos | left_items . remove ( lpos ) ) ;
. map ( | lpos | left_items . remove ( lpos ) ) ;
let mvalue = match lvalue {
let mvalue = match lvalue {
Some ( lvalue ) = > merge_toml_values ( lvalue , rvalue , false ) ,
Some ( lvalue ) = > merge_toml_values ( lvalue , rvalue , merge_depth - 1 ) ,
None = > rvalue ,
None = > rvalue ,
} ;
} ;
left_items . push ( mvalue ) ;
left_items . push ( mvalue ) ;
@ -151,18 +147,22 @@ pub fn merge_toml_values(
}
}
}
}
( Value ::Table ( mut left_map ) , Value ::Table ( right_map ) ) = > {
( Value ::Table ( mut left_map ) , Value ::Table ( right_map ) ) = > {
for ( rname , rvalue ) in right_map {
if merge_depth > 0 {
match left_map . remove ( & rname ) {
for ( rname , rvalue ) in right_map {
Some ( lvalue ) = > {
match left_map . remove ( & rname ) {
let merged_value = merge_toml_values ( lvalue , rvalue , merge_toplevel_arrays ) ;
Some ( lvalue ) = > {
left_map . insert ( rname , merged_value ) ;
let merged_value = merge_toml_values ( lvalue , rvalue , merge_depth - 1 ) ;
}
left_map . insert ( rname , merged_value ) ;
None = > {
}
left_map . insert ( rname , rvalue ) ;
None = > {
left_map . insert ( rname , rvalue ) ;
}
}
}
}
}
Value ::Table ( left_map )
} else {
Value ::Table ( right_map )
}
}
Value ::Table ( left_map )
}
}
// Catch everything else we didn't handle, and use the right value
// Catch everything else we didn't handle, and use the right value
( _ , value ) = > value ,
( _ , value ) = > value ,
@ -187,7 +187,7 @@ mod merge_toml_tests {
. expect ( "Couldn't parse built-in languages config" ) ;
. expect ( "Couldn't parse built-in languages config" ) ;
let user : Value = toml ::from_str ( USER ) . unwrap ( ) ;
let user : Value = toml ::from_str ( USER ) . unwrap ( ) ;
let merged = merge_toml_values ( base , user , true ) ;
let merged = merge_toml_values ( base , user , 3 ) ;
let languages = merged . get ( "language" ) . unwrap ( ) . as_array ( ) . unwrap ( ) ;
let languages = merged . get ( "language" ) . unwrap ( ) . as_array ( ) . unwrap ( ) ;
let nix = languages
let nix = languages
. iter ( )
. iter ( )
@ -220,7 +220,7 @@ mod merge_toml_tests {
. expect ( "Couldn't parse built-in languages config" ) ;
. expect ( "Couldn't parse built-in languages config" ) ;
let user : Value = toml ::from_str ( USER ) . unwrap ( ) ;
let user : Value = toml ::from_str ( USER ) . unwrap ( ) ;
let merged = merge_toml_values ( base , user , true ) ;
let merged = merge_toml_values ( base , user , 3 ) ;
let languages = merged . get ( "language" ) . unwrap ( ) . as_array ( ) . unwrap ( ) ;
let languages = merged . get ( "language" ) . unwrap ( ) . as_array ( ) . unwrap ( ) ;
let ts = languages
let ts = languages
. iter ( )
. iter ( )