Attaching sources in IntelliJ IDEA for scala project - scala

I have Playframework 2 project with Scala (very small one). It uses Scala Anorm library.
I have the code like this:
package models
..
import anorm.SqlParser._
...
val rowParser = scalar[Long]
So, I would like to see source of scalar method. Trying to attache the sources I have in my plaframework source folder, but IDEA just swallows my request and does nothing back.
I use last version of IDEA and Scala plugin. Is it bug, probably?

First download source code :
1.) Click on this link : http://www.scala-lang.org/download/all.html
2.) Choose any scala version.
3.) Now in the last section 'Other Resources' you can see 'Sources' link. Click on it to download. In my case (https://codeload.github.com/scala/scala/tar.gz/v2.11.7)
Now point to this Source Code from IntelliJ.
1.) Open Project Structure in IntelliJ. Shorcut (Cmd + DownArrow)
2.) Select Global Libraries from the left section.
3.) Then on right side, Under 'Scala Library' section. Click '+' and point to source directory. See the screenshot.

Aha.. fixed.
So: I used to use 'attach source' in the top of the window editor - it does not work (I guess it is a bug - maybe related to the scala plugin, because usually it works).
But if try to attache sources to particular library - in "Project Structure -> Library -> +Attach File or Directories -> Sources" then it works.
Thanks.

To fix it we need to do some changes into IntelliJ IDEA platform. Most probably it will be fixed in IntelliJ IDEA 13.1.

I have the same problem. I installed "Scala Imports Organizer", and problem was solved.

for "automatically attaching source jars" when using Bloop and BSP you can add one line:
bloopExportJarClassifiers in Global := Some(Set("sources"))
to your build.sbt, also sbt updateClassifiers, also Reload all BSP Projects to refresh, also Invalidate Caches/Restart to trigger indexing (if not done automatically)
reference

Related

IntelliJ source code editor shows false compilation errors

I have a problem with the following odd behaviour of the IntelliJ IDEA development environment (version 14) with scala:
After I reset the (global) scala SDK from a 2.11 version to version 2.10.4 the source code editor shows many compilation errors: list, map, print, (1 to n) etc. are all red.
The SDK is added to global libraries and the jars are all found.
However the code compiles and runs correctly (I rebuilt the project to force recompilation). Has anyone ever experienced something like this?
How can this be fixed?
I am using tha java sdk 1.7.0_55.
In file menu, Invalidate Caches / Restart
also set Incremental Type to SBT in Settings -> Build, Execution, Deployment-> Compiler -> Scala Compiler
There are high chances that you didn't load your project properly, and that's why the errors are appearing. I have written a complete blog post about how you can fix it. And following are the two methods from the write up which can correct your issue.
These two steps should be able to help you, for rest of them please check the link.
1. Importing Project as an SBT Project:
First of all, close your project and import it again after selecting the directory and choose SBT as an External Module. Select Import Project, then go to the project directory, select your project, choose Import Project from external model, opt for SBT and click Next and then Finish. IntelliJ IDEA will ask you to overwrite the .idea file, say Yes to it. After IDEA is done indexing and loading your project, all the issues will be sorted out. The editor will work fine, and you will have your missing features restored.
2. Fixing missing project files with SBT-IDEA:
Sometimes IntelliJ IDEA misses out on the most important project files and shows editor errors along with a broken code completion, however, during the code compilation, everything works as per your expectation. This solution works with a plugin called as SBT-IDEA. Now to enable this plugin, open your terminal and follow the instruction:
a. Create a file plugins.sbt in the directory ~/.sbt/0.13/plugins
b. Now, write the following content to it:
resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.7.0-SNAPSHOT")
Note: Please check for the latest version at the plugin’s GitHub page.
Save and quit, and execute sudo sbt gen-idea in your project root directory.
I hope it helps!
Thank you.
In my case I changed build.sbt file (added empty line and saved file). Then I Idea re-checked dependencies and indexed. Then everything worked fine.
To remove all the false errors sometimes you need to disable type-aware highlighting
by pressing Ctrl + alt + shift + e for example:
https://blog.jetbrains.com/scala/2011/03/02/type-aware-highlighting/
The errors seem to be caused because Scala plugin re-implements compiler logic and that is imperfect.
More information here in the comments:
https://www.reddit.com/r/scala/comments/5pcwfv/question_what_causes_intellij_idea_highlighter/
This occurs when IntelliJ tries to build your code by itself and fails miserably.
If you know your build is successful, all you need to do is edit the Run Configuration and remove the Build part inside the Before Launch part.
This solution solved it for me!
Try invalidating your caches and restarting IDEA.
(File > Invalidate Cache)
Wait for IDEA to re-index your project (Status indicator on lower right status bar). The caches will sometimes become corrupted. This is most likely to occur upon reorganizing a project as you mentioned you did.

IntelliJ IDEA: Cannot import SBT project

