Resolve "Enhacement proposal: LCM text replace operator"

Implements a text replace operator by defining a new block with three inputs:

  • The text that will be modified (input)
  • The text whose occurrences will be replaced (before)
  • The text replacement (after)

This then generates Java code using the replace() method, roughly as follows:

input.replace(before, after)

That will replace all occurrences of before in input with after.

Closes #45 (closed)

Merge request reports

Loading
+17 −0
Changes for src/app/p_services/admin/lifeCycleManagement/service-rule-design/service-rule-design.component.html: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -198,6 +198,23 @@
                  </shadow>
                </value>
              </block>
              <block type="text_replace">
                <value name="STRING">
                  <block type="variables_get">
                    <field name="VAR">textVariable</field>
                  </block>
                </value>
                <value name="SEARCH">
                  <shadow type="text">
                    <field name="TEXT">before</field>
                  </shadow>
                </value>
                <value name="REPLACE">
                  <shadow type="text">
                    <field name="TEXT">after</field>
                  </shadow>
                </value>
              </block>


          </category> 
+12 −0
Changes for src/app/p_services/admin/lifeCycleManagement/services/blockly-java.service.ts: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -803,6 +803,18 @@ export class BlocklyJavaService {
      return [argument0 + operator, Blockly.Java.ORDER_FUNCTION_CALL];
    };

    Blockly.Java['text_replace'] = function (block: { getFieldValue: (arg0: string) => string; }) {
      // Search the text for a substring.
      var argument0 = Blockly.Java.valueToCode(block, 'SEARCH',
        Blockly.Java.ORDER_NONE) || '""';
      var argument1 = Blockly.Java.valueToCode(block, 'REPLACE',
        Blockly.Java.ORDER_NONE) || '""';
      var argument2 = Blockly.Java.valueToCode(block, 'STRING',
        Blockly.Java.ORDER_MEMBER) || '""';
      var code = argument2 + '.replace(' + argument0 + ',' + argument1 + ')';
      return [code, Blockly.Java.ORDER_MEMBER];
    };

    Blockly.Java['text_print'] = function (block: any) {
      // Print statement.
      var argument0 = Blockly.Java.valueToCode(block, 'TEXT',
+26 −0
Changes for src/assets/blockly/custom_java_blocks.js: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -706,6 +706,32 @@ Blockly.Blocks['variable_set_string'] = {
  customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
};

Blockly.Blocks['text_replace'] = {
  /**
   * Block for string replacement.
   * @this Blockly.Block
   */
  init: function() {
    this.setStyle('text_blocks');

    this.appendValueInput("STRING")
      .setCheck("String")
      .appendField("in text");

    this.appendValueInput("SEARCH")
      .setCheck("String")
      .appendField("replace");

    this.appendValueInput('REPLACE')
      .setCheck("String")
      .appendField("with");

    this.setInputsInline(true);
    this.setOutput(true,'String');
    this.setTooltip("Replaces all occurrences of a substring in a string with another");
  },
};




Loading