On 09/02/2015 12:38 PM, Peter Haworth wrote:
Assuming the errors you are referring to are of the "undeclared variable"
type, I have a utility that inserts local statements for any undeclared
variables.  Maybe I should offer it to the team.

A number of reasons why this isn't a good idea.
But there's a function for it in the glx2 script editor as well.

The late Eric Chatonet and I had a great conversation with Jerry Daniels where we talked about why not to do this (and in the process almost had him convinced to use explicitVars)... I finally said something like I could maybe stomach it if the function grouped variables by theme, keeping like variables together, but that it was beyond what computers were capable of.

The next morning Eric emailed an algorithm that did just that, and we tweaked it over the next few hours and came up with a function that would group variables very nicely. It does rely on CamelCase variable names, but that's the only restriction. Posted here for your amusement. Watch the line wrap - it's deadly.


/**
glx2GetSortedVars
Eric Chatonet's contribution:
Sort a CamelCase variable list thematically
pVarList is a list of variables to sort, one per line
*/
function glx2GetSortedVars pVarList

    local tVar, tChar, tFound, tNumOfChars, tVarList
    local tDeclaration
    local tStart, tEnd
    local tLength

    if pVarList is not empty then
        set the itemdelimiter to comma
        repeat for each line tVar in pVarList
            if IsCaps(char 2 of tVar) then
                delete the first char of tVar --CamelCase notation
            end if
            -- We don't care about trailing digits
repeat until the last char of tVar is not an integer -- incremented vars
                delete the last char of tVar
            end repeat

            -- get the length of the variable name
            put the number of chars of tVar into tNumOfChars
            put tNumOfChars into tEnd

            -- Gather a list of words
-- Relies on CamelCase notation: capitalized words will end up on this list -- tHorizontalOffset will put Horizontal and offset into the list repeat with tStart = tNumOfChars down to 1 -- unfortunately repeat for each can't be used
                -- if we have found the start of a word
-- (make sure we're not looking at just the last char of the word)
                put tEnd - tStart into tLength
if IsCaps(char tStart of tVar) and tStart < tNumOfChars then -- if we've already found a keyword in this variable name
                    if IsCaps(char tEnd of tVar) then --
if char tStart to tEnd - 1 of tVar is not among the items of tFound and tStart is not tEnd - 1 then
                            if tLength > 2 then
put char tStart to tEnd - 1 of tVar & comma before tFound
                            end if
                        end if
                    else
-- This is the first keyword we've found in this variable name
                        -- if we don't have this word in our list yet
if char tStart to tEnd of tVar is not among the items of tFound then
                            -- add it to the list
                            if tLength > 2 then
put char tStart to tEnd of tVar & comma after tFound
                            end if
                        end if
                    end if
                    put tStart into tEnd
                end if
            end repeat -- with i = tNumOfChars down to 1
        end repeat -- for each line tVar in pVarList

        -- now work our way through the list
        repeat for each item tWord in tFound
            put pVarList into tVarList
            filter tVarList with "*" & tWord & "*"
            filter pVarList without "*" & tWord & "*"
            if tVarList is not empty then
                sort tVarList
                -- group similar items onto the same line
                replace cr with comma & space in tVarList
put the cIndent of me & "local" && tVarList & cr after tDeclaration
            end if
        end repeat

        -- if there's anything left in pVarList at this point then
        -- we want to be sure to add it. This covers the case of variables
        -- not in CamelCase notation.
        put 1 into tNumOfChars
        repeat for each line tLine in pVarList
            if tNumOfChars is 1 then
                put the cIndent of me & "local " after tDeclaration
            end if
            put tLine after tDeclaration
            add 1 to tNumOfChars
            -- See if we've put four variables on this line already
            if tNumOfChars is 4 then
                put cr after tDeclaration
                put 1 into tNumOfChars
            else
                put comma after tDeclaration
            end if
        end repeat
        if char -1 of tDeclaration is comma then
            put cr into char -1 of tDeclaration
        end if
        sort tDeclaration

-- end with a delimiter to separate the automatically-generated declarations
        -- from any declarations previously entered by the user
        -- This will help identify problem declarations
        put the cIndent of me & "-----" after tDeclaration
        put the cIndent of me & "-----" & cr before tDeclaration
    end if
    return tDeclaration
end glx2GetSortedVars

--
 Mark Wieder
 ahsoftw...@gmail.com

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to