Shape now implements Clone and Ast allows setting/removing id. (https://github.com/enso-org/ide/pull/261)

Original commit: 79e886c3de
This commit is contained in:
Michał Wawrzyniec Urbańczyk 2020-03-14 03:44:35 +01:00 committed by GitHub
parent a013563ce2
commit 757b4cbe7d
2 changed files with 26 additions and 5 deletions

View File

@ -74,7 +74,7 @@ pub struct WrongEnum { pub expected_con: String }
/// number of children nodes, each marked with a single `K`.
///
/// It is used to describe ambiguous macro match.
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
#[derive(Clone,Eq,PartialEq,Debug,Serialize,Deserialize)]
pub struct Tree<K,V> {
pub value : Option<V>,
pub branches : Vec<(K, Tree<K,V>)>,
@ -87,7 +87,7 @@ pub struct Tree<K,V> {
// ===============
/// A value of type `T` annotated with offset value `off`.
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Shrinkwrap, Iterator)]
#[derive(Clone,Eq,PartialEq,Debug,Serialize,Deserialize,Shrinkwrap,Iterator)]
#[shrinkwrap(mutable)]
pub struct Shifted<T> {
#[shrinkwrap(main_field)]
@ -96,7 +96,7 @@ pub struct Shifted<T> {
}
/// A non-empty sequence of `T`s interspersed by offsets.
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize, Iterator)]
#[derive(Clone,Eq,PartialEq,Debug,Serialize,Deserialize,Iterator)]
pub struct ShiftedVec1<T> {
pub head: T,
pub tail: Vec<Shifted<T>>
@ -212,6 +212,16 @@ impl Ast {
pub fn iter_recursive(&self) -> impl Iterator<Item=&Ast> {
internal::iterate_subtree(self)
}
/// Returns this AST node with ID set to given value.
pub fn with_id(&self, id:ID) -> Ast {
Ast::from_ast_id_len(self.shape().clone(), Some(id), self.len())
}
/// Returns this AST node with removed ID.
pub fn without_id(&self) -> Ast {
Ast::from_ast_id_len(self.shape().clone(), None, self.len())
}
}
/// Fills `id` with `None` by default.
@ -1072,6 +1082,17 @@ mod tests {
assert_eq!(*input_val, deserialized_val);
}
#[test]
fn ast_updating_id() {
let var = Var {name:"foo".into()};
let ast = Ast::new(var, None);
assert!(ast.id.is_none());
let id = Uuid::default();
let ast = ast.with_id(id);
assert_eq!(ast.id, Some(id));
}
#[test]
fn var_smart_constructor() {
let name = "foo".to_string();

View File

@ -36,9 +36,9 @@ pub fn ast_node
) -> proc_macro::TokenStream {
let input: TokenStream = input.into();
let output = quote! {
#[derive(Eq, PartialEq, Debug)]
#[derive(Clone,Eq,PartialEq,Debug)]
#[derive(Iterator)]
#[derive(Serialize, Deserialize)]
#[derive(Serialize,Deserialize)]
#input
};
output.into()