How to pretty format a LUA script?

How to pretty format a LUA script?

4

    Jan 21, 2018#1

    Hi everyone!

    I am searching for a method to reformat a whole LUA script file.

    What I want to do is delete all unused spaces/tabs in file and automatically add or remove tab indentation to make the script more easily readable. What I have in mind is that when I select a whole script, I can clean it by removing all unused whitespace before and after the lines. Then macro/script should add tab indent the same way that the used syntax highlighting wordfile allow it during writing.

    An UltraEdit script or a macro would be fine for this task. It doesn't need to be automatic on each file open / save. A manual execution by me is okay.

    Is there a way to do this? Could you please tell me more about macros / scripts? What could be the easiest way to do this?

    6,680583
    Grand MasterGrand Master
    6,680583

      Jan 23, 2018#2

      UltraEdit has the command ReIndent Selection to re-indent all code lines of a selection according to /Indent Strings = and /Unindent Strings = in wordfile (*.uew) used to syntax highlight active file.

      I don't know which wordfile you are using for syntax highlighting LUA scripts. I suppose you are using the user contributed wordfile lua.uew containing the indent/unindent strings definition:

      Code: Select all

      /Indent Strings = "{" "function" "then" "while" "for" "else"
      /Unindent Strings = "}" "end" "else"
      I don't know anything about LUA syntax. So let us assume the command ReIndent Selection makes already a good job on reformatting the code lines of a LUA script file using this definition.

      Then there is the command Trim Trailing Spaces to remove spaces and horizontal tabs from end of each line.

      Those two commands can be used in an UltraEdit macro:

      Code: Select all

      IfExtIs "lua"
      InsertMode
      ColumnModeOff
      Top
      TrimTrailingSpaces
      SelectAll
      ReIndentSelection
      EndIf
      
      It is also possible to use those commands in an UltraEdit script:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Has the active file the file extension LUA in any case?
         if (UltraEdit.activeDocument.isExt("lua"))
         {
            // Define environment for this script.
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
            // Move caret to top of the active file.
            UltraEdit.activeDocument.top();
      
            // Remove all trailing spaces and tabs.
            UltraEdit.activeDocument.trimTrailingSpaces();
      
            // Select everything in active LUA file.
            UltraEdit.activeDocument.selectAll();
      
            // Re-indent all lines in active file.
            UltraEdit.activeDocument.reIndentSelection();
         }
      }
      
      The line indents are done according to tab and indent settings as defined at Advanced - Settings or Configuration - Editor - Word wrap/tab settings either using Default settings or the settings explicitly defined for files with extension lua. See also forum topic File extension based word wrap, tab and indent settings.

      It is not necessary to remove first the leading spaces/tabs. This is automatically done by the re-indent selection command.

      Let me know if the macro/script with that minimal code works already well or if additional commands are necessary. Additional reformatting can be done with regular expression replaces in macro/script. With an UltraEdit script it is even possible to write real code in JavaScript to reformat a LUA file completely in memory to get every LUA file reformatted exactly as you want. But I would need a description of all reformatting requirements with lots of before and after examples because I don't know anything about syntax of LUA scripts in case of such a very smart reformat script for LUA files is really needed.
      Best regards from an UC/UE/UES for Windows user from Austria

      4

        Jan 23, 2018#3

        Awesome, it works like a charm!

        Thank you Mofi!

        With your macro, I just had to tweak the wordfile to have indents at right place:

        Code: Select all

        /Indent Strings = "{" "do" "else" "elseif" "function" "if"
        /Unindent Strings = "}" "else" "elseif" "end"
        Before:

        Code: Select all

        function Hud:AddCenterMessage(text,time)
        self.centermessage=text;
        if(time)then
        self.centermessagetime=time;
        else
        self.centermessagetime=1;
        end
        end
        After:

        Code: Select all

        function Hud:AddCenterMessage(text,time)
            self.centermessage=text;
            if(time)then
                self.centermessagetime=time;
            else
                self.centermessagetime=1;
            end
        end
        That's exactly what I wanted to do!

        Thanks 👍