I'm completely new to development using Play or IntelliJ for that matter. I've created a simple HelloWorld application using Activator, and this is an sbt project.
I've been trying to import this to IntelliJ and this is the screen I'm stuck at:
https://www.dropbox.com/s/we1a4a3184sojvb/Screenshot%202014-07-24%2016.57.11.png
In almost all tutorials I've been through online, I've seen people using an sbt option on the import screen. I've installed the SBT plugin as well, but that hasn't helped. I've restarted IntelliJ several times to no avail.
Where am I going wrong?
I'm running 13.1.4 with the SBT plugin installed.
I had this error when importing a new module from existing sources.
On the right-hand side of your IntelliJ window, you will see a list of vertical tabs, open the SBT tab.
Select the module that's causing trouble, right click on it, select "Detach external project"
Try to import the module again.
Move to /your-project-folder
Run ./activator
Type idea . It will generate IDEA project.
Open (not import) File->Open.. project in IDEA.
I had a similar problem which came from a different place than the other solutions here so adding it in case it occurs for others.
In my case the problem was caused by accidentally opening the IdeaProjects directory. When you do this, it creates the .Idea folder in that directory, and populates it as a project. This apparently also confuses the set import.
The solution is to remove the .Idea folder and open (not import) the directory again.
I encountered the same error when I was trying to import a new scala project A to an existing IntelliJ project B as a module. I solved the problem by following step below:
Go to your folder of project B. Open file .idea/sbt.xml.
Deleting the entry which includes the path of project A.
After a series of struggles, I must say something weird solved this problem – moving to IntelliJ IDEA Ultimate. That has built in Play application support and can run/debug/test the application out of the box, which is very handy.
All I can say after this experience was that JetBrains wants you to upgrade to Ultimate if you want things to work right out of the box.
I'm sure there are ways to get this working with CE also, but I wasn't successful with that.
Solved for
IntelliJ IDEA 2022.2.1 (Ultimate Edition)
scala plugin: 2022.2.12
sbt version: 1.7.1
doing these steps:
Unlink sbt Project(sbt toolbar), then link it again.
Delete modules (ctrl + alt + shift + s).
Remove .bsp/, and .idea/ config files.
Invalidate caches..(ctrl + f).
Restart IDEA.
The number of steps to make it work may vary.
Uninstall everything
Reinstall IntelliJ
Install the JetBrains official Scala plugin
Import the project
Make sure that the project has a build.sbt file in the right place, so that IntelliJ can find it as an SBT project.
This is about IntelliJ IDEA version 13.1.4.
This issue is solved in the latest updates of the SBT and Scala plugins.
Make sure that your java path is correct (IntelliJ can use other path than system defined)
Preferences -> Build, Execution, Deployment -> sbt -> JVM - choose a
correct JRE

IntelliJ Idea Scala files not available in 'New' context menu

I am new to both IntelliJ and Scala. I am attending the course "Functional Programming Principles in Scala" on Coursera. I downloaded the zip file for the sample assignment, which contained a sample Scala project.
I imported the project successfully (I guess) in IntelliJ. However, when I right-click on a package in the project explorer, there are no Scala-related templates. I can only select "New Java class", XML files and some forms.
Does anyone know why this happens and if there is another way to create a new Scala class or object from a template?
Thanks.
Import the project by selecting its build.sbt file - not the project folder, not the eclipse project file. Then everything is imported correctly.
For me it works as following:
Right clicked on src and selected "mark directory as" -> source root.
This may also be a caching issue.
If so, try File then Invalidate Caches / Restart
Yes, you need to make the scala directory a source directory (in Menu / Project structure / Modules ) but I also had to do the following:
Still in Project Structure goto Global libraries.
Select Scala SDK.
Right click the Scala SDK and Add to module...
Now my context menu gives me New / Scala class and so on.
Once you've created a project and sbt has finished initializing, try creating another project. It should show up correctly in the new project, as it worked for me. It seems to be some kind of caching issue though Invalidate Caches/Restart didn't help me. Also, ensure that source directories are marked properly (source folders appear in blue color, test sources are in green).
I had this problem everytime I created a new project with spaces in the name. eg "Hell Wev". Using "HellWev" as a project name seems to work fine
Same issue (and solution) as #cwadeevans.
A project named "Week 1" did not show Scala files in the context menu "New". The solution was to delete the project and create a new one named "Week1".

F3 on Eclipse doesn't go to the class that was selected

Background
I have multiple android libraries, each has its own classes.
The problem
For some reason, for classes that extend from one of those libraries' classes, when I click F3 to go to the class itself, I get an error:
"The Jar of this class file belongs to container 'Android Dependencies' which does not allow modifications to source attachments on its entries"
What i've tried
I've done some searching of this issue, and found multiple solutions (for example here, here and here), but none has worked for me:
clean project
set the order of the build path to have src at the end, while gen folder is before it.
update ADT, as it was probably fixed.
add "Source attachment", but this is impossible for android libraries (blocked for some reason).
The question
Why does it occur?
How come it's still occuring even on the newest ADT ( i have 22.3.0.v201310242005-887826 )?
Quoting from mik3y In Eclipse, how can I quickly access the source of an "android library project" from the source of a project that uses it?, which worked for me:
Right click on the non-library project (Project B) and select Build Path:
In the Projects tab, add the library project Project A as a dependency.
In the Order and Exports tab, be sure Project A is given higher precedence than the placeholder/bundle Library Projects.
I had to do both of these steps in order to fix this issue, with ADT15, Eclipse 3.7 and new projects.
EDIT:

