Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
Nodes added by searcher will always use assignment form to enable cac…
Browse files Browse the repository at this point in the history
…hing. Ref #1067
  • Loading branch information
mwu-tow committed Feb 15, 2021
1 parent 8308c1c commit 8b10596
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
44 changes: 34 additions & 10 deletions src/rust/ide/src/controller/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,21 @@ pub struct NewNodeInfo {
/// ID to be given to the node.
pub id : Option<ast::Id>,
/// Where line created by adding this node should appear.
pub location_hint : LocationHint
pub location_hint : LocationHint,
/// Introduce variable name for the node, even if `expression` is not an assignment.
pub introduce_pattern : bool,

}

impl NewNodeInfo {
/// New node with given expression added at the end of the graph's blocks.
pub fn new_pushed_back(expression:impl Str) -> NewNodeInfo {
NewNodeInfo {
expression : expression.into(),
metadata : default(),
id : default(),
location_hint : LocationHint::End,
expression : expression.into(),
metadata : default(),
id : default(),
location_hint : LocationHint::End,
introduce_pattern : default(),
}
}
}
Expand Down Expand Up @@ -715,6 +719,11 @@ impl Handle {
node_info.set_id(desired_id)
}

if node.introduce_pattern && node_info.pattern().is_none() {
let var = self.variable_name_for(&node_info)?;
node_info.set_pattern(var.into());
}

self.update_definition_ast(|definition| {
let mut graph = GraphInfo::from_definition(definition);
let node_ast = node_info.ast().clone();
Expand Down Expand Up @@ -1244,12 +1253,13 @@ main =
let position = Some(model::module::Position::new(10.0,20.0));
let metadata = NodeMetadata {position,..default()};
let info = NewNodeInfo {
expression : "a+b".into(),
metadata : Some(metadata),
id : Some(id),
location_hint : LocationHint::End,
expression : "a+b".into(),
metadata : Some(metadata),
id : Some(id),
location_hint : LocationHint::End,
introduce_pattern : false,
};
graph.add_node(info).unwrap();
graph.add_node(info.clone()).unwrap();
let expected_program = r"
main =
foo = 2
Expand Down Expand Up @@ -1283,6 +1293,20 @@ main =
assert!(graph.module.node_metadata(id).is_err());

model::module::test::expect_code(&*graph.module, PROGRAM);


// === Test adding node with automatically generated pattern ===
let info_w_pattern = NewNodeInfo {
introduce_pattern : true,
..info
};
graph.add_node(info_w_pattern).unwrap();
let expected_program = r"
main =
foo = 2
print foo
sum1 = a+b";
model::module::test::expect_code(&*graph.module,expected_program);
})
}

Expand Down
21 changes: 12 additions & 9 deletions src/rust/ide/src/controller/searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,11 @@ impl Searcher {

let id = match *self.mode {
Mode::NewNode {position} => {
let mut new_node = NewNodeInfo::new_pushed_back(expression);
new_node.metadata = Some(NodeMetadata {position,intended_method});
let mut new_node = NewNodeInfo::new_pushed_back(expression);
new_node.metadata = Some(NodeMetadata {position,intended_method});
// We force introducing variables to enable engine's caching of user-added nodes.
// See: https://github.com/enso-org/ide/issues/1067
new_node.introduce_pattern = true;
let graph = self.graph.graph();
if let Some(this) = self.this_arg.deref().as_ref() {
this.introduce_pattern(graph.clone_ref())?;
Expand Down Expand Up @@ -1553,26 +1556,26 @@ pub mod test {

let cases = vec![
// Completion was picked.
Case::new("2 + 2",&["sum1 = 2 + 2","sum1.testFunction1"], |f| {
Case::new("2 + 2",&["sum1 = 2 + 2","operator1 = sum1.testFunction1"], |f| {
f.searcher.use_suggestion(f.entry1.clone()).unwrap();
}),
// The input was manually written (not picked).
Case::new("2 + 2",&["sum1 = 2 + 2","sum1.testFunction1"], |f| {
Case::new("2 + 2",&["sum1 = 2 + 2","operator1 = sum1.testFunction1"], |f| {
f.searcher.set_input("testFunction1 ".to_owned()).unwrap();
}),
// Completion was picked and edited.
Case::new("2 + 2",&["sum1 = 2 + 2","sum1.var.testFunction1"], |f| {
Case::new("2 + 2",&["sum1 = 2 + 2","operator1 = sum1.var.testFunction1"], |f| {
f.searcher.use_suggestion(f.entry1.clone()).unwrap();
let new_parsed_input = ParsedInput::new("var.testFunction1",&f.searcher.parser);
f.searcher.data.borrow_mut().input = new_parsed_input.unwrap();
}),
// Variable name already present, need to use it. And not break it.
Case::new("my_var = 2 + 2",&["my_var = 2 + 2","my_var.testFunction1"], |f| {
Case::new("my_var = 2 + 2",&["my_var = 2 + 2","operator1 = my_var.testFunction1"], |f| {
f.searcher.use_suggestion(f.entry1.clone()).unwrap();
}),
// Variable names unusable (subpatterns are not yet supported).
// Don't use "this" argument adjustments at all.
Case::new("[x,y] = 2 + 2",&["[x,y] = 2 + 2","testFunction1"], |f| {
Case::new("[x,y] = 2 + 2",&["[x,y] = 2 + 2","testfunction11 = testFunction1"], |f| {
f.searcher.use_suggestion(f.entry1.clone()).unwrap();
}),
];
Expand Down Expand Up @@ -1610,7 +1613,7 @@ pub mod test {
searcher.mode = Immutable(Mode::NewNode {position});
searcher.commit_node().unwrap();

let expected_code = "import Test.Test\nmain = \n 2 + 2\n Test.testMethod1";
let expected_code = "import Test.Test\nmain = \n 2 + 2\n operator1 = Test.testMethod1";
assert_eq!(module.ast().repr(), expected_code);
let (node1,node2) = searcher.graph.graph().nodes().unwrap().expect_tuple();
let expected_intended_method = Some(MethodId {
Expand All @@ -1623,7 +1626,7 @@ pub mod test {
// Edit existing node.
searcher.mode = Immutable(Mode::EditNode {node_id:node1.info.id()});
searcher.commit_node().unwrap();
let expected_code = "import Test.Test\nmain = \n Test.testMethod1\n Test.testMethod1";
let expected_code = "import Test.Test\nmain = \n Test.testMethod1\n operator1 = Test.testMethod1";
let (node1,_) = searcher.graph.graph().nodes().unwrap().expect_tuple();
assert_eq!(node1.metadata.unwrap().intended_method, expected_intended_method);
assert_eq!(module.ast().repr(), expected_code);
Expand Down

0 comments on commit 8b10596

Please sign in to comment.