use super::*;
impl ConfigManager {
pub fn config(&self) -> &VTCodeConfig {
&self.config
}
pub fn phase_timing(&self) -> Option<ConfigPhaseTiming> {
self.phase_timing
}
pub fn config_path(&self) -> Option<&Path> {
self.config_path.as_deref()
}
#[must_use]
pub fn is_repository_controlled_path(&self, path: &Path) -> bool {
if self.explicit_config_is_trusted {
return false;
}
if self
.layer_stack
.layers()
.iter()
.any(|layer| layer.is_enabled() && Self::repository_source_matches_path(&layer.source, path))
{
return true;
}
self.config_path.is_none()
&& self.workspace_root.as_deref().is_some_and(|workspace| {
Self::repository_source_matches_path(
&ConfigLayerSource::Workspace { file: workspace.join(&self.config_file_name) },
path,
)
})
}
pub fn workspace_root(&self) -> Option<&Path> {
self.workspace_root.as_deref()
}
pub fn preferred_workspace_config_path(&self, workspace: &Path) -> PathBuf {
self.layer_stack
.layers()
.iter()
.rev()
.find_map(|layer| match &layer.source {
ConfigLayerSource::Workspace { file } if layer.is_enabled() => Some(file.clone()),
_ => None,
})
.unwrap_or_else(|| workspace.join(&self.config_file_name))
}
pub fn config_file_name(&self) -> &str {
&self.config_file_name
}
pub fn layer_stack(&self) -> &ConfigLayerStack {
&self.layer_stack
}
pub fn preferred_user_config_path(&self) -> Option<PathBuf> {
self.canonical_user_config_path.clone()
}
pub fn user_config_paths(&self) -> Vec<PathBuf> {
let mut paths = self.tracked_user_config_paths.clone();
for path in self
.layer_stack
.layers()
.iter()
.filter_map(|layer| match (&layer.source, layer.is_enabled()) {
(ConfigLayerSource::User { file }, true) => Some(file),
_ => None,
})
{
if !paths.iter().any(|existing| existing == path) {
paths.push(path.clone());
}
}
paths
}
pub fn watched_config_paths(workspace: &Path) -> Vec<PathBuf> {
let provider = defaults::current_config_defaults();
let config_file_name = provider.config_file_name().to_string();
let workspace_paths = provider.workspace_paths_for(workspace);
let workspace_root = workspace_paths.workspace_root().to_path_buf();
let mut paths = Vec::new();
let mut push_unique = |path: PathBuf| {
if !paths.iter().any(|existing| existing == &path) {
paths.push(path);
}
};
if let Some(explicit_path) = session_override::explicit_config_path() {
push_unique(explicit_path);
}
if let Ok(system_paths) = provider.system_config_paths(&config_file_name) {
for path in system_paths {
push_unique(path);
}
}
for path in provider.home_config_paths(&config_file_name) {
push_unique(path);
}
if let Ok(Some(canonical_path)) = provider.canonical_user_config_path(&config_file_name) {
push_unique(canonical_path);
}
if let Some(project_name) = Self::current_project_name(&workspace_root) {
push_unique(
workspace_paths
.config_dir()
.join("projects")
.join(project_name)
.join("config")
.join(&config_file_name),
);
}
push_unique(workspace_paths.config_dir().join(&config_file_name));
push_unique(workspace_root.join(&config_file_name));
push_unique(workspace_root.join(".vtcode").join("theme.toml"));
paths
}
pub(super) fn repository_config_paths(workspace: &Path) -> Vec<PathBuf> {
let provider = defaults::current_config_defaults();
let config_file_name = provider.config_file_name().to_string();
let workspace_paths = provider.workspace_paths_for(workspace);
let workspace_root = canonicalize_workspace_root(workspace_paths.workspace_root());
let config_dir = workspace_paths.config_dir();
let mut paths = Vec::with_capacity(3);
let mut push_unique = |path: PathBuf| {
if !paths.iter().any(|existing| existing == &path) {
paths.push(path);
}
};
if let Some(project_path) = Self::project_config_path(&config_dir, &workspace_root, &config_file_name) {
push_unique(project_path);
}
push_unique(config_dir.join(&config_file_name));
push_unique(workspace_root.join(&config_file_name));
paths
}
pub(super) fn repository_source_matches_path(source: &ConfigLayerSource, path: &Path) -> bool {
let Some(source_file) = (match source {
ConfigLayerSource::Project { file } | ConfigLayerSource::Workspace { file } => Some(file),
ConfigLayerSource::System { .. } | ConfigLayerSource::User { .. } | ConfigLayerSource::Runtime => None,
}) else {
return false;
};
canonicalize_workspace_root(source_file) == canonicalize_workspace_root(path)
}
pub fn effective_config(&self) -> toml::Value {
self.layer_stack.effective_config()
}
pub fn has_explicit_top_level_key(&self, key: &str) -> bool {
self.layer_stack
.layers()
.iter()
.filter(|layer| layer.is_enabled())
.any(|layer| layer.config.as_table().is_some_and(|table| table.contains_key(key)))
}
pub fn session_duration(&self) -> std::time::Duration {
std::time::Duration::from_secs(60 * 60) }
}