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

fix: grit node types #351

Merged
merged 17 commits into from
May 23, 2024
11 changes: 6 additions & 5 deletions crates/language/src/grit_ts_node.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use crate::language::{fields_for_nodes, Field, NodeTypes, TSLanguage};
use lazy_static::lazy_static;

static NODE_TYPES_STRING: &str = include_str!("../../../resources/node-types/grit-node-types.json");
pub static NODE_TYPES_STRING: &str =
include_str!("../../../resources/node-types/grit-node-types.json");

lazy_static! {
static ref GRIT_NODE_TYPES: Vec<Vec<Field>> = fields_for_nodes(&language(), NODE_TYPES_STRING);
}

pub struct GritNodeTypes {
node_types: &'static [Vec<Field>],
pub struct GritNodeTypes<'a> {
pub node_types: &'a [Vec<Field>],
}

impl NodeTypes for GritNodeTypes {
impl NodeTypes for GritNodeTypes<'_> {
fn node_types(&self) -> &[Vec<Field>] {
self.node_types
}
}

pub fn grit_node_types() -> GritNodeTypes {
pub fn grit_node_types() -> GritNodeTypes<'static> {
GritNodeTypes {
node_types: &GRIT_NODE_TYPES,
}
Expand Down
19 changes: 17 additions & 2 deletions crates/wasm-bindings/src/match_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Context;
use grit_util::Language;
use grit_util::{Ast, Position};
use marzano_core::pattern_compiler::PatternBuilder;
use marzano_core::{
Expand All @@ -7,6 +8,8 @@ use marzano_core::{
pattern_compiler::CompilationResult,
tree_sitter_serde::tree_sitter_node_to_json,
};
use marzano_language::grit_ts_node::{grit_node_types, GritNodeTypes, NODE_TYPES_STRING};
use marzano_language::language::{fields_for_nodes, Field, NodeTypes};
use marzano_language::{
grit_parser::MarzanoGritParser,
language::Tree,
Expand All @@ -23,6 +26,7 @@ use tree_sitter::{Language as TSLanguage, Parser as TSParser};
use wasm_bindgen::prelude::*;

static GRIT_LANGUAGE: OnceLock<TSLanguage> = OnceLock::new();
static GRIT_NODE_TYPES: OnceLock<Vec<Vec<Field>>> = OnceLock::new();
static JAVASCRIPT_LANGUAGE: OnceLock<TSLanguage> = OnceLock::new();
static TYPESCRIPT_LANGUAGE: OnceLock<TSLanguage> = OnceLock::new();
static TSX_LANGUAGE: OnceLock<TSLanguage> = OnceLock::new();
Expand Down Expand Up @@ -78,7 +82,13 @@ pub async fn parse_input_files_internal(
let ParsedPattern { libs, tree, lang } =
get_parsed_pattern(&pattern, lib_paths, lib_contents, parser).await?;
let node = tree.root_node();
let parsed_pattern = tree_sitter_node_to_json(&node.node, &pattern, &lang).to_string();
let fields = GRIT_NODE_TYPES
.get_or_init(|| fields_for_nodes(&GRIT_LANGUAGE.get().unwrap(), NODE_TYPES_STRING));
let grit_node_types = GritNodeTypes {
node_types: &fields,
};
let parsed_pattern =
tree_sitter_node_to_json(&node.node, &pattern, &grit_node_types).to_string();

let mut results: Vec<MatchResult> = Vec::new();
for (path, content) in paths.into_iter().zip(contents) {
Expand Down Expand Up @@ -337,7 +347,12 @@ async fn setup_grit_parser() -> anyhow::Result<MarzanoGritParser> {
let lang = if let Some(lang) = GRIT_LANGUAGE.get() {
lang
} else {
let _language_already_set = GRIT_LANGUAGE.set(get_lang(&lang_path).await?);
let new_lang = get_lang(&lang_path).await?;
let _language_already_set = GRIT_LANGUAGE.set(new_lang);
let _ = GRIT_NODE_TYPES.set(fields_for_nodes(
&GRIT_LANGUAGE.get().unwrap(),
NODE_TYPES_STRING,
));
GRIT_LANGUAGE
.get()
.ok_or_else(|| anyhow::anyhow!("Failed to setup GRIT_LANGUAGE"))?
Expand Down
Loading