The lua code formatter extension for Defold Editor

I don’t want to use VSCode, but I need a lua code formatter for Defold Editor. So I made it.


Todos:

  1. Create a Github repository for it so users can add it as dependency in game.project
  2. Add it to Defold Assets

latest version: 1.1.0

now the extension is just a editor_script file, but I will improve it later.

new features:

  1. Now you can add a shortcut for it in File → Preferences → keymap . Then the Defold Editor will save and format the currently opened file.
  2. add support for editor_script files

version: 1.0.0

now the extension is just a editor_script file, but I will improve it later.

features:

  1. right click a script, gui_script, lua, render_script file on Assets to format it
  2. support stylua.toml
4 Likes

1.0.0

stylua.editor_script

local M = {}

local STYLUA_PATH = "E:/Softwares/stylua-windows-x86_64/stylua.exe"

local function ends_with(str, suffix)
    return str:sub(-#suffix) == suffix
end

local function is_lua_file(path)
    return ends_with(path, ".lua")
    or ends_with(path, ".script")
    or ends_with(path, ".gui_script")
    or ends_with(path, ".render_script")
end

local function format_file(path)
    local local_path = "." .. path

    local ok, result_or_err = pcall(
        editor.execute,
        STYLUA_PATH,
        "--search-parent-directories",
        local_path,
        {
            reload_resources = true,
            out = "capture",
            err = "stdout"
        }
    )

    if not ok then
        editor.ui.show_dialog(editor.ui.dialog({
            title = "StyLua Error",
            content = editor.ui.label({
                text = tostring(result_or_err)
            }),
            buttons = {
                editor.ui.dialog_button({
                    text = "OK",
                    default = true,
                    result = "ok"
                })
            }
        }))
    end
end

function M.get_commands()
return {
    editor.command({
        label = "Format Lua (StyLua)",
        locations = { "Edit", "Assets" },
        query = {
            selection = {
                type = "resource",
                cardinality = "one"
            }
        },
        active = function(opts)
            local path = editor.get(opts.selection, "path")
            return is_lua_file(path)
        end,
        run = function(opts)
            editor.save()

            local path = editor.get(opts.selection, "path")
            format_file(path)
        end
    })
}
end

return M

How to use it?

step1: copy this editor_script to your project directory

step2: download stylua and set its path in editor_script

step3: reload editor scripts

step4: right click a script file in Assets

step5: if you want to write stylua.toml, you can create it on project directory

1 Like

Really nice! Is there any way to make it format on save?

This is one of those features I wish came with the editor by default.

1 Like

Thanks!

I also want to add format-on-save support. I’ll try to implement it when I have some spare time.

2 Likes

Very useful and super straight-forward to use, thanks!

Can’t wait to format-on-save support!

1 Like

Thanks! I’m glad you found it useful.

1.1.0

I just added one line of code to M.get_commands() function to implement this handy feature.

stylua.editor_script

local M = {}

local STYLUA_PATH = "E:/Softwares/stylua-windows-x86_64/stylua.exe"

local function ends_with(str, suffix)
    return str:sub(-#suffix) == suffix
end

local function is_lua_file(path)
    return ends_with(path, ".lua")
    or ends_with(path, ".script")
    or ends_with(path, ".gui_script")
    or ends_with(path, ".render_script")
    or ends_with(path, ".editor_script")
end

local function format_file(path)
    local local_path = "." .. path

    local ok, result_or_err = pcall(
        editor.execute,
        STYLUA_PATH,
        "--search-parent-directories",
        local_path,
        {
            reload_resources = true,
            out = "capture",
            err = "stdout"
        }
    )

    if not ok then
        editor.ui.show_dialog(editor.ui.dialog({
            title = "StyLua Error",
            content = editor.ui.label({
                text = tostring(result_or_err)
            }),
            buttons = {
                editor.ui.dialog_button({
                    text = "OK",
                    default = true,
                    result = "ok"
                })
            }
        }))
    end
end

function M.get_commands()
return {
    editor.command({
        id = "format_lua_stylua",     -- I added a id for keymap
        label = "Format Lua (StyLua)",
        locations = { "Edit", "Assets" },
        query = {
            selection = {
                type = "resource",
                cardinality = "one"
            }
        },
        active = function(opts)
            local path = editor.get(opts.selection, "path")
            return is_lua_file(path)
        end,
        run = function(opts)
            editor.save()

            local path = editor.get(opts.selection, "path")
            format_file(path)
        end
    })
}
end

return M

1 Like

I just found out today that someone had already made a similar extension.

After reading druid.editor_script, I finally understand how to define, use and display preferences. Now I can improve my stylua.editor_script

Thank you! @Insality

1 Like