Clone wiki

Varsity / Home

Varsity

Varsity is a plugin for IntelliJ IDEA (the community edition) that mimics C#'s var keyword in Java:

example screenshot

It folds redundant type information in local variable declarations (including for and foreach loops) to just display "var". Redundant means that a local variable's type is the same as the compile-time type of the initialization (or, for anonymous classes, the base class). This includes normal for loops. In foreach loops, it works analogously. This is just code folding; the actual Java file isn't modified, and you can always expand to see the actual types.

The default behavior is to fold all types, including primitive types, as long as the type name has at least 3 characters. This can be customized in the "Varsity" section of the IDE settings.

Conversely, if you enter var as the type in a local variable declaration, a for loop, or a foreach loop, it will (once you press Enter) replace the "var" with the righthand side's type.

You can also enable Scala-like handling of val as final var (both for folding and for Enter-expanding). This functionality is disabled by default, but can be turned on in the Varsity settings.

Yes, until you press enter, the var (and possibly val) will be marked as an error. This plugin is just a hack, after all. Which brings me to the …

Disclaimer

I am a total newbie in the following areas:

  • Java
  • the Java type system
  • IntelliJ plugin development

– so basically everything that would qualify me for writing this. The plugin code is awfully hackish and ad-hoc. Use at your own risk. (And pull requests welcome!)

Installation

Pull the code and build it yourself, or download the newest version from the Downloads page (I'll try to upload a new build everytime I push code changes). In IntelliJ, go to File -> Settings -> IDE settings -> Plugins, and click "Install plugin from disk", choosing the varsity-X.Y.Z.jar file.

Be aware that there's no auto-update mechanism.

License

Third-party code

Parts of VarsityConfigurable.java are Copyright 2000-2009 JetBrains s.r.o., licensed under the Apache License, Version 2.0.

Original code

Copyright (c) 2013, Benjamin Dumke-von der Ehe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Updated