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

generator: insert newline to if, let, and record definition #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions core/bound_variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,25 @@ Blockly.BoundVariables.renameVariable = function(variable) {
};
promptAndCheckWithAlert('');
};

/**
* Input an inline comment for field. Open a user prompt dialog to
* input an inline comment.
* @param {Blockly.Field} field The field to add inline comment.
*/
Blockly.BoundVariables.inputInlineComment = function(field) {
var promptText = 'Input inline comment';
var defaultComment = field.inlineComment ? field.inlineComment : '';
// TODO: Define the message in the Blockly.Msg class.
Blockly.prompt(promptText, defaultComment,
function(newComment) {
if (newComment === '') {
// clear the inline comment.
field.inlineComment = null;
} else if (!newComment) {
// NOP. User canceled prompt.
} else {
field.inlineComment = newComment;
}
});
};
9 changes: 9 additions & 0 deletions core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,12 @@ Blockly.RENAME_VARIABLE_ID = 'RENAME_VARIABLE_ID';
* @const {string}
*/
Blockly.DELETE_VARIABLE_ID = 'DELETE_VARIABLE_ID';

/**
* String for use in the dropdown created in record_field_variable.
* This string indicates that this option in the dropdown is 'Input
* inline comment...' and if selected, should trigger the prompt to
* input inline comment.
* @const {string}
*/
Blockly.INPUT_INLINE_COMMENT = 'INPUT_INLINE_COMMENT'
7 changes: 7 additions & 0 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,10 @@ Blockly.Field.prototype.getAbsoluteXY_ = function() {
Blockly.Field.prototype.referencesVariables = function() {
return Blockly.FIELD_VARIABLE_NONE;
};

/**
* Inline comment for this field.
* @type {string}
* @private
*/
Blockly.Field.prototype.inlineComment = null;
6 changes: 6 additions & 0 deletions core/field_bound_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ Blockly.FieldBoundVariable.dropdownCreate = function() {
goog.asserts.fail('Not implemented for this type of variable.');
}
options.push([message, Blockly.RENAME_VARIABLE_ID]);
if (this.isForRecordField()) {
var message = 'Input inline comment...';
options.push([message, Blockly.INPUT_INLINE_COMMENT]);
}
return options;
};

Expand All @@ -617,6 +621,8 @@ Blockly.FieldBoundVariable.prototype.onItemSelected = function(menu, menuItem) {
Blockly.BoundVariables.renameVariable(this.variable_);
} else if (id == Blockly.DELETE_VARIABLE_ID) {
throw 'Not implemented yet.';
} else if (id == Blockly.INPUT_INLINE_COMMENT) {
Blockly.BoundVariables.inputInlineComment(this);
}
};

Expand Down
20 changes: 13 additions & 7 deletions generators/typedlang/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Blockly.TypedLang['logic_ternary_typed'] = function(block) {
Blockly.TypedLang.ORDER_THEN) || '?';
var value_else = Blockly.TypedLang.valueToCode(block, 'ELSE',
Blockly.TypedLang.ORDER_ELSE) || '?';
var code = 'if ' + value_if + ' then ' + value_then + ' else ' +
var code = 'if ' + value_if + ' then ' + value_then + '\n else ' +
value_else;
return [code, Blockly.TypedLang.ORDER_EXPR];
};
Expand Down Expand Up @@ -288,15 +288,19 @@ Blockly.TypedLang['let_typed'] = function(block) {

var code = 'let ';
if (block.isRecursive()) code += 'rec ';
code += varname + arg + ' = ' + exp1;
if (args.length == 0) {
code += varname + ' = ' + exp1;
} else {
code += varname + arg + ' =\n ' + exp1;
}

if (block.getIsStatement()) {
code += '\n';
return code;
}
var exp2 = Blockly.TypedLang.valueToCode(block, 'EXP2',
Blockly.TypedLang.ORDER_IN) || '?';
code += ' in ' + exp2;
code += '\n in ' + exp2;
return [code, Blockly.TypedLang.ORDER_EXPR];
};

Expand All @@ -320,16 +324,18 @@ Blockly.TypedLang['defined_recordtype_typed'] = function(block) {
}
var field = block.getField('DATANAME');
var dataName = field.getVariableName();
var code = 'type ' + dataName + ' = {';
var code = 'type ' + dataName + ' = {\n';
for (var i = 0; i < block.itemCount_; i++) {
var recordField = block.getField('FIELD' + i);
code += recordField.getVariableName();
code += ' ' + recordField.getVariableName();
code += ' : ';
var typeCtor = Blockly.TypedLang.valueToCode(block, 'FIELD_INP' + i,
Blockly.TypedLang.ORDER_SEMI) || '?';
code += typeCtor;
if (i != block.itemCount_ - 1) {
code += '; ';
if (recordField.inlineComment) {
code += ';\t(* ' + recordField.inlineComment + ' *)\n';
} else {
code += ';\n';
}
}
code += '}\n';
Expand Down