feat: remove lazy_static in favor of LazyLock
This switches to the built-in `std::sync::LazyLock`
This commit is contained in:
parent
f23a52f410
commit
48059b72a8
20 changed files with 234 additions and 215 deletions
|
|
@ -1,6 +1,5 @@
|
|||
use std::{collections::HashMap, env, fs, path::Path};
|
||||
use std::{collections::HashMap, env, fs, path::Path, sync::LazyLock};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use rand::Rng;
|
||||
use regex::Regex;
|
||||
use tree_sitter::{Language, Parser};
|
||||
|
|
@ -23,16 +22,27 @@ use crate::{
|
|||
test::{parse_tests, print_diff, print_diff_key, strip_sexp_fields, TestEntry},
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LOG_ENABLED: bool = env::var("TREE_SITTER_LOG").is_ok();
|
||||
pub static ref LOG_GRAPH_ENABLED: bool = env::var("TREE_SITTER_LOG_GRAPHS").is_ok();
|
||||
pub static ref LANGUAGE_FILTER: Option<String> = env::var("TREE_SITTER_LANGUAGE").ok();
|
||||
pub static ref EXAMPLE_INCLUDE: Option<Regex> = regex_env_var("TREE_SITTER_EXAMPLE_INCLUDE");
|
||||
pub static ref EXAMPLE_EXCLUDE: Option<Regex> = regex_env_var("TREE_SITTER_EXAMPLE_EXCLUDE");
|
||||
pub static ref START_SEED: usize = new_seed();
|
||||
pub static ref EDIT_COUNT: usize = int_env_var("TREE_SITTER_EDITS").unwrap_or(3);
|
||||
pub static ref ITERATION_COUNT: usize = int_env_var("TREE_SITTER_ITERATIONS").unwrap_or(10);
|
||||
}
|
||||
pub static LOG_ENABLED: LazyLock<bool> = LazyLock::new(|| env::var("TREE_SITTER_LOG").is_ok());
|
||||
|
||||
pub static LOG_GRAPH_ENABLED: LazyLock<bool> =
|
||||
LazyLock::new(|| env::var("TREE_SITTER_LOG_GRAPHS").is_ok());
|
||||
|
||||
pub static LANGUAGE_FILTER: LazyLock<Option<String>> =
|
||||
LazyLock::new(|| env::var("TREE_SITTER_LANGUAGE").ok());
|
||||
|
||||
pub static EXAMPLE_INCLUDE: LazyLock<Option<Regex>> =
|
||||
LazyLock::new(|| regex_env_var("TREE_SITTER_EXAMPLE_INCLUDE"));
|
||||
|
||||
pub static EXAMPLE_EXCLUDE: LazyLock<Option<Regex>> =
|
||||
LazyLock::new(|| regex_env_var("TREE_SITTER_EXAMPLE_EXCLUDE"));
|
||||
|
||||
pub static START_SEED: LazyLock<usize> = LazyLock::new(new_seed);
|
||||
|
||||
pub static EDIT_COUNT: LazyLock<usize> =
|
||||
LazyLock::new(|| int_env_var("TREE_SITTER_EDITS").unwrap_or(3));
|
||||
|
||||
pub static ITERATION_COUNT: LazyLock<usize> =
|
||||
LazyLock::new(|| int_env_var("TREE_SITTER_ITERATIONS").unwrap_or(10));
|
||||
|
||||
fn int_env_var(name: &'static str) -> Option<usize> {
|
||||
env::var(name).ok().and_then(|e| e.parse().ok())
|
||||
|
|
|
|||
|
|
@ -5,13 +5,12 @@ use std::{
|
|||
io::{self, Write as _},
|
||||
path::{self, Path, PathBuf},
|
||||
str,
|
||||
sync::{atomic::AtomicUsize, Arc},
|
||||
sync::{atomic::AtomicUsize, Arc, LazyLock},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use anstyle::{Ansi256Color, AnsiColor, Color, Effects, RgbColor};
|
||||
use anyhow::Result;
|
||||
use lazy_static::lazy_static;
|
||||
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde_json::{json, Value};
|
||||
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter, HtmlRenderer};
|
||||
|
|
@ -45,10 +44,8 @@ pub const HTML_FOOTER: &str = "
|
|||
</body>
|
||||
";
|
||||
|
||||
lazy_static! {
|
||||
static ref CSS_STYLES_BY_COLOR_ID: Vec<String> =
|
||||
serde_json::from_str(include_str!("../vendor/xterm-colors.json")).unwrap();
|
||||
}
|
||||
static CSS_STYLES_BY_COLOR_ID: LazyLock<Vec<String>> =
|
||||
LazyLock::new(|| serde_json::from_str(include_str!("../vendor/xterm-colors.json")).unwrap());
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Style {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
use std::{fs, path::Path};
|
||||
use std::{fs, path::Path, sync::LazyLock};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use bstr::{BStr, ByteSlice};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use tree_sitter::{Language, Parser, Point};
|
||||
|
||||
lazy_static! {
|
||||
static ref CAPTURE_NAME_REGEX: Regex = Regex::new("[\\w_\\-.]+").unwrap();
|
||||
}
|
||||
static CAPTURE_NAME_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("[\\w_\\-.]+").unwrap());
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Utf8Point {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use std::{
|
|||
io::{self, Write},
|
||||
path::{Path, PathBuf},
|
||||
str,
|
||||
sync::LazyLock,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
|
|
@ -13,7 +14,6 @@ use anstyle::{AnsiColor, Color, Style};
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::ValueEnum;
|
||||
use indoc::indoc;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::{
|
||||
bytes::{Regex as ByteRegex, RegexBuilder as ByteRegexBuilder},
|
||||
Regex,
|
||||
|
|
@ -25,29 +25,36 @@ use walkdir::WalkDir;
|
|||
use super::util;
|
||||
use crate::parse::Stats;
|
||||
|
||||
lazy_static! {
|
||||
static ref HEADER_REGEX: ByteRegex = ByteRegexBuilder::new(
|
||||
static HEADER_REGEX: LazyLock<ByteRegex> = LazyLock::new(|| {
|
||||
ByteRegexBuilder::new(
|
||||
r"^(?x)
|
||||
(?P<equals>(?:=+){3,})
|
||||
(?P<suffix1>[^=\r\n][^\r\n]*)?
|
||||
\r?\n
|
||||
(?P<test_name_and_markers>(?:([^=\r\n]|\s+:)[^\r\n]*\r?\n)+)
|
||||
===+
|
||||
(?P<suffix2>[^=\r\n][^\r\n]*)?\r?\n"
|
||||
(?P<suffix2>[^=\r\n][^\r\n]*)?\r?\n",
|
||||
)
|
||||
.multi_line(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
static ref DIVIDER_REGEX: ByteRegex =
|
||||
ByteRegexBuilder::new(r"^(?P<hyphens>(?:-+){3,})(?P<suffix>[^-\r\n][^\r\n]*)?\r?\n")
|
||||
.multi_line(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
static ref COMMENT_REGEX: Regex = Regex::new(r"(?m)^\s*;.*$").unwrap();
|
||||
static ref WHITESPACE_REGEX: Regex = Regex::new(r"\s+").unwrap();
|
||||
static ref SEXP_FIELD_REGEX: Regex = Regex::new(r" \w+: \(").unwrap();
|
||||
static ref POINT_REGEX: Regex = Regex::new(r"\s*\[\s*\d+\s*,\s*\d+\s*\]\s*").unwrap();
|
||||
}
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
static DIVIDER_REGEX: LazyLock<ByteRegex> = LazyLock::new(|| {
|
||||
ByteRegexBuilder::new(r"^(?P<hyphens>(?:-+){3,})(?P<suffix>[^-\r\n][^\r\n]*)?\r?\n")
|
||||
.multi_line(true)
|
||||
.build()
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
static COMMENT_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?m)^\s*;.*$").unwrap());
|
||||
|
||||
static WHITESPACE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
||||
|
||||
static SEXP_FIELD_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" \w+: \(").unwrap());
|
||||
|
||||
static POINT_REGEX: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"\s*\[\s*\d+\s*,\s*\d+\s*\]\s*").unwrap());
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum TestEntry {
|
||||
|
|
|
|||
|
|
@ -1,47 +1,63 @@
|
|||
lazy_static! {
|
||||
pub static ref ROOT_DIR: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap().to_owned();
|
||||
pub static ref FIXTURES_DIR: PathBuf = ROOT_DIR.join("test").join("fixtures");
|
||||
pub static ref HEADER_DIR: PathBuf = ROOT_DIR.join("lib").join("include");
|
||||
pub static ref GRAMMARS_DIR: PathBuf = ROOT_DIR.join("test").join("fixtures").join("grammars");
|
||||
pub static ref SCRATCH_BASE_DIR: PathBuf = {
|
||||
let result = ROOT_DIR.join("target").join("scratch");
|
||||
fs::create_dir_all(&result).unwrap();
|
||||
result
|
||||
};
|
||||
pub static ref WASM_DIR: PathBuf = ROOT_DIR.join("target").join("release");
|
||||
pub static ref SCRATCH_DIR: PathBuf = {
|
||||
// https://doc.rust-lang.org/reference/conditional-compilation.html
|
||||
let vendor = if cfg!(target_vendor = "apple") {
|
||||
"apple"
|
||||
} else if cfg!(target_vendor = "fortanix") {
|
||||
"fortanix"
|
||||
} else if cfg!(target_vendor = "pc") {
|
||||
"pc"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
let env = if cfg!(target_env = "gnu") {
|
||||
"gnu"
|
||||
} else if cfg!(target_env = "msvc") {
|
||||
"msvc"
|
||||
} else if cfg!(target_env = "musl") {
|
||||
"musl"
|
||||
} else if cfg!(target_env = "sgx") {
|
||||
"sgx"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
let endian = if cfg!(target_endian = "little") {
|
||||
"little"
|
||||
} else if cfg!(target_endian = "big") {
|
||||
"big"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
pub static ROOT_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_owned()
|
||||
});
|
||||
|
||||
let machine = format!("{}-{}-{vendor}-{env}-{endian}", std::env::consts::ARCH, std::env::consts::OS);
|
||||
let result = SCRATCH_BASE_DIR.join(machine);
|
||||
fs::create_dir_all(&result).unwrap();
|
||||
result
|
||||
pub static FIXTURES_DIR: LazyLock<PathBuf> =
|
||||
LazyLock::new(|| ROOT_DIR.join("test").join("fixtures"));
|
||||
|
||||
pub static HEADER_DIR: LazyLock<PathBuf> = LazyLock::new(|| ROOT_DIR.join("lib").join("include"));
|
||||
|
||||
pub static GRAMMARS_DIR: LazyLock<PathBuf> =
|
||||
LazyLock::new(|| ROOT_DIR.join("test").join("fixtures").join("grammars"));
|
||||
|
||||
pub static SCRATCH_BASE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
let result = ROOT_DIR.join("target").join("scratch");
|
||||
fs::create_dir_all(&result).unwrap();
|
||||
result
|
||||
});
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
pub static WASM_DIR: LazyLock<PathBuf> = LazyLock::new(|| ROOT_DIR.join("target").join("release"));
|
||||
|
||||
pub static SCRATCH_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
// https://doc.rust-lang.org/reference/conditional-compilation.html
|
||||
let vendor = if cfg!(target_vendor = "apple") {
|
||||
"apple"
|
||||
} else if cfg!(target_vendor = "fortanix") {
|
||||
"fortanix"
|
||||
} else if cfg!(target_vendor = "pc") {
|
||||
"pc"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
}
|
||||
let env = if cfg!(target_env = "gnu") {
|
||||
"gnu"
|
||||
} else if cfg!(target_env = "msvc") {
|
||||
"msvc"
|
||||
} else if cfg!(target_env = "musl") {
|
||||
"musl"
|
||||
} else if cfg!(target_env = "sgx") {
|
||||
"sgx"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
let endian = if cfg!(target_endian = "little") {
|
||||
"little"
|
||||
} else if cfg!(target_endian = "big") {
|
||||
"big"
|
||||
} else {
|
||||
"unknown"
|
||||
};
|
||||
|
||||
let machine = format!(
|
||||
"{}-{}-{vendor}-{env}-{endian}",
|
||||
std::env::consts::ARCH,
|
||||
std::env::consts::OS
|
||||
);
|
||||
let result = SCRATCH_BASE_DIR.join(machine);
|
||||
fs::create_dir_all(&result).unwrap();
|
||||
result
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
sync::LazyLock,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use lazy_static::lazy_static;
|
||||
use tree_sitter::Language;
|
||||
use tree_sitter_generate::{ALLOC_HEADER, ARRAY_HEADER};
|
||||
use tree_sitter_highlight::HighlightConfiguration;
|
||||
|
|
@ -13,15 +13,13 @@ use tree_sitter_tags::TagsConfiguration;
|
|||
|
||||
include!("./dirs.rs");
|
||||
|
||||
lazy_static! {
|
||||
static ref TEST_LOADER: Loader = {
|
||||
let mut loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
|
||||
if env::var("TREE_SITTER_GRAMMAR_DEBUG").is_ok() {
|
||||
loader.debug_build(true);
|
||||
}
|
||||
loader
|
||||
};
|
||||
}
|
||||
static TEST_LOADER: LazyLock<Loader> = LazyLock::new(|| {
|
||||
let mut loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
|
||||
if env::var("TREE_SITTER_GRAMMAR_DEBUG").is_ok() {
|
||||
loader.debug_build(true);
|
||||
}
|
||||
loader
|
||||
});
|
||||
|
||||
pub fn test_loader() -> &'static Loader {
|
||||
&TEST_LOADER
|
||||
|
|
|
|||
|
|
@ -3,31 +3,40 @@ use std::{
|
|||
fs,
|
||||
os::raw::c_char,
|
||||
ptr, slice, str,
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
LazyLock,
|
||||
},
|
||||
};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use tree_sitter_highlight::{
|
||||
c, Error, Highlight, HighlightConfiguration, HighlightEvent, Highlighter, HtmlRenderer,
|
||||
};
|
||||
|
||||
use super::helpers::fixtures::{get_highlight_config, get_language, get_language_queries_path};
|
||||
|
||||
lazy_static! {
|
||||
static ref JS_HIGHLIGHT: HighlightConfiguration =
|
||||
get_highlight_config("javascript", Some("injections.scm"), &HIGHLIGHT_NAMES);
|
||||
static ref JSDOC_HIGHLIGHT: HighlightConfiguration =
|
||||
get_highlight_config("jsdoc", None, &HIGHLIGHT_NAMES);
|
||||
static ref HTML_HIGHLIGHT: HighlightConfiguration =
|
||||
get_highlight_config("html", Some("injections.scm"), &HIGHLIGHT_NAMES);
|
||||
static ref EJS_HIGHLIGHT: HighlightConfiguration = get_highlight_config(
|
||||
static JS_HIGHLIGHT: LazyLock<HighlightConfiguration> =
|
||||
LazyLock::new(|| get_highlight_config("javascript", Some("injections.scm"), &HIGHLIGHT_NAMES));
|
||||
|
||||
static JSDOC_HIGHLIGHT: LazyLock<HighlightConfiguration> =
|
||||
LazyLock::new(|| get_highlight_config("jsdoc", None, &HIGHLIGHT_NAMES));
|
||||
|
||||
static HTML_HIGHLIGHT: LazyLock<HighlightConfiguration> =
|
||||
LazyLock::new(|| get_highlight_config("html", Some("injections.scm"), &HIGHLIGHT_NAMES));
|
||||
|
||||
static EJS_HIGHLIGHT: LazyLock<HighlightConfiguration> = LazyLock::new(|| {
|
||||
get_highlight_config(
|
||||
"embedded-template",
|
||||
Some("injections-ejs.scm"),
|
||||
&HIGHLIGHT_NAMES
|
||||
);
|
||||
static ref RUST_HIGHLIGHT: HighlightConfiguration =
|
||||
get_highlight_config("rust", Some("injections.scm"), &HIGHLIGHT_NAMES);
|
||||
static ref HIGHLIGHT_NAMES: Vec<String> = [
|
||||
&HIGHLIGHT_NAMES,
|
||||
)
|
||||
});
|
||||
|
||||
static RUST_HIGHLIGHT: LazyLock<HighlightConfiguration> =
|
||||
LazyLock::new(|| get_highlight_config("rust", Some("injections.scm"), &HIGHLIGHT_NAMES));
|
||||
|
||||
static HIGHLIGHT_NAMES: LazyLock<Vec<String>> = LazyLock::new(|| {
|
||||
[
|
||||
"attribute",
|
||||
"boolean",
|
||||
"carriage-return",
|
||||
|
|
@ -60,12 +69,15 @@ lazy_static! {
|
|||
.iter()
|
||||
.copied()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
static ref HTML_ATTRS: Vec<String> = HIGHLIGHT_NAMES
|
||||
.collect()
|
||||
});
|
||||
|
||||
static HTML_ATTRS: LazyLock<Vec<String>> = LazyLock::new(|| {
|
||||
HIGHLIGHT_NAMES
|
||||
.iter()
|
||||
.map(|s| format!("class={s}"))
|
||||
.collect();
|
||||
}
|
||||
.collect()
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn test_highlighting_javascript() {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use std::{env, fmt::Write};
|
||||
use std::{env, fmt::Write, sync::LazyLock};
|
||||
|
||||
use indoc::indoc;
|
||||
use lazy_static::lazy_static;
|
||||
use rand::{prelude::StdRng, SeedableRng};
|
||||
use streaming_iterator::StreamingIterator;
|
||||
use tree_sitter::{
|
||||
|
|
@ -22,9 +21,8 @@ use crate::tests::{
|
|||
ITERATION_COUNT,
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
static ref EXAMPLE_FILTER: Option<String> = env::var("TREE_SITTER_TEST_EXAMPLE_FILTER").ok();
|
||||
}
|
||||
static EXAMPLE_FILTER: LazyLock<Option<String>> =
|
||||
LazyLock::new(|| env::var("TREE_SITTER_TEST_EXAMPLE_FILTER").ok());
|
||||
|
||||
#[test]
|
||||
fn test_query_errors_on_invalid_syntax() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use std::fs;
|
||||
use std::{fs, sync::LazyLock};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use streaming_iterator::StreamingIterator;
|
||||
use tree_sitter::{
|
||||
wasmtime::Engine, Parser, Query, QueryCursor, WasmError, WasmErrorKind, WasmStore,
|
||||
|
|
@ -8,9 +7,7 @@ use tree_sitter::{
|
|||
|
||||
use crate::tests::helpers::{allocations, fixtures::WASM_DIR};
|
||||
|
||||
lazy_static! {
|
||||
static ref ENGINE: Engine = Engine::default();
|
||||
}
|
||||
static ENGINE: LazyLock<Engine> = LazyLock::new(Engine::default);
|
||||
|
||||
#[test]
|
||||
fn test_wasm_stdlib_symbols() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue