Tip #288: A keymapping to generate Java setters and getters automatically
tip karma |
Rating 6/3, Viewed by 3064
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
July 25, 2002 19:25 |
|
complexity: |
|
intermediate |
author: |
|
Sheer El-Showk |
|
as of Vim: |
|
5.7 |
This mapping makes it much simpler to write new java classes by simplifying some of the dull repetative coding (ie setters and getters).
To use, first write a basic class with the following format:
public class MyClass
{
private <type> <varname> = <initvalue>;
private <type> <varname> = initvalue>;
// getters
// setters
}
Note the getters/setters comment -- they are important as they are used to place the getters and setters.
The mapping is:
map jgs mawv/ <Enter>"ty/ <Enter>wvwh"ny/getters<Enter>$a<Enter><Enter>public <Esc>"tpa<Esc>"npbiget<Esc>l~ea()<Enter>{<Enter><Tab>return <Esc>"npa;<Enter>}<Esc>=<Enter><Esc>/setters<Enter>$a<Enter><Enter>public void <Esc>"npbiset<Esc>l~ea(<Esc>"tpa <Esc>"npa)<Enter>{<Enter><Tab>this.<Esc>"npa=<Esc>"npa;<Enter>}<Esc>=<Enter>`ak
(the above should be one long line with no spaces between the end of the lines above).
To use this to generate a class go to the variable that should have a setter/getter and place the curser at the beginning of the 'private':
private <type> <variable> = <initvalue>'
^
Then type:
jgs
this will create the first getter/setter and then move up to the next variable. You can just keep typing jgs until all the getters/setters have been generated.
This should mapping isn't perfect and someone could probably make it a little cleaner. It could also relatively easily be adapted to C++. Please feel free to send me any feedback/enhancements as I am trying to compile a list of these.
<< Cool trick to change numbers |
Alternative <escape> that allows you to do a "quick and dirty insert" and get out into normal mode >>
Additional Notes
[email protected],
July 26, 2002 0:23
|
This is just an update on the above -- I had my original email removed for SPAM reasons. If you want to email me regarding this tip please send to sheerpub _at_ yahoo dot com.
Thanks,
Sheer
|
[email protected],
September 19, 2002 8:21
|
This is a great idea, but this implementation is a little lacking. When I first used it, it put my getters and setters in comments (since it searchers for // getters, and doing an $a<Enter> on that line continues the comment). Additionally, it assumes a tabstop of 8 instead of letting the file type indent do the work, plus the spacing is a little messed up.
What I usually do is type all my private members. I got to the first one and hit "qa" to start macro recording to register a. Then I make my getter and setter using only commands that operate on words and not characters. When done, hit "q" again, and now register a has your macro.
|
pete-vim AT kazmier.com,
November 27, 2002 8:43
|
If anyone is interested, I wrote a script that does this and a little more. You can find it here: http://vim.sourceforge.net/script.php?script_id=490 ;
|
|