Prettier Reformatting

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
Site Admin
Posts: 2547
Joined: Fri Aug 13, 2004 5:10 pm

Prettier Reformatting

Post by jussij »

This macro can be used to run the Prettier to format the current document by hand or whenever the file is saved.

As an example consider the test.js file which contains the following code:

Code: Select all

foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
With this macro in place when the file is saved the contents of the file will be automatically changed to be the following:

Code: Select all

NOTE: I following error is produced then it means the Windows user id contains a space which then then means there is a space in the Windows user folder:
npm ERR! Could not install from "User Name\AppData\Roaming\npm-cache\_npx\19748" as it does not contain a package.json file.
To fix this you need to point the npm cache to the short-name version of the user folder as shown below.

Find out the short-name of the users folder:

Code: Select all

C:\Users>dir /x
Use that short-name of that folder to set the npm cache:

Code: Select all

npm config set cache "C:\Users\UserName~1\AppData\Roaming\npm-cache" --global[code]

Save the code below to the Zeus [i]zScript[/i] folder using the [i]prettier_fmt.lua[/i] file name and setup the macro as per the instruction found in the comment section of that macro script: 
--        Name: Prettier Reformatting Macro
--    Language: Lua Macro
-- Description: This macro will execute the Prettier utility to reformat
--              the current file.
--              To run the macro directly use the Macro, Execute menu.
--              To automatically run the macro of any file save action edit the
--              document type and in the Triggers section add the following
--              command to Save and Save As postfix section:
--                  $zud\zScript\prettier_fmt.lua
local utils = require "utils"

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

  if (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 documents.")

  -- More command line options found here:

  -- write file in place with not logging information command line options
  local cmd_options = "--write --loglevel silent"

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

  -- the package file
  local package_file = dir .. "package.json"

  -- NOTE: Without a package.json file the Prettier formatting request will fail with
  --       an error so we check to make sure that file exists and if it is not found
  --       change the command line options accordingly.
  --       An alternative to this approach would be changing the command line to use
  --       a global configuration file option.
  if (zeus.access(package_file, 0) ~= 0) then
    -- revert to a 'no-config' setup
    cmd_options = cmd_options .. " --no-config"

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

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

  -- build up the final command line
  local cmd = "npx --quiet prettier" .. " " .. cmd_options .. " " .. utils.checkLFN(file_name)

  -- for debugging only
  --message_box(1, cmd, "Zeus Debuggging Only - 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

  -- use 'save', 'capture output', 'capture error', 'MS-DOS', 'wait' and 'compiler' options
  local flags = 1+2+4+16+32+128

  local caption = "Prettier 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 docuemnt

    -- 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


    -- no errors detected

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

key_macro() -- run the macro
Post Reply