简体   繁体   中英

Generate getters and setters using UltiSnip in Vim

I want to create an UltiSnip code in which is similar to the one used in their Github README .

I understand it involves python interpolation and I am finding it difficult to write my own interpretation. Their documenation gives a good list what UltiSnip can do, yet it still difficult to get what I am trying to do.

I want to automatically generate public methods to get and set instance variables, such as I type in this...

class Foo()
{
     int x;
     double y;
     ...

and UltiSnip should add this portion...

     public void setX(int _x)
     {
          this.x = _x;
     }

     public int getX()
     {
          return this.x;
     }

     public void setY(double _y)
     {
          this.y = _y;
     }

     public double getY()
     {
          return this.y;
     }
}

lh-refactor provides the generate getter/setter refactoring methods for Java and a few other languages.

Naming conventions can be tuned independently of the snippets used internally.

That's not what UltiSnips (and other snippets plugins) are about. You can indeed easily write setter and getter snippets that expand to the corresponding function, with placeholders for the attribute name and type. But defining all getters and setters for a class just from what's currently defined is stretching this functionality too much; you'd need a (rudimentary) language parser, and might want to implement attribute additions / attribute type or name changes, too.

I know that most IDEs provide such functionality out of the box, but they are special-purpose tools for a very limited set of programming languages, not a general-purpose text editor like Vim. If you really love this functionality, just use an IDE of your choice in paralled to editing the same source code file with Vim. Or use the mentioned simpler snippets and accept a bit more typing from your side.

Hi I have problems with posting here the code with the symbol ` then I used github: github.com/robertbnd/notes/blob/master/stack

only you need to write "int x" and press the key you selected in your snippets My case is : let g:UltiSnipsExpandTrigger="`" you can personalize it

Use eclim for this. Just select the attributes with visual mode and then :JavaGetSet :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM