Jump to content

CreateFunctionHeader LUA Functions


Beege
 Share

Recommended Posts

Some time ago I was playing around in the AutoITTools.lua file and learned that there was a function for generating function headers. For reasons I don't know, it was never added to the scite tools menu. So if you would like to get it working here is how I got it to work.

Add this code to file au3.properties located in "C:\Program Files\AutoIt3\SciTE\properties". You can also get to it from Scite menu Options --> open au3.properties. Restart Scite and it should be in your menu.

# 37 Generates a function header and inserts it into the document.
command.name.37.$(au3)=Generate Function Header
command.subsystem.37.$(au3)=3
command.37.$(au3)=InvokeTool AutoItTools.InsertFunctionHeader
command.shortcut.37.$(au3)=Ctrl+Alt+H
command.save.before.37.$(au3)=2

The generated header will look something like this

; ===================================================================
; _ExampleFunction($iKey, ByRef $second, $third, $forth = 4, $fifth = 5, $sixth =  6)
;
; Description -
; Parameters:
;   $iKey - IN -
;   $second - IN/OUT -
;   $third - IN -
;   $forth - IN/OPTIONAL -
;   $fifth - IN/OPTIONAL -
;   $sixth - IN/OPTIONAL -
; Returns:
;   Success -
;   Error -
; Remarks -
; ===================================================================
Func _ExampleFunction($iKey, ByRef $second, $third, $forth = 4, $fifth = 5, $sixth =  6)
    ;;
EndFunc

I also made a modified version of these functions that will generate a header more like the ones you are used to seeing around autoit.

To use copy and paste these two functions attached to file AutoITtools.lua located in C:\Program Files\AutoIt3\SciTE\LUA.

CreateAutoITFunctionHeader.zip

Then again, add this code to file au3.properties (code is different from above code)

# 37 Generates a function header and inserts it into the document.
command.name.37.$(au3)=Generate Function Header
command.subsystem.37.$(au3)=3
command.37.$(au3)=InvokeTool AutoItTools.InsertAutoitFunctionHeader
command.shortcut.37.$(au3)=Ctrl+Alt+H
command.save.before.37.$(au3)=2

Only part I couldn't figure out was how to set default parameters..

; #FUNCTION# ====================================================================================================
; Name...........: _ExampleFunction
; Description ...:
; Syntax.........: _ExampleFunction($iKey , ByRef $second, $third [, $forth =  [, $fifth =  [, $sixth = ]]])
; Parameters ....: $iKey    -
;                  $second  -
;                  $third   -
;                  $forth   -
;                  $fifth   -
;                  $sixth   -
; Return values .: Success  -
;                  Failure  -
; Author ........: Beege
; Modified.......:
; Remarks .......: None
; Link ..........:
; Example .......:
; ===============================================================================================================
Func _ExampleFunction($iKey, ByRef $second, $third, $forth = 4, $fifth = 5, $sixth =  6)
    ;;
EndFunc

Don't forget to add your name to the "Author" variable. Let me know if you have any problems. :(

Edited by Beege
Link to comment
Share on other sites

I use this script (modified):

function InsertFunctionHeader()
    local line, pos = editor:GetCurLine()
    local pos = editor.CurrentPos - pos
    local lineNum = editor:LineFromPosition(pos)
-- "()([Ff][Uu][Nn][Cc][%s]*([%w_]*)%(.-%))([^\r\n]*)"
--    local from, to, name = string.find(line, "Func ([%w_]*)")
    local from, to, name = string.find(line, "[Ff][Uu][Nn][Cc][%s]*([%w_]*)")
    if to ~= nil then
--        local pfrom, pto, pname = string.find(line, "(%([%w%s\"_,$%=@-%.]*[%)_])")
        local pfrom, pto, pname = string.find(line, "(%(.-%))")
        local i = 0
        while string.find(pname, "_%s*$") do    -- Found an underscore, so need to get the next line, too
            i = i + 1
            local tmp = editor:GetLine(lineNum+i)
            local wfrom = string.find(pname, "_%s*$")
            local nfrom = string.find(tmp, "[^%s]")
            pname = string.sub(pname, 1, wfrom-1) .. string.sub(tmp, nfrom, -1)
            pname =     string.gsub(pname, "[\n\r]", "")
        end
        editor:Home()

        editor:AddText(_CreateFunctionHeader(name,pname))
    else
        local ws = editor:WordStartPosition(editor.CurrentPos)
        local we = editor:WordEndPosition(editor.CurrentPos)
        local word = editor:textrange(ws, we)
        -- Must strip the new lines for this to work.
        line = string.gsub(line, "[\n\r]", "")
        word = string.gsub(word, "[\n\r]", "")
        if word == "" then
            editor:AddText(_CreateFunctionHeader("Name", ""))
        elseif word == line then
            editor:LineEnd()
            local lend = editor.CurrentPos
            editor:Home()
            local lstart = editor.CurrentPos
            editor:SetSel(lstart, lend)
            editor:ReplaceSel(_CreateFunctionHeader(word, "") .. "Func " .. word .. "()\r\nEndFunc; " .. word)
         end
    end
end    -- InsertFunctionheader()

function _CreateFunctionHeader(s, p)
    local params = false
    local res = "; #FUNCTION# " .. string.rep("=", 100)
    res = res .. "\r\n; Name...........:\t" .. s
    res = res .. "\r\n; Description....:\t"
    res = res .. "\r\n; Syntax.........:\t" .. s .. p
    res = res .. "\r\n; Parameters.....:\t"
    
    for byref, parameter, optional in string.gfind(p, "(%w-)%s*($[%w_]+)%s*([=]?)") do
        if parameter ~= "" and parameter ~= nil then
            params = true
            if byref ~= "" and byref ~= nil then
                res = res .. parameter .. " - [ByRef] \r\n;\t\t\t\t\t"
            elseif optional ~= "" and optional ~= nil then
                res = res .. parameter .. " - [Optional] \r\n;\t\t\t\t\t"
            else
                res = res .. parameter .. " - \r\n;\t\t\t\t\t"
            end
        end
    end
    
    if params == false then
        res = res .. "None."
    end
    
    res = res .. "\r\n; Return values..:\tSuccess - \r\n;\t\t\t\t\tFailure - "
    res = res .. "\r\n; Author.........:\t"
    res = res .. "\r\n; Modified.......:\t"
    res = res .. "\r\n; Remarks........:\t"
    res = res .. "\r\n; Related........:\t"
    res = res .. "\r\n; Link...........:\t"
    res = res .. "\r\n; Example........:\t"
    
    res = res .. "\r\n; " .. string.rep("=", 111) .. "\r\n"
    
    return res
end    -- _CreateFunctionHeader()

And this is the output:

; #FUNCTION# ====================================================================================================
; Name...........:  TestFunc
; Description....:  
; Syntax.........:  TestFunc($sVar, $sOptVar=1)
; Parameters.....:  $sVar - 
;                   $sOptVar - [Optional] 
;                   
; Return values..:  Success - 
;                   Failure - 
; Author.........:  
; Modified.......:  
; Remarks........:  
; Related........:  
; Link...........:  
; Example........:  
; ===============================================================================================================
Func TestFunc($sVar, $sOptVar=1)
    
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...