Skip to content

Commit

Permalink
Merge pull request #528 from syphar/rustfmt
Browse files Browse the repository at this point in the history
add cargo fmt to CI, run initial format
  • Loading branch information
Enselic committed Apr 5, 2024
2 parents bceb076 + 67a5542 commit 53413d5
Show file tree
Hide file tree
Showing 30 changed files with 2,052 additions and 1,133 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
@@ -0,0 +1,2 @@
# initial cargo fmt
cc41c3803b20b79147fa606f950658bc12e50dc2
6 changes: 5 additions & 1 deletion .github/workflows/CI.yml
Expand Up @@ -20,7 +20,11 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable minus 2 releases # MSRV policy = last three versions of stable
components: clippy
components: clippy, rustfmt

- name: Run cargo fmt --check
run: cargo fmt -- --check

- name: Run cargo clippy
run: |
# Must run before `cargo check` until we use Rust 1.52
Expand Down
9 changes: 7 additions & 2 deletions benches/highlight_utils/mod.rs
@@ -1,9 +1,14 @@
use syntect::easy::HighlightLines;
use syntect::highlighting::{Theme};
use syntect::highlighting::Theme;
use syntect::parsing::{SyntaxReference, SyntaxSet};

/// Common helper for benchmarking highlighting.
pub fn do_highlight(s: &str, syntax_set: &SyntaxSet, syntax: &SyntaxReference, theme: &Theme) -> usize {
pub fn do_highlight(
s: &str,
syntax_set: &SyntaxSet,
syntax: &SyntaxReference,
theme: &Theme,
) -> usize {
let mut h = HighlightLines::new(syntax, theme);
let mut count = 0;
for line in s.lines() {
Expand Down
20 changes: 7 additions & 13 deletions benches/highlighting.rs
@@ -1,8 +1,8 @@
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
use syntect::parsing::{SyntaxSet, ScopeStack};
use syntect::highlighting::{ThemeSet};
use syntect::html::highlighted_html_for_string;
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use std::str::FromStr;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::{ScopeStack, SyntaxSet};

mod highlight_utils;
mod utils;
Expand All @@ -17,18 +17,14 @@ fn highlight_file(b: &mut Bencher, file: &str) {
let syntax = ss.find_syntax_for_file(path).unwrap().unwrap();
let s = std::fs::read_to_string(path).unwrap();

b.iter(|| {
highlight_utils::do_highlight(&s, &ss, syntax, &ts.themes["base16-ocean.dark"])
});
b.iter(|| highlight_utils::do_highlight(&s, &ss, syntax, &ts.themes["base16-ocean.dark"]));
}

fn stack_matching(b: &mut Bencher) {
let s = "source.js meta.group.js meta.group.js meta.block.js meta.function-call.method.js meta.group.js meta.object-literal.js meta.block.js meta.function-call.method.js meta.group.js variable.other.readwrite.js";
let stack = ScopeStack::from_str(s).unwrap();
let selector = ScopeStack::from_str("source meta.function-call.method").unwrap();
b.iter(|| {
selector.does_match(stack.as_slice())
});
b.iter(|| selector.does_match(stack.as_slice()));
}

fn highlight_html(b: &mut Bencher) {
Expand All @@ -39,9 +35,7 @@ fn highlight_html(b: &mut Bencher) {
let syntax = ss.find_syntax_for_file(path).unwrap().unwrap();
let s = std::fs::read_to_string(path).unwrap();

b.iter(|| {
highlighted_html_for_string(&s, &ss, syntax, &ts.themes["base16-ocean.dark"])
});
b.iter(|| highlighted_html_for_string(&s, &ss, syntax, &ts.themes["base16-ocean.dark"]));
}

fn highlighting_benchmark(c: &mut Criterion) {
Expand Down
2 changes: 1 addition & 1 deletion benches/load_and_highlight.rs
Expand Up @@ -25,7 +25,7 @@ fn load_and_highlight_benchmark(c: &mut criterion::Criterion) {
"highlight_test.erb",
"InspiredGitHub.tmTheme",
"Ruby.sublime-syntax",
"parser.rs"
"parser.rs",
] {
group.bench_with_input(format!("\"{}\"", input), input, |b, s| run(b, s));
}
Expand Down
21 changes: 8 additions & 13 deletions benches/loading.rs
@@ -1,24 +1,17 @@
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
use syntect::parsing::{SyntaxSet, SyntaxSetBuilder};
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use syntect::highlighting::ThemeSet;

use syntect::parsing::{SyntaxSet, SyntaxSetBuilder};

fn bench_load_internal_dump(b: &mut Bencher) {
b.iter(|| {
SyntaxSet::load_defaults_newlines()
});
b.iter(|| SyntaxSet::load_defaults_newlines());
}

fn bench_load_internal_themes(b: &mut Bencher) {
b.iter(|| {
ThemeSet::load_defaults()
});
b.iter(|| ThemeSet::load_defaults());
}

fn bench_load_theme(b: &mut Bencher) {
b.iter(|| {
ThemeSet::get_theme("testdata/spacegray/base16-ocean.dark.tmTheme")
});
b.iter(|| ThemeSet::get_theme("testdata/spacegray/base16-ocean.dark.tmTheme"));
}

fn bench_add_from_folder(b: &mut Bencher) {
Expand All @@ -38,7 +31,9 @@ fn bench_link_syntaxes(b: &mut Bencher) {

fn bench_from_dump_file(b: &mut Bencher) {
b.iter(|| {
let _: SyntaxSet = syntect::dumps::from_uncompressed_dump_file("assets/default_newlines.packdump").unwrap();
let _: SyntaxSet =
syntect::dumps::from_uncompressed_dump_file("assets/default_newlines.packdump")
.unwrap();
})
}

Expand Down
2 changes: 1 addition & 1 deletion benches/parsing.rs
@@ -1,4 +1,4 @@
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use std::time::Duration;
use syntect::parsing::{ParseState, SyntaxReference, SyntaxSet};

Expand Down
36 changes: 21 additions & 15 deletions examples/gendata.rs
Expand Up @@ -5,29 +5,32 @@
//!
//! An example of how this script is used to generate the pack files included
//! with syntect can be found under `make packs` in the Makefile.
use syntect::parsing::SyntaxSetBuilder;
use syntect::highlighting::ThemeSet;
use syntect::dumps::*;
use std::env;
use syntect::dumps::*;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSetBuilder;

fn usage_and_exit() -> ! {
println!("USAGE: gendata synpack source-dir \
println!(
"USAGE: gendata synpack source-dir \
newlines.packdump nonewlines.packdump \
[metadata.packdump] [metadata extra-source-dir]\n \
gendata themepack source-dir themepack.themedump");
gendata themepack source-dir themepack.themedump"
);
::std::process::exit(2);
}

fn main() {
let mut a = env::args().skip(1);
match (a.next(), a.next(), a.next(), a.next(), a.next(), a.next()) {
(Some(ref cmd),
Some(ref package_dir),
Some(ref packpath_newlines),
Some(ref packpath_nonewlines),
ref _option_metapath,
ref _option_metasource,
) if cmd == "synpack" => {
(
Some(ref cmd),
Some(ref package_dir),
Some(ref packpath_newlines),
Some(ref packpath_nonewlines),
ref _option_metapath,
ref _option_metasource,
) if cmd == "synpack" => {
let mut builder = SyntaxSetBuilder::new();
builder.add_plain_text_syntax();
builder.add_from_folder(package_dir, true).unwrap();
Expand All @@ -36,12 +39,16 @@ fn main() {

let mut builder_nonewlines = SyntaxSetBuilder::new();
builder_nonewlines.add_plain_text_syntax();
builder_nonewlines.add_from_folder(package_dir, false).unwrap();
builder_nonewlines
.add_from_folder(package_dir, false)
.unwrap();

#[cfg(feature = "metadata")]
{
if let Some(metasource) = _option_metasource {
builder_nonewlines.add_from_folder(metasource, false).unwrap();
builder_nonewlines
.add_from_folder(metasource, false)
.unwrap();
}
}

Expand All @@ -54,7 +61,6 @@ fn main() {
dump_to_file(&ss_nonewlines.metadata(), metapath).unwrap();
}
}

}
(Some(ref s), Some(ref theme_dir), Some(ref packpath), ..) if s == "themepack" => {
let ts = ThemeSet::load_from_folder(theme_dir).unwrap();
Expand Down
7 changes: 4 additions & 3 deletions examples/latex-demo.rs
@@ -1,7 +1,7 @@
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet,Style};
use syntect::util::{as_latex_escaped,LinesWithEndings};
use syntect::util::{as_latex_escaped, LinesWithEndings};

fn main() {
// Load these once at the start of your program
Expand All @@ -12,7 +12,8 @@ fn main() {
let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}\n";

let mut h = HighlightLines::new(syntax, &ts.themes["InspiredGitHub"]);
for line in LinesWithEndings::from(s) { // LinesWithEndings enables use of newlines mode
for line in LinesWithEndings::from(s) {
// LinesWithEndings enables use of newlines mode
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps).unwrap();
let escaped = as_latex_escaped(&ranges[..]);
println!("\n{:?}", line);
Expand Down
25 changes: 17 additions & 8 deletions examples/parsyncat.rs
@@ -1,13 +1,13 @@
//! Highlights the files given on the command line, in parallel.
//! Prints the highlighted output to stdout.

use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet, Style};
use syntect::easy::HighlightFile;
use rayon::prelude::*;
use syntect::easy::HighlightFile;
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;

use std::fs::File;
use std::io::{BufReader, BufRead};
use std::io::{BufRead, BufReader};

fn main() {
let files: Vec<String> = std::env::args().skip(1).collect();
Expand All @@ -21,7 +21,8 @@ fn main() {
let theme_set = ThemeSet::load_defaults();

// We first collect the contents of the files...
let contents: Vec<Vec<String>> = files.par_iter()
let contents: Vec<Vec<String>> = files
.par_iter()
.map(|filename| {
let mut lines = Vec::new();
// We use `String::new()` and `read_line()` instead of `BufRead::lines()`
Expand All @@ -37,15 +38,20 @@ fn main() {
.collect();

// ...so that the highlighted regions have valid lifetimes...
let regions: Vec<Vec<(Style, &str)>> = files.par_iter()
let regions: Vec<Vec<(Style, &str)>> = files
.par_iter()
.zip(&contents)
.map(|(filename, contents)| {
let mut regions = Vec::new();
let theme = &theme_set.themes["base16-ocean.dark"];
let mut highlighter = HighlightFile::new(filename, &syntax_set, theme).unwrap();

for line in contents {
for region in highlighter.highlight_lines.highlight_line(line, &syntax_set).unwrap() {
for region in highlighter
.highlight_lines
.highlight_line(line, &syntax_set)
.unwrap()
{
regions.push(region);
}
}
Expand All @@ -56,6 +62,9 @@ fn main() {

// ...and then print them all out.
for file_regions in regions {
print!("{}", syntect::util::as_24_bit_terminal_escaped(&file_regions[..], true));
print!(
"{}",
syntect::util::as_24_bit_terminal_escaped(&file_regions[..], true)
);
}
}
56 changes: 40 additions & 16 deletions examples/syncat.rs
Expand Up @@ -2,11 +2,11 @@ use getopts::Options;
use std::borrow::Cow;
use std::io::BufRead;
use std::path::Path;
use syntect::dumps::{dump_to_file, from_dump_file};
use syntect::easy::HighlightFile;
use syntect::highlighting::{Style, Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{Theme, ThemeSet, Style};
use syntect::util::as_24_bit_terminal_escaped;
use syntect::easy::HighlightFile;
use syntect::dumps::{from_dump_file, dump_to_file};

fn load_theme(tm_file: &str, enable_caching: bool) -> Theme {
let tm_path = Path::new(tm_file);
Expand All @@ -30,16 +30,35 @@ fn main() {
let args: Vec<String> = std::env::args().collect();
let mut opts = Options::new();
opts.optflag("l", "list-file-types", "Lists supported file types");
opts.optflag("L", "list-embedded-themes", "Lists themes present in the executable");
opts.optflag(
"L",
"list-embedded-themes",
"Lists themes present in the executable",
);
opts.optopt("t", "theme-file", "THEME_FILE", "Theme file to use. May be a path, or an embedded theme. Embedded themes will take precendence. Default: base16-ocean.dark");
opts.optopt("s", "extra-syntaxes", "SYNTAX_FOLDER", "Additional folder to search for .sublime-syntax files in.");
opts.optflag("e", "no-default-syntaxes", "Doesn't load default syntaxes, intended for use with --extra-syntaxes.");
opts.optflag("n", "no-newlines", "Uses the no newlines versions of syntaxes and dumps.");
opts.optopt(
"s",
"extra-syntaxes",
"SYNTAX_FOLDER",
"Additional folder to search for .sublime-syntax files in.",
);
opts.optflag(
"e",
"no-default-syntaxes",
"Doesn't load default syntaxes, intended for use with --extra-syntaxes.",
);
opts.optflag(
"n",
"no-newlines",
"Uses the no newlines versions of syntaxes and dumps.",
);
opts.optflag("c", "cache-theme", "Cache the parsed theme file.");

let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => { panic!("{}", f.to_string()) }
Ok(m) => m,
Err(f) => {
panic!("{}", f.to_string())
}
};

let no_newlines = matches.opt_present("no-newlines");
Expand All @@ -65,25 +84,27 @@ fn main() {
for sd in ss.syntaxes() {
println!("- {} (.{})", sd.name, sd.file_extensions.join(", ."));
}

} else if matches.opt_present("list-embedded-themes") {
println!("Embedded themes:");

for t in ts.themes.keys() {
println!("- {}", t);
}

} else if matches.free.is_empty() {
let brief = format!("USAGE: {} [options] FILES", args[0]);
println!("{}", opts.usage(&brief));

} else {
let theme_file : String = matches.opt_str("theme-file")
let theme_file: String = matches
.opt_str("theme-file")
.unwrap_or_else(|| "base16-ocean.dark".to_string());

let theme = ts.themes.get(&theme_file)
let theme = ts
.themes
.get(&theme_file)
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(load_theme(&theme_file, matches.opt_present("cache-theme"))));
.unwrap_or_else(|| {
Cow::Owned(load_theme(&theme_file, matches.opt_present("cache-theme")))
});

for src in &matches.free[..] {
if matches.free.len() > 1 {
Expand All @@ -103,7 +124,10 @@ fn main() {
}

{
let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight_line(&line, &ss).unwrap();
let regions: Vec<(Style, &str)> = highlighter
.highlight_lines
.highlight_line(&line, &ss)
.unwrap();
print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
}
line.clear();
Expand Down

0 comments on commit 53413d5

Please sign in to comment.