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

Add some additional list functions #844

Merged
merged 5 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions blocks/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,27 @@ Blockly.Blocks['lists_split'] = {
this.updateType_(xmlElement.getAttribute('mode'));
}
};

Blockly.Blocks['lists_reverse'] = {
/**
* Block for reversing a list.
* @this Blockly.Block
**/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.LISTS_REVERSE_MESSAGE0,
"args0": [
{
"type": "input_value",
"name": "LIST",
"check": "Array"
}
],
"output": "Array",
"inputsInline": true,
"colour": Blockly.Blocks.lists.HUE,
"tooltip": Blockly.Msg.LISTS_REVERSE_TOOLTIP,
"helpUrl": Blockly.Msg.LISTS_REVERSE_HELPURL
});
}
};
9 changes: 9 additions & 0 deletions generators/dart/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,12 @@ Blockly.Dart['lists_split'] = function(block) {
var code = input + '.' + functionName + '(' + delimiter + ')';
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
};

Blockly.Dart['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.Dart.valueToCode(block, 'LIST',
Blockly.Dart.ORDER_NONE) || '[]';
// XXX What should the operator precedence be for a `new`?
var code = 'new List.from(' + list + '.reversed)';
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
};
8 changes: 8 additions & 0 deletions generators/javascript/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,11 @@ Blockly.JavaScript['lists_split'] = function(block) {
var code = input + '.' + functionName + '(' + delimiter + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};

Blockly.JavaScript['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
var code = list + '.slice().reverse()';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
17 changes: 17 additions & 0 deletions generators/lua/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,20 @@ Blockly.Lua['lists_split'] = function(block) {
var code = functionName + '(' + input + ', ' + delimiter + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_NONE) || '{}';
var functionName = Blockly.Lua.provideFunction_(
'list_reverse',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
' local reversed = {}',
' for i = #input, 1, -1 do',
' table.insert(reversed, input[i])',
' end',
' return reversed',
'end']);
var code = 'list_reverse(' + list + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
8 changes: 8 additions & 0 deletions generators/php/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,11 @@ Blockly.PHP['lists_split'] = function(block) {
var code = functionName + '(' + value_delim + ', ' + value_input + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};

Blockly.PHP['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_COMMA) || '[]';
var code = 'array_reverse(' + list + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
8 changes: 8 additions & 0 deletions generators/python/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,11 @@ Blockly.Python['lists_split'] = function(block) {
}
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};

