Skip to content

Commit

Permalink
feat: let tinymist::Config pull environment variables on start of s…
Browse files Browse the repository at this point in the history
…erver (#1390)

* feat: add `Config::new`

* feat: use `Config::new`

* fix: lose config if errors happens
  • Loading branch information
Myriad-Dreamin authored Feb 24, 2025
1 parent 99bc4ac commit dc34825
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions crates/tinymist/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::Parser;
use itertools::Itertools;
use lsp_types::*;
use once_cell::sync::{Lazy, OnceCell};
use reflexo::error::IgnoreLogging;
use reflexo_typst::{ImmutPath, TypstDict};
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value as JsonValue};
Expand Down Expand Up @@ -86,18 +87,12 @@ impl Initializer for RegularInit {
.into_iter()
.collect(),
};
let mut config = Config {
const_config: ConstConfig::from(&params),
compile: CompileConfig {
entry_resolver: EntryResolver {
roots,
..Default::default()
},
font_opts: std::mem::take(&mut self.font_opts),
..CompileConfig::default()
},
..Config::default()
};
let mut config = Config::new(
ConstConfig::from(&params),
roots,
std::mem::take(&mut self.font_opts),
);

let err = params.initialization_options.and_then(|init| {
config
.update(&init)
Expand Down Expand Up @@ -291,9 +286,11 @@ const CONFIG_ITEMS: &[&str] = &[
];
// endregion Configuration Items

// todo: Config::default() doesn't initialize arguments from environment
// variables
/// The user configuration read from the editor.
///
/// Note: `Config::default` is intentionally to be "pure" and not to be
/// affected by system environment variables.
/// To get the configuration with system defaults, use [`Config::new`] intead.
#[derive(Debug, Default, Clone)]
pub struct Config {
/// The resolution kind of the project.
Expand All @@ -320,6 +317,30 @@ pub struct Config {
}

impl Config {
/// Creates a new configuration with system defaults.
pub fn new(
const_config: ConstConfig,
roots: Vec<ImmutPath>,
font_opts: CompileFontArgs,
) -> Self {
let mut config = Self {
const_config,
compile: CompileConfig {
entry_resolver: EntryResolver {
roots,
..EntryResolver::default()
},
font_opts,
..CompileConfig::default()
},
..Self::default()
};
config
.update_by_map(&Map::default())
.log_error("failed to assign Config defaults");
config
}

/// Gets items for serialization.
pub fn get_items() -> Vec<ConfigurationItem> {
let sections = CONFIG_ITEMS
Expand Down

0 comments on commit dc34825

Please sign in to comment.