Skip to content

Commit

Permalink
Remove tree-sitter-facade from native build of core
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Sep 22, 2023
1 parent c160eb9 commit d3f9955
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 116 deletions.
16 changes: 1 addition & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ test-log = "0.2.11"
tokio = "1.32"
tokio-test = "0.4"
toml = "0.7"
tree-sitter = "0.20"
tree-sitter-bash = { git = "https://github.com/tree-sitter/tree-sitter-bash" }
tree-sitter-facade = { git = "https://github.com/tweag/tree-sitter-facade" }
tree-sitter-json = { git = "https://github.com/tree-sitter/tree-sitter-json.git" }
tree-sitter-nickel = { git = "https://github.com/nickel-lang/tree-sitter-nickel", rev = "b1a4718601ebd29a62bf3a7fd1069a99ccf48093" }
tree-sitter-ocaml = { git = "https://github.com/tree-sitter/tree-sitter-ocaml.git" }
Expand Down
1 change: 0 additions & 1 deletion topiary-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ tokio = { workspace = true, features = ["fs", "rt-multi-thread", "sync", "macros
toml = { workspace = true }
topiary-core = { path = "../topiary-core" }
topiary-queries = { path = "../topiary-queries" }
tree-sitter-facade = { workspace = true }

tree-sitter-json = { workspace = true }
tree-sitter-rust = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion topiary-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
toml = { workspace = true }
tree-sitter-facade = { workspace = true }
tree-sitter = { workspace = true }
unescape = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions topiary-core/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn format() {
let configuration = Configuration::parse_default_configuration().unwrap();
let language = configuration.get_language("ocaml").unwrap();
let grammar = language.grammar().await.unwrap();
let query = TopiaryQuery::new(&grammar, &query_content).unwrap();
let query = TopiaryQuery::new(grammar, &query_content).unwrap();

let mut input = input.as_bytes();
let mut output = io::BufWriter::new(Vec::new());
Expand All @@ -21,7 +21,7 @@ async fn format() {
&mut output,
&query,
language,
&grammar,
grammar,
Operation::Format {
skip_idempotence: true,
tolerate_parsing_errors: false,
Expand Down
18 changes: 9 additions & 9 deletions topiary-core/src/atom_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
ops::Deref,
};

use tree_sitter_facade::Node;
use tree_sitter::Node;

use crate::{Atom, FormatterError, FormatterResult, ScopeCondition, ScopeInformation};

Expand Down Expand Up @@ -177,13 +177,13 @@ impl AtomCollection {
// instead of creating it for both branches, create them once here.
let scope_information_prepend = || -> FormatterResult<ScopeInformation> {
Ok(ScopeInformation {
line_number: node.start_position().row(),
line_number: node.start_position().row,
scope_id: requires_scope_id()?.to_owned(),
})
};
let scope_information_append = || -> FormatterResult<ScopeInformation> {
Ok(ScopeInformation {
line_number: node.end_position().row(),
line_number: node.end_position().row,
scope_id: requires_scope_id()?.to_owned(),
})
};
Expand Down Expand Up @@ -592,7 +592,7 @@ impl AtomCollection {
/// The second pass applies the modifications to the atoms.
fn post_process_scopes(&mut self) {
type ScopeId = String;
type LineIndex = u32;
type LineIndex = usize;
type ScopedNodeId = usize;
// `opened_scopes` maintains stacks of opened scopes,
// the line at which they started,
Expand Down Expand Up @@ -1026,8 +1026,8 @@ fn detect_multi_line_nodes(dfs_nodes: &[Node]) -> HashSet<usize> {
dfs_nodes
.iter()
.filter_map(|node| {
let start_line = node.start_position().row();
let end_line = node.end_position().row();
let start_line = node.start_position().row;
let end_line = node.end_position().row;

if end_line > start_line {
log::debug!("Multi-line node {}: {:?}", node.id(), node,);
Expand Down Expand Up @@ -1055,16 +1055,16 @@ fn detect_multi_line_nodes(dfs_nodes: &[Node]) -> HashSet<usize> {
///
/// A `NodesWithLinebreaks` struct that contains two sets of node IDs: one for the nodes that have a line break
/// before them, and one for the nodes that have a line break after them.
fn detect_line_breaks(dfs_nodes: &[Node], minimum_line_breaks: u32) -> NodesWithLinebreaks {
fn detect_line_breaks(dfs_nodes: &[Node], minimum_line_breaks: usize) -> NodesWithLinebreaks {
// Zip the flattened vector with its own tail => Iterator of pairs of adjacent nodes
// Filter this by the threshold distance between pair components
// Unzip into "nodes with spaces before" and "after" sets, respectively
let (before, after) = dfs_nodes
.iter()
.zip(dfs_nodes[1..].iter())
.filter_map(|(left, right)| {
let last = left.end_position().row();
let next = right.start_position().row();
let last = left.end_position().row;
let next = right.start_position().row;

if next >= last + minimum_line_breaks {
log::debug!(
Expand Down
20 changes: 7 additions & 13 deletions topiary-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub enum FormatterError {

/// Tree-sitter could not parse the input without errors.
Parsing {
start_line: u32,
start_column: u32,
end_line: u32,
end_column: u32,
start_line: usize,
start_column: usize,
end_line: usize,
end_column: usize,
},

/// The query contains a pattern that had no match in the input file.
Expand All @@ -33,7 +33,7 @@ pub enum FormatterError {

/// There was an error in the query file. If this happened using our
/// provided query files, it is a bug. Please log an issue.
Query(String, Option<tree_sitter_facade::QueryError>),
Query(String, Option<tree_sitter::QueryError>),

/// Could not detect the input language from the (filename,
/// Option<extension>)
Expand Down Expand Up @@ -204,21 +204,15 @@ impl From<serde_json::Error> for FormatterError {
}
}

impl From<tree_sitter_facade::LanguageError> for FormatterError {
fn from(e: tree_sitter_facade::LanguageError) -> Self {
impl From<tree_sitter::LanguageError> for FormatterError {
fn from(e: tree_sitter::LanguageError) -> Self {
Self::Internal(
"Error while loading language grammar".into(),
Some(Box::new(e)),
)
}
}

impl From<tree_sitter_facade::ParserError> for FormatterError {
fn from(e: tree_sitter_facade::ParserError) -> Self {
Self::Internal("Error while parsing".into(), Some(Box::new(e)))
}
}

impl From<toml::de::Error> for FormatterError {
fn from(e: toml::de::Error) -> Self {
Self::Internal(
Expand Down
2 changes: 1 addition & 1 deletion topiary-core/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Language {
///
/// If the language is not supported, a `FormatterError` will be returned.
#[cfg(not(target_arch = "wasm32"))]
pub async fn grammar(&self) -> FormatterResult<tree_sitter_facade::Language> {
pub async fn grammar(&self) -> FormatterResult<tree_sitter::Language> {
Ok(match self.name.as_str() {
"bash" => tree_sitter_bash::language(),
"json" => tree_sitter_json::language(),
Expand Down
16 changes: 8 additions & 8 deletions topiary-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use std::io;

use crate::tree_sitter::Position;
use itertools::Itertools;
use pretty_assertions::StrComparison;
use tree_sitter::Position;

pub use crate::{
configuration::{default_configuration_toml, Configuration},
Expand All @@ -36,7 +36,7 @@ pub mod test_utils;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ScopeInformation {
line_number: u32,
line_number: usize,
scope_id: String,
}

Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn formatter(
output: &mut impl io::Write,
query: &TopiaryQuery,
language: &Language,
grammar: &tree_sitter_facade::Language,
grammar: ::tree_sitter::Language,
operation: Operation,
) -> FormatterResult<()> {
let content = read_input(input).map_err(|e| {
Expand Down Expand Up @@ -277,7 +277,7 @@ fn idempotence_check(
content: &str,
query: &TopiaryQuery,
language: &Language,
grammar: &tree_sitter_facade::Language,
grammar: ::tree_sitter::Language,
tolerate_parsing_errors: bool,
) -> FormatterResult<()> {
log::info!("Checking for idempotence ...");
Expand Down Expand Up @@ -334,14 +334,14 @@ mod tests {
let configuration = Configuration::parse_default_configuration().unwrap();
let language = configuration.get_language("json").unwrap();
let grammar = language.grammar().await.unwrap();
let query = TopiaryQuery::new(&grammar, query_content).unwrap();
let query = TopiaryQuery::new(grammar, query_content).unwrap();

match formatter(
&mut input,
&mut output,
&query,
language,
&grammar,
grammar,
Operation::Format {
skip_idempotence: true,
tolerate_parsing_errors: false,
Expand Down Expand Up @@ -369,14 +369,14 @@ mod tests {
let configuration = Configuration::parse_default_configuration().unwrap();
let language = configuration.get_language("json").unwrap();
let grammar = language.grammar().await.unwrap();
let query = TopiaryQuery::new(&grammar, &query_content).unwrap();
let query = TopiaryQuery::new(grammar, &query_content).unwrap();

formatter(
&mut input,
&mut output,
&query,
language,
&grammar,
grammar,
Operation::Format {
skip_idempotence: true,
tolerate_parsing_errors: true,
Expand Down
Loading

0 comments on commit d3f9955

Please sign in to comment.