Blockly.Python['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.Python.valueToCode(block, 'LIST',
Blockly.Python.ORDER_NONE) || '[]';
var code = 'list(reversed(' + list + '))';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
5 changes: 4 additions & 1 deletion msg/json/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@metadata": {
"author": "Ellen Spertus <[email protected]>",
"lastupdated": "2017-01-12 13:53:04.889198",
"lastupdated": "2017-01-20 09:40:15.080443",
"locale": "en",
"messagedocumentation" : "qqq"
},
Expand Down Expand Up @@ -334,6 +334,9 @@
"LISTS_SPLIT_WITH_DELIMITER": "with delimiter",
"LISTS_SPLIT_TOOLTIP_SPLIT": "Split text into a list of texts, breaking at each delimiter.",
"LISTS_SPLIT_TOOLTIP_JOIN": "Join a list of texts into one text, separated by a delimiter.",
"LISTS_REVERSE_HELPURL": "",
"LISTS_REVERSE_MESSAGE0": "reverse %1",
"LISTS_REVERSE_TOOLTIP": "Reverse a copy of a list.",
"ORDINAL_NUMBER_SUFFIX": "",
"VARIABLES_GET_HELPURL": "https://github.com/google/blockly/wiki/Variables#get",
"VARIABLES_GET_TOOLTIP": "Returns the value of this variable.",
Expand Down
10 changes: 3 additions & 7 deletions msg/json/qqq.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{
"@metadata": {
"authors": [
"Espertus",
"Liuxinyu970226",
"Shirayuki"
]
},
"VARIABLES_DEFAULT_NAME": "default name - A simple, general default name for a variable, preferably short. For more context, see [[Translating:Blockly#infrequent_message_types]].\n{{Identical|Item}}",
"TODAY": "button text - Button that sets a calendar to today's date.\n{{Identical|Today}}",
"DUPLICATE_BLOCK": "context menu - Make a copy of the selected block (and any blocks it contains).\n{{Identical|Duplicate}}",
Expand Down Expand Up @@ -335,6 +328,9 @@
"LISTS_SPLIT_WITH_DELIMITER": "block text - Prompts for a letter to be used as a separator when splitting or joining text.",
"LISTS_SPLIT_TOOLTIP_SPLIT": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-list-from-text https://github.com/google/blockly/wiki/Lists#make-list-from-text] for more information.",
"LISTS_SPLIT_TOOLTIP_JOIN": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-text-from-list https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.",
"LISTS_REVERSE_HELPURL": "url - Information describing reversing a list.",
"LISTS_REVERSE_MESSAGE0": "Reverse a list of items %1.",
"LISTS_REVERSE_TOOLTIP": "tooltip - See [https://github.com/google/blockly/wiki/Lists#reversing-a-list].",
"ORDINAL_NUMBER_SUFFIX": "grammar - Text that follows an ordinal number (a number that indicates position relative to other numbers). In most languages, such text appears before the number, so this should be blank. An exception is Hungarian. See [[Translating:Blockly#Ordinal_numbers]] for more information.",
"VARIABLES_GET_HELPURL": "url - Information about ''variables'' in computer programming. Consider using your language's translation of [https://en.wikipedia.org/wiki/Variable_(computer_science) https://en.wikipedia.org/wiki/Variable_(computer_science)], if it exists.",
"VARIABLES_GET_TOOLTIP": "tooltip - This gets the value of the named variable without modifying it.",
Expand Down
7 changes: 7 additions & 0 deletions msg/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,13 @@ Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT = 'Split text into a list of texts, breaki
/// https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.
Blockly.Msg.LISTS_SPLIT_TOOLTIP_JOIN = 'Join a list of texts into one text, separated by a delimiter.';

/// url - Information describing reversing a list.
Blockly.Msg.LISTS_REVERSE_HELPURL = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and fill this in as: https://github.com/google/blockly/wiki/Lists#reversing-a-list

When you submit, also make a new issue to create this wiki content. We can probably make you an editor for the wiki and would greatly appreciate help adding the docs for your new blocks there.

Unless you find a Wikipedia or similar description of reversing a list. That would be good, too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies for each of the new text blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

/// Reverse a list of items %1.
Copy link
Contributor

@AnmAtAnm AnmAtAnm Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"block text - Title of block that returns a copy of a list (%1) with the order of items reversed."

Blockly.Msg.LISTS_REVERSE_MESSAGE0 = 'reverse %1';
/// tooltip - See [https://github.com/google/blockly/wiki/Lists#reversing-a-list].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until the wiki docs are created, this will not be sufficient for translators.

"tooltip - Short description for a block that reverses a copy of a list."

Blockly.Msg.LISTS_REVERSE_TOOLTIP = 'Reverse a copy of a list.';

/// grammar - Text that follows an ordinal number (a number that indicates
/// position relative to other numbers). In most languages, such text appears
/// before the number, so this should be blank. An exception is Hungarian.
Expand Down
1 change: 1 addition & 0 deletions tests/generators/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
<block type="lists_getSublist"></block>
<block type="lists_sort"></block>
<block type="lists_split"></block>
<block type="lists_reverse"></block>
</category>
<category name="Colour" colour="20">
<block type="colour_picker"></block>
Expand Down
150 changes: 150 additions & 0 deletions tests/generators/lists.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<next>
<block type="procedures_callnoreturn">
<mutation name="test sort numeric"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test reverse"></mutation>
</block>
</next>
</block>
</next>
</block>
Expand Down Expand Up @@ -8131,4 +8136,149 @@
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="13" y="20463">
<field name="NAME">test reverse</field>
<comment pinned="false" h="80" w="160">Tests the "list reverse" block.</comment>
<statement name="STACK">
<block type="variables_set">
<field name="VAR">list</field>
<value name="VALUE">
<block type="lists_create_with" inline="true">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">18</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">-1</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">64</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">reverse a copy</field>
</block>
</value>
<value name="ACTUAL">
<block type="lists_reverse">
<value name="LIST">
<block type="variables_get">
<field name="VAR">list</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="true">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">64</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">-1</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">18</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">reverse a copy original</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">list</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="true">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">18</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">-1</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">64</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set">
<field name="VAR">list</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">empty list</field>
</block>
</value>
<value name="ACTUAL">
<block type="lists_reverse">
<value name="LIST">
<block type="variables_get">
<field name="VAR">list</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_empty"></block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>
1 change: 1 addition & 0 deletions tests/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ <h1>Blockly Playground</h1>
</value>
</block>
<block type="lists_sort"></block>
<block type="lists_reverse"></block>
</category>
<category name="Colour" colour="20">
<block type="colour_picker"></block>
Expand Down