Skip to content

Commit

Permalink
Rename syntax_highlighting to better_syntax_highlighting
Browse files Browse the repository at this point in the history
Basic syntax highlighting is enabled by default now, so the old feature name
was misleading.
  • Loading branch information
lampsitter committed Sep 21, 2023
1 parent a4470c2 commit 5ac8256
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

### Added

- Code blocks now use the syntax highlighting theme's caret and selection colors.
- Primitive syntax highlighting by default
- Code blocks now use the syntax highlighting theme's caret and selection colors while using the
`better_syntax_highlighting` feature.
- Image loading errors are now shown ([#8](https://github.com/lampsitter/egui_commonmark/pull/8) by [@emilk](https://github.com/emilk)).
- `CommonMarkCache` now implements `Debug` ([#7](https://github.com/lampsitter/egui_commonmark/pull/7) by [@ChristopherPerry6060](https://github.com/ChristopherPerry6060)).
- `CommonMarkCache::add_syntax_themes_from_folder`
Expand All @@ -15,6 +17,7 @@

- Use new image API from egui ([#11](https://github.com/lampsitter/egui_commonmark/pull/11) by [@jprochazk](https://github.com/jprochazk)).
- Updated dependencies.
- Feature `syntax_highlighting` has been renamed to `better_syntax_highlighting`.

### Removed

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ document-features = { version = "0.2", optional = true }

[features]
default = ["load-images"]
## Syntax highlighting for code blocks
syntax_highlighting = ["syntect"]
## Syntax highlighting for code blocks using syntect
better_syntax_highlighting = ["syntect"]

load-images = ["egui_extras/image", "egui_extras/file", "image/png"]

Expand All @@ -42,4 +42,4 @@ image = { version = "0.24", default-features = false, features = ["png"] }

[package.metadata.docs.rs]
# docs.rs build can fail with the fetch feature enabled
features = ["syntax_highlighting", "svg", "document-features"]
features = ["better_syntax_highlighting", "document-features"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CommonMarkViewer::new("viewer").show(ui, &mut cache, markdown);

## Features

* `syntax_highlighting`: Syntax highlighting inside code blocks with
* `better_syntax_highlighting`: Syntax highlighting inside code blocks with
[`syntect`](https://crates.io/crates/syntect)
* `svg`: Support for viewing svg images
* `fetch`: Images with urls will be downloaded and displayed
Expand Down
52 changes: 26 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::collections::HashMap;
use egui::{self, epaint, Id, NumExt, Pos2, RichText, Sense, TextStyle, Ui, Vec2};
use pulldown_cmark::{CowStr, HeadingLevel, Options};

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
use syntect::{
easy::HighlightLines,
highlighting::{Theme, ThemeSet},
Expand All @@ -46,10 +46,10 @@ struct ScrollableCache {
pub struct CommonMarkCache {
// Everything stored in `CommonMarkCache` must take into account that
// the cache is for multiple `CommonMarkviewer`s with different source_ids.
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
ps: SyntaxSet,

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
ts: ThemeSet,

link_hooks: HashMap<String, bool>,
Expand All @@ -58,7 +58,7 @@ pub struct CommonMarkCache {
has_installed_loaders: bool,
}

#[cfg(not(feature = "syntax_highlighting"))]
#[cfg(not(feature = "better_syntax_highlighting"))]
impl std::fmt::Debug for CommonMarkCache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommonMarkCache")
Expand All @@ -69,7 +69,7 @@ impl std::fmt::Debug for CommonMarkCache {
}
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
impl std::fmt::Debug for CommonMarkCache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommonMarkCache")
Expand All @@ -86,9 +86,9 @@ impl std::fmt::Debug for CommonMarkCache {
impl Default for CommonMarkCache {
fn default() -> Self {
Self {
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
ps: SyntaxSet::load_defaults_newlines(),
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
ts: ThemeSet::load_defaults(),
link_hooks: HashMap::new(),
scroll: Default::default(),
Expand All @@ -98,21 +98,21 @@ impl Default for CommonMarkCache {
}

impl CommonMarkCache {
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
pub fn add_syntax_from_folder(&mut self, path: &str) {
let mut builder = self.ps.clone().into_builder();
let _ = builder.add_from_folder(path, true);
self.ps = builder.build();
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
pub fn add_syntax_from_str(&mut self, s: &str, fallback_name: Option<&str>) {
let mut builder = self.ps.clone().into_builder();
let _ = SyntaxDefinition::load_from_str(s, true, fallback_name).map(|d| builder.add(d));
self.ps = builder.build();
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
/// Add more color themes for code blocks(.tmTheme files). Set the color theme with
/// [`syntax_theme_dark`](CommonMarkViewer::syntax_theme_dark) and
/// [`syntax_theme_light`](CommonMarkViewer::syntax_theme_light)
Expand All @@ -123,7 +123,7 @@ impl CommonMarkCache {
self.ts.add_from_folder(path)
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
/// Add color theme for code blocks(.tmTheme files). Set the color theme with
/// [`syntax_theme_dark`](CommonMarkViewer::syntax_theme_dark) and
/// [`syntax_theme_light`](CommonMarkViewer::syntax_theme_light)
Expand Down Expand Up @@ -191,7 +191,7 @@ impl CommonMarkCache {
}
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
fn curr_theme(&self, ui: &Ui, options: &CommonMarkOptions) -> &Theme {
self.ts
.themes
Expand Down Expand Up @@ -224,19 +224,19 @@ impl CommonMarkCache {
}
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
const DEFAULT_THEME_LIGHT: &str = "base16-ocean.light";
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
const DEFAULT_THEME_DARK: &str = "base16-ocean.dark";

struct CommonMarkOptions {
indentation_spaces: usize,
max_image_width: Option<usize>,
show_alt_text_on_hover: bool,
default_width: Option<usize>,
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
theme_light: String,
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
theme_dark: String,
use_explicit_uri_scheme: bool,
}
Expand All @@ -248,17 +248,17 @@ impl Default for CommonMarkOptions {
max_image_width: None,
show_alt_text_on_hover: true,
default_width: None,
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
theme_light: DEFAULT_THEME_LIGHT.to_owned(),
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
theme_dark: DEFAULT_THEME_DARK.to_owned(),
use_explicit_uri_scheme: false,
}
}
}

impl CommonMarkOptions {
#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
fn curr_theme(&self, ui: &Ui) -> &str {
if ui.style().visuals.dark_mode {
&self.theme_dark
Expand Down Expand Up @@ -317,22 +317,22 @@ impl CommonMarkViewer {
self
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
#[deprecated(note = "use `syntax_theme_light` or `syntax_theme_dark` instead")]
pub fn syntax_theme(mut self, theme: String) -> Self {
self.options.theme_light = theme.clone();
self.options.theme_dark = theme;
self
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
/// Set the syntax theme to be used inside code blocks in light mode
pub fn syntax_theme_light<S: Into<String>>(mut self, theme: S) -> Self {
self.options.theme_light = theme.into();
self
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
/// Set the syntax theme to be used inside code blocks in dark mode
pub fn syntax_theme_dark<S: Into<String>>(mut self, theme: S) -> Self {
self.options.theme_dark = theme.into();
Expand Down Expand Up @@ -981,7 +981,7 @@ impl CommonMarkViewerInternal {
}
}

#[cfg(not(feature = "syntax_highlighting"))]
#[cfg(not(feature = "better_syntax_highlighting"))]
impl CommonMarkViewerInternal {
fn pre_syntax_highlighting(
_cache: &mut CommonMarkCache,
Expand All @@ -1003,7 +1003,7 @@ impl CommonMarkViewerInternal {
}
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
impl CommonMarkViewerInternal {
fn pre_syntax_highlighting(
cache: &mut CommonMarkCache,
Expand Down Expand Up @@ -1071,7 +1071,7 @@ fn plain_highlighting(ui: &Ui, text: &str, extension: &str) -> egui::text::Layou
)
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
fn syntect_color_to_egui(color: syntect::highlighting::Color) -> egui::Color32 {
egui::Color32::from_rgb(color.r, color.g, color.b)
}
Expand Down Expand Up @@ -1146,7 +1146,7 @@ fn width_body_space(ui: &Ui) -> f32 {
ui.fonts(|f| f.glyph_width(&id, ' '))
}

#[cfg(feature = "syntax_highlighting")]
#[cfg(feature = "better_syntax_highlighting")]
fn default_theme(ui: &Ui) -> &str {
if ui.style().visuals.dark_mode {
DEFAULT_THEME_DARK
Expand Down

0 comments on commit 5ac8256

Please sign in to comment.