Don't run syntax highlighting when dumping the tree-sitter output

For large files, tree-sitter syntax highlighting is much more
expensive than the parse itself. We spend most of the runtime
advancing the tree-sitter query cursor.

This doesn't affect runtime of normal usage, but it helps debugging
and makes flamegraphs more readable.

Spotted in #153
pull/166/head
Wilfred Hughes 2022-03-12 11:53:39 +07:00
parent 2f8ccd94da
commit dba68d1d2a
2 changed files with 15 additions and 10 deletions

@ -89,7 +89,7 @@ fn main() {
match guess(path, &src) { match guess(path, &src) {
Some(lang) => { Some(lang) => {
let ts_lang = tsp::from_language(lang); let ts_lang = tsp::from_language(lang);
let (tree, _) = tsp::parse_to_tree(&src, &ts_lang); let tree = tsp::parse_to_tree(&src, &ts_lang);
tsp::print_tree(&src, &tree); tsp::print_tree(&src, &tree);
} }
None => { None => {

@ -458,17 +458,22 @@ pub fn from_language(language: guess::Language) -> TreeSitterConfig {
} }
/// Parse `src` with tree-sitter. /// Parse `src` with tree-sitter.
pub fn parse_to_tree( pub fn parse_to_tree(src: &str, config: &TreeSitterConfig) -> tree_sitter::Tree {
src: &str,
config: &TreeSitterConfig,
) -> (tree_sitter::Tree, HighlightedNodeIds) {
let mut parser = ts::Parser::new(); let mut parser = ts::Parser::new();
parser parser
.set_language(config.language) .set_language(config.language)
.expect("Incompatible tree-sitter version"); .expect("Incompatible tree-sitter version");
let tree = parser.parse(src, None).unwrap(); parser.parse(src, None).unwrap()
}
/// Calculate which tree-sitter node IDs should have which syntax
/// highlighting.
fn tree_highlights(
tree: &tree_sitter::Tree,
src: &str,
config: &TreeSitterConfig,
) -> HighlightedNodeIds {
let mut keyword_ish_capture_ids = vec![]; let mut keyword_ish_capture_ids = vec![];
if let Some(idx) = config.highlight_query.capture_index_for_name("keyword") { if let Some(idx) = config.highlight_query.capture_index_for_name("keyword") {
keyword_ish_capture_ids.push(idx); keyword_ish_capture_ids.push(idx);
@ -528,12 +533,11 @@ pub fn parse_to_tree(
} }
} }
let highlights = HighlightedNodeIds { HighlightedNodeIds {
keyword_ids, keyword_ids,
string_ids, string_ids,
type_ids, type_ids,
}; }
(tree, highlights)
} }
pub fn print_tree(src: &str, tree: &tree_sitter::Tree) { pub fn print_tree(src: &str, tree: &tree_sitter::Tree) {
@ -577,7 +581,8 @@ pub fn parse<'a>(
return vec![]; return vec![];
} }
let (tree, highlights) = parse_to_tree(src, config); let tree = parse_to_tree(src, config);
let highlights = tree_highlights(&tree, src, config);
let nl_pos = NewlinePositions::from(src); let nl_pos = NewlinePositions::from(src);
let mut cursor = tree.walk(); let mut cursor = tree.walk();