Skip to content
Open
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

Breaking changes

- Replace `syntect::parsing::SCOPE_REPO` with the `scope_repo` function exposing the same functionality

### Dependencies

- Replace `once_cell` with `std::sync::{LazyLock, OnceLock}`

## [Version 5.2.0](https://github.com/trishume/syntect/compare/v5.1.0...v5.2.0) (2024-02-07)

### Improvements
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fnv = { version = "1.0", optional = true }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
once_cell = "1.8"
thiserror = "1.0"

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions examples/syntest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::str::FromStr;
use std::sync::LazyLock;
use std::time::Instant;

use getopts::Options;
use once_cell::sync::Lazy;
use regex::Regex;
use walkdir::{DirEntry, WalkDir};

Expand All @@ -36,7 +36,7 @@ pub enum SyntaxTestFileResult {
Success(usize),
}

pub static SYNTAX_TEST_HEADER_PATTERN: Lazy<Regex> = Lazy::new(|| {
pub static SYNTAX_TEST_HEADER_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?xm)
^(?P<testtoken_start>\s*\S+)
Expand All @@ -47,7 +47,7 @@ pub static SYNTAX_TEST_HEADER_PATTERN: Lazy<Regex> = Lazy::new(|| {
)
.unwrap()
});
pub static SYNTAX_TEST_ASSERTION_PATTERN: Lazy<Regex> = Lazy::new(|| {
pub static SYNTAX_TEST_ASSERTION_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?xm)
\s*(?:
Expand Down
8 changes: 4 additions & 4 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::easy::{HighlightFile, HighlightLines};
use crate::escape::Escape;
use crate::highlighting::{Color, FontStyle, Style, Theme};
use crate::parsing::{
BasicScopeStackOp, ParseState, Scope, ScopeStack, ScopeStackOp, SyntaxReference, SyntaxSet,
SCOPE_REPO,
scope_repo, BasicScopeStackOp, ParseState, Scope, ScopeStack, ScopeStackOp, SyntaxReference,
SyntaxSet,
};
use crate::util::LinesWithEndings;
use crate::Error;
Expand Down Expand Up @@ -241,7 +241,7 @@ pub enum ClassStyle {
}

fn scope_to_classes(s: &mut String, scope: Scope, style: ClassStyle) {
let repo = SCOPE_REPO.lock().unwrap();
let repo = scope_repo().lock().unwrap();
for i in 0..(scope.len()) {
let atom = scope.atom_at(i as usize);
let atom_s = repo.atom_str(atom);
Expand All @@ -259,7 +259,7 @@ fn scope_to_classes(s: &mut String, scope: Scope, style: ClassStyle) {
}

fn scope_to_selector(s: &mut String, scope: Scope, style: ClassStyle) {
let repo = SCOPE_REPO.lock().unwrap();
let repo = scope_repo().lock().unwrap();
for i in 0..(scope.len()) {
let atom = scope.atom_at(i as usize);
let atom_s = repo.atom_str(atom);
Expand Down
8 changes: 4 additions & 4 deletions src/parsing/regex.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use once_cell::sync::OnceCell;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use std::error::Error;
use std::sync::OnceLock;

/// An abstraction for regex patterns.
///
Expand All @@ -11,7 +11,7 @@ use std::error::Error;
#[derive(Debug)]
pub struct Regex {
regex_str: String,
regex: OnceCell<regex_impl::Regex>,
regex: OnceLock<regex_impl::Regex>,
}

/// A region contains text positions for capture groups in a match result.
Expand All @@ -28,7 +28,7 @@ impl Regex {
pub fn new(regex_str: String) -> Self {
Self {
regex_str,
regex: OnceCell::new(),
regex: OnceLock::new(),
}
}

Expand Down Expand Up @@ -76,7 +76,7 @@ impl Clone for Regex {
fn clone(&self) -> Self {
Regex {
regex_str: self.regex_str.clone(),
regex: OnceCell::new(),
regex: OnceLock::new(),
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/parsing/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::collections::HashMap;
use std::fmt;
use std::mem;
use std::str::FromStr;
use std::sync::Mutex;
use std::sync::{Mutex, OnceLock};
use std::u16;
use std::u64;

use once_cell::sync::Lazy;
use serde::de::{Deserialize, Deserializer, Error, Visitor};
use serde::ser::{Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
Expand All @@ -29,11 +28,13 @@ pub const ATOM_LEN_BITS: u16 = 3;

/// The global scope repo, exposed in case you want to minimize locking and unlocking.
///
/// Ths shouldn't be necessary for you to use. See the [`ScopeRepository`] docs.
/// This shouldn't be necessary for you to use. See the [`ScopeRepository`] docs.
///
/// [`ScopeRepository`]: struct.ScopeRepository.html
pub static SCOPE_REPO: Lazy<Mutex<ScopeRepository>> =
Lazy::new(|| Mutex::new(ScopeRepository::new()));
pub fn scope_repo() -> &'static Mutex<ScopeRepository> {
static SCOPE_REPO: OnceLock<Mutex<ScopeRepository>> = OnceLock::new();
SCOPE_REPO.get_or_init(|| Mutex::new(ScopeRepository::new()))
}

/// A hierarchy of atoms with semi-standardized names used to accord semantic information to a
/// specific piece of text.
Expand Down Expand Up @@ -73,12 +74,11 @@ pub enum ParseScopeError {
/// The structure used to keep track of the mapping between scope atom numbers and their string
/// names
///
/// It is only exposed in case you want to lock [`SCOPE_REPO`] and then allocate a bunch of scopes
/// It is only exposed in case you want to lock [`scope_repo()`] and then allocate a bunch of scopes
/// at once without thrashing the lock. In general, you should just use [`Scope::new()`].
///
/// Only [`Scope`]s created by the same repository have valid comparison results.
///
/// [`SCOPE_REPO`]: struct.SCOPE_REPO.html
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function should get auto-linked, so I dropped this explicit link, but lmk if that's wrong for some reason

/// [`Scope::new()`]: struct.Scope.html#method.new
/// [`Scope`]: struct.Scope.html
#[derive(Debug)]
Expand Down Expand Up @@ -223,7 +223,7 @@ impl Scope {
///
/// Example: `Scope::new("meta.rails.controller")`
pub fn new(s: &str) -> Result<Scope, ParseScopeError> {
let mut repo = SCOPE_REPO.lock().unwrap();
let mut repo = scope_repo().lock().unwrap();
repo.build(s.trim())
}

Expand Down Expand Up @@ -268,7 +268,7 @@ impl Scope {
///
/// This requires locking a global repo and shouldn't be done frequently.
pub fn build_string(self) -> String {
let repo = SCOPE_REPO.lock().unwrap();
let repo = scope_repo().lock().unwrap();
repo.to_string(self)
}

Expand Down
16 changes: 8 additions & 8 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::mem;
use std::path::Path;
use std::sync::OnceLock;

use super::regex::Regex;
use crate::parsing::syntax_definition::ContextId;
use once_cell::sync::OnceCell;
use serde_derive::{Deserialize, Serialize};

/// A syntax set holds multiple syntaxes that have been linked together.
Expand All @@ -34,8 +34,8 @@ pub struct SyntaxSet {
/// Stores the syntax index for every path that was loaded
path_syntaxes: Vec<(String, usize)>,

#[serde(skip_serializing, skip_deserializing, default = "OnceCell::new")]
first_line_cache: OnceCell<FirstLineCache>,
#[serde(skip_serializing, skip_deserializing, default = "OnceLock::new")]
first_line_cache: OnceLock<FirstLineCache>,
/// Metadata, e.g. indent and commenting information.
///
/// NOTE: if serializing, you should handle metadata manually; that is, you should serialize and
Expand All @@ -58,7 +58,7 @@ pub struct SyntaxReference {
#[serde(serialize_with = "ordered_map")]
pub variables: HashMap<String, String>,
#[serde(skip)]
pub(crate) lazy_contexts: OnceCell<LazyContexts>,
pub(crate) lazy_contexts: OnceLock<LazyContexts>,
pub(crate) serialized_lazy_contexts: Vec<u8>,
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl Clone for SyntaxSet {
syntaxes: self.syntaxes.clone(),
path_syntaxes: self.path_syntaxes.clone(),
// Will need to be re-initialized
first_line_cache: OnceCell::new(),
first_line_cache: OnceLock::new(),
#[cfg(feature = "metadata")]
metadata: self.metadata.clone(),
}
Expand All @@ -126,7 +126,7 @@ impl Default for SyntaxSet {
SyntaxSet {
syntaxes: Vec::new(),
path_syntaxes: Vec::new(),
first_line_cache: OnceCell::new(),
first_line_cache: OnceLock::new(),
#[cfg(feature = "metadata")]
metadata: Metadata::default(),
}
Expand Down Expand Up @@ -627,7 +627,7 @@ impl SyntaxSetBuilder {
first_line_match,
hidden,
variables,
lazy_contexts: OnceCell::new(),
lazy_contexts: OnceLock::new(),
serialized_lazy_contexts: Vec::new(), // initialized in the last step
};
syntaxes.push(syntax);
Expand Down Expand Up @@ -711,7 +711,7 @@ impl SyntaxSetBuilder {
SyntaxSet {
syntaxes,
path_syntaxes,
first_line_cache: OnceCell::new(),
first_line_cache: OnceLock::new(),
#[cfg(feature = "metadata")]
metadata,
}
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/yaml_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl SyntaxDefinition {
return Err(ParseSyntaxError::EmptyFile);
}
let doc = &docs[0];
let mut scope_repo = SCOPE_REPO.lock().unwrap();
let mut scope_repo = scope_repo().lock().unwrap();
SyntaxDefinition::parse_top_level(
doc,
scope_repo.deref_mut(),
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/public_api__public_api.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ impl core::marker::Unpin for syntect::parsing::SyntaxSetBuilder
impl core::panic::unwind_safe::RefUnwindSafe for syntect::parsing::SyntaxSetBuilder
impl core::panic::unwind_safe::UnwindSafe for syntect::parsing::SyntaxSetBuilder
pub const syntect::parsing::ATOM_LEN_BITS: u16 = 3u16
pub static syntect::parsing::SCOPE_REPO: once_cell::sync::Lazy<std::sync::mutex::Mutex<syntect::parsing::ScopeRepository>>
pub fn syntect::parsing::scope_repo() -> &'static std::sync::mutex::Mutex<syntect::parsing::ScopeRepository>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already mentioned in #575, but this is a breaking change 💥

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to break semver anyway, it is probably better to completely remove it from the public API? We can always add it back in a minor release if someone depends on it.

pub mod syntect::util
pub struct syntect::util::LinesWithEndings<'a>
impl<'a> syntect::util::LinesWithEndings<'a>
Expand Down