sponsor Vim development Vim logo Vim Book Ad

cppgetset.vim : Convert a data type definition into get/set data members.

 script karma  Rating 0/0, Downloaded by 1728  Comments, bugs, improvements  Vim wiki

created by
Robert KellyIV
 
script type
ftplugin
 
description
Ment for c++ class data members; modify for others...
Does not work with static data members. (see comment in script, easy enough to rem out the check if you like)

The Although inserting both get and set member functions is currently hard coded the format (brace format and some buityfication of the var name) is handled by optional global option vars. NOTE you do not have to define anything unless you want different ouput from the default.

NOTE this sniplet is in the .vim file
"Place something like the below in your .vimrc or where ever you like to keep
"    your global option vars and change as desired.
""
""*****************************************************************
"" GETSET Options: {{{
""*****************************************************************
"" You are welcomed to email me if there is another format you would like for
""    this, please include example code!
"" See cppgetset.vim for more documtation.
"" Member Function Name Prefix:
""        0 for (default)
""            "Get_"
""            "Set_"
""        else
""            "get"
""            "set"
"let g:getset_StyleOfGetSetMethod                 = 1
"" Brace Style:
""        0 for (default)
""            func()
""            {
""            }
""        else
""            func() {
""            }
"let g:getset_StyleOfBraces                       = 1
"" HowTo Trim Var Prefix:
""        1 for (default)
""            if Var prefix is m_ remove the m so the prefix is _
""            else prepend _
""        2 for
""            Prepend _
""        else
""            do not modify Var
""    I.e.
""        0 = m_lFlags    ->    m_lFlags
""            mlFlags     ->    mlFlags
""        1 = m_lFlags    ->    _lFlags
""            mlFlags     ->    _mlFlags
""        2 = m_lFlags    ->    __lFlags
""            mlFlags     ->    _mlFlags
"let g:getset_Trim_VarPrefix                      = 2
"" Howto Trim Member Function Name Var Prefix:
""        1 for (default)
""            Remove m_
""        2 for
""            Remove m
""        else
""            do not modify Var
"let g:getset_Trim_MemberFunctionNameVarPrefix    = 2
"" }}}
""





Example:
class CTester
{
    char    m_caBuffer[_MAX_PATH];
    int        m_iLenBuffer; // 2
    long    m_lFlags;
    // Static member declaration.
    // [Feral:275/02@15:02] this SHOULD abort out;
    static long m_bytecount;
}
Place cursor on line with m_caBuffer and :GETSET
Place cursor in .cpp file where you want the get/set member function implimentations and :GETSET gives you:


// {{{
/*!
* \brief    Get m_caBuffer.
*
* \return    m_caBuffer as a char.
*/ // }}}
char CTester::Get_caBuffer()
{
    return(m_caBuffer);
}

// {{{
/*!
* \brief    Set m_caBuffer.
*
* \param    _caBuffer    The new value for m_caBuffer (as a char).
*/ // }}}
void CTester::Set_caBuffer(char _caBuffer)
{
    m_caBuffer = _caBuffer;
    return;
}


Note in this example in particular you'll probably need to edit the actual code to get/set but this is a nice fast starting spot...
Note the comment method used is suitable for Doxygen (I think!) easy enough to change though, email me and I'll add support for others... provide examples though!

Note that you call :GETSET twice (I was playing with this idea--one less thing to remember) The function decides what to do based on the file extension. If :GETSET's attempt at being clever is incorect, or you want to place inline member functions you can easily overide what :GETSET does with the optional params g (get), p (put), i (inline), thus to place inline memberfunctions you :GETSET i

Inspired by javaGetSet.vim by Tom Bast See: http://vim.sourceforge.net/script.php?script_id=436
Register pasting origial idea from one of them scripts at vim.sf.net :)

Also be sure to look at Luc Hermitte's cpp_InsertAccessors.vim (See: http://hermitte.free.fr/vim) it is basicaly superior (but a bit larger)

Happy VIMing!
 
install details
Drop in ftplugin/cpp or the like.

Uninstall is simply remove the script file.

To TRY this drop it in your ftplugin/cpp then open a header file, locate a class data member, :GETSET on it, then move where you want inline get set methods and :GETSET i ... if you don't like it, just remove it else look at the options :)
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
cppgetset.vim 0.2 2002-10-17 6.0 Robert KellyIV * More robust class/struct handling
* Global vars for options to determin how the get set methods look.
* Proper ftplugin, MUST be placed in ftplugin/cpp or the like.
cppgetset.vim 0.1 2002-10-07 6.0 Robert KellyIV Initial upload
ip used for rating: 3.137.185.180

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github