Jump to content

Auto Add Scite CallTips for my UDF folder


boomingranny
 Share

Recommended Posts

The attached Script Adds Scite Call tips for all functions in all the AU3 files in the (specified) UDF folder.
This is great if you (like me) have heaps of UDFs and you can't remember the syntax for them.

It will also use the description: or description . . . : comment above a function as the Call tip description
(and if the text "Internal function" is in an above comment then it will ignore that function)

Lastly, it will add a comment "header" to the functions in your UDFs if they don't have one,
allowing you to edit the file, give better descriptions for the functions, then run this script again to update your Call tips.

CAUTION: This script modifies your AU3 UDF files (as mentioned in the previous paragraph), use at your own risk (it does back them up to c:\temp\UDF-backup first), I have only done limited testing.

;NOT A UDF
#include <file.au3>

;add NOT A UDF description to the top of an AU3 file to let this script know to skip it
;
;This script will parse all the AU3 files in [$UDFFolder] and then add Scite Calltips for them
;(A CallTip is that popup that appears after your type a command in Scite)
;It will also add description headers to your functions that look like the following:
; #FUNCTION# ====================================================================================================================
; Name ..........: Example
; Description ...:  (Requires: example.au3)
; ===============================================================================================================================

;--------------------------------------------------------------------------------------------------------------------------------
;SETTINGS:
;--------------------------------------------------------------------------------------------------------------------------------
Local $AutoITIncludesFolder = RegRead("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "Include")
$AutoITIncludesFolder = StringSplit($AutoITIncludesFolder, ";")
$UDFFolder = FileSelectFolder("Select your UDF folder",$AutoITIncludesFolder[1],6,$AutoITIncludesFolder[1])
$bAutoAddFunctionHeadersToUDFFiles = True ;the original UDF files will be backed up in $UDFBackupLocation (unless they already exist, to prevent good backups being overwritten by bad ones)
$bDeleteExistingFiles = True ;delete (and regenerate) the existing user CallTip and Keyword files
$UDFBackupLocation = "c:\temp\UDF-Backup"
;--------------------------------------------------------------------------------------------------------------------------------

;Global Variables
Global $array[1] ;[row][0] = Function Name, [row][1] = Function Parameters, [row][2] = Function Description
Global $SciTEUserFolder = FindSciteUserFolder()
Global $CallTipFile = $SciTEUserFolder & "\au3.user.calltips.api"
Global $KeywordFile = $SciTEUserFolder & "\au3.userudfs.properties"

If $bDeleteExistingFiles Then
    FileDelete($CallTipFile)
    FileDelete($KeywordFile)
endif

ConsoleWrite("Calltip file: "&$CallTipFile&@CRLF)
ConsoleWrite("Keyword file: "&$KeywordFile&@CRLF)
ConsoleWrite("UDF Folder:   " &$UDFFolder&@CRLF)

main()

Func main()
    loadCallTipFile() ;load $array from CallTip file
    $UDFFiles = _FileListToArrayRec($UDFFolder,"*.au3",$FLTAR_FILES ,$FLTAR_RECUR,$FLTAR_NOSORT,$FLTAR_FULLPATH) ;Load list of UDF files in UDF path and subfolders

    ParseAll($UDFFiles) ;find all the functions in all the UDF files
    WriteKeywordFile() ;write them all to the keyword file
    WriteCallTipFile() ;write them all to the calltip file

    ConsoleWrite ("Displaying list of Functions"&@CRLF)
    _ArrayDisplay($array,"List of User Defined Functions") ;show the user the array
EndFunc

Func loadCallTipFile()
    Local $aKeywordFile

    ; Read in existing CallTip file
    _FileReadToArray($CallTipFile, $aKeywordFile)
    If Not IsArray($aKeywordFile) Then local $aKeywordFile[1] ;if file doesn't exist start with a blank array
    ;Remove any empty lines
    For $i = $aKeywordFile[0] To 1 Step -1
        If $aKeywordFile[$i] = "" Then _ArrayDelete($aKeywordFile,$i)
    Next

    ;Sort array
    _ArraySort($aKeywordFile, 0, 1)

    ;split data to 3 columns (function name, params, description)
    ReDim $array[UBound($aKeywordFile)][3]
    $array[0][0] = $aKeywordFile[0]

    For $i = 1 To $aKeywordFile[0]
        $read = $aKeywordFile[$i]
        $array[$i][0] = StringBefore($read, "(",true)
        $array[$i][1] = StringFormatParam(StringBetween($read, "(",")",true))
        $array[$i][2] = StringAfter($read, ")",true)
    Next
EndFunc

Func ParseAll($UDFFiles)
    ;parse UDF files one by one, adding functions inside
    For $i = 1 To $UDFFiles[0]
        ConsoleWrite ("Parsing "&$UDFFiles[$i]&@CRLF)
        Parse($UDFFiles[$i])
    Next
EndFunc

Func Parse($fileName)
    Local $bFileModified
    Local $aData[1]
    Local $isInternal ;is this an internal function? (has Internal Function in comment prior
    Local $description
    _FileReadToArray($fileName,$aData)
    For $i = 1 To $aData[0]
        $read = $aData[$i]
        $read = StringStripWS($read,1)
        select
            Case StringLeft($read,1) = ";" ;comment
                $read = StringTrimLeft($read,1)
                If $read = "NOT A UDF" Then Return ;skip this file, it is not a UDF
                If Not $description Then
                    If StringInStr($read,"Description:") Then
                        $description = StringAfter($read,":",true)
                    elseIf StringInStr($read,"Description ...:") Then
                        $description = StringAfter($read,":",true)
                    endif
                endif
                If Not $isInternal Then
                    If StringInStr($read,"Internal Function") Then $isInternal = true
                endif
            Case StringLeft($read,5) = "func " ;function
                If Not $isInternal Then
                    $read = StringTrimLeft($read,5) ;take off the "func " at the front
                    $function = StringBefore($read,"(",true)
                    $param = StringBetween($read,"(",")",true)
                    $param = StringFormatParam($param)
                    $StringRequiresInclude = "(Requires: " & FileTrimFolder($fileName) & ")"

                    If $description Then
                        ;add the Requires Include text on the end of the description if it isn't already there
                        If StringRight($description,StringLen($StringRequiresInclude)) <> $StringRequiresInclude Then
                            $description &= " " &$StringRequiresInclude
                        endif
                    ElseIf $bAutoAddFunctionHeadersToUDFFiles Then  ;no description, lets add a header to this function so a description can more easily be added
                        $bFileModified = true
                        $write = @CRLF
                        $write &= "; #FUNCTION# ===================================================================================================================="&@CRLF
                        $write &= "; Name ..........: "&$function&@CRLF
                        $write &= "; Description ...: "&$StringRequiresInclude&@CRLF
                        $write &= "; ==============================================================================================================================="&@CRLF
                        $write &= $aData[$i]
                        $aData[$i] = $write
                    endif


                    If AddToArray($function,$param,$description) Then
                        ConsoleWrite (" +"&$function&" "&$param&" "&$description&@CRLF)
                    Else
                        ConsoleWrite ("  "&$function&" "&$param&" "&$description&@CRLF)
                    endif
                endif
                ;reset variables
                $isInternal = false
                $description = ""
        EndSelect
    Next
    If $bFileModified Then
        ConsoleWrite ("Added headers to "&$fileName&@CRLF)
        DirCreate($UDFBackupLocation)
        FileMove($fileName,$UDFBackupLocation,0)
        _FileWriteFromArray($fileName ,$aData,1)
    endif
EndFunc

Func AddToArray($function,$param,$description)
    For $i = 1 To $array[0][0]
        If $array[$i][0] = $function Then
            $array[$i][1] = $param
            $array[$i][2] = $description
            Return false
        EndIf
    Next

    ;add item to array
    ReDim $array[UBound($array)+1][3]
    $array[0][0]+= 1
    $count = $array[0][0]
    $array[$count][0] = $function
    $array[$count][1] = $param
    $array[$count][2] = $description
    Return true
EndFunc



Func WriteKeywordFile()
    ;Backup existing Keyword file
    ConsoleWrite ("Backing up Keyword File ("&$KeywordFile&")"&@CRLF)
    FileMove($KeyWordFile, $KeyWordfile & ".bak", 1)
    If FileExists($KeywordFile) Then
        ConsoleWrite ("!Failed to backup Keyword File ("&$KeywordFile&")"&@CRLF)
        Return
    endif

    ConsoleWrite ("Writing Keyword File ("&$KeywordFile&")"&@CRLF)
    Local $output
    Local $buffer = "au3.keywords.user.udfs="
    For $i = 1 To $array[0][0]
        $FunctionName = $array[$i][0]
        $buffer &= StringLower($FunctionName)

        If StringLen($buffer) > 100 Then
            $buffer &= " \" & @CRLF
            $output &= $buffer
            $buffer = @TAB
        Else
            $buffer &= " "
        EndIf
    Next
    $output &= $buffer

    FileWrite($KeywordFile,$output)
EndFunc

Func WriteCallTipFile()
    ;Backup existing CallTip file
    ConsoleWrite ("Backing up CallTip File ("&$CallTipFile&")"&@CRLF)
    FileMove($CallTipFile, $CallTipFile & ".bak", 1)
    If FileExists($CallTipFile) Then
        ConsoleWrite ("!Failed to backup CallTip File ("&$CallTipFile&")"&@CRLF)
        Return
    endif

    ConsoleWrite ("Writing CallTip File ("&$CallTipFile&")"&@CRLF)
    For $i = 1 To $array[0][0]
        $FunctionName = $array[$i][0]
        $param = $array[$i][1]
        $description= $array[$i][2]
        FileWriteLine($CallTipFile,$FunctionName&" "&$param&" "&$description)
    Next
EndFunc

Func StringFormatParam($text)
    Local $output
    Local $start = "( "
    Local $end
    $text = StringStripWS($text,7)
    $aParam = StringSplit($text,",")
    For $i = 1 To $aParam[0]
        $read = $aParam[$i]
        If StringLeft($read,5) = "ByRef" Then $read = StringReplace($read,"ByRef","ByRef") ;fix the case
        If StringInStr($read,"=") Then
            $read = StringBefore($read,"=",true)
            $output &= "[, "&$read
            $end &= "]"
        Else
            $output &= ", "&$read
        endif
    Next
    $output = StringTrimLeft($output,2)
    $output = $start & $output & $end&" )"

    Return $output
EndFunc

Func FindSciteUserFolder()
    ; Check for SCITE_USERHOME Env variable and used that when specified.
    ; Else when OS is better than Winxp we use the INI from LocalAPPDATA
    If EnvGet("SCITE_USERHOME") <> "" And FileExists(EnvGet("SCITE_USERHOME")) Then
        Return EnvGet("SCITE_USERHOME")
    ElseIf EnvGet("SCITE_HOME") <> "" And FileExists(EnvGet("SCITE_HOME")) Then
        return EnvGet("SCITE_HOME")
    Else
        return @UserProfileDir ; original sciTE default location
    EndIf
EndFunc

func FileGetFolder($fileName)
    ;Get the file Path/folder
    if FileIsFolder($fileName) then return $fileName ;already is a foldername
    if FileIsFile($fileName) or StringInStr($fileName,".") then ;looks to be a filename
        Local $tmp = StringInStr ($fileName,"\",0,-1)
        if $tmp <> 0 Then
            $fileName = StringLeft($fileName,$tmp-1)
        endif
    endif
    $fileName = FilePathRemoveTrailingSlash($fileName)
    return $fileName
EndFunc


Func FileTrimFolder($fileName)
    $folder = FileGetFolder($fileName)
    return StringTrimleft($fileName,StringLen($folder)+1) ;chop the folder and the \ after teh folder
EndFunc

Func StringBetween($text, $startString, $endString, $StringStripWS = False)
    Local $loc, $loc2
    $loc = StringInStr($text, $startString)
    If $loc = 0 Then Return
    $loc += StringLen($startString)
    $loc2 = StringInStr($text, $endString, 0, 1, $loc)
    If $loc2 = 0 Then Return

    $return = StringMid($text, $loc, $loc2 - $loc)

    If $StringStripWS Then
        $return = StringStripWS($return, 3)
    EndIf

    Return $return

EndFunc   ;==>StringBetween

Func StringBefore($string, $substring, $StringStripWS = False)
    Local $return
    Local $loc = StringInStr($string, $substring)
    If $loc Then
        $Return = StringLeft($string, $loc - 1)
    EndIf
    If $StringStripWS Then
        $return = StringStripWS($return, 3)
    EndIf
    Return $return
EndFunc   ;==>StringBefore

Func StringAfter($string, $substring, $StringStripWS = False)
    Local $return
    Local $loc = StringInStr($string, $substring)
    If $loc Then
        $return = StringTrimLeft($string, $loc + StringLen($substring)-1)
    EndIf
    If $StringStripWS Then
        $return = StringStripWS($return, 3)
    EndIf
    Return $return
EndFunc   ;==>StringAfter

func FileIsFolder($fileName)
    ;is this file actually a folder?
    if not FileExists($fileName) then return false ;doesn't exist
    if StringInStr(FileGetAttrib($fileName),"D") then return true ;filename is a folder
EndFunc

func FileIsFile($fileName)
    ;Does this file exist, but its not a folder?
    if not FileExists($fileName) then return false ;doesn't exist
    if not FileIsFolder($fileName) then return true ;not a folder
EndFunc

Func FilePathRemoveTrailingSlash($fileName)
    while StringRight($fileName,1) = "\"
        $fileName = StringTrimRight($fileName,1)
    WEnd
    return $fileName
EndFunc

Also - I am aware of The User CallTip Manager in the Scite Config script (I used it as my starting point)
It didn't meet my needs in that it required me to enter each function one by one (and I have a large mess of a UDF folder)

_DanAddCallTips.au3

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