Skip to content

Commit

Permalink
fix: grit node types (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 committed May 23, 2024
1 parent c879bde commit a4d22eb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
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

0 comments on commit a4d22eb

Please sign in to comment.