dev: stateful requests now accept snapshot (#1581)

* dev: stateful requests now accept snapshot

* dev: add some convenient methods

* dev: remove unused latest_doc state

* dev: use graph

* docs: comment

* fix: bad flag
This commit is contained in:
Myriad-Dreamin 2025-03-25 16:28:00 +08:00 committed by GitHub
parent 5b42231a77
commit 10ec787cc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 383 additions and 504 deletions

View file

@ -10,14 +10,12 @@ use std::{
use once_cell::sync::Lazy;
use serde_json::{ser::PrettyFormatter, Serializer, Value};
use tinymist_project::{CompileFontArgs, ExportTarget};
use tinymist_project::{CompileFontArgs, ExportTarget, LspCompileSnapshot, LspComputeGraph};
use tinymist_std::debug_loc::LspRange;
use tinymist_std::typst::TypstDocument;
use tinymist_world::package::PackageSpec;
use tinymist_world::vfs::WorkspaceResolver;
use tinymist_world::EntryState;
use tinymist_world::TaskInputs;
use tinymist_world::{EntryManager, EntryReader, ShadowApi};
use tinymist_world::{EntryManager, EntryReader, EntryState, ShadowApi, TaskInputs};
use typst::foundations::Bytes;
use typst::syntax::ast::{self, AstNode};
use typst::syntax::{LinkedNode, Source, SyntaxKind, VirtualPath};
@ -26,12 +24,11 @@ pub use insta::assert_snapshot;
pub use serde::Serialize;
pub use serde_json::json;
pub use tinymist_project::{LspUniverse, LspUniverseBuilder};
pub use tinymist_world::WorldComputeGraph;
use typst_shim::syntax::LinkedNodeExt;
use crate::syntax::find_module_level_docs;
use crate::{
analysis::Analysis, prelude::LocalContext, LspPosition, PositionEncoding, VersionedDocument,
};
use crate::{analysis::Analysis, prelude::LocalContext, LspPosition, PositionEncoding};
use crate::{to_lsp_position, CompletionFeat, LspWorldExt};
pub fn snapshot_testing(name: &str, f: &impl Fn(&mut LocalContext, PathBuf)) {
@ -122,13 +119,13 @@ pub fn get_test_properties(s: &str) -> HashMap<&'_ str, &'_ str> {
pub fn compile_doc_for_test(
ctx: &mut LocalContext,
properties: &HashMap<&str, &str>,
) -> Option<VersionedDocument> {
) -> LspComputeGraph {
let prev = ctx.world.entry_state();
let next = match properties.get("compile")?.trim() {
"true" => prev.clone(),
"false" => return None,
path if path.ends_with(".typ") => prev.select_in_workspace(Path::new(path)),
v => panic!("invalid value for 'compile' property: {v}"),
let next = match properties.get("compile").map(|s| s.trim()) {
Some("true") => prev.clone(),
None | Some("false") => return WorldComputeGraph::from_world(ctx.world.clone()),
Some(path) if path.ends_with(".typ") => prev.select_in_workspace(Path::new(path)),
v => panic!("invalid value for 'compile' property: {v:?}"),
};
let mut world = Cow::Borrowed(&ctx.world);
@ -138,14 +135,12 @@ pub fn compile_doc_for_test(
..Default::default()
}));
}
let mut world = world.into_owned();
world.set_is_compiling(true);
let mut snap = LspCompileSnapshot::from_world(world.into_owned());
snap.world.set_is_compiling(true);
let doc = typst::compile(&world).output.unwrap();
Some(VersionedDocument {
version: 0,
document: TypstDocument::Paged(Arc::new(doc)),
})
let doc = typst::compile(&snap.world).output.unwrap();
snap.success_doc = Some(TypstDocument::Paged(Arc::new(doc)));
WorldComputeGraph::new(snap)
}
pub fn run_with_sources<T>(source: &str, f: impl FnOnce(&mut LspUniverse, PathBuf) -> T) -> T {