-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load require aliases from the nearest `.luaurc` file for each processed file. --------- Co-authored-by: jeparlefrancais <[email protected]>
- Loading branch information
1 parent
a6a8f33
commit eebf917
Showing
11 changed files
with
223 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::{ | ||
cell::RefCell, | ||
collections::HashMap, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::{DarkluaError, Resources}; | ||
|
||
const LUAU_RC_FILE_NAME: &str = ".luaurc"; | ||
|
||
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq)] | ||
pub(crate) struct LuauConfiguration { | ||
#[serde(default)] | ||
pub(crate) aliases: HashMap<String, PathBuf>, | ||
} | ||
|
||
fn find_luau_configuration_private( | ||
luau_file: &Path, | ||
resources: &Resources, | ||
) -> Result<Option<LuauConfiguration>, DarkluaError> { | ||
log::debug!( | ||
"find closest {} for '{}'", | ||
LUAU_RC_FILE_NAME, | ||
luau_file.display() | ||
); | ||
|
||
for ancestor in luau_file.ancestors() { | ||
let config_path = ancestor.join(LUAU_RC_FILE_NAME); | ||
|
||
if resources.exists(&config_path)? { | ||
let config = resources.get(&config_path)?; | ||
|
||
return serde_json::from_str(&config) | ||
.map(|mut config: LuauConfiguration| { | ||
log::debug!("found luau configuration at '{}'", config_path.display()); | ||
|
||
config.aliases = config | ||
.aliases | ||
.into_iter() | ||
.map(|(mut key, value)| { | ||
key.insert(0, '@'); | ||
(key, ancestor.join(value)) | ||
}) | ||
.collect(); | ||
|
||
Some(config) | ||
}) | ||
.map_err(Into::into); | ||
} | ||
} | ||
|
||
Ok(None) | ||
} | ||
|
||
thread_local! { | ||
static LUAU_RC_CACHE: RefCell<HashMap<Option<PathBuf>, Option<LuauConfiguration>>> = RefCell::new(HashMap::new()); | ||
} | ||
|
||
pub(crate) fn find_luau_configuration( | ||
luau_file: &Path, | ||
resources: &Resources, | ||
) -> Result<Option<LuauConfiguration>, DarkluaError> { | ||
let key = luau_file.parent().map(Path::to_path_buf); | ||
|
||
LUAU_RC_CACHE.with(|luau_rc_cache| { | ||
{ | ||
let cache = luau_rc_cache.borrow(); | ||
|
||
let res = cache.get(&key); | ||
if let Some(res) = res { | ||
return Ok(res.clone()); | ||
} | ||
} | ||
|
||
let mut cache = luau_rc_cache.borrow_mut(); | ||
|
||
let value = find_luau_configuration_private(luau_file, resources)?; | ||
|
||
cache.insert(key, value.clone()); | ||
|
||
Ok(value) | ||
}) | ||
} | ||
|
||
pub fn clear_luau_configuration_cache() { | ||
LUAU_RC_CACHE.with(|luau_rc_cache| { | ||
let mut cache = luau_rc_cache.borrow_mut(); | ||
cache.clear(); | ||
log::debug!("luau configuration cache cleared"); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters