Skip to content

Commit

Permalink
clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Sep 11, 2023
1 parent 63cb9d4 commit 0251fd3
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 150 deletions.
36 changes: 3 additions & 33 deletions libs/proto/src/ast/get_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,9 @@ pub fn get_expr_dep<'a>(id: &str, graph: &'a Graph) -> Option<(ExpressionWrapper

pub fn get_ref_id(expr: ExpressionWrapper, graph: &Graph) -> Option<String> {
match &expr {
ExpressionWrapper::Element(element) => get_element_origin_dep(element, graph)
.document
.as_ref()
.unwrap()
.get_components()
.iter()
.find_map(|component| {
if &component.name == &element.tag_name {
Some(component.id.to_string())
} else {
None
}
}),
ExpressionWrapper::Element(element) => element
.get_instance_component(graph)
.and_then(|component| Some(component.id.clone())),
_ => None,
}
}

fn get_element_origin_dep<'a>(element: &Element, graph: &'a Graph) -> &'a Dependency {
let dep = get_expr_dep(&element.id, graph).unwrap().1;

if let Some(namespace) = &element.namespace {
if let Some(imp) = dep
.document
.as_ref()
.unwrap()
.get_imports()
.iter()
.find(|imp| &imp.namespace == namespace)
{
let imp_path = dep.imports.get(&imp.path).unwrap();
return graph.dependencies.get(imp_path).as_ref().unwrap();
}
}

dep
}
18 changes: 16 additions & 2 deletions libs/proto/src/ast/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ include!(concat!(env!("OUT_DIR"), "/ast.graph.rs"));
use std::collections::{HashMap, HashSet};

pub use super::graph_ext::*;
use super::pc::{Document, Element};
use super::{
all::ExpressionWrapper,
get_expr::GetExpr,
pc::{Document, Element},
};

impl<'a> Dependency {
pub fn resolve_import_from_ns(&'a self, ns: &str, graph: &'a Graph) -> Option<&'a Dependency> {
Expand All @@ -21,6 +25,9 @@ impl<'a> Dependency {
pub fn get_document(&self) -> &Document {
self.document.as_ref().expect("Document must exist")
}
pub fn get_expr(&self, id: &str) -> Option<ExpressionWrapper> {
GetExpr::get_expr(id, &self.document.as_ref().expect("Document must exist"))
}
pub fn find_instances_of(&self, component_name: &str, component_source: &str) -> Vec<&Element> {
let import_rel_path = self
.imports
Expand Down Expand Up @@ -48,7 +55,6 @@ impl Graph {
dependencies: HashMap::new(),
}
}

pub fn merge(self, graph: Graph) -> Graph {
let mut dependencies = self.dependencies;
dependencies.extend(graph.dependencies);
Expand All @@ -67,6 +73,14 @@ impl Graph {
}
dependents
}
pub fn get_expr_dep<'a>(&'a self, id: &str) -> Option<(ExpressionWrapper, &'a Dependency)> {
for (_path, dep) in &self.dependencies {
if let Some(expr) = dep.get_expr(id) {
return Some((expr, dep));
}
}
return None;
}
pub fn get_all_dependents(&self, path: &str) -> Vec<&Dependency> {
let mut all_dependents: Vec<&Dependency> = vec![];
let mut used: HashSet<&str> = HashSet::new();
Expand Down
21 changes: 21 additions & 0 deletions libs/proto/src/ast/pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::add_inner_wrapper;

use super::{
get_expr::GetExpr,
graph::{Dependency, Graph},
};
include!(concat!(env!("OUT_DIR"), "/ast.pc.rs"));

macro_rules! body_contains {
Expand Down Expand Up @@ -166,6 +171,22 @@ impl Element {
pub fn get_inserts(&self) -> Vec<&Insert> {
get_body_items!(&self.body, node::Inner::Insert, Insert)
}
pub fn get_source_dep<'a>(&'a self, graph: &'a Graph) -> &'a Dependency {
let (_, owner_dep) =
GetExpr::get_expr_from_graph(&self.id, graph).expect("Expr must exist in graph");
if let Some(ns) = &self.namespace {
owner_dep
.resolve_import_from_ns(&ns, graph)
.expect("Dependency must exist")
} else {
owner_dep
}
}
pub fn get_instance_component<'a>(&'a self, graph: &'a Graph) -> Option<&'a Component> {
self.get_source_dep(graph)
.get_document()
.get_component_by_name(&self.tag_name)
}
}

impl Node {
Expand Down
58 changes: 1 addition & 57 deletions libs/proto_ext/src/ast/get_expr.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use paperclip_proto::ast;
use paperclip_proto::ast::all::visit::{Visitable, Visitor, VisitorResult};
use paperclip_proto::ast::graph_ext::{Dependency, Graph};
use paperclip_proto::ast::pc::{document_body_item, DocumentBodyItem, Element};
use paperclip_proto::ast::{all::ExpressionWrapper, pc::Component};

// use crate::ast::all::Visitable;
use paperclip_proto::ast::all::visit::{Visitable, Visitor, VisitorResult};

// use super::all::{Visitor, VisitorResult};

pub struct GetExpr {
id: String,
reference: Option<ExpressionWrapper>,
Expand Down Expand Up @@ -87,55 +83,3 @@ impl<'expr> GetExpr {
})
}
}

pub fn get_expr(id: &str, dep: &Dependency) -> Option<ExpressionWrapper> {
GetExpr::get_expr(id, &dep.document.as_ref().expect("Document must exist"))
}

pub fn get_expr_dep<'a>(id: &str, graph: &'a Graph) -> Option<(ExpressionWrapper, &'a Dependency)> {
for (_path, dep) in &graph.dependencies {
if let Some(expr) = get_expr(id, dep) {
return Some((expr, dep));
}
}
return None;
}

pub fn get_ref_id(expr: ExpressionWrapper, graph: &Graph) -> Option<String> {
match &expr {
ExpressionWrapper::Element(element) => get_element_origin_dep(element, graph)
.document
.as_ref()
.unwrap()
.get_components()
.iter()
.find_map(|component| {
if &component.name == &element.tag_name {
Some(component.id.to_string())
} else {
None
}
}),
_ => None,
}
}

fn get_element_origin_dep<'a>(element: &Element, graph: &'a Graph) -> &'a Dependency {
let dep = get_expr_dep(&element.id, graph).unwrap().1;

if let Some(namespace) = &element.namespace {
if let Some(imp) = dep
.document
.as_ref()
.unwrap()
.get_imports()
.iter()
.find(|imp| &imp.namespace == namespace)
{
let imp_path = dep.imports.get(&imp.path).unwrap();
return graph.dependencies.get(imp_path).as_ref().unwrap();
}
}

dep
}
7 changes: 3 additions & 4 deletions libs/proto_ext/src/ast_mutate/move_expression_to_file.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::path::Component;

use crate::ast_mutate::utils::resolve_import_ns;

use super::{base::EditContext, utils::import_dep};
use paperclip_common::{fs::FileResolver, get_or_short};
use paperclip_common::get_or_short;
use paperclip_proto::{
ast::css::FunctionCall,
ast::pc::Element,
ast::{
all::{Expression, ExpressionWrapper},
graph_ext::Dependency,
pc::{document_body_item, Document, Style},
},
ast_mutate::MoveExpressionToFile,
Expand Down Expand Up @@ -97,6 +94,8 @@ impl MutableVisitor<()> for EditContext<MoveExpressionToFile> {
}

fn visit_style(&mut self, style: &mut Style) -> VisitorResult<()> {
for extends in &style.extends {}

VisitorResult::Continue
}

Expand Down
6 changes: 3 additions & 3 deletions libs/proto_ext/src/ast_mutate/set_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use paperclip_proto::{
ast_mutate::{mutation_result, ExpressionUpdated, SetId},
};

use paperclip_proto::ast::get_expr::{get_expr_dep, get_ref_id};
use paperclip_proto::ast::get_expr::get_expr_dep;

use super::EditContext;

Expand Down Expand Up @@ -39,8 +39,8 @@ impl MutableVisitor<()> for EditContext<SetId> {
return VisitorResult::Continue;
}

if let Some(ref_id) = get_ref_id(expr.into(), &self.graph) {
if ref_id == self.mutation.expression_id {
if let Some(component) = expr.get_instance_component(&self.graph) {
if component.id == self.mutation.expression_id {
let info = get_expr_dep(&self.mutation.expression_id, &self.graph).unwrap();
match &info.0 {
ExpressionWrapper::Component(comp) => {
Expand Down
102 changes: 51 additions & 51 deletions libs/proto_ext/src/ast_mutate/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4261,54 +4261,54 @@ case! {
)]
}

// case! {
// refs_are_updated_when_style_is_moved,
// [
// (
// "/entry.pc", r#"
// import "/a.pc" as mod

// div {
// style extends mod.test
// }
// "#
// ),
// (
// "/a.pc", r#"
// public style test {
// color: blue
// }
// "#
// ),

// (
// "/b.pc", r#"
// "#
// )
// ],
// mutation::Inner::MoveExpressionToFile(MoveExpressionToFile {
// expression_id: "98523c41-3".to_string(),
// new_file_path: "/b.pc".to_string()
// }).get_outer(),
// [(
// "/entry.pc", r#"
// import "./b.pc" as module
// import "/a.pc" as mod

// div {
// style extends module.test
// }
// "#
// ),
// (
// "/a.pc", r#"
// "#
// ),
// (
// "/b.pc", r#"
// public style test {
// color: blue
// }
// "#
// )]
// }
case! {
refs_are_updated_when_style_is_moved,
[
(
"/entry.pc", r#"
import "/a.pc" as mod
div {
style extends mod.test
}
"#
),
(
"/a.pc", r#"
public style test {
color: blue
}
"#
),

(
"/b.pc", r#"
"#
)
],
mutation::Inner::MoveExpressionToFile(MoveExpressionToFile {
expression_id: "98523c41-3".to_string(),
new_file_path: "/b.pc".to_string()
}).get_outer(),
[(
"/entry.pc", r#"
import "./b.pc" as module
import "/a.pc" as mod
div {
style extends module.test
}
"#
),
(
"/a.pc", r#"
"#
),
(
"/b.pc", r#"
public style test {
color: blue
}
"#
)]
}

0 comments on commit 0251fd3

Please sign in to comment.