How can I get Eclipse to index an entire C/C++ source tree that contains multiple "make projects"?

I'm having trouble getting Eclipse to index my source code. I'm using Eclipse Helios on Windows XP at the moment.
I have a directory called src. Within it, I have a bunch of dirs, something like this:
src
-include (common headers)
-lib
-libIO (source code for this library)
-libGUI (source code for this library)
-pgms
-pgm1 (source code and headers for this pgm)
-pgm2 (source code and headers for this pgm)
Each leaf has its own Makefile. There is no top-level Makefile in src. pgm1 can and does include files from include and lib/libIO and lib/libGUI.
Basically, I want Eclipse to index my entire src directory, without having to set up a C/C++ project for every leaf in my tree. I can't seem to find a way to make this happen.
Here's my symptoms; what I'm trying to solve for:
When editing source in the pgm1 dir, it references functions that are declared in my include dir header files and defined in source files found in lib/libIO.
However, when I press F3 to get to the declaration of a function, Eclipse says "Could not find symbol 'X' in index". I can't seem to get Eclipse to find either the header declaration or the source definition for the method under my cursor (like the Java module does perfectly). Is this possible?
I had exactly same issue as OP but for some reason the menus in Eclipse I was using (Helios Service Release 2) were not "Go to Properties -> C/C++ General -> Paths and Symbols -> Source Location.".
The way I fixed the issue was :
in C/C++ view, right-click on top project name;
in the menu, select New->C++ Project;
in the window that appears, select Convert to and then select C++ project;
click OK to close the window.
Eclipse will start indexing right away. Depending on the size of source tree it may take a while but you will have the indexing working finally after that.
I faced similar situation. I solved it in this way: right-click the project in project View-->select Index-->rebuild.
I managed to solve this thanks to comments here.... I ended up recreating my project. I used the Import method to download a project from CVS, and told it to use the New Project wizard to do so. When I got the New Project dialog, I told it this was a C++ Project, and the indexing now works fine.
I still wish I could index files without having to attach a specific project type to it, but at least I found an answer.
Thanks for the help all.
Exit Eclipse. In workspace go to ".metadata/.plugins/org.eclipse.cdt.core" and delete everything in there.
in the project explorer panel, right click the project you want to re-index, then select index, then select the action you want
Indexing of files and variables under different flags is always complicated when we have a huge project, lot of files and more importantly lot of different build options. I prefer playing around with the Indexer option to help me browse the entire code.
You can find it : Project Properties>C/C++ General/Indexer.
You can choose "Enable project specific settings"
Then it's up to you to choose the options you want for your project.
For a particular build we can choose "Use active build configuration" so that only the files and MACROS are considered which are used by the build script.
Or Index all source files in the entire project.
I am using Eclipse Neon 3, here's the solution that worked for me :
go to File
New
Convert to C/C++ Autotools project
then select your project and finish.
It can take some time to finish indexing, it depends on your project size.
I have experienced problems with the indexer of Eclipse Luna when there was an unresolved friend declaration in the class declaration - it seems then that the indexer skipped indexing the rest of that class, and all references to it was shown as unresolved by the indexer.
Solution: removed the unresolved friend declaration (in my case, it was legacy code that was no longer needed).
Please try the following (my project is set up differently, so I am not sure this will work for you).
Go to Properties -> C/C++ General -> Paths and Symbols -> Source Location.
Do you see your source folder there?
I observe this behavior:
When I add a source folder and then exclude it from build, it disappears from the above list. After that the folder is no longer indexed. Re-adding it to "Source Location" solves the problem: the folder is now indexed; remains excluded from build (as intended); is visible among source locations.
I believe it is a bug -- excluding a source folder from build should not remove it from source locations list.
In Coocox IDE (Eclipse + gcc) the problem is resolved by going from file menu
Edit > Preferences > C/C++ / Indexer > Build configuration for Indexer set to Active build configuration and the rebuild the project (Ctrl R).
If the project is already converted to C/C++ and still the index is not working you can right click on the project and Index and rebuild. The project will start indexing right away.
I am using
Eclipse IDE for C/C++ Linux Developers
Version: Helios Service Release 2
Build id: 20110218-0911
I followed the suggestions above and in addition I had to mark all referenced projects (with in the work space) using project properties->Project references
The following has worked for me in Eclipse Neon:
New Project -> C/C++ -> Makefile project with existing code -> Next. Then Navigate to the code and finish the project creation. Indexing starts automatically.