refactor: clean up the config crate (#1528)

* feat: clean up config crates

* refactor: reorder config items

* refactor: a bit
This commit is contained in:
Myriad-Dreamin 2025-03-17 20:04:25 +08:00 committed by GitHub
parent 6a5bea8bcf
commit c67b2020e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 391 additions and 459 deletions

View file

@ -20,7 +20,7 @@ fn load_embedded() {
fn load_system() { fn load_system() {
let config = Config::default(); let config = Config::default();
let _fonts = config.compile.determine_fonts(); let _fonts = config.fonts();
} }
/* /*

View file

@ -69,7 +69,7 @@ impl ServerState {
.map_err(|e| invalid_params(format!("Cannot parse creation timestamp: {e}")))?, .map_err(|e| invalid_params(format!("Cannot parse creation timestamp: {e}")))?,
) )
} else { } else {
self.config.compile.determine_creation_timestamp() self.config.creation_timestamp()
}; };
let export = self.config.export_task(); let export = self.config.export_task();

File diff suppressed because it is too large Load diff

View file

@ -136,7 +136,7 @@ impl ServerState {
pub fn focus_main_file(&mut self, new_entry: Option<ImmutPath>) -> Result<bool> { pub fn focus_main_file(&mut self, new_entry: Option<ImmutPath>) -> Result<bool> {
if self.pinning_by_user if self.pinning_by_user
|| (self.pinning_by_preview && !self.pinning_by_browsing_preview) || (self.pinning_by_preview && !self.pinning_by_browsing_preview)
|| self.config.compile.has_default_entry_path || self.config.has_default_entry_path
{ {
self.focusing = new_entry; self.focusing = new_entry;
return Ok(false); return Ok(false);
@ -207,7 +207,7 @@ impl ServerState {
pub(crate) fn resolve_task(&mut self, path: ImmutPath) -> TaskInputs { pub(crate) fn resolve_task(&mut self, path: ImmutPath) -> TaskInputs {
let proj_input = matches!( let proj_input = matches!(
self.config.project_resolution, self.entry_resolver().project_resolution,
ProjectResolutionKind::LockDatabase ProjectResolutionKind::LockDatabase
) )
.then(|| { .then(|| {

View file

@ -139,8 +139,8 @@ impl ServerState {
self.change_export_config(new_export_config); self.change_export_config(new_export_config);
} }
if old_config.compile.primary_opts() != self.config.compile.primary_opts() { if old_config.primary_opts() != self.config.primary_opts() {
self.config.compile.fonts = OnceCell::new(); // todo: don't reload fonts if not changed self.config.fonts = OnceCell::new(); // todo: don't reload fonts if not changed
self.reload_projects() self.reload_projects()
.log_error("could not restart primary"); .log_error("could not restart primary");
} }

View file

@ -22,9 +22,7 @@ use tinymist::tool::project::{
compile_main, coverage_main, generate_script_main, project_main, task_main, compile_main, coverage_main, generate_script_main, project_main, task_main,
}; };
use tinymist::world::TaskInputs; use tinymist::world::TaskInputs;
use tinymist::{ use tinymist::{Config, DapRegularInit, RegularInit, ServerState, SuperInit, UserActionTask};
CompileConfig, Config, DapRegularInit, RegularInit, ServerState, SuperInit, UserActionTask,
};
use tinymist_core::LONG_VERSION; use tinymist_core::LONG_VERSION;
use tinymist_project::EntryResolver; use tinymist_project::EntryResolver;
use tinymist_query::package::PackageInfo; use tinymist_query::package::PackageInfo;
@ -205,14 +203,11 @@ pub fn trace_lsp_main(args: TraceLspArgs) -> Result<()> {
let client = client_root.weak(); let client = client_root.weak();
let roots = vec![ImmutPath::from(root_path)]; let roots = vec![ImmutPath::from(root_path)];
let config = Config { let config = Config {
compile: CompileConfig { entry_resolver: EntryResolver {
entry_resolver: EntryResolver { roots,
roots, ..EntryResolver::default()
..EntryResolver::default()
},
font_opts: args.compile.font,
..CompileConfig::default()
}, },
font_opts: args.compile.font,
..Config::default() ..Config::default()
}; };

View file

@ -123,7 +123,7 @@ impl ServerState {
dedicate: &str, dedicate: &str,
entry: Option<ImmutPath>, entry: Option<ImmutPath>,
) -> Result<ProjectInsId> { ) -> Result<ProjectInsId> {
let entry = self.config.compile.entry_resolver.resolve(entry); let entry = self.config.entry_resolver.resolve(entry);
let enable_html = matches!(self.config.export_target, ExportTarget::Html); let enable_html = matches!(self.config.export_target, ExportTarget::Html);
self.project.restart_dedicate(dedicate, entry, enable_html) self.project.restart_dedicate(dedicate, entry, enable_html)
} }
@ -145,7 +145,7 @@ impl ServerState {
); );
// Create the compile handler for client consuming results. // Create the compile handler for client consuming results.
let periscope_args = config.compile.periscope_args.clone(); let periscope_args = config.periscope_args.clone();
let handle = Arc::new(CompileHandlerImpl { let handle = Arc::new(CompileHandlerImpl {
#[cfg(feature = "preview")] #[cfg(feature = "preview")]
preview, preview,
@ -159,7 +159,7 @@ impl ServerState {
allow_multiline_token: const_config.tokens_multiline_token_support, allow_multiline_token: const_config.tokens_multiline_token_support,
remove_html: !config.support_html_in_markdown, remove_html: !config.support_html_in_markdown,
completion_feat: config.completion.clone(), completion_feat: config.completion.clone(),
color_theme: match config.compile.color_theme.as_deref() { color_theme: match config.color_theme.as_deref() {
Some("dark") => tinymist_query::ColorTheme::Dark, Some("dark") => tinymist_query::ColorTheme::Dark,
_ => tinymist_query::ColorTheme::Light, _ => tinymist_query::ColorTheme::Light,
}, },
@ -179,15 +179,15 @@ impl ServerState {
}); });
let export_target = config.export_target; let export_target = config.export_target;
let default_path = config.compile.entry_resolver.resolve_default(); let default_path = config.entry_resolver.resolve_default();
let entry = config.compile.entry_resolver.resolve(default_path); let entry = config.entry_resolver.resolve(default_path);
let inputs = config.compile.determine_inputs(); let inputs = config.inputs();
let cert_path = config.compile.determine_certification_path(); let cert_path = config.certification_path();
let package = config.compile.determine_package_opts(); let package = config.package_opts();
log::info!("ServerState: creating ProjectState, entry: {entry:?}, inputs: {inputs:?}"); log::info!("ServerState: creating ProjectState, entry: {entry:?}, inputs: {inputs:?}");
let fonts = config.compile.determine_fonts(); let fonts = config.fonts();
let package_registry = let package_registry =
LspUniverseBuilder::resolve_package(cert_path.clone(), Some(&package)); LspUniverseBuilder::resolve_package(cert_path.clone(), Some(&package));
let verse = let verse =

View file

@ -141,14 +141,9 @@ impl ServerState {
&self.config.const_config &self.config.const_config
} }
/// Gets the compile configuration.
pub fn compile_config(&self) -> &CompileConfig {
&self.config.compile
}
/// Gets the entry resolver. /// Gets the entry resolver.
pub fn entry_resolver(&self) -> &EntryResolver { pub fn entry_resolver(&self) -> &EntryResolver {
&self.compile_config().entry_resolver &self.config.entry_resolver
} }
/// Whether the main file is pinning. /// Whether the main file is pinning.
@ -162,7 +157,7 @@ impl ServerState {
/// The entry point for the language server. /// The entry point for the language server.
pub fn main(client: TypedLspClient<Self>, config: Config, start: bool) -> Self { pub fn main(client: TypedLspClient<Self>, config: Config, start: bool) -> Self {
log::info!("LanguageState: initialized with config {config:?}"); log::info!("ServerState: initialized with config {config:?}");
// Bootstrap server // Bootstrap server
let (editor_tx, editor_rx) = mpsc::unbounded_channel(); let (editor_tx, editor_rx) = mpsc::unbounded_channel();
@ -173,7 +168,7 @@ impl ServerState {
let editor_actor = EditorActor::new( let editor_actor = EditorActor::new(
client.clone().to_untyped(), client.clone().to_untyped(),
editor_rx, editor_rx,
service.config.compile.notify_status, service.config.notify_status,
); );
service service
@ -393,7 +388,7 @@ impl ServerState {
pub fn on_export(&mut self, req: OnExportRequest) -> QueryFuture { pub fn on_export(&mut self, req: OnExportRequest) -> QueryFuture {
let OnExportRequest { path, task, open } = req; let OnExportRequest { path, task, open } = req;
let entry = self.entry_resolver().resolve(Some(path.as_path().into())); let entry = self.entry_resolver().resolve(Some(path.as_path().into()));
let lock_dir = self.compile_config().entry_resolver.resolve_lock(&entry); let lock_dir = self.entry_resolver().resolve_lock(&entry);
let update_dep = lock_dir.clone().map(|lock_dir| { let update_dep = lock_dir.clone().map(|lock_dir| {
|snap: LspCompileSnapshot| async move { |snap: LspCompileSnapshot| async move {

View file

@ -52,7 +52,3 @@ pub fn try_<T>(f: impl FnOnce() -> Option<T>) -> Option<T> {
pub fn try_or<T>(f: impl FnOnce() -> Option<T>, default: T) -> T { pub fn try_or<T>(f: impl FnOnce() -> Option<T>, default: T) -> T {
f().unwrap_or(default) f().unwrap_or(default)
} }
pub fn try_or_default<T: Default>(f: impl FnOnce() -> Option<T>) -> T {
f().unwrap_or_default()
}

View file

@ -30,7 +30,7 @@ There are three servers in the `server` directory:
- `trace` provides the trace server (profiling typst programs), initialized by `trace_lsp_main` in `main.rs`. - `trace` provides the trace server (profiling typst programs), initialized by `trace_lsp_main` in `main.rs`.
- `preview` provides a `typst-preview` compatible preview server, initialized by `preview_main` in `tool/preview.rs`. - `preview` provides a `typst-preview` compatible preview server, initialized by `preview_main` in `tool/preview.rs`.
The long-running servers are contributed by the `LanguageState` in the `server.rs` file. The long-running servers are contributed by the `ServerState` in the `server.rs` file.
They will bootstrap actors in the `actor` directory and start tasks in the `task` directory. They will bootstrap actors in the `actor` directory and start tasks in the `task` directory.