feat: disable typst::compile in syntax only mode

This commit is contained in:
Myriad-Dreamin 2025-11-16 06:28:11 +08:00
parent 91312cb5f8
commit 0420558fd4
19 changed files with 58 additions and 13 deletions

3
Cargo.lock generated
View file

@ -4562,6 +4562,7 @@ dependencies = [
"tinymist-world",
"typst",
"typst-library",
"typst-shim",
]
[[package]]
@ -4580,6 +4581,7 @@ dependencies = [
"tinymist-world",
"typst",
"typst-library",
"typst-shim",
]
[[package]]
@ -4903,6 +4905,7 @@ dependencies = [
"ttf-parser",
"typst",
"typst-assets",
"typst-shim",
"wasm-bindgen",
"web-sys",
]

View file

@ -219,7 +219,7 @@ pub async fn test_main(args: TestArgs) -> Result<()> {
}
fn test_once(world: &LspWorld, ctx: &TestContext) -> Result<bool> {
let doc = typst::compile::<TypstPagedDocument>(&world).output?;
let doc = typst_shim::compile_opt::<TypstPagedDocument>(&world).output?;
let mut snap = CompileSnapshot::from_world(world.clone());
snap.success_doc = Some(TypstDocument::Paged(Arc::new(doc)));
@ -449,7 +449,7 @@ impl<'a> TestRunner<'a> {
}
fn build_example<T: typst::Document>(&self, world: &dyn World) -> (bool, Option<T>) {
let result = typst::compile::<T>(world);
let result = typst_shim::compile_opt::<T>(world);
if !result.warnings.is_empty() {
self.diagnostics.lock().push(result.warnings);
}

View file

@ -13,6 +13,7 @@ rust-version.workspace = true
[dependencies]
typst-library.workspace = true
typst-shim.workspace = true
typst.workspace = true
tinymist-std.workspace = true
tinymist-analysis.workspace = true

View file

@ -79,7 +79,7 @@ pub fn start_session<F: CompilerFeat>(
adaptor.before_compile();
step_global(BreakpointKind::BeforeCompile, &world);
let result = typst::compile::<PagedDocument>(&world);
let result = typst_shim::compile_opt::<PagedDocument>(&world);
adaptor.after_compile(result);
step_global(BreakpointKind::AfterCompile, &world);

View file

@ -13,6 +13,7 @@ rust-version.workspace = true
[dependencies]
typst-library.workspace = true
typst-shim.workspace = true
typst.workspace = true
tinymist-std.workspace = true
tinymist-analysis.workspace = true

View file

@ -30,7 +30,7 @@ pub fn collect_coverage<D: typst::Document, F: CompilerFeat>(
base: &CompilerWorld<F>,
) -> Result<CoverageResult> {
let (cov, result) = with_cov(base, |instr| {
if let Err(e) = typst::compile::<D>(&instr).output {
if let Err(e) = typst_shim::compile_opt::<D>(&instr).output {
print_diagnostics(instr, e.iter(), tinymist_world::DiagnosticFormat::Human)
.context_ut("failed to print diagnostics")?;
bail!("");

View file

@ -131,7 +131,7 @@ mod tests {
let Warned {
output,
warnings: compiler_warnings,
} = typst::compile::<PagedDocument>(ctx.world());
} = typst_shim::compile_opt::<PagedDocument>(ctx.world());
let compiler_errors = output.err().unwrap_or_default();
let compiler_diags = compiler_warnings.iter().chain(compiler_errors.iter());

View file

@ -147,7 +147,7 @@ pub fn compile_doc_for_test(
let mut snap = LspCompileSnapshot::from_world(world.into_owned());
snap.world.set_is_compiling(true);
let doc = typst::compile(&snap.world).output.unwrap();
let doc = typst_shim::compile_opt(&snap.world).output.unwrap();
snap.success_doc = Some(TypstDocument::Paged(Arc::new(doc)));
WorldComputeGraph::new(snap)
}

View file

@ -44,6 +44,7 @@ tar.workspace = true
tinymist-package.workspace = true
tinymist-std.workspace = true
tinymist-vfs.workspace = true
typst-shim.workspace = true
typst.workspace = true
typst-assets.workspace = true
ttf-parser.workspace = true

View file

@ -16,9 +16,9 @@ let universe = args
```rust
let world = verse.snapshot();
// in current thread
let doc = typst::compile(&world)?;
let doc = typst_shim::compile_opt(&world)?;
// the snapshot is Send + Sync
std::thread::spawn(move || {
let doc = typst::compile(&world)?;
let doc = typst_shim::compile_opt(&world)?;
});
```

View file

@ -303,7 +303,7 @@ impl<D: typst::Document + Send + Sync + 'static> CompilationTask<D> {
};
world.to_mut().set_is_compiling(true);
let compiled = ::typst::compile::<D>(world.as_ref());
let compiled = ::typst_shim::compile_opt::<D>(world.as_ref());
world.to_mut().set_is_compiling(false);
let exclude_html_warnings = if !is_html_compilation {

View file

@ -136,7 +136,7 @@ mod tests {
.expect("failed to resolve system universe");
let world = verse.snapshot();
let _res = typst::compile::<TypstPagedDocument>(&world);
let _res = typst_shim::compile_opt::<TypstPagedDocument>(&world);
}
static FONT_COMPUTED: AtomicBool = AtomicBool::new(false);

View file

@ -238,7 +238,7 @@ async fn trace_main(
req_id: RequestId,
) -> ! {
typst_timing::enable();
let res = typst::compile::<TypstPagedDocument>(g.world());
let res = typst_shim::compile_opt::<TypstPagedDocument>(g.world());
let diags = match &res.output {
Ok(_res) => res.warnings,
Err(errors) => errors.clone(),

View file

@ -436,6 +436,7 @@ impl Typlite {
format: Format,
world: Arc<LspWorld>,
) -> tinymist_std::Result<MarkdownDocument> {
// this is not affected by syntax-only mode (typst_shim::compile_opt)
let compiled = typst::compile(&world);
let collector = WarningCollector::default();
collector.extend(

View file

@ -278,6 +278,7 @@ impl HtmlToAstParser {
)
.unwrap();
// this is not affected by syntax-only mode (typst_shim::compile_opt)
let compiled = typst::compile(&world);
self.warnings.extend(compiled.warnings.iter().cloned());
let doc = match compiled.output {

View file

@ -1,5 +1,8 @@
//! # typst-shim
mod syntax_only;
pub use syntax_only::*;
pub use cfg_if::cfg_if;
cfg_if! {

View file

@ -0,0 +1,34 @@
use std::sync::atomic::{AtomicBool, Ordering};
use typst::{
Document, World,
diag::{SourceDiagnostic, SourceResult, Warned},
ecow::eco_vec,
};
use typst_syntax::Span;
/// Global flag indicating whether syntax-only mode is enabled.
pub static SYNTAX_ONLY: AtomicBool = AtomicBool::new(false);
/// Check if syntax-only mode is enabled.
pub fn is_syntax_only() -> bool {
SYNTAX_ONLY.load(Ordering::Relaxed)
}
/// Compile the document if syntax-only mode is disabled; otherwise, return an
/// error.
pub fn compile_opt<D>(world: &dyn World) -> Warned<SourceResult<D>>
where
D: Document,
{
if is_syntax_only() {
Warned {
output: Err(eco_vec![SourceDiagnostic::error(
Span::detached(),
"Compilation is disabled in syntax-only mode.",
)]),
warnings: Default::default(),
}
} else {
typst::compile::<D>(world)
}
}

View file

@ -14,7 +14,7 @@ Given a file, tinymist will try to discover tests related to the file.
- All dependent files in the same workspace will be checked.
- For example, if file `a.typ` contains `import "b.typ"` or `include "b.typ"`, tinymist will check `b.typ` for tests as well.
- For each file including the entry file itself, tinymist will check the file for tests.
- If a file is named `example-*.typ`, it is considered an *example document* and will be compiled using `typst::compile`.
- If a file is named `example-*.typ`, it is considered an *example document* and will be compiled using `typst_shim::compile_opt`.
- Both png export and html export may be called.
- For now, png export is always called for each example file.
- If the label `<test-html-example>` can be found in the example file, html export will be called.

View file

@ -1036,7 +1036,7 @@ en = "Configure whether to enable syntax-only mode for the language server. In s
zh = "配置是否为语言服务器启用仅语法模式。在仅语法模式下,语言服务器将仅提供语法检查和基本的代码补全,但不会执行完整的文档编译或代码分析。这对于提高低端设备的性能或处理大型文档时非常有用。"
[extension.tinymist.config.tinymist.lsp.syntaxOnly.string.enum.auto]
en = "Always enable syntax-only mode. The strategy may be changed in the future, for example, automatically enable syntax-only mode when the system is in power-saving mode."
en = "Always disable syntax-only mode. The strategy may be changed in the future, for example, automatically enable syntax-only mode when the system is in power-saving mode."
zh = "始终启用仅语法模式。未来可能会更改策略,例如,当系统处于省电模式时自动启用仅语法模式。"
[extension.tinymist.config.tinymist.lsp.syntaxOnly.string.enum.onPowerSaving]