Though we’ve been silent for quite a while recently, it doesn’t mean we’ve not been busy, so today we’re presenting the new features for Scala plugin RC for IntelliJ IDEA 2016.3 that we’ve just created. Continue reading
They say “with great power comes great responsibility.” Because of the IntelliJ Scala plugin’s huge user base, most default settings tend to become de-facto standards in the Scala community, so we strive to choose the defaults wisely. Moreover, we believe that sometimes such kind of decisions are worthy of detailed explanations, and subject to approval by the community.
As experience has shown, many introduced novelties are often misunderstood and criticized in the beginning. For example, method signature enforcement was initially touted as some quirk that only keeps getting in the way. But as time went by, those rules and the side-effect thing became “self-evident” in the Scala community, mostly because that’s how all this was intended in the language from the inception. In other cases, like with the deprecation of postfix calls (before SIP-18), we referred to Martin’s tweet as a justification. Because there’s no appropriate Martin’s tweet (yet?) for what we’re introducing this time, we dare validate the step by ourselves.
We’re expanding the notion of “code style” for Scala beyond the typical constituents like spaces, parentheses and braces. For a start, we have introduced a special set of configurable rules of when to add / enforce type annotations (check Settings / Editor / Code style / Scala / Type Annotations). Truth be told, we added those settings about 4 years ago, but only now did we pick up our courage to enable some of those by default (on the other hand, you can’t say we’re rushing it :).
From now on, the type annotation settings will be used in the following functionality:
- Introduce Variable / Field, Extract Method, Override / Implement Method refactorings,
- Create Variable / Method from usage,
- Java-to-Scala code conversion,
- Reformat Code action with “Add type annotations” enabled,
- Type Annotation inspection.
In particular, we’re considering enabling the enforcement of type annotations on public methods, and we’re definitely expecting a mixed reaction to that. Why declare some result types explicitly? Well, we believe that it’s one of those places where tools can mend possible abuse of certain liberties in the language design. Here are the reasons:
- Public methods and properties represent a so-called “public API,” which is a form of abstraction. Forcing API users to investigate the inner workings of public methods (or to guess the result type) is a sure way to break encapsulation and to degrade code quality. Explicit type annotation make public API well-marked and easy to use.
- Explicit result types can greatly improve code editing performance. Unlike the compiler, an IDE has to re-infer types on each code update. To optimize the workload, IntelliJ IDEA tries its best to cache and preserve as much type information as possible. However, as the result type of a method with omitted type annotation can change even on “external” update (i.e. update outside of the method body), IntelliJ has to re-process the whole content of such a method on almost any change (and the content might often consists of implicits, type-level arithmetic and whatnot). Considering the first point, adding type annotations to public methods makes your code more IDE-friendly (for the same reason, you may also choose to add explicit types to really complex yet non-public methods).
- Type annotations on public methods increase the speed of incremental compilation by minimizing the amount of recompiled code. Algorithms of incremental compilers (like SBT or IntelliJ) track “public API changes” and dependencies to determine what files need to be recompiled. Thus, if type of some method in a frequently used class is adjusted after code editing (but still lays within some “implied” base type), almost the whole project might often be unnecessary recompiled as a result. In a sense, this argument is a combination of the two previous ones but from the compiler’s standpoint. Official SBT documentation supports this argument by saying that “explicitly annotating return types of a public API is a good practice in general.”
Note: One possible exception to these rules are simple declarations like
val answer = 42 where the result type is clearly evident, both for humans and for the compiler. Currently we’re determined to treat this kind of expressions separately.
So, adding explicit types to public methods can make our code cleaner and also speed up editing / compilation. Sounds good, doesn’t it? But what about tons of those “righteous” warnings, aren’t they supposed to be annoying? The good news is that there is a straightforward way to process them all at once:
- Go to Settings / Editor / Code Style / Scala / Type Annotations and make sure that Add & Check option is selected for public members.
- Invoke Analyze / Inspect Code, select Whole project as a scope and create an inspection profile with only the Type annotation required inspection enabled.
- Run the inspection and then choose Apply fix “Add type annotation” to batch-add all the explicit type results.
When SCL-10501 will be implemented, another way to add required type annotations in batch is to invoke Reformat Code with Add type annotations enabled (but this also reformats code for good measure).
Because the functionality is already present in all the Scala plugin builds, you don’t have to wait for it to be enabled by default: you can tweak the code style and try this out right now. Likewise, if you consider the undertaking to be too much of a hassle, just tweak the code style settings as you see fit.
At the next step, we will add many more options to the code style (currently listed as inspections), so that it becomes possible to choose, for example, what form of anonymous function definitions you prefer (
person => person.name or
_.name) or to require eta-expansions to be explicit (like
f _), and so on.
We encourage you to try the feature and, as always, we’d love to hear your feedback!
It may well be that you haven’t considered contributing to any open source project, let alone the IntelliJ Scala plugin. However, if you use IntelliJ IDEA to write Scala code, it’s actually a very reasonable thing to do. Here’s how you can benefit from contributing to the plugin code base.
There’s a common misconception about attracting contributors where the process is viewed as “a way to get developers for free.” Nothing could be further from the truth, at least in our case. While the Scala plugin code is open source, there are currently 10 people in the Scala team at JetBrains working on the project full-time. Believe me, we can implement features and fix bugs on our own just fine, and that’s the straightforward way to do that. We’re making additional efforts by creating documentations, publishing tasks, recording videos and doing consulting in order to get to know people and to establish relationship with the Scala community.
Another prejudice that prevents many people from contributing is a notion that “programming an IDE is hard.” I admit that there’s an element of truth in that one, because both IDEA and the Scala plugin code bases are somewhat complex. However, on the one hand, the complexity of different tasks varies greatly, and on the other hand, this provides a great opportunity to learn something new about your tools of the trade. Make no mistake, writing IDE code can be both educational and entertaining!
If you want to contribute, where to start? First I recommend that you watch the video recording of our recent ScalaSphere presentation, Inside IntelliJ Scala plugin. It shows you how to configure the development environment and gradually introduces you to the inner workings of IntelliJ IDEA and the Scala plugin:
You can select a task by searching for Up For Grabs tickets in our issue tracker. But don’t feel restricted by that list, as in principle, you are welcome to choose any existing ticket or any task you can imagine.
Here’s a fuller list of resources you can rely on:
- IntelliJ Scala plugin project on Github
- IntelliJ Platform SDK Documentation
- Viewing PSI Structure help topic
- IntelliJ IDEA project on GitHub (the platform API docs are fairly good)
- The Scala Language Specification (PDF)
If you have any questions, you can ask them:
- Directly in the YouTrack tickets,
- In our Scala community forum,
- Via IntelliJ Scala team email,
- In the project Gitter,
- …and on Twitter, sure, why not?
Please don’t hesitate to ask any questions, even “obvious” or, on the contrary, “too hard” and “rhetorical” ones. We actually encourage you to ask questions and to get in touch.
Although the process is a goal in itself, if you happen to implement some feature or fix a bug along the way, just create a pull request or send us a patch, as usual. We also send cookies some small but cool stuff to active contributors, just for the fun of it.
Contribute with pleasure!
The IntelliJ Scala Team
Because of its nature, Scala code is really difficult to edit without proper coding assistance. And sometimes said assistance is too slow which makes it not that useful. That’s why we never stop improving our Scala plugin performance. This time we’ve reduced the time needed to provide coding assistance within methods with a defined return type. As a result, code completion and inspections in our latest EAP build now work much faster.
After (see how much faster it is, the animation is 100% real time snapshot):
To give the new EAP build a try, download it from the EAP page, or if you use the EAP plugin upgrade channel, upgrade from the IDE.
Develop with Pleasure!
A macro is executed at compile-time and modifies the AST of your code: e.g. it can extend the class or its companion object with new methods or other code represented by the AST returned by the macro. Since IntelliJ IDEA’s coding assistance is based on static code analysis, the IDE is not aware of AST changes, and can’t provide appropriate code completion and inspections for the generated code.
Since macros are heavily used in some projects and libraries, we’ve added a special API to the platform that lets build IntelliJ plugins supporting particular macros. Using this API, we earlier added support for Monocle, and now Simulacrum:
Now, everyone can use this API to make their macros more friendly to their favorite IDE. To do that, you have to implement SyntheticMembersInjector, and register it in the plugin.xml file:
The Monocle and Simulacrum implementations will help you understand how to use the API. To help you with setting up a project, we’ve prepared a skeleton. You’re welcome to fork it, and add your implementation.
Once you’ve built a working plugin, it can be installed to the IDE via Settings → Plugins → Install plugin from disk…. If you want to share your plugin with other IntelliJ IDEA users, feel free to upload it to our plugin repository.
In case you’d like to have particular macros supported by the Scala plugin, send us pull requests.
Develop with Pleasure!
For Java, in addition to the regular completion, IntelliJ IDEA provides so-called Postfix completion: it lets you modify a previously entered expression by adding a “postfix” to it and pressing Tab. This way you can transform your code without unnecessary backward caret jumps.
Many users’ve asked us to support this feature for Scala as well. You asked, we did it:
When a transformation can be applied to several expressions, the IDE will offer you a choice:
Postfix completion suggestions are included in the common completion list, and if you want to see only postfix completion applicable in the context, press Ctrl + J (Cmd + J for OS X):
To see the full list of postfix completion templates, go to Settings → Editor → General → Postfix Completion:
Develop with Pleasure!
We’ve recently improved the support for Scala tests in IntelliJ IDEA 15 EAP a bit. Read on to find out more details.
Statically computed expressions in scalaTest/specs2/uTest2 test names are now supported, which means that along with string constants you can use constructions like immutable string variables, concatenation, substring, stripSuffix, stripPrefix, toLowerCase, replace, or trim.
scalaTest/specs2/uTest2 tests and their hierarchy is displayed in the File Structure view when you click Show scala tests.
Develop with Pleasure!
Debugging: stopping at Lambda Expressions
When you add a breakpoint to a line that contains a lambda expression, the IDE offers you several options where it should stop. Choose any lambda expression if there are several of them, or stop at the whole line. You can use the Run to cursor function on a lambda expression as well.
You can use Ctrl+F8 to add a breakpoint. Then the lambda expression that the cursor is placed on will be selected in the list. Pressing Ctrl+F8 again will complete creating a breakpoint on the selected lambda.
Macro Expansion Extractor
Sometimes it’s useful to extract a macro expansion in the place where you are using it, which can help you debug the macro. Before using this, compile the code with “-Ymacro-debug-lite”.
Press Ctrl+Alt+Shift+Z with the cursor placed on the macro.
If the cursor is not on a particular macro, all macros in your file will be expanded.
Develop with Pleasure!
Inline refactoring for type aliases
Invoked just as any other inline refactoring, by placing the caret on an alias you want to inline and pressing Ctrl+Alt+N.
For now this refactoring is only available for non-parameterized types and for aliases that are used inside of the same class.
Kind projector support
Kind Projector is a Scala compiler plugin that provides an easy way to create type lambdas. Scala 1.7 plugin includes inspection that suggests to rewrite existing lambdas using the Kind Projector function syntax.
You can use the settings under Settings | Editor | Code Style to select whether you want to denote lambdas with the Lambda keyword, or the λ symbol.
Feel free to share your feedback in our discussion forums and issue tracker.
Develop with Pleasure!
If you being in the forefront of it all, and all includes the latest language and IDE features, you’re welcome to download IntelliJ IDEA 15 EAP, a very early preview of what the next big Java IDE is going to be like. Among other things, it brings you an update to the Scala plugin 1.7 we released yesterday.
One of the 1.7 highlights is the support for experimental single abstract method types (SAM) added in Scala 2.11:
This is the first version of the plugin available exclusively for IntelliJ IDEA 15 EAP. More new features are coming soon, so stay tuned. If you have feedback to share, please do so in our discussion forum and issue tracker.
Develop with Pleasure!