Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Macro annotations and IntelliJ code completion [duplicate]

Background

I have an sbt-managed Scala project that uses the usual sbt project layout for Scala projects with macros, i.e., a subproject that contains the macros a main project that is the actual application and that depends on the macro subproject. The macros are macro annotations which, in essence, generate companion objects for regular classes. The generated companion objects declare, amongst other members, apply/unapply methods.

I used the sbt-idea plugin to generate a corresponding IntelliJ IDEA project, and I use the sbt console from IDEA's sbt-plugin to compile and run my Scala application.

Everything works more or less fine, except that the generated companion objects, and more importantly, their members such as apply/unapply, are not recognised by IDEA. Thus, I get a squiggly line everywhere I, e.g., an apply method.

My setup is IntelliJ IDEA CE 133.471 with the plugins SBT 1.5.1 and Scala 0.28.363 on Windows 7 x64.

Questions

How do I get IntelliJ IDEA to recognise code (classes, objects, methods, ...) that has been generated by Scala macros (macro annotations, to be precise)?

Are other IDEs, e.g., Eclipse, known to work better in such a setting?

Related

This question (which is less detailed) essentially asks the same, but has not gotten a reply yet (2014-02-26).

According to a JetBrains developer the feature I requested is on their long-term to-do list, but won't be implemented any time soon (2014-03-05).

like image 363
Malte Schwerhoff Avatar asked Feb 26 '14 16:02

Malte Schwerhoff


People also ask

How do I ignore duplicate codes in IntelliJ?

Ignore names and values while searching for duplicates Press Ctrl+Alt+S to open the IDE settings and select Editor | Duplicates. Select file types to which the analysis should apply and select the checkboxes next to the constructs that you want to anonymize. Apply the changes and close the dialog.

How do I remove duplicates in IntelliJ?

From the main or context menu, select Refactor | Find and Replace Code Duplicates. In the dialog that opens, select the scope where IntelliJ IDEA shall look for code duplicates. For each found code duplicate, IntelliJ IDEA will prompt you to confirm the replacement.

How do I enable auto completion in IntelliJ?

By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.

What does duplicated code fragment mean?

Code Inspection: Duplicated code fragment Reports duplicated blocks of code from the selected scope: the same file or the entire project. The inspection features quick-fixes that help you to set the size of detected duplicates, navigate to repetitive code fragments, and compare them in a tool window.


2 Answers

With the latest Scala plugin build, there is an API which can be used to write your own plugin to support your macros: http://blog.jetbrains.com/scala/2015/10/14/intellij-api-to-build-scala-macros-support/

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:

<extensions defaultExtensionNs="org.intellij.scala">
  <syntheticMemberInjector implementation="org.jetbrains.example.injector.Injector"/>
</extensions>
like image 105
Alexey Romanov Avatar answered Nov 15 '22 09:11

Alexey Romanov


Seems like there's limited support if any.

Quote by this link: http://blog.jetbrains.com/scala/2014/01/23/heading-to-the-perfect-scala-code-analysis/

Alexander Podkhalyuzin says:    

January 30, 2014 at 10:13 am

We started support for Scala macros, but it’s not a simple task, so I can’t promise it will be done soon.

Best regards, Alexander Podkhalyuzin.

like image 40
Dr.Knowitall Avatar answered Nov 15 '22 10:11

Dr.Knowitall