Skip to content

Commit

Permalink
dev: borrow the document state inside of compiler (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Mar 14, 2024
1 parent c7825c3 commit 9d34457
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crates/tinymist-query/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ impl CompletionRequest {
pub fn request(
self,
world: &TypstSystemWorld,
doc: Option<Arc<TypstDocument>>,
doc: Option<VersionedDocument>,
position_encoding: PositionEncoding,
) -> Option<CompletionResponse> {
let doc = doc.as_deref();
let doc = doc.as_ref().map(|doc| doc.document.as_ref());
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
let cursor = lsp_to_typst::position(self.position, position_encoding, &source)?;

Expand Down
6 changes: 4 additions & 2 deletions crates/tinymist-query/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ impl HoverRequest {
pub fn request(
self,
world: &TypstSystemWorld,
doc: Option<Arc<TypstDocument>>,
doc: Option<VersionedDocument>,
position_encoding: PositionEncoding,
) -> Option<Hover> {
let doc = doc.as_ref().map(|doc| doc.document.as_ref());

let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
let offset = lsp_to_typst::position(self.position, position_encoding, &source)?;
// the typst's cursor is 1-based, so we need to add 1 to the offset
let cursor = offset + 1;

let typst_tooltip = typst_ide::tooltip(world, doc.as_deref(), &source, cursor)?;
let typst_tooltip = typst_ide::tooltip(world, doc, &source, cursor)?;

let ast_node = LinkedNode::new(source.root()).leaf_at(cursor)?;
let range = typst_to_lsp::range(ast_node.range(), &source, position_encoding);
Expand Down
10 changes: 10 additions & 0 deletions crates/tinymist-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ pub mod analysis;

pub(crate) mod diagnostics;

use std::sync::Arc;

use typst_ts_core::TypstDocument;

pub use diagnostics::*;
pub(crate) mod signature_help;
pub use signature_help::*;
Expand Down Expand Up @@ -40,6 +44,12 @@ pub use lsp_typst_boundary::*;

mod prelude;

#[derive(Debug, Clone)]
pub struct VersionedDocument {
pub version: usize,
pub document: Arc<TypstDocument>,
}

mod polymorphic {
use super::prelude::*;
use super::*;
Expand Down
3 changes: 2 additions & 1 deletion crates/tinymist-query/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ pub use typst::syntax::{
pub use typst::World;
use typst_ts_compiler::service::WorkspaceProvider;
pub use typst_ts_compiler::TypstSystemWorld;
pub use typst_ts_core::{TypstDocument, TypstFileId};
pub use typst_ts_core::TypstFileId;

pub use crate::analysis::analyze_expr;
pub use crate::lsp_typst_boundary::{
lsp_to_typst, typst_to_lsp, LspDiagnostic, LspRange, LspSeverity, PositionEncoding,
TypstDiagnostic, TypstSeverity, TypstSpan,
};
pub use crate::VersionedDocument;

pub fn get_suitable_source_in_workspace(w: &TypstSystemWorld, p: &Path) -> FileResult<Source> {
// todo: source in packages
Expand Down
7 changes: 1 addition & 6 deletions crates/tinymist/src/actor/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use serde::Serialize;
use tinymist_query::VersionedDocument;
use tokio::sync::{mpsc, oneshot};
use typst::{
layout::{Frame, FrameItem, Point, Position},
Expand All @@ -34,12 +35,6 @@ use typst_ts_compiler::service::{

use crate::{task::BorrowTask, utils};

#[derive(Debug, Clone)]
pub struct VersionedDocument {
pub version: usize,
pub document: Arc<TypstDocument>,
}

/// Interrupts for external sources
enum ExternalInterrupt<Ctx> {
/// Compile anyway.
Expand Down
31 changes: 15 additions & 16 deletions crates/tinymist/src/actor/typst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use tinymist_query::{
CompilerQueryRequest, CompilerQueryResponse, DiagnosticsMap, FoldRequestFeature,
OnExportRequest, OnSaveExportRequest, PositionEncoding,
OnExportRequest, OnSaveExportRequest, PositionEncoding, VersionedDocument,
};
use tokio::sync::{broadcast, mpsc, oneshot, watch};
use typst::{
Expand Down Expand Up @@ -88,15 +88,13 @@ pub fn create_server(

let (root_tx, root_rx) = oneshot::channel();

let handler = CompileHandler {
result: Arc::new(SyncMutex::new(Err(CompileStatus::Compiling))),
inner: Arc::new(SyncMutex::new(None)),
};

let inner = Deferred::new({
let current_runtime = tokio::runtime::Handle::current();
let handler = CompileHandler {
inner: Arc::new(SyncMutex::new(None)),
};

let diag_group = diag_group.clone();
let handler = handler.clone();
let entry = entry.clone();
let render_tx = render_tx.clone();

Expand Down Expand Up @@ -170,7 +168,6 @@ pub fn create_server(
root,
entry,
pos_encoding,
handler,
inner,
render_tx,
)
Expand All @@ -192,9 +189,8 @@ impl<'a> fmt::Debug for ShowOpts<'a> {

macro_rules! query_state {
($self:ident, $method:ident, $req:expr) => {{
let doc = $self.handler.result.lock().unwrap().clone().ok();
let enc = $self.position_encoding;
let res = $self.steal_world(move |w| $req.request(w, doc, enc));
let res = $self.steal_state(move |w, doc| $req.request(w, doc, enc));
res.map(CompilerQueryResponse::$method)
}};
}
Expand All @@ -209,7 +205,6 @@ macro_rules! query_world {

#[derive(Clone)]
pub struct CompileHandler {
result: Arc<SyncMutex<Result<Arc<TypstDocument>, CompileStatus>>>,
inner: Arc<SyncMutex<Option<CompilationHandleImpl>>>,
}

Expand All @@ -222,8 +217,6 @@ impl CompilationHandle for CompileHandler {
}

fn notify_compile(&self, result: Result<Arc<TypstDocument>, CompileStatus>) {
*self.result.lock().unwrap() = result.clone();

let inner = self.inner.lock().unwrap();
if let Some(inner) = inner.as_ref() {
inner.notify_compile(result.clone());
Expand Down Expand Up @@ -370,7 +363,6 @@ impl<C: Compiler<World = TypstSystemWorld>, H> Reporter<C, H> {
pub struct CompileActor {
diag_group: String,
position_encoding: PositionEncoding,
handler: CompileHandler,
root_tx: Mutex<Option<oneshot::Sender<Option<ImmutPath>>>>,
root: OnceCell<Option<ImmutPath>>,
entry: Arc<Mutex<Option<ImmutPath>>>,
Expand All @@ -386,7 +378,6 @@ impl CompileActor {
root: Option<ImmutPath>,
entry: Option<ImmutPath>,
position_encoding: PositionEncoding,
handler: CompileHandler,
inner: Deferred<CompileClient<CompileHandler>>,
render_tx: broadcast::Sender<RenderActorRequest>,
) -> Self {
Expand All @@ -398,7 +389,6 @@ impl CompileActor {
None => OnceCell::new(),
},
position_encoding,
handler,
entry: Arc::new(Mutex::new(entry)),
inner,
render_tx,
Expand Down Expand Up @@ -729,6 +719,15 @@ impl CompileActor {
Ok(())
}

fn steal_state<T: Send + Sync + 'static>(
&self,
f: impl FnOnce(&TypstSystemWorld, Option<VersionedDocument>) -> T + Send + Sync + 'static,
) -> anyhow::Result<T> {
let fut = self.steal(move |compiler| f(compiler.compiler.world(), compiler.doc()));

Ok(fut?)
}

fn steal_world<T: Send + Sync + 'static>(
&self,
f: impl FnOnce(&TypstSystemWorld) -> T + Send + Sync + 'static,
Expand Down

0 comments on commit 9d34457

Please sign in to comment.