22
33use std:: { env, fs, path:: PathBuf } ;
44
5- use anyhow:: { anyhow, Context , Result } ;
5+ use anyhow:: { Context , Result } ;
6+ use etcetera:: BaseStrategy as _;
67use serde:: { Deserialize , Serialize } ;
78use serde_json:: Value ;
89
@@ -38,8 +39,24 @@ impl Config {
3839 return Ok ( Some ( xdg_path) ) ;
3940 }
4041
41- let legacy_path = dirs:: home_dir ( )
42- . ok_or_else ( || anyhow ! ( "Cannot determine home directory" ) ) ?
42+ if cfg ! ( target_os = "macos" ) {
43+ let legacy_apple_path = etcetera:: base_strategy:: Apple :: new ( ) ?
44+ . data_dir ( ) // `$HOME/Library/Application Support/`
45+ . join ( "tree-sitter" )
46+ . join ( "config.json" ) ;
47+ if legacy_apple_path. is_file ( ) {
48+ fs:: create_dir_all ( xdg_path. parent ( ) . unwrap ( ) ) ?;
49+ fs:: rename ( & legacy_apple_path, & xdg_path) ?;
50+ println ! (
51+ "Warning: your config.json file has been automatically migrated from \" {}\" to \" {}\" " ,
52+ legacy_apple_path. display( ) ,
53+ xdg_path. display( )
54+ ) ;
55+ return Ok ( Some ( xdg_path) ) ;
56+ }
57+ }
58+
59+ let legacy_path = etcetera:: home_dir ( ) ?
4360 . join ( ".tree-sitter" )
4461 . join ( "config.json" ) ;
4562 if legacy_path. is_file ( ) {
@@ -50,8 +67,8 @@ impl Config {
5067 }
5168
5269 fn xdg_config_file ( ) -> Result < PathBuf > {
53- let xdg_path = dirs :: config_dir ( )
54- . ok_or_else ( || anyhow ! ( "Cannot determine config directory" ) ) ?
70+ let xdg_path = etcetera :: choose_base_strategy ( ) ?
71+ . config_dir ( )
5572 . join ( "tree-sitter" )
5673 . join ( "config.json" ) ;
5774 Ok ( xdg_path)
@@ -63,7 +80,7 @@ impl Config {
6380 /// - Location specified by the path parameter if provided
6481 /// - `$TREE_SITTER_DIR/config.json`, if the `TREE_SITTER_DIR` environment variable is set
6582 /// - `tree-sitter/config.json` in your default user configuration directory, as determined by
66- /// [`dirs::config_dir `](https://docs.rs/dirs /*/dirs/fn.config_dir.html )
83+ /// [`etcetera::choose_base_strategy `](https://docs.rs/etcetera /*/etcetera/#basestrategy )
6784 /// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store
6885 /// its configuration
6986 pub fn load ( path : Option < PathBuf > ) -> Result < Self > {
0 commit comments