Ruff Python Formatting

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

Ruff Python Formatting

Post by jussij »

Ruff is a linter and formatter for Python: https://github.com/astral-sh/ruff/

For details on how to install and configure Ruff for Zeus go here: https://www.zeusedit.com/phpBB3/viewtopic.php?t=8407

With Ruff installed the following macro can be used to format a Python file inside of Zeus.

Using the Macro
To use this macro, save the code found below to the following Zeus file/folder location:

Code: Select all

zScript\ruff_fmt.lua
With any Python file as the active document use the Macros, Execute Script menu to run this script file:
ruff_fmt.lua
This should result will be that the active Python file will be reformatted.

File Save Macro
The macro can be configured to be run automatically on the file save action. To do this use the Options, Document Types menu, edit the Python Document Type and in the Triggers section add the following changes:

Code: Select all

File Save Postfix: $zud\zScript\ruff_fmt.lua
File Save As Postfix: $zud\zScript\ruff_fmt.lua
With those changes in place the macro will be automatically run any time a Python file is saved.

Jussi

Code: Select all

--
--        Name: Ruff Reformatting Macro
--
--    Language: Lua Macro
--
-- Description: This macro will execute the ruff.exe utility to reformat
--              the current file.
--

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 ~= "Python 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 Python documents.")
        beep()
    end
    return 0
  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("Python is formatting the code....")

  local cmd_options = "format  "

  -- build up the final styling command line
  local cmd = "ruff.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 NEVER = 1
  local ERRORS = 2
  local ANYOUTPUT = 4

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

  local caption = "Ruff fmt: " .. file_name

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

  if (system(cmd, dir, flags, caption, NEVER) == 0) then
    -- reload the newly formatted docuemnt
    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

        -- check for an underflow of the line number
        if line_current <= 0 then line_current = 1 end

        -- maintain the current line postion
        set_line_pos(line_current)
    end

    -- no errors detected
    set_return_code(1)

    -- some feedback
    message("The formatting of the document is complete.")
  else
    message("The Python code in this file contains errors. Use the Window menu and refer to the Ruff fmt output for more details.")

    beep();

    -- formatting errors detected
    set_return_code(0)
  end
end

key_macro() -- run the macro
Post Reply