C# Getter/Setter Macro

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

C# Getter/Setter Macro

Post by jussij »

The macro below will wrap a variable in a get/set property.

Consider this code:

Code: Select all

        private static Dictionary<String, int> someDictionary = new Dictionary<String, int>;
Placing the cursor on the someDictionary variable and running the macro will result in this code being added to the file:

Code: Select all

    public Dictionary<String, int> SomeDictionary
    {
        get { return this.someDictionary; }
        set { this.someDictionary = value; }
    }
Here is the Lua macro:

Code: Select all

--
--        Name: C# Getter/Setter Macro
--
--    Language: Lua Macro
--
-- Description: This macro will create the C# getter and setter properties 
--              for the parameter under the current cursor.
--              
--              To use this macro place the cursor on the variable to be
--              encapsulated and run the macro.
--              

function trim(s)
    if s ~= nil then
        -- trim the string
        s = s:gsub("^%s*(.-)%s*$", "%1")
    end
    return s
end

function remove_keywords(s)
    -- remove any white space
    s = trim(s)

    -- remove any protected attribute
    s = s:gsub("internal" , "")
    s = s:gsub("private"  , "")
    s = s:gsub("protected", "")
    s = s:gsub("public"   , "")
    s = s:gsub("static"   , "")

    -- do a final clean up
    s = trim(s)
    return s
end

function key_macro()
    -- 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
        -- must have a document
        message("This macro can only be run with a writable document file.")
        beep()
        return
    end

    -- get the current word
    local name = macro_tag("$w")

    if (string.len(name) == 0) then
        -- must have a word
        message("Please place the cursor on the variable that is to be refactored.")
        beep()
        return
    end

    -- upper case the first letter of the word selected
    local Name = name:gsub("%a", string.upper, 1)

    -- make sure we actually changed the name
    if Name == name then
        Name = name:gsub("%a", string.lower, 1)
    end

    local line_text = get_line_text()

    local pattern = nil

    -- watch out for generic items
    if string.match(line_text, "<") ~= nil then
        --
        -- Example: private static Dictionary<String, int> fs = new Dictionary<String, int>;
        --
        pattern = "^%s*([%w_]+<.*>[%[,%]]*)%s+" .. name .. "%s*=%s*.*"
    else
        --
        -- Example: public FileStream fs = new FileStream();
        --
        pattern = "^%s*([%w_]+[%[,%d%]]*)%s+" .. name .. "%s*=%s*.*"
    end

    -- reduce the line of text
    line_text = remove_keywords(line_text)

    -- do a pattern search for the type information
    local definition = string.match(line_text, pattern)

    if definition ~= nil then
        -- disable screen updates
        screen_update_disable()
    
        -- save the current cursor
        cursor_save()
    
        -- move to the end of the current line
        MoveLineEnd()
    
        -- the lines that make up the template text
        local line1 = "\n\npublic {0} {1}\n"
        local line2 = "{\n"
        local line3 = "get { return this.{2}; }\n"
        local line4 = "set { this.{2} = value; }"
    
        -- substitute in the text details
        line1 = string.gsub(line1, "{0}", definition)
        line1 = string.gsub(line1, "{1}", Name      )
        line3 = string.gsub(line3, "{2}", name      )
        line4 = string.gsub(line4, "{2}", name      )

        --
        -- NOTE: We write out the lines letting the smart indenting
        -- and bracing do the reset
        --
        write(line1)
        write(line2)
        write(line3)
        write(line4)

        -- restore cursor
        cursor_restore()
    
        -- restore screen updates
        screen_update_enable()
        screen_update()
    else
        message("No type information found. Make sure the cursor is on the variable.")
    end
end

key_macro() -- run the macro
Post Reply