Loading ...
Sorry, an error occurred while loading the content.
 

Re: How to generate set/get method pairs

Expand Messages
  • Martin Metz
    Hello, ... I solved this problem a little bit different: First, I write the data member by itself, e. g. private String name; Then I stay on this line and
    Message 1 of 10 , Jul 1, 2001
      Hello,

      Sergei Gnezdov wrote:
      >
      > I need to code sometimes set/get methods in Java. For example:
      >
      > access_modifier void setValue(Type value) {
      > this.value = value;
      > }
      >
      > access_modifier Type getValue() {
      > return value;
      > }
      >
      > I'd like to use some kind of Vim script which will ask me for he
      > - access_modifier
      > - type
      > - value_name
      > and generate set/get methods for me.

      I solved this problem a little bit different:

      First, I write the data member by itself, e. g.
      private String name;
      Then I stay on this line and print my java get/set macro which takes the
      last word as the variable name and the second last word as the type.

      The following code is within my vimrc:

      " create get...() and set...() for type/name of current line:
      map jsg $B"nywB"tywo<CR>public void set
      <ESC>"npb<Left>x~A(<ESC>"tpAnewValue)
      {x<ESC>x"npa=newValue;<ESC><Down>o<CR>public <ESC>"tpAget
      <ESC>"npb<Left>x~A() {return <ESC>"npa;


      This mapping can handle:
      int value;
      private static final String KEY="Test";

      but it fails e. g. with:
      String key = "Test;

      Also, I'm not really happy with the "jsg" mapping because the "j"
      command now is really slow. :-(

      PS: I nearly forgot: This mapping needs another mapping:

      " Ease the usage of '{' and '}'
      inoremap { {<CR>}<ESC>O

      If you don't want this bracket mapping (or e. g. auto indent), you have
      to modify my first mapping a little bit.

      --
      Greetings,

      Martin Metz

      /***************************************************************
      * Dipl. Inform. (FH) Martin Metz - Software engineering
      *
      * OneVision Software AG | Phone: +49-941-780040
      * Dr.-Leo-Ritter-Str. 9 | FAX: +49-941-7800449
      * 93049 Regensburg | mailto:Martin.Metz@...
      * Germany | WWW: http://www.OneVision.de
      **************************************************************/
    • Benji Fisher
      ... Untested: function! Foo() let a_m = input( access modifier: ) let type = input( type: ) let name = input( value name: ) put=a_m . void setValue( .
      Message 2 of 10 , Jul 2, 2001
        Sergei Gnezdov wrote:
        >
        > I need to code sometimes set/get methods in Java. For example:
        >
        > access_modifier void setValue(Type value) {
        > this.value = value;
        > }
        >
        > access_modifier Type getValue() {
        > return value;
        > }
        >
        > I'd like to use some kind of Vim script which will ask me for he
        > - access_modifier
        > - type
        > - value_name
        > and generate set/get methods for me.

        Untested:

        function! Foo()
        let a_m = input("access modifier: ")
        let type = input("type: ")
        let name = input("value name: ")
        put=a_m . ' void setValue(' . type . ' value) {'
        put='this.' . name . ' = value;'
        put='}'
        normal =%''
        " You should be able to figure out the second half!
        endfun

        The last line (if it works well) will adjust the indentation.

        :help :normal
        :help =

        For the rest of the function,

        :help input()
        :help :put

        HTH --Benji Fisher
      • Martin Metz
        ... Hmmm... where to start... ;-( You map to the command msg the following command sequence (the rest of the long term). To learn more about this term you
        Message 3 of 10 , Jul 3, 2001
          Sergei Gnezdov wrote:
          >
          > I have no idea what the code above does, but I played with it and modified
          > it a little bit for Java.
          > Can anybody point me to the right place in the documentation about it?

          Hmmm... where to start... ;-(
          You map to the command "msg" the following command sequence (the rest of
          the long term). To learn more about this term you must split it into the
          different commands like:

          $ Go to the end of the line
          B Go back to the last word separated with a whitespace
          "nyw copy the next word into the buffer named "n" (like name,
          should be the variable name)

          And so on...
          You can use all this commands without the mapping in vim (or even vi!)
          and see live, what they do and ask the documentation about details (e.
          g. ":help $" or ":help y" or ":help w").

          Short overview: I copy the name of the variable into the register n (for
          name) and the type into t. Than I write "public void set " + the
          variable name, go back, switch the first charater of the name to upper
          case, remove the space, add the "(" next the type and the name, next the
          ") {\nthis." + variable name... and so on.
          So it's really just a long list of easy commands and some dirty tricks
          (e. g. to get the data name in the method names uppercase).

          By the way: The original mapping was not done by me. I found it on a
          link, postet here in this list. But I extended it a lot (e. g using a
          variable type or make the first variable name in the method name
          uppercase).
          When I've time again, I want to expand it much more (e. g. java
          autodoc). :-)

          --
          Greetings,

          Martin Metz

          /***************************************************************
          * Dipl. Inform. (FH) Martin Metz - Software engineering
          *
          * OneVision Software AG | Phone: +49-941-780040
          * Dr.-Leo-Ritter-Str. 9 | FAX: +49-941-7800449
          * 93049 Regensburg | mailto:Martin.Metz@...
          * Germany | WWW: http://www.OneVision.de
          **************************************************************/
        • Michael Roehrl
          ... The original mapping and a lot of useful others can be found at http://www.ut.ee/~moro/vim/java.vimrc Bye, Michael
          Message 4 of 10 , Jul 3, 2001
            Martin Metz schrieb:
            > By the way: The original mapping was not done by me.
            The original mapping and a lot of useful others can be found at
            http://www.ut.ee/~moro/vim/java.vimrc

            Bye,

            Michael
          • Sergei Gnezdov
            ... I have no idea what the code above does, but I played with it and modified it a little bit for Java. Can anybody point me to the right place in the
            Message 5 of 10 , Jul 3, 2001
              >
              >I solved this problem a little bit different:
              >
              >First, I write the data member by itself, e. g.
              > private String name;
              >Then I stay on this line and print my java get/set macro which takes the
              >last word as the variable name and the second last word as the type.
              >
              >The following code is within my vimrc:
              >
              >" create get...() and set...() for type/name of current line:
              >map jsg $B"nywB"tywo<CR>public void set
              ><ESC>"npb<Left>x~A(<ESC>"tpAnewValue)
              >{x<ESC>x"npa=newValue;<ESC><Down>o<CR>public <ESC>"tpAget
              ><ESC>"npb<Left>x~A() {return <ESC>"npa;

              I have no idea what the code above does, but I played with it and modified
              it a little bit for Java.
              Can anybody point me to the right place in the documentation about it?

              Here is the result of my try-and-error cycle:

              " create get...() and set...() for type/name of the current line:
              map msg $B"nywB"tywo<CR>public void set
              <ESC>"npb<Left>x~A(<ESC>"tpA <ESC>"npa) {
              <TAB>this.x<ESC>x"npa = <ESC>"npa;
              <ESC><Down>o<CR>public <ESC>"tpAget <ESC>"npb<Left>x~A() {
              <TAB>return <ESC>"npa;

              " Ease the usage of '{' and '}'
              inoremap { {<CR>}<ESC>O

              Given the line:

              protected String name;

              It genrates the code like this for me:

              public void setName(String name) {
              this.name = name;
              }

              public String getName() {
              return name;
              }

              I really like it :) Thanks a lot.


              >Also, I'm not really happy with the "jsg" mapping because the "j"
              >command now is really slow. :-(

              Me neither. I noticed that when I press "j" VIM is really slow to
              react. I mapped the first letter to m and it seems to work fine for me.
            • Edwin
              Hello everyone, I haven t been on this e-mail alias long and was wondering if there is a website that has mappings and/or macros for Verilog programmers.
              Message 6 of 10 , Jul 3, 2001
                Hello everyone,

                I haven't been on this e-mail alias long and was wondering
                if there is a website that has mappings and/or macros for
                Verilog programmers. Also, is it possible to navigate the
                unix directory structure from within the gvim xterm like xemacs
                does?

                Thanks,
                Edwin-

                __________________________________________________
                Do You Yahoo!?
                Get personalized email addresses from Yahoo! Mail
                http://personal.mail.yahoo.com/
              • Bohdan Vlasyuk
                ... see :h file-explorer -- Ditat Deus. [God enriches]
                Message 7 of 10 , Jul 3, 2001
                  On Tue, Jul 03, 2001 at 09:35:35AM -0700, Edwin wrote:

                  > Also, is it possible to navigate the unix directory structure from
                  > within the gvim xterm like xemacs does?
                  :e .

                  see :h file-explorer


                  --
                  Ditat Deus.
                  [God enriches]
                • Edwin
                  I am using Version 5.8. ... (gives a message stating) . is a directory (and does not show directory structure in window.) ... (gives a message stating)
                  Message 8 of 10 , Jul 3, 2001
                    I am using Version 5.8.

                    :e .
                    (gives a message stating)
                    "." is a directory
                    (and does not show directory structure in window.)

                    :h file-explorer
                    (gives a message stating)
                    Sorry, no help for file-explorer

                    Are these features in Version 6.0?

                    Thanks,
                    Edwin-

                    --- Bohdan Vlasyuk <bohdan@...> wrote:
                    > On Tue, Jul 03, 2001 at 09:35:35AM -0700, Edwin wrote:
                    >
                    > > Also, is it possible to navigate the unix directory structure from
                    > > within the gvim xterm like xemacs does?
                    > :e .
                    >
                    > see :h file-explorer
                    >
                    >
                    > --
                    > Ditat Deus.
                    > [God enriches]


                    __________________________________________________
                    Do You Yahoo!?
                    Get personalized email addresses from Yahoo! Mail
                    http://personal.mail.yahoo.com/
                  • Benji Fisher
                    ... Yes. There is a version of explorer.vim (called bufexplorer.vim) that works with Vim 5.x. One place you can get it is
                    Message 9 of 10 , Jul 3, 2001
                      Edwin wrote:
                      >
                      > I am using Version 5.8.
                      >
                      > :e .
                      > (gives a message stating)
                      > "." is a directory
                      > (and does not show directory structure in window.)
                      >
                      > :h file-explorer
                      > (gives a message stating)
                      > Sorry, no help for file-explorer
                      >
                      > Are these features in Version 6.0?

                      Yes. There is a version of explorer.vim (called bufexplorer.vim) that
                      works with Vim 5.x. One place you can get it is

                      http://members.tripod.com/lanzarotta/vim/macros/5/

                      I am not sure whether this is the most recent version. (Those of us who write
                      scripts should get around to posting them at
                      http://vim.sourceforge.net/scripts/ ! Maybe tomorrow...)

                      Using just standard vim, you may find that <C-D> and <Tab> on the command
                      line are enough to make you happy. Either experiment, or read

                      :help c_<Tab>
                      :help c_ctrl-d

                      HTH --Benji Fisher
                    Your message has been successfully submitted and would be delivered to recipients shortly.