From 932cb223cf0eb73d536cec03ccb74b7b6a105294 Mon Sep 17 00:00:00 2001 From: Christopher Harrison Date: Mon, 2 Dec 2024 16:56:43 +0000 Subject: [PATCH] Trigger CI on PR, as well as push (#797) * Trigger CI on PR, as well as push * Update benchmark to use new tree-sitter-ocaml API * WASM: Fix usize/u32 discrepancy * WASM: Don't import unnecessary modules * WASM: Don't impl NodeExt for tree_sitter::Node * Appease Clippy :P * Comment unwrap of u32 to usize * Trigger CI on PRs against main and pushes to main * Allow dead code, rather than conditional compile, for imports * Attempt 2 of ...: Allow dead code for the module, in WASM builds * Attempt 3 of ...: Allow unused imports for the module, in WASM builds --- .github/workflows/ci.yml | 6 +++++- topiary-core/benches/benchmark.rs | 2 +- topiary-core/src/tree_sitter.rs | 14 +++++++++++--- topiary-tree-sitter-facade/src/query.rs | 2 +- topiary-tree-sitter-facade/src/query_match.rs | 6 ++++-- topiary-web-tree-sitter-sys/src/lib.rs | 2 +- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6bad34ad..b09d7cec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,8 @@ -on: push +on: + push: + branches: main + pull_request: + branches: main jobs: build: diff --git a/topiary-core/benches/benchmark.rs b/topiary-core/benches/benchmark.rs index 64776017..f25b9b53 100644 --- a/topiary-core/benches/benchmark.rs +++ b/topiary-core/benches/benchmark.rs @@ -7,7 +7,7 @@ use topiary_core::{formatter, Language, Operation, TopiaryQuery}; async fn format() { let input = fs::read_to_string("../topiary-cli/tests/samples/input/ocaml.ml").unwrap(); let query_content = fs::read_to_string("../topiary-queries/queries/ocaml.scm").unwrap(); - let ocaml = tree_sitter_ocaml::language_ocaml(); + let ocaml = tree_sitter_ocaml::LANGUAGE_OCAML; let mut input = input.as_bytes(); let mut output = io::BufWriter::new(Vec::new()); diff --git a/topiary-core/src/tree_sitter.rs b/topiary-core/src/tree_sitter.rs index a69d1915..5b75d4d7 100644 --- a/topiary-core/src/tree_sitter.rs +++ b/topiary-core/src/tree_sitter.rs @@ -1,11 +1,17 @@ +// WASM build doesn't use topiary_tree_sitter_facade::QueryMatch or +// streaming_iterator::StreamingIterator +#![cfg_attr(target_arch = "wasm32", allow(unused_imports))] + use std::{collections::HashSet, fmt::Display}; use serde::Serialize; -use streaming_iterator::StreamingIterator; + use topiary_tree_sitter_facade::{ Node, Parser, Point, Query, QueryCapture, QueryCursor, QueryMatch, QueryPredicate, Tree, }; +use streaming_iterator::StreamingIterator; + use crate::{ atom_collection::{AtomCollection, QueryPredicates}, error::FormatterError, @@ -159,6 +165,7 @@ impl<'a> NodeExt for Node<'a> { } } +#[cfg(not(target_arch = "wasm32"))] impl<'a> NodeExt for tree_sitter::Node<'a> { fn display_one_based(&self) -> String { format!( @@ -236,6 +243,7 @@ pub fn apply_query( let capture_names = query.query.capture_names(); let mut query_matches = query.query.matches(&root, source, &mut cursor); + #[allow(clippy::while_let_on_iterator)] // This is not a normal iterator while let Some(query_match) = query_matches.next() { let local_captures: Vec = query_match.captures().collect(); @@ -284,8 +292,8 @@ pub fn apply_query( if log::log_enabled!(log::Level::Info) { #[cfg(target_arch = "wasm32")] // Resize the pattern_positions vector if we need to store more positions - if m.pattern_index as usize >= pattern_positions.len() { - pattern_positions.resize(m.pattern_index as usize + 1, None); + if m.pattern_index >= pattern_positions.len() { + pattern_positions.resize(m.pattern_index + 1, None); } // Fetch from pattern_positions, otherwise insert diff --git a/topiary-tree-sitter-facade/src/query.rs b/topiary-tree-sitter-facade/src/query.rs index 967f9935..ca85ae5f 100644 --- a/topiary-tree-sitter-facade/src/query.rs +++ b/topiary-tree-sitter-facade/src/query.rs @@ -135,7 +135,7 @@ mod wasm { } #[inline] - pub fn general_predicates(&self, index: u32) -> Vec { + pub fn general_predicates(&self, index: usize) -> Vec { let predicates: Vec<_> = self .inner .predicates_for_pattern(index) diff --git a/topiary-tree-sitter-facade/src/query_match.rs b/topiary-tree-sitter-facade/src/query_match.rs index 716eb6f9..f451a088 100644 --- a/topiary-tree-sitter-facade/src/query_match.rs +++ b/topiary-tree-sitter-facade/src/query_match.rs @@ -28,6 +28,7 @@ pub use native::*; #[cfg(target_arch = "wasm32")] mod wasm { use crate::query_capture::QueryCapture; + use std::convert::TryInto; use wasm_bindgen::JsCast; #[derive(Clone)] @@ -38,8 +39,9 @@ mod wasm { impl<'tree> QueryMatch<'tree> { #[inline] - pub fn pattern_index(&self) -> u32 { - self.inner.pattern() + pub fn pattern_index(&self) -> usize { + // On WASM32, usize is the same as u32, so the unwrap is safe + self.inner.pattern().try_into().unwrap() } #[inline] diff --git a/topiary-web-tree-sitter-sys/src/lib.rs b/topiary-web-tree-sitter-sys/src/lib.rs index 73d68343..f3020fb5 100644 --- a/topiary-web-tree-sitter-sys/src/lib.rs +++ b/topiary-web-tree-sitter-sys/src/lib.rs @@ -477,7 +477,7 @@ extern "C" { // -> PredicateResult[] #[wasm_bindgen(method, js_name = predicatesForPattern)] - pub fn predicates_for_pattern(this: &Query, pattern_index: u32) -> Box<[JsValue]>; + pub fn predicates_for_pattern(this: &Query, pattern_index: usize) -> Box<[JsValue]>; } #[wasm_bindgen]