feat: pretty errors in docstrings (#1876)
Some checks are pending
tinymist::ci / E2E Tests (linux-x64 on ubuntu-latest) (push) Blocked by required conditions
tinymist::ci / Duplicate Actions Detection (push) Waiting to run
tinymist::ci / Check Clippy, Formatting, Completion, Documentation, and Tests (Linux) (push) Waiting to run
tinymist::ci / Check Minimum Rust version and Tests (Windows) (push) Waiting to run
tinymist::ci / E2E Tests (darwin-arm64 on macos-latest) (push) Blocked by required conditions
tinymist::ci / E2E Tests (linux-x64 on ubuntu-22.04) (push) Blocked by required conditions
tinymist::ci / E2E Tests (win32-x64 on windows-2022) (push) Blocked by required conditions
tinymist::ci / E2E Tests (win32-x64 on windows-latest) (push) Blocked by required conditions
tinymist::ci / prepare-build (push) Waiting to run
tinymist::ci / build-binary (push) Blocked by required conditions
tinymist::ci / build-vsc-assets (push) Blocked by required conditions
tinymist::ci / build-vscode (push) Blocked by required conditions
tinymist::ci / build-vscode-others (push) Blocked by required conditions
tinymist::ci / publish-vscode (push) Blocked by required conditions
tinymist::gh_pages / build-gh-pages (push) Waiting to run

* feat: print doc errors

* fix: test errors on windows

* fix: tests on windows again

* fix: tests on windows again 2

* Revert "fix: tests on windows again 2"

This reverts commit 63973dcc1f.

* fix: tests on windows again 3
This commit is contained in:
Myriad-Dreamin 2025-07-06 19:40:02 +08:00 committed by GitHub
parent 9787432029
commit 4426b31ed7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 488 additions and 132 deletions

View file

@ -3,9 +3,12 @@ use std::sync::Arc;
use ecow::{eco_format, EcoString};
use tinymist_std::path::unix_slash;
use tinymist_world::system::print_diagnostics_to_string;
use tinymist_world::vfs::WorkspaceResolver;
use tinymist_world::{EntryReader, EntryState, ShadowApi, TaskInputs};
use typlite::TypliteFeat;
use tinymist_world::{
DiagnosticFormat, EntryReader, EntryState, ShadowApi, SourceWorld, TaskInputs,
};
use typlite::{Format, TypliteFeat};
use typst::diag::StrResult;
use typst::foundations::Bytes;
use typst::syntax::FileId;
@ -19,7 +22,7 @@ pub(crate) fn convert_docs(
source_fid: Option<FileId>,
) -> StrResult<EcoString> {
let mut entry = ctx.world.entry_state();
let (contextual_content, import_context) = if let Some(fid) = source_fid {
let import_context = source_fid.map(|fid| {
let root = ctx
.world
.vfs()
@ -42,16 +45,15 @@ pub(crate) fn convert_docs(
"#import {:?}: *",
unix_slash(fid.vpath().as_rooted_path())
));
let imports = imports.join("\n");
let content_with_import: String = if !imports.is_empty() {
format!("{imports}\n\n{content}")
} else {
content.to_owned()
};
(content_with_import, Some(imports))
} else {
(content.to_owned(), None)
imports.join("; ")
});
let feat = TypliteFeat {
color_theme: Some(ctx.analysis.color_theme),
annotate_elem: true,
soft_error: true,
remove_html: ctx.analysis.remove_html,
import_context,
..Default::default()
};
let entry = entry.select_in_workspace(Path::new("__tinymist_docs__.typ"));
@ -61,21 +63,39 @@ pub(crate) fn convert_docs(
inputs: None,
});
w.map_shadow_by_id(w.main(), Bytes::from_string(contextual_content))?;
// todo: bad performance: content.to_owned()
w.map_shadow_by_id(w.main(), Bytes::from_string(content.to_owned()))?;
// todo: bad performance
w.take_db();
let w = feat
.prepare_world(&w, Format::Md)
.map_err(|e| eco_format!("failed to prepare world: {e}"))?;
let conv = typlite::Typlite::new(Arc::new(w))
.with_feature(TypliteFeat {
color_theme: Some(ctx.analysis.color_theme),
annotate_elem: true,
soft_error: true,
remove_html: ctx.analysis.remove_html,
import_context,
..Default::default()
})
.convert()
.map_err(|err| eco_format!("failed to convert to markdown: {err}"))?;
let w = Arc::new(w);
let res = typlite::Typlite::new(w.clone())
.with_feature(feat)
.convert();
let conv = print_diag_or_error(w.as_ref(), res)?;
Ok(conv.replace("```example", "```typ"))
}
fn print_diag_or_error<T>(
world: &impl SourceWorld,
result: tinymist_std::Result<T>,
) -> StrResult<T> {
match result {
Ok(v) => Ok(v),
Err(err) => {
if let Some(diagnostics) = err.diagnostics() {
return Err(print_diagnostics_to_string(
world,
diagnostics.iter(),
DiagnosticFormat::Human,
)?);
}
Err(eco_format!("failed to convert docs: {err}"))
}
}
}