Run StyLua Lua Formatter on File Save

This forum allows you to share scripts with other Zeus users. Please do not post bug reports, feature requests or questions to this forum, but rather use it exclusively for posting scripts or for the discussion of scripts that have been posted.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Run StyLua Lua Formatter on File Save

Post by jussij »

The StyLua formatter can be used to automatically format Lua code on the file save event as described below.

1. Download a release executable from the following page: https://github.com/JohnnyMorganz/StyLua/releases

In this case the stylua-windows-x86_64.zip asset was downloaded.

2. Extract the zip file to a suitable folder location, making sure that folder is also found in the system PATH. A good option is to use the Zeus installation folder or the Zeus zGNU folder, as both of these will be found in the PATH. Alternative add the folder to the PATH.

3. Use the Options, Document Type menu and edit the Lua document type

In the Triggers section add the following values:

Code: Select all

   File Save Postfix: $zud\zScript\stylua_fmt.lua
File Save As Postfix: $zud\zScript\stylua_fmt.lua
These settings should appear as follows:
stylua-trigger.png
stylua-trigger.png (130.1 KiB) Viewed 11 times
4. Save the following Lua code to the stylua_fmt.lua file and put that file in the Zeus zScript folder:

Code: Select all

--
--        Name: Lua Reformatting Macro
--
--    Language: Lua Macro
--
-- Description: This macro will execute the StyLua.exe utility to reformat
--              the current file.
--
--   IMPORTANT: Make sure the StyLua.exe is located in the system PATH. The
--              easy way to do this, is to copy the file to the Zeus install
--              folder, or the zGNU folder.
--

local utils = require "utils"

function key_macro()
  -- details about the active document
  local document, named, read_only, document_type = utils.document_details()

  if (document_type ~= "Lua Document Type") or (document == 0) or (named == 0) then
    -- only complain if this is not a trigger
    if is_trigger() == 0 then
        message("This macro only works with named, writable Lua documents.")
        beep()
    end

    return
  end

  -- the directory of the current document
  local dir = macro_tag("$fdd")

  -- the name of the current document
  local file_name = macro_tag("$f")

  message("Lua is formatting the code....")

  -------------------------------------------------------------------------
  --  NOTE: Additional StyLua options can by added here
  ------------------------------------------------------------------------
  local cmd_options = ""

  -- build up the final styling command line
  local cmd = "StyLua.exe" .. " " .. cmd_options .. utils.checkLFN(file_name)

  -- for debugging only
  --message_box(1, cmd, "Command Line")

  -------------------------------------------------------------------------------
  -- System Control Values
  -------------------------------------------------------------------------------
  --   1 - save the document before running the program
  --   2 - capture any standard output generated by the program
  --   4 - capture any standard error generated by the program
  --   8 - ask for additional arguments
  --  16 - the program will use the MS-DOS command interpreter (ie. dir *.* etc)
  --  32 - wait for the program to complete (the ESC key will cancel the wait)
  --  64 - run the program in a visible DOS session (otherwise runs hidden)
  -- 128 - capture output to a compiler window (not the default tool window)
  -------------------------------------------------------------------------------
  -- Output Control Flags:
  -------------------------------------------------------------------------------
  --   0 - ALWAYS
  --   1 - NEVER
  --   2 - ERRORS
  --   3 - WARNINGS
  --   4 - ANYOUTPUT
  -------------------------------------------------------------------------------

  -- display the tool window if any output is generated
  local ANYOUTPUT = 4

  -- run with 'save', 'capture output' and 'wait' and 'compiler' options
  local flags = 1+2+4+32+128

  local caption = "Lua fmt: " .. file_name

  -- remember the current line count
  local current_count = get_line_count()

  if (system(cmd, dir, flags, caption, ANYOUTPUT) == 0) then
    -- reload the newly formatted document
    FileReloadCurrent()

    -- get the new line count
    local line_change = get_line_count() - current_count

    -- maintain the current line location
    if line_change ~= 0 then
        local line_current = get_line_pos() + line_change

        if line_current < 0 then line_current = 1 end

        set_line_pos(line_current)
    end

    -- find the window created by the system call
    local window_id = window_find(caption);

    -- dispose of the system call window
    window_close(window_id);

    -- no errors detected
    set_return_code(1)

    -- some feedback
    message("The formatting of the document is complete.")
  else
    -- formatting errors detected
    set_return_code(0)
  end
end

key_macro() -- run the macro
With these changes in place the Lua code will be automatically formatted each time the file is saved.

Jussi
Post Reply