LUA: Comments keep indentation

LUA: Comments keep indentation

4

    Sep 03, 2021#1

    Hi everyone!

    I'm looking for a way to keep indentation with comments.
    I mean that when I comment a line with add / remove command shortcut, then comment start at the right indentation instead of the beginning of the line for better readability.
    I experimented a bit with wordfile but I had no luck.
    Is there a way to do that?

    Here are some examples:

    Uncommented code: (indentations use tabs)

    Code: Select all

    function Hud:DrawEnergy(player)
        if (player.entity_type ~="spectator") then
            local health=player.cnt.health/player.cnt.max_health*100;
            local armor=player.cnt.armor/player.cnt.max_armor*100;
            local stamina=self.staminalevel*100;
    What I had before: Comments start at the beginning of the line, no space after "--"

    Code: Select all

    --function Hud:DrawEnergy(player)
    --    if (player.entity_type ~="spectator") then
    --        local health=player.cnt.health/player.cnt.max_health*100;
    --        local armor=player.cnt.armor/player.cnt.max_armor*100;
    --        local stamina=self.staminalevel*100;
    What I have now: Comments start at the beginning of the line, with a space after "--"

    Code: Select all

    -- function Hud:DrawEnergy(player)
    --     if (player.entity_type ~="spectator") then
    --         local health=player.cnt.health/player.cnt.max_health*100;
    --         local armor=player.cnt.armor/player.cnt.max_armor*100;
    --         local stamina=self.staminalevel*100;
    What I wish to achieve: Comments start at first character position, keep indentations (tabs), with a space after "--"

    Code: Select all

    -- function Hud:DrawEnergy(player)
        -- if (player.entity_type ~="spectator") then
            -- local health=player.cnt.health/player.cnt.max_health*100;
            -- local armor=player.cnt.armor/player.cnt.max_armor*100;
            -- local stamina=self.staminalevel*100;

    6,603548
    Grand MasterGrand Master
    6,603548

      Sep 04, 2021#2

      It is not possible to customize the behavior of the built-in commands Comment add and Comment remove. They are designed to insert/remove always the line comment as defined in the wordfile at beginning of the selected line(s).

      However, it is possible to use an UltraEdit script to get the comment add/remove behavior as you would like it for LUA scripts. I wrote the script for you. Please copy and paste the script code and save it into a *.js file with name of your choice in folder of your choice. Then add this script to the Script list without or with a hotkey or chord of your choice for fast execution by key. It is also possible to execute the script with a click on an icon on a custom ribbon tab or an icon on custom toolbar or a customized document window context menu item on using UltraEdit for Windows ≥ v25.00.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Is there nothing selected in active file?
         if (!UltraEdit.activeDocument.isSel())
         {
            // Select the current line.
            UltraEdit.activeDocument.selectLine();
         }
         if (UltraEdit.activeDocument.isSel())
         {
            // It is necessary that entire lines are selected for correct comment add/remove.
            // But the user can start a selection anywhere inside a line and end it also
            // anywhere inside a line. For that reason lots of extra code is necessary to
            // more or less expand the selection of the user to beginning of first selected
            // line and end of last selected line with including the line termination of last
            // selected line. Further there must be taken into account that the selection ends
            // at end of the file and there is no line termination at end of the active file.
      
            // Load the selected block as string into memory of script.
            var sBlock = UltraEdit.activeDocument.selection;
            // Delete the selection.
            UltraEdit.activeDocument.deleteText();
      
            // Is the caret now not at column 1?
            if (UltraEdit.activeDocument.isColNumGt(1))
            {
                // The selection started somewhere in the middle of a line and for that
                // reason the string from current caret position to beginning of the line
                // must be also loaded from file and prepended to the already loaded block.
                UltraEdit.activeDocument.gotoLineSelect(0,1);
                sBlock = UltraEdit.activeDocument.selection + sBlock;
                UltraEdit.activeDocument.deleteText();
            }
      
            // Determine the line termination type from selected block.
            var sLineTerm = sBlock.replace(/^[^\r\n]*(\r?\n|\r)[\s\S]*$/,"$1");
      
            // Is there no carriage return + line-feed or just line-feed
            // or just carriage return in the loaded block?
            if (sLineTerm == sBlock)
            {
               // The user selected most likely just a part within a line. So select
               // the rest of the line and append it to the already loaded block.
               UltraEdit.activeDocument.gotoLine(0,1);
               UltraEdit.activeDocument.selectLine();
               if (UltraEdit.activeDocument.isSel())
               {
                  sBlock += UltraEdit.activeDocument.selection;
                  UltraEdit.activeDocument.deleteText();
                  sLineTerm = sBlock.replace(/^[^\r\n]*(\r?\n|\r)[\s\S]*$/,"$1");
               }
               if (sLineTerm == sBlock) sLineTerm = "";
            }
      
            var asLines = [];
            // Is the line termination already determined?
            if (sLineTerm.length)
            {
               // Does the selected block not end with a line termination?
               if (sBlock.substr(sBlock.length - sLineTerm.length) != sLineTerm)
               {
                  // The user selected a block spanning over multiple lines, but
                  // the last selected line was not completely selected or the
                  // selection ends at end of file with no line termination.
                  // Select the rest of last selected line and append it to
                  // already loaded block with deletion from the active file.
                  UltraEdit.activeDocument.gotoLine(0,1);
                  UltraEdit.activeDocument.selectLine();
                  if (UltraEdit.activeDocument.isSel())
                  {
                     sBlock += UltraEdit.activeDocument.selection;
                     UltraEdit.activeDocument.deleteText();
                  }
               }
               // Split up the loaded block into an array of lines.
               asLines = sBlock.split(sLineTerm);
            }
            else asLines.push(sBlock);
      
            var nFirstCharPos;
            var nLineIndex;
            var rReplaceRegExp;
            var sReplaceString;
      
            // Find out if the non-blank line has a line comment at the beginning
            // after 0 or more spaces/tabs in which case a comment remove is
            // applied on all lines of the selection, otherwise a comment add.
            for (nLineIndex = 0; nLineIndex < asLines.length; nLineIndex++)
            {
               if (!asLines[nLineIndex].length) continue;   // Ignore empty lines.
      
               nFirstCharPos = asLines[nLineIndex].search(/[^\t ]/);
               if (nFirstCharPos < 0)  // Does the line contain only sapces/tabs?
               {
      //            asLines[nLineIndex] = "";  // Trim lines containing only spaces/tabs.
                  continue;   // Ignore lines containing only spaces/tabs.
               }
      
               // Get the first two characters of the line after 0 or more spaces/tabs.
               var sFirstTwoChars = asLines[nLineIndex].substr(nFirstCharPos,2);
               if (sFirstTwoChars == "--")
               {
                  rReplaceRegExp = /^([\t ]*)(?:-- ?)?/;
                  sReplaceString = "$1";
                  UltraEdit.outputWindow.write("comment remove");
               }
               else
               {
                  rReplaceRegExp = /^([\t ]*)/;
                  sReplaceString = "$1-- ";
                  UltraEdit.outputWindow.write("comment add");
               }
               break;
            }
      
            // Do the comment add/remove on all remaining non-blank lines.
            for (; nLineIndex < asLines.length; nLineIndex++)
            {
               if (!asLines[nLineIndex].length) continue;   // Ignore empty lines.
      
               nFirstCharPos = asLines[nLineIndex].search(/[^\t ]/);
               if (nFirstCharPos < 0)  // Does the line contain only sapces/tabs?
               {
      //            asLines[nLineIndex] = "";  // Trim lines containing only spaces/tabs.
                  continue;   // Ignore lines containing only spaces/tabs.
               }
               // Add/remove comment from all non-blank lines.
               asLines[nLineIndex] = asLines[nLineIndex].replace(rReplaceRegExp,sReplaceString);
            }
      
            // Join the lines back to a single string and write it to the file.
            UltraEdit.activeDocument.write(asLines.join(sLineTerm));
      
            // Reselect the processed block.
            var nLineNumber = UltraEdit.activeDocument.currentLineNum;
            nLineNumber -= asLines.length - 1;
            if (asLines[asLines.length-1].length) UltraEdit.activeDocument.bottom();
            UltraEdit.activeDocument.gotoLineSelect(nLineNumber,1);
         }
      }
      
      I tested the script with UltraEdit for Windows v22.20 (last for Windows XP) and v28.10 (currently latest version) for hopefully all possible selection scenarios.

      Please let me know if you want the single script to add/remove line comments depending on -- in first non-blank line after 0 or more spaces/tabs split up into two scripts, one which adds always line comments and another one which removes always line comments. Please let me also know if something is not working as expected by you. Finally please note that I commented out two code lines. You might uncomment these two code lines if you want lines with just spaces/tabs to be automatically trimmed by the script too.
      Best regards from an UC/UE/UES for Windows user from Austria

      4

        Sep 04, 2021#3

        Wow thank you very much for your help Mofi!

        I was guessing it's not possible to edit comment add/remove behavior since I searched in options with no luck.
        I'll try your script and let you know if there is any issue with it, I use last version of UE so it should work well.

        Thanks again for your much appreciated help 👍