fix: source switch on primary node

This commit is contained in:
Myriad-Dreamin 2024-03-08 17:06:08 +08:00
parent efe07c7b4a
commit f6d473f320
3 changed files with 61 additions and 8 deletions

View file

@ -1,6 +1,7 @@
use std::{borrow::Cow, ops::Range}; use std::{borrow::Cow, ops::Range};
use comemo::Prehashed; use comemo::Prehashed;
use log::debug;
use tower_lsp::lsp_types::{InlayHintKind, InlayHintLabel}; use tower_lsp::lsp_types::{InlayHintKind, InlayHintLabel};
use typst::{ use typst::{
foundations::{Args, Closure}, foundations::{Args, Closure},
@ -32,7 +33,15 @@ impl InlayHintRequest {
); );
let hints = inlay_hints(world, &source, range, position_encoding).ok()?; let hints = inlay_hints(world, &source, range, position_encoding).ok()?;
trace!("got inlay hints {hints:?}"); debug!(
"got inlay hints on {source:?} => {hints:?}",
source = source.id(),
hints = hints.len()
);
if hints.is_empty() {
let root = LinkedNode::new(source.root());
debug!("debug root {root:#?}");
}
Some(hints) Some(hints)
} }

View file

@ -57,6 +57,24 @@ mod polymorphic {
FoldingRange(FoldingRangeRequest), FoldingRange(FoldingRangeRequest),
SelectionRange(SelectionRangeRequest), SelectionRange(SelectionRangeRequest),
} }
impl CompilerQueryRequest {
pub fn associated_path(&self) -> Option<&Path> {
Some(match self {
CompilerQueryRequest::OnSaveExport(req) => &req.path,
CompilerQueryRequest::Hover(req) => &req.path,
CompilerQueryRequest::GotoDefinition(req) => &req.path,
CompilerQueryRequest::InlayHint(req) => &req.path,
CompilerQueryRequest::Completion(req) => &req.path,
CompilerQueryRequest::SignatureHelp(req) => &req.path,
CompilerQueryRequest::DocumentSymbol(req) => &req.path,
CompilerQueryRequest::Symbol(..) => return None,
CompilerQueryRequest::SemanticTokensFull(req) => &req.path,
CompilerQueryRequest::SemanticTokensDelta(req) => &req.path,
CompilerQueryRequest::FoldingRange(req) => &req.path,
CompilerQueryRequest::SelectionRange(req) => &req.path,
})
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum CompilerQueryResponse { pub enum CompilerQueryResponse {

View file

@ -6,7 +6,7 @@ use std::{
use anyhow::anyhow; use anyhow::anyhow;
use futures::future::join_all; use futures::future::join_all;
use log::{error, trace, warn}; use log::{debug, error, trace, warn};
use tinymist_query::{ use tinymist_query::{
CompilerQueryRequest, CompilerQueryResponse, DiagnosticsMap, LspDiagnostic, LspRange, CompilerQueryRequest, CompilerQueryResponse, DiagnosticsMap, LspDiagnostic, LspRange,
OnSaveExportRequest, PositionEncoding, SemanticTokenCache, OnSaveExportRequest, PositionEncoding, SemanticTokenCache,
@ -100,7 +100,7 @@ pub fn create_server(
}, },
)); ));
let driver = Reporter { let driver = Reporter {
diag_group, diag_group: diag_group.clone(),
position_encoding: cfg.position_encoding, position_encoding: cfg.position_encoding,
diag_tx, diag_tx,
inner: driver, inner: driver,
@ -112,7 +112,7 @@ pub fn create_server(
tokio::spawn(server.spawn()); tokio::spawn(server.spawn());
CompileNode::new(cfg.position_encoding, handler, client) CompileNode::new(diag_group, cfg.position_encoding, handler, client)
} }
pub struct CompileClusterActor { pub struct CompileClusterActor {
@ -205,7 +205,6 @@ impl CompileCluster {
let content: Bytes = content.as_bytes().into(); let content: Bytes = content.as_bytes().into();
self.primary.change_entry(path.clone()).await?;
// todo: is it safe to believe that the path is normalized? // todo: is it safe to believe that the path is normalized?
let files = FileChangeSet::new_inserts(vec![(path, FileResult::Ok((now, content)).into())]); let files = FileChangeSet::new_inserts(vec![(path, FileResult::Ok((now, content)).into())]);
let iw = self.primary.inner.lock().await; let iw = self.primary.inner.lock().await;
@ -263,7 +262,6 @@ impl CompileCluster {
drop(memory_changes); drop(memory_changes);
self.primary.change_entry(path.clone()).await?;
let files = FileChangeSet::new_inserts(vec![(path.clone(), snapshot)]); let files = FileChangeSet::new_inserts(vec![(path.clone(), snapshot)]);
let iw = self.primary.inner.lock().await; let iw = self.primary.inner.lock().await;
iw.add_memory_changes(MemoryEvent::Update(files)); iw.add_memory_changes(MemoryEvent::Update(files));
@ -312,7 +310,12 @@ impl CompileCluster {
match query { match query {
SemanticTokensFull(req) => query_tokens_cache!(self, SemanticTokensFull, req), SemanticTokensFull(req) => query_tokens_cache!(self, SemanticTokensFull, req),
SemanticTokensDelta(req) => query_tokens_cache!(self, SemanticTokensDelta, req), SemanticTokensDelta(req) => query_tokens_cache!(self, SemanticTokensDelta, req),
_ => self.primary.query(query).await, _ => {
if let Some(path) = query.associated_path() {
self.primary.change_entry(path.into()).await?;
}
self.primary.query(query).await
}
} }
} }
} }
@ -362,7 +365,16 @@ impl CompileMiddleware for CompileDriver {
impl CompileDriver { impl CompileDriver {
pub fn new(roots: Vec<PathBuf>, opts: CompileOpts) -> Self { pub fn new(roots: Vec<PathBuf>, opts: CompileOpts) -> Self {
let world = TypstSystemWorld::new(opts).expect("incorrect options"); let world = TypstSystemWorld::new(opts).expect("incorrect options");
let driver = CompileDriverInner::new(world); let mut driver = CompileDriverInner::new(world);
driver.entry_file = "detached.typ".into();
// todo: suitable approach to avoid panic
driver.notify_fs_event(typst_ts_compiler::vfs::notify::FilesystemEvent::Update(
typst_ts_compiler::vfs::notify::FileChangeSet::new_inserts(vec![(
driver.world.root.join("detached.typ").into(),
Ok((Time::now(), Bytes::from("".as_bytes()))).into(),
)]),
));
Self { Self {
inner: driver, inner: driver,
@ -468,6 +480,7 @@ impl<C: Compiler<World = TypstSystemWorld>, H> Reporter<C, H> {
} }
pub struct CompileNode<H: CompilationHandle> { pub struct CompileNode<H: CompilationHandle> {
diag_group: String,
position_encoding: PositionEncoding, position_encoding: PositionEncoding,
handler: CompileHandler, handler: CompileHandler,
entry: Arc<SyncMutex<Option<ImmutPath>>>, entry: Arc<SyncMutex<Option<ImmutPath>>>,
@ -505,6 +518,12 @@ impl<H: CompilationHandle> CompileNode<H> {
}; };
if should_change { if should_change {
debug!(
"the entry file of TypstActor({}) is changed to {}",
self.diag_group,
path.display()
);
self.steal_async(move |compiler, _| { self.steal_async(move |compiler, _| {
let root = compiler.compiler.world().workspace_root(); let root = compiler.compiler.world().workspace_root();
if !path.starts_with(&root) { if !path.starts_with(&root) {
@ -516,6 +535,11 @@ impl<H: CompilationHandle> CompileNode<H> {
driver.set_entry_file(path.as_ref().to_owned()); driver.set_entry_file(path.as_ref().to_owned());
}) })
.await?; .await?;
// todo: trigger recompile
let files = FileChangeSet::new_inserts(vec![]);
let inner = self.inner.lock().await;
inner.add_memory_changes(MemoryEvent::Update(files))
} }
Ok(()) Ok(())
@ -609,11 +633,13 @@ impl<H: CompilationHandle> CompileHost for CompileNode<H> {}
impl<H: CompilationHandle> CompileNode<H> { impl<H: CompilationHandle> CompileNode<H> {
fn new( fn new(
diag_group: String,
position_encoding: PositionEncoding, position_encoding: PositionEncoding,
handler: CompileHandler, handler: CompileHandler,
inner: CompileClient<H>, inner: CompileClient<H>,
) -> Self { ) -> Self {
Self { Self {
diag_group,
position_encoding, position_encoding,
handler, handler,
entry: Arc::new(SyncMutex::new(None)), entry: Arc::new(SyncMutex::new(None)),