first pass of merging in warp (doesn't build)
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
//! Module containing the definition of [`OpenedFilesModel`],
|
||||
//! which tracks files that have been opened, organized by repository.
|
||||
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use galaxyui::{Entity, ModelContext, SingletonEntity};
|
||||
use instant::Instant;
|
||||
use galaxy_util::local_or_remote_path::LocalOrRemotePath;
|
||||
|
||||
/// Tracks opened files within a single repository.
|
||||
/// Keys are repo-relative file paths (e.g. `src/main.rs`).
|
||||
#[derive(Default, Clone)]
|
||||
pub struct OpenedFilesInRepo(HashMap<PathBuf, Instant>);
|
||||
pub struct OpenedFilesInRepo(HashMap<String, Instant>);
|
||||
|
||||
impl OpenedFilesInRepo {
|
||||
pub fn get(&self, file_path: &PathBuf) -> Option<&Instant> {
|
||||
self.0.get(file_path)
|
||||
pub fn get(&self, relative_path: &str) -> Option<&Instant> {
|
||||
self.0.get(relative_path)
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&PathBuf, &Instant)> {
|
||||
#[cfg_attr(not(feature = "local_fs"), allow(dead_code))]
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&String, &Instant)> {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// Model that tracks files that have been opened, organized by repository.
|
||||
/// Maps repository paths to files and when they were last opened.
|
||||
/// Maps repository root locations (local or remote) to their opened files.
|
||||
#[derive(Default)]
|
||||
pub struct OpenedFilesModel {
|
||||
opened_files: HashMap<PathBuf, OpenedFilesInRepo>,
|
||||
opened_files: HashMap<LocalOrRemotePath, OpenedFilesInRepo>,
|
||||
}
|
||||
|
||||
impl Entity for OpenedFilesModel {
|
||||
@@ -38,31 +42,35 @@ impl OpenedFilesModel {
|
||||
}
|
||||
|
||||
/// Get all opened files for a specific repository.
|
||||
pub fn opened_files_for_repo(&self, repo_path: &PathBuf) -> Option<&OpenedFilesInRepo> {
|
||||
self.opened_files.get(repo_path)
|
||||
pub fn opened_files_for_repo(
|
||||
&self,
|
||||
repo_root: &LocalOrRemotePath,
|
||||
) -> Option<&OpenedFilesInRepo> {
|
||||
self.opened_files.get(repo_root)
|
||||
}
|
||||
|
||||
/// Record that a file has been opened in a repository. If the `file_path` is not within the `repo_path`,
|
||||
/// then the file is not recorded.
|
||||
/// Record that a file has been opened in a repository.
|
||||
///
|
||||
/// `repo_root` is the repository root location (local or remote).
|
||||
/// `file_location` is the absolute file location. If it is not within
|
||||
/// `repo_root`, the file is not recorded.
|
||||
#[cfg_attr(not(feature = "local_fs"), allow(dead_code))]
|
||||
pub fn file_opened(
|
||||
&mut self,
|
||||
repo_path: PathBuf,
|
||||
file_path: PathBuf,
|
||||
repo_root: LocalOrRemotePath,
|
||||
file_location: &LocalOrRemotePath,
|
||||
ctx: &mut ModelContext<Self>,
|
||||
) {
|
||||
let opened_at = Instant::now();
|
||||
|
||||
// Convert absolute file path to relative path from repo root
|
||||
let Ok(relative_file_path) = file_path.strip_prefix(&repo_path) else {
|
||||
let Some(relative_path) = repo_root.strip_repo_prefix(file_location) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let opened_at = Instant::now();
|
||||
self.opened_files
|
||||
.entry(repo_path.clone())
|
||||
.entry(repo_root)
|
||||
.or_default()
|
||||
.0
|
||||
.insert(relative_file_path.into(), opened_at);
|
||||
.insert(relative_path, opened_at);
|
||||
|
||||
ctx.notify();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user