Vim Tips Wiki
No edit summary
 
m (Reverted edits by 101.222.177.43 (talk | block) to last version by JohnBot)
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=288
 
|id=288
  +
|previous=286
|title=A keymapping to generate Java setters and getters automatically
 
  +
|next=290
|created=July 25, 2002 19:25
+
|created=2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Sheer El-Showk
 
|author=Sheer El-Showk
 
|version=5.7
 
|version=5.7
 
|rating=6/3
 
|rating=6/3
  +
|category1=Automated Text Insertion
|text=
 
  +
|category2=Java
This mapping makes it much simpler to write new java classes by simplifying some of the dull repetative coding (ie setters and getters).
 
 
}}
 
This mapping makes it much simpler to write new java classes by automating a repetitive coding task, the creation of setters and getters. Note that a ready-made [[#Plugins|plugin]] exists to do much the same thing.
   
 
To use, first write a basic class with the following format:
  +
<pre>
 
public class MyClass
 
{
 
private <type> <varname> = <initvalue>;
 
private <type> <varname> = initvalue>;
 
// getters
 
// setters
 
}
  +
</pre>
   
 
Note the getters/setters comment -- these are important as they are used to place the getters and setters.
   
  +
The mapping is which should be added to the .vimrc file is:
To use, first write a basic class with the following format:
 
  +
<pre>
 
  +
map jgs mawv/ <CR>"ty/ <CR>wvwh"ny/getters<CR>$a<CR><CR><Esc>xxapublic
 
  +
<Esc>"tpa<Esc>"npbiget<Esc>l~ea()<CR>{<CR><Tab>return
 
  +
<Esc>"npa;<CR>}<Esc>=<CR><Esc>/setters<CR>$a<CR><CR><Esc>xxapublic void
public class MyClass
 
  +
<Esc>"npbiset<Esc>l~ea(<Esc>"tpa <Esc>"npa)<CR>{<CR><Tab>this.<Esc>"npa=
 
  +
<Esc>"npa;<CR>}<Esc>=<CR>`ak
{
 
  +
</pre>
 
 
 
private &lt;type&gt; &lt;varname&gt; = &lt;initvalue&gt;;
 
 
private &lt;type&gt; &lt;varname&gt; = initvalue&gt;;
 
 
 
 
// 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/ &lt;Enter&gt;"ty/ &lt;Enter&gt;wvwh"ny/getters&lt;Enter&gt;$a&lt;Enter&gt;&lt;Enter&gt;public &lt;Esc&gt;"tpa&lt;Esc&gt;"npbiget&lt;Esc&gt;l~ea()&lt;Enter&gt;{&lt;Enter&gt;&lt;Tab&gt;return &lt;Esc&gt;"npa;&lt;Enter&gt;}&lt;Esc&gt;=&lt;Enter&gt;&lt;Esc&gt;/setters&lt;Enter&gt;$a&lt;Enter&gt;&lt;Enter&gt;public void &lt;Esc&gt;"npbiset&lt;Esc&gt;l~ea(&lt;Esc&gt;"tpa &lt;Esc&gt;"npa)&lt;Enter&gt;{&lt;Enter&gt;&lt;Tab&gt;this.&lt;Esc&gt;"npa=&lt;Esc&gt;"npa;&lt;Enter&gt;}&lt;Esc&gt;=&lt;Enter&gt;`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 &lt;type&gt; &lt;variable&gt; = &lt;initvalue&gt;'
 
 
^
 
 
 
 
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.
 
 
 
 
 
}}
 
   
 
The above should be one long line with no spaces between the end of the lines above.
== Comments ==
 
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.
 
   
 
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':
Thanks,
 
  +
<pre>
Sheer
 
 
private <type> <variable> = <initvalue>'
  +
^
  +
</pre>
   
 
Then type:
sheerpub--AT--yahoo.com
 
  +
<pre>
, July 26, 2002 0:23
 
 
jgs
----
 
  +
</pre>
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&lt;Enter&gt; 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.
 
   
 
This will create the first getter/setter and then move up to the next variable. A user can continue typing <code>jgs</code> until all the getters/setters have been generated.
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.
 
   
  +
==Plugins==
  +
*The {{script|id=490|text=java_getset.vim}} script makes it dead simple to auto generate getters and setters, plus its easy to install and configure.
  +
*[http://code.google.com/p/lh-vim/wiki/lhRefactor lh-refactor] handles getters and setters among other refactorings (and languages)
   
 
==Comments==
dcop--AT--mindspring.com
 
 
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 <code>$a<CR></code> 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.
, September 19, 2002 8:21
 
----
 
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   http://vim.sourceforge.net/script.php?script_id=490 ];
 
   
 
What I usually do is type all my private members. I got to the first one and hit <code>qa</code> 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 <code>q</code> again, and now register a has your macro.
pete-vim AT kazmier.com
 
, November 27, 2002 8:43
 
----
 
<!-- parsed by vimtips.py in 0.492764 seconds-->
 

Revision as of 23:21, 5 April 2015

Tip 288 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Sheer El-Showk · version 5.7


This mapping makes it much simpler to write new java classes by automating a repetitive coding task, the creation of setters and getters. Note that a ready-made plugin exists to do much the same thing.

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 -- these are important as they are used to place the getters and setters.

The mapping is which should be added to the .vimrc file is:

map jgs mawv/ <CR>"ty/ <CR>wvwh"ny/getters<CR>$a<CR><CR><Esc>xxapublic
<Esc>"tpa<Esc>"npbiget<Esc>l~ea()<CR>{<CR><Tab>return
<Esc>"npa;<CR>}<Esc>=<CR><Esc>/setters<CR>$a<CR><CR><Esc>xxapublic void
<Esc>"npbiset<Esc>l~ea(<Esc>"tpa <Esc>"npa)<CR>{<CR><Tab>this.<Esc>"npa=
<Esc>"npa;<CR>}<Esc>=<CR>`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. A user can continue typing jgs until all the getters/setters have been generated.

Plugins

  • The java_getset.vim script makes it dead simple to auto generate getters and setters, plus its easy to install and configure.
  • lh-refactor handles getters and setters among other refactorings (and languages)

Comments

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