Skip to content

Allow for loading a single default theme #478

@CosmicHorrorDev

Description

@CosmicHorrorDev

Motivation

I have a project where we allow users to pick one of the builtin syntect themes as the syntax highlighter. Only one theme will be used for the whole execution of the program, so to get that theme we have some code that's something along the lines of

use syntect::highlighting::{Theme as SyntectTheme, ThemeSet as SyntectThemeSet};

/// These represent the theme that the user selects
pub enum ThemeDefaults {
    Base16OceanDark,
    Base16EightiesDark,
    Base16MochaDark,
    Base16OceanLight,
    InspiredGithub,
    SolarizedDark,
    SolarizedLight,
}

impl ThemeDefaults {
    pub fn as_syntect_name(self) -> &'static str {
        match self {
            Self::Base16OceanDark => "base16-ocean.dark",
            Self::Base16EightiesDark => "base16-eighties.dark",
            Self::Base16MochaDark => "base16-mocha.dark",
            Self::Base16OceanLight => "base16-ocean.light",
            Self::InspiredGithub => "InspiredGitHub",
            Self::SolarizedDark => "Solarized (dark)",
            Self::SolarizedLight => "Solarized (light)",
        }
    }
}

impl From<ThemeDefaults> for SyntectTheme {
    fn from(default: ThemeDefaults) -> Self {
        let mut default_themes = SyntectThemeSet::load_defaults();
        default_themes
            .themes
            .remove(default.as_syntect_name())
            .expect("Included with defaults")
    }
}

It would be nice if syntect exposed a way to do something like the above with a Theme::load_default(ThemeDefaults) method that allowed for loading a single theme. I see that the current implementation compresses all of the themes together which makes sense for trying to reduce binary size. This means that loading a single theme will still have to decompress and deserialize everything, but this should still be more ergonomic when the user only wants to load a single theme (which I would assume is the common case)

I'd be happy to open a PR if this seems like a reasonable addition

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions