Skip to content

Commit

Permalink
refactor: rearrange project state a bit (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Feb 2, 2025
1 parent 2b56e71 commit 902b9c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
6 changes: 3 additions & 3 deletions crates/tinymist/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl ServerState {
let previewer = typst_preview::PreviewBuilder::new(cli_args.preview.clone());
let watcher = previewer.compile_watcher();

let primary = &mut self.project.state.primary;
let primary = &mut self.project.compiler.primary;
if !cli_args.not_as_primary && self.preview.watchers.register(&primary.id, watcher) {
let id = primary.id.clone();
// todo: recover pin status reliably
Expand All @@ -335,8 +335,8 @@ impl ServerState {
.map_err(internal_error)?;

// Gets the task-dedicated compile server.
let Some(dedicate) = self.project.state.dedicates.iter_mut().find(|d| d.id == id)
else {
let mut dedicates = self.project.compiler.dedicates.iter_mut();
let Some(dedicate) = dedicates.find(|d| d.id == id) else {
return Err(invalid_params(
"just restarted compiler instance for the task is not found",
));
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist/src/state/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl ServerState {

log::info!("the entry file of TypstActor(primary) is changing to {next_entry:?}");

let id = self.project.state.primary.id.clone();
let id = self.project.primary_id().clone();
let task = TaskInputs {
entry: Some(next_entry.clone()),
..Default::default()
Expand Down
83 changes: 42 additions & 41 deletions crates/tinymist/src/state/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,29 @@ type EditorSender = mpsc::UnboundedSender<EditorRequest>;
/// LSP project compiler.
pub type LspProjectCompiler = ProjectCompiler<LspCompilerFeat, ProjectInsStateExt>;

#[derive(Default, Clone)]
pub struct ProjectPreviewState {
#[cfg(feature = "preview")]
pub(crate) inner: Arc<Mutex<FxHashMap<ProjectInsId, Arc<typst_preview::CompileWatcher>>>>,
}

#[cfg(feature = "preview")]
impl ProjectPreviewState {
// todo: multiple preview support
#[must_use]
pub fn register(&self, id: &ProjectInsId, handle: &Arc<typst_preview::CompileWatcher>) -> bool {
let mut p = self.inner.lock();
if p.contains_key(id) {
return false;
}

p.insert(id.clone(), handle.clone());
true
}

#[must_use]
pub fn unregister(&self, task_id: &ProjectInsId) -> bool {
let mut p = self.inner.lock();
if p.remove(task_id).is_some() {
return true;
}

false
}

#[must_use]
pub fn get(&self, task_id: &ProjectInsId) -> Option<Arc<typst_preview::CompileWatcher>> {
self.inner.lock().get(task_id).cloned()
}
}

#[derive(Default)]
pub struct ProjectInsStateExt {
pub is_compiling: bool,
pub last_compilation: Option<LspCompiledArtifact>,
}

pub struct ProjectState {
pub state: LspProjectCompiler,
pub compiler: LspProjectCompiler,
pub preview: ProjectPreviewState,
pub analysis: Arc<Analysis>,
pub stats: CompilerQueryStats,
pub export: crate::task::ExportTask,
}

impl ProjectState {
/// The primary instance id
pub fn primary_id(&self) -> &ProjectInsId {
&self.compiler.primary.id
}

/// Snapshot the compiler thread for tasks
pub fn snapshot(&mut self) -> Result<LspCompileSnapshot> {
Ok(self.state.snapshot())
Ok(self.compiler.snapshot())
}

/// Snapshot the compiler thread for language queries
Expand All @@ -115,14 +84,14 @@ impl ProjectState {

pub fn interrupt(&mut self, intr: Interrupt<LspCompilerFeat>) {
if let Interrupt::Compiled(compiled) = &intr {
let proj = self.state.projects().find(|p| p.id == compiled.id);
let proj = self.compiler.projects().find(|p| p.id == compiled.id);
if let Some(proj) = proj {
proj.ext.is_compiling = false;
proj.ext.last_compilation = Some(compiled.clone());
}
}

self.state.process(intr);
self.compiler.process(intr);
}

pub(crate) fn stop(&mut self) {
Expand All @@ -134,7 +103,39 @@ impl ProjectState {
group: &str,
entry: EntryState,
) -> Result<ProjectInsId> {
self.state.restart_dedicate(group, entry)
self.compiler.restart_dedicate(group, entry)
}
}

#[derive(Default, Clone)]
pub struct ProjectPreviewState {
#[cfg(feature = "preview")]
pub(crate) inner: Arc<Mutex<FxHashMap<ProjectInsId, Arc<typst_preview::CompileWatcher>>>>,
}

#[cfg(feature = "preview")]
impl ProjectPreviewState {
#[must_use]
pub fn register(&self, id: &ProjectInsId, handle: &Arc<typst_preview::CompileWatcher>) -> bool {
let mut p = self.inner.lock();

// Don't replace the existing watcher if it exists
if p.contains_key(id) {
return false;
}

p.insert(id.clone(), handle.clone());
true
}

#[must_use]
pub fn unregister(&self, task_id: &ProjectInsId) -> bool {
self.inner.lock().remove(task_id).is_some()
}

#[must_use]
pub fn get(&self, task_id: &ProjectInsId) -> Option<Arc<typst_preview::CompileWatcher>> {
self.inner.lock().get(task_id).cloned()
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/tinymist/src/state/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl ServerState {
impl ServerState {
/// Get the current server info.
pub fn collect_server_info(&mut self) -> QueryFuture {
let dg = self.project.state.primary.id.to_string();
let dg = self.project.primary_id().to_string();
let api_stats = self.project.stats.report();
let query_stats = self.project.analysis.report_query_stats();
let alloc_stats = self.project.analysis.report_alloc_stats();
Expand Down Expand Up @@ -365,7 +365,7 @@ impl ServerState {
old_project.stop();
});

Ok(self.project.state.primary.id.clone())
Ok(self.project.primary_id().clone())
}

/// Restart the server with the given group.
Expand Down Expand Up @@ -462,7 +462,7 @@ impl ServerState {

// Create the actor
let compile_handle = handle.clone();
let server = ProjectCompiler::new(
let compiler = ProjectCompiler::new(
verse,
dep_tx,
CompileServerOpts {
Expand All @@ -481,7 +481,7 @@ impl ServerState {
});

ProjectState {
state: server,
compiler,
preview: Default::default(),
analysis: handle.analysis.clone(),
stats: CompilerQueryStats::default(),
Expand Down

0 comments on commit 902b9c6

Please sign in to comment.