PHP Function DocBlock

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: 2436
Joined: Fri Aug 13, 2004 5:10 pm

PHP Function DocBlock

Post by jussij » Thu May 10, 2012 4:11 am

The following Zeus Lua macro creates a PHP function DocBloc.

Consider a PHP function like this.

Code: Select all

    function myCmp ($a, $b)
    {
        ....
    }
Running this macro will then create the following DocBlock:

Code: Select all

    /**
     *
     * @param type $a
     * @param type $b
     * 
     * @return type
     */
    function myCmp ($a, $b)
    {
        ....
    }
Here is the macro source code:

Code: Select all

--
--        Name: PHP Function DocBlock
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This macro creates a PHP function DocBlock. To use the macro
--              place the cursor on the line containing the function or the
--              line preceeding the function and just run the macro.
--
--    Example:  Consider a function like this.
--
--                  function myCmp ($a, $b)
--                  {
--                      ....
--                  }
--
--              Running this macro will then create the following DocBlock:
--                  /**
--                   *
--                   * @param type $a
--                   * @param type $b
--                   *
--                   * @return type
--                   */
--                  function myCmp ($a, $b)
--                  {
--                      ....
--                  }
--

-- trim a string
function string:trim()
    return (self:gsub("^%s*(.-)%s*$", "%1"))
end

-- split a string on the delimiter
function string:split(delimiter)
    local result = { }
    local from = 1
    local delim_from, delim_to = string.find(self, delimiter, from)

    while delim_from do
        table.insert(result, string.sub(self, from, delim_from - 1))
        from = delim_to + 1
        delim_from, delim_to = string.find(self, delimiter, from)
    end

    table.insert( result, string.sub(self, from ))

    return result
end

function key_macro()
    -- function myCmp ($a, $b)

    -- macro only works for documents
    local document = is_document()

    -- macro only works for read/write documents.
    local locked = is_read_only()

    if (locked == 1) or (document == 0) then
        message("This macro only works for writable document files.");
        beep();
        return;
    end

    local current = true
    local line    = get_line_pos()
    local text    = get_line_text(line)

    -- look for the function details on the current line
    local index1, index2, func, brace1, args, brace2 = string.find(text, "function ([%a]+)[%s]*(%(*)(.*)(%)+)"  )

    if func == nil then
        text = get_line_text(line + 1)

        -- look for the function details on the next line
        index1, index2, func, brace1, args, brace2 = string.find(text, "function ([%a]+)[%s]*(%(*)(.*)(%)+)"  )

        current = false
    end

    if func ~= nil then
        local result = func
        local params = ""

        if args ~= nil then
            for index, arg in pairs(string.split(args, ',')) do
                params = params .. " * @param type " .. string.trim(arg) ..  "\n"
            end

            params = params .. " *" .. "\n"
        end

        -- /**
        --  *
        --  * @param type $a
        --  * @param type $b
        --  * @return type
        --  */
        --
        local result = "";

        result = result .. "/**"               .. "\n"
        result = result .. " *"                .. "\n"
        result = result .. params
        result = result .. " * @return type"   .. "\n"
        result = result .. " */"

        if current == true then
            MoveLineUp()
        end

        MoveLineHome()
        cursor_save()
        -- add the result to the document without using smart indenting
        write(result, false)
        cursor_restore()
    else
        message("No function details found!")
        beep()
    end

    return
end

key_macro() -- run the macro

Post Reply
kuchnie warszawa opinie

Who is online

Users browsing this forum: No registered users and 1 guest