Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default value of raw.theme to auto, and allow setting raw.theme to auto #4186

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions crates/typst/src/text/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ pub struct RawElem {
/// ````
#[parse(
let (theme_path, theme_data) = parse_theme(engine, args)?;
theme_path.map(Some)
theme_path
)]
#[borrowed]
pub theme: Option<EcoString>,
pub theme: Smart<EcoString>,

/// The raw file buffer of syntax theme file.
#[internal]
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Packed<RawElem> {
.unwrap()
});

let theme = theme.as_deref().unwrap_or(&RAW_THEME);
let theme = theme.as_ref().map(std::ops::Deref::deref).unwrap_or(&RAW_THEME);
let foreground = theme.settings.foreground.unwrap_or(synt::Color::BLACK);

let mut seq = vec![];
Expand Down Expand Up @@ -784,20 +784,27 @@ fn load_theme(path: &str, bytes: &Bytes) -> StrResult<Arc<synt::Theme>> {
fn parse_theme(
engine: &mut Engine,
args: &mut Args,
) -> SourceResult<(Option<EcoString>, Option<Bytes>)> {
let Some(Spanned { v: path, span }) = args.named::<Spanned<EcoString>>("theme")?
) -> SourceResult<(Option<Smart<EcoString>>, Option<Bytes>)> {
let Some(Spanned { v: path, span }) =
args.named::<Spanned<Smart<EcoString>>>("theme")?
else {
// Argument `theme` not found.
return Ok((None, None));
};

let Smart::Custom(path) = path else {
// Argument `theme` is `auto`.
return Ok((Some(Smart::Auto), None));
};

// Load theme file.
let id = span.resolve_path(&path).at(span)?;
let data = engine.world.file(id).at(span)?;

// Check that parsing works.
let _ = load_theme(&path, &data).at(span)?;

Ok((Some(path), Some(data)))
Ok((Some(Smart::Custom(path)), Some(data)))
}

/// The syntect syntax definitions.
Expand Down
Binary file added tests/ref/raw-theme-set-to-auto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/suite/text/raw.typ
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,21 @@ fn main() {
`code`
```

--- raw-theme-set-to-auto ---
```typ
#let hi = "Hello World"
```

#set raw(theme: "/assets/themes/halcyon.tmTheme")
```typ
#let hi = "Hello World"
```

#set raw(theme: auto)
```typ
#let hi = "Hello World"
```

--- raw-unclosed ---
// Test unterminated raw text.
//
Expand Down