Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store ContextId in ParseState instead of &Context #190

Merged
Merged
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
4 changes: 2 additions & 2 deletions benches/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use std::fs::File;
use std::io::Read;

fn do_highlight(s: &str, syntax_set: &SyntaxSet, syntax: &SyntaxReference, theme: &Theme) -> usize {
let mut h = HighlightLines::new(syntax_set, syntax, theme);
let mut h = HighlightLines::new(syntax, theme);
let mut count = 0;
for line in s.lines() {
let regions = h.highlight(line);
let regions = h.highlight(line, syntax_set);
count += regions.len();
}
count
Expand Down
4 changes: 2 additions & 2 deletions benches/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::io::Read;
use syntect::parsing::{ParseState, SyntaxReference, SyntaxSet};

fn do_parse(s: &str, ss: &SyntaxSet, syntax: &SyntaxReference) -> usize {
let mut state = ParseState::new(ss, syntax);
let mut state = ParseState::new(syntax);
let mut count = 0;
for line in s.lines() {
let ops = state.parse_line(line);
let ops = state.parse_line(line, ss);
count += ops.len();
}
count
Expand Down
2 changes: 1 addition & 1 deletion examples/parsyncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
let mut highlighter = HighlightFile::new(filename, &syntax_set, theme).unwrap();

for line in contents {
for region in highlighter.highlight_lines.highlight(line) {
for region in highlighter.highlight_lines.highlight(line, &syntax_set) {
regions.push(region);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/syncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn main() {
}

{
let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line);
let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss);
print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
}
line.clear();
Expand Down
4 changes: 2 additions & 2 deletions examples/synstats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ fn count(ss: &SyntaxSet, path: &Path, stats: &mut Stats) {
None => return
};
stats.files += 1;
let mut state = ParseState::new(&ss, syntax);
let mut state = ParseState::new(syntax);

let f = File::open(path).unwrap();
let mut reader = BufReader::new(f);
let mut line = String::new();
let mut stack = ScopeStack::new();
while reader.read_line(&mut line).unwrap() > 0 {
{
let ops = state.parse_line(&line);
let ops = state.parse_line(&line, &ss);
stats.chars += line.len();
count_line(&ops, &line, &mut stack, stats);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/syntest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool, out_opts: Outp
let syntax = ss.find_syntax_by_path(syntax_file).ok_or(SyntaxTestHeaderError::SyntaxDefinitionNotFound)?;

// iterate over the lines of the file, testing them
let mut state = ParseState::new(&ss, syntax);
let mut state = ParseState::new(syntax);
let mut stack = ScopeStack::new();

let mut current_line_number = 1;
Expand Down Expand Up @@ -214,7 +214,7 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool, out_opts: Outp
if out_opts.debug && !line_only_has_assertion {
println!("-- debugging line {} -- scope stack: {:?}", current_line_number, stack);
}
let ops = state.parse_line(&line);
let ops = state.parse_line(&line, &ss);
if out_opts.debug && !line_only_has_assertion {
if ops.is_empty() && !line.is_empty() {
println!("no operations for this line...");
Expand Down
33 changes: 16 additions & 17 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,35 @@ use std::path::Path;
/// let ts = ThemeSet::load_defaults();
///
/// let syntax = ps.find_syntax_by_extension("rs").unwrap();
/// let mut h = HighlightLines::new(&ps, syntax, &ts.themes["base16-ocean.dark"]);
/// let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
/// let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
/// for line in s.lines() {
/// let ranges: Vec<(Style, &str)> = h.highlight(line);
/// let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
/// let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
/// println!("{}", escaped);
/// }
/// ```
pub struct HighlightLines<'a> {
highlighter: Highlighter<'a>,
parse_state: ParseState<'a>,
parse_state: ParseState,
highlight_state: HighlightState,
}

impl<'a> HighlightLines<'a> {
// TODO: should syntax come first or the set?
pub fn new(syntax_set: &'a SyntaxSet, syntax: &'a SyntaxReference, theme: &'a Theme) -> HighlightLines<'a> {
pub fn new(syntax: &SyntaxReference, theme: &'a Theme) -> HighlightLines<'a> {
let highlighter = Highlighter::new(theme);
let hstate = HighlightState::new(&highlighter, ScopeStack::new());
HighlightLines {
highlighter: highlighter,
parse_state: ParseState::new(syntax_set, syntax),
parse_state: ParseState::new(syntax),
highlight_state: hstate,
}
}

/// Highlights a line of a file
pub fn highlight<'b>(&mut self, line: &'b str) -> Vec<(Style, &'b str)> {
pub fn highlight<'b>(&mut self, line: &'b str, syntax_set: &SyntaxSet) -> Vec<(Style, &'b str)> {
// println!("{}", self.highlight_state.path);
let ops = self.parse_state.parse_line(line);
let ops = self.parse_state.parse_line(line, syntax_set);
// use util::debug_print_ops;
// debug_print_ops(line, &ops);
let iter =
Expand Down Expand Up @@ -97,12 +96,12 @@ impl<'a> HighlightFile<'a> {
/// let mut highlighter = HighlightFile::new("testdata/highlight_test.erb", &ss, &ts.themes["base16-ocean.dark"]).unwrap();
/// for maybe_line in highlighter.reader.lines() {
/// let line = maybe_line.unwrap();
/// let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line);
/// let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss);
/// println!("{}", as_24_bit_terminal_escaped(&regions[..], true));
/// }
/// ```
pub fn new<P: AsRef<Path>>(path_obj: P,
ss: &'a SyntaxSet,
ss: &SyntaxSet,
theme: &'a Theme)
-> io::Result<HighlightFile<'a>> {
let path: &Path = path_obj.as_ref();
Expand All @@ -112,7 +111,7 @@ impl<'a> HighlightFile<'a> {

Ok(HighlightFile {
reader: BufReader::new(f),
highlight_lines: HighlightLines::new(ss, syntax, theme),
highlight_lines: HighlightLines::new(syntax, theme),
})
}
}
Expand Down Expand Up @@ -188,8 +187,8 @@ mod tests {
let ss = SyntaxSet::load_defaults_nonewlines();
let ts = ThemeSet::load_defaults();
let syntax = ss.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(&ss, syntax, &ts.themes["base16-ocean.dark"]);
let ranges = h.highlight("pub struct Wow { hi: u64 }");
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
let ranges = h.highlight("pub struct Wow { hi: u64 }", &ss);
assert!(ranges.len() > 4);
}

Expand All @@ -206,9 +205,9 @@ mod tests {
#[test]
fn can_find_regions() {
let ss = SyntaxSet::load_defaults_nonewlines();
let mut state = ParseState::new(&ss, ss.find_syntax_by_extension("rb").unwrap());
let mut state = ParseState::new(ss.find_syntax_by_extension("rb").unwrap());
let line = "lol =5+2";
let ops = state.parse_line(line);
let ops = state.parse_line(line, &ss);

let mut stack = ScopeStack::new();
let mut token_count = 0;
Expand All @@ -230,12 +229,12 @@ mod tests {
#[test]
fn can_find_regions_with_trailing_newline() {
let ss = SyntaxSet::load_defaults_newlines();
let mut state = ParseState::new(&ss, ss.find_syntax_by_extension("rb").unwrap());
let mut state = ParseState::new(ss.find_syntax_by_extension("rb").unwrap());
let lines = ["# hello world\n", "lol=5+2\n"];
let mut stack = ScopeStack::new();

for line in lines.iter() {
let ops = state.parse_line(&line);
let ops = state.parse_line(&line, &ss);
println!("{:?}", ops);

let mut iterated_ops: Vec<&ScopeStackOp> = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions src/highlighting/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ mod tests {
let ps = SyntaxSet::load_from_folder("testdata/Packages").unwrap();
let mut state = {
let syntax = ps.find_syntax_by_name("Ruby on Rails").unwrap();
ParseState::new(&ps, syntax)
ParseState::new(syntax)
};
let ts = ThemeSet::load_defaults();
let highlighter = Highlighter::new(&ts.themes["base16-ocean.dark"]);

let mut highlight_state = HighlightState::new(&highlighter, ScopeStack::new());
let line = "module Bob::Wow::Troll::Five; 5; end";
let ops = state.parse_line(line);
let ops = state.parse_line(line, &ps);
let iter = HighlightIterator::new(&mut highlight_state, &ops[..], line, &highlighter);
let regions: Vec<(Style, &str)> = iter.collect();
// println!("{:#?}", regions);
Expand Down
14 changes: 7 additions & 7 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn scope_to_classes(s: &mut String, scope: Scope, style: ClassStyle) {
/// choice of `SyntaxSet` to accept, I'm not sure of it either, email me.
pub fn highlighted_snippet_for_string(s: &str, ss: &SyntaxSet, syntax: &SyntaxReference, theme: &Theme) -> String {
let mut output = String::new();
let mut highlighter = HighlightLines::new(ss, syntax, theme);
let mut highlighter = HighlightLines::new(syntax, theme);
let c = theme.settings.background.unwrap_or(Color::WHITE);
write!(output,
"<pre style=\"background-color:#{:02x}{:02x}{:02x};\">\n",
Expand All @@ -49,7 +49,7 @@ pub fn highlighted_snippet_for_string(s: &str, ss: &SyntaxSet, syntax: &SyntaxRe
c.b)
.unwrap();
for line in s.lines() {
let regions = highlighter.highlight(line);
let regions = highlighter.highlight(line, ss);
let html = styles_to_coloured_html(&regions[..], IncludeBackground::IfDifferent(c));
output.push_str(&html);
output.push('\n');
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn highlighted_snippet_for_file<P: AsRef<Path>>(path: P,
.unwrap();
for maybe_line in highlighter.reader.lines() {
let line = maybe_line?;
let regions = highlighter.highlight_lines.highlight(&line);
let regions = highlighter.highlight_lines.highlight(&line, ss);
let html = styles_to_coloured_html(&regions[..], IncludeBackground::IfDifferent(c));
output.push_str(&html);
output.push('\n');
Expand Down Expand Up @@ -164,8 +164,8 @@ fn write_css_color(s: &mut String, c: Color) {
/// let ts = ThemeSet::load_defaults();
///
/// let syntax = ps.find_syntax_by_name("Ruby").unwrap();
/// let mut h = HighlightLines::new(&ps, syntax, &ts.themes["base16-ocean.dark"]);
/// let regions = h.highlight("5");
/// let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
/// let regions = h.highlight("5", &ps);
/// let html = styles_to_coloured_html(&regions[..], IncludeBackground::No);
/// assert_eq!(html, "<span style=\"color:#d08770;\">5</span>");
/// ```
Expand Down Expand Up @@ -243,9 +243,9 @@ mod tests {
fn tokens() {
let ss = SyntaxSet::load_defaults_nonewlines();
let syntax = ss.find_syntax_by_name("Markdown").unwrap();
let mut state = ParseState::new(&ss, syntax);
let mut state = ParseState::new(syntax);
let line = "[w](t.co) *hi* **five**";
let ops = state.parse_line(line);
let ops = state.parse_line(line, &ss);

// use util::debug_print_ops;
// debug_print_ops(line, &ops);
Expand Down
Loading