mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-04 10:18:16 +00:00
dev: borrow the document state inside of compiler (#33)
This commit is contained in:
parent
c7825c3174
commit
9d344570b4
6 changed files with 34 additions and 27 deletions
|
@ -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)?;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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::*;
|
||||
|
@ -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::*;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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},
|
||||
|
@ -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.
|
||||
|
|
|
@ -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::{
|
||||
|
@ -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();
|
||||
|
||||
|
@ -170,7 +168,6 @@ pub fn create_server(
|
|||
root,
|
||||
entry,
|
||||
pos_encoding,
|
||||
handler,
|
||||
inner,
|
||||
render_tx,
|
||||
)
|
||||
|
@ -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)
|
||||
}};
|
||||
}
|
||||
|
@ -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>>>,
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
|
@ -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>>>,
|
||||
|
@ -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 {
|
||||
|
@ -398,7 +389,6 @@ impl CompileActor {
|
|||
None => OnceCell::new(),
|
||||
},
|
||||
position_encoding,
|
||||
handler,
|
||||
entry: Arc::new(Mutex::new(entry)),
|
||||
inner,
|
||||
render_tx,
|
||||
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue