Share |

IndexRepeat Command

Let’s say you are coding something like this:

var a0 = array[0];
var a1 = array[1];
var a2 = array[2];
var a3 = array[3];
var a4 = array[4]; 

Usually I use to copy and paste the first line n times and change the index n times. Boring and easy to make a mistake.

AutoCode to the rescue. I’ve just created this command that will help us code these repetitive tasks incrementing the index automatically, so now I just need to write the first line, copy it to the clipboard, and execute the IndexRepeat (ix) command n times.

Since the TAB key take me to the last executed command, this is the key sequence I need to type:

CTRL+ENTER, ix, ENTER

CTRL+ENTER, TAB, ENTER

CTRL+ENTER, TAB, ENTER

CTRL+ENTER, TAB, ENTER

That will produce the 4 lines of code. Easy, fast and sharp.

How it works

The command takes the selected lines of code from the Clipboard, then increment any number found in the line using a regular expression. Finally, after inserting the code, copy the new line to the Clipboard, so next time will use the right index.

protected override void Render()
{
    // Get the code line from the clipboard
    string text = Clipboard.GetText();

    // Increment any number using a regular expression
    text = System.Text.RegularExpressions.Regex.Replace
        (text, @"\d+", m => (Int32.Parse(m.Value) + 1).ToString());

    // Write the line in the editor
    TextPoint point = Selection.ActivePoint;
    InsertBlock(point, text);

    // Save the line in the clipboard
    Clipboard.SetText(text);
}

The complete AutoCode command is shown bellow. To add this command to your set of commands just copy the following code into a text file and save it as IndexRepeat.autox into the AutoCode 20xx\Commands folder. This folder is located in My Documents folder.

<?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v4.0">
  <Command name="IndexRepeat" version="1.0">

    <CommandBehavior>
      <CommandLine shortcut="ix" />
      <ActiveDocument extensions="*"/>
    </CommandBehavior>

    <CommandInfo>
      <LanguageCategory>Common</LanguageCategory>
      <Category>Clipboard</Category>
      <Usage>
        <![CDATA[ix]]>
      </Usage>
      <Example>(Copy first line) ix, ix, ...</Example>
      <Description>
        <![CDATA[Paste the content of the Clipboard replacing all occurrences of n with n+1.]]>
      </Description>
      <Author>DevProjects</Author>
      <HelpUrl>http://help.devprojects.net/gallery/command/indexrepeat</HelpUrl>
    </CommandInfo>

    <CommandCode language="csharp">

      <Render>
        <![CDATA[
        string text = Clipboard.GetText();

        text = System.Text.RegularExpressions.Regex.Replace(text, @"\d+", m => (Int32.Parse(m.Value) + 1).ToString());

        TextPoint point = Selection.ActivePoint;
        InsertBlock(point, text);

        Clipboard.SetText(text);
        ]]>
      </Render>

    </CommandCode>
  
    <Signature><![CDATA[
      GYOG+9amwD5jRH7AfldJp0ne3FwdrvP0e0CI4AR21jQ8eUTFADHopiMJu
      xN/WK0jKOy06HBU8F7DOv4GaMXlRF43jU9czDrJdBXEys2XtkdEpxjKv4
      oMP3GoM1CmOtdXxx7uZIf5QoW+waZK7dudPrKHZ8EjOM1M1ZanPMmA3GI=
    ]]></Signature>

</Command>
</Commands>

Hope this command helps you coding fater.

 


Comments

 


Add a comment