How to start with Scala, intelliJ IDEA and SBT? - scala

I downloaded IntelliJ IDEA 14.0.3, SBT, Scala and SBT plugins, set environment variables correctly, i guess (if i can run Scala repl is it correct?)
When i start IntelliJ IDEA and choose new SBT-based project i get some problems:
1) no /src/ folder in project structure. How i can fix that?
2) can't compile manually created Scala file: IDEA suggest me edit configuration with Alt+Shift+F10 then i get window with various settings, so what directly should i edit?
Sorry for possibly stupid questions, but i just want to run miserable
"hello world" in Scala...

You don't need any extra plugins besides the Scala plugin. Assuming you have that installed, then
Step One: Fire up IntelliJ and Click on "Create New Project"
Step Two: Choose Scala from the left hand side panel and choose SBT on the right hand side panel (i.e SBT-based Scala project) and click Next.
Step Three: Fill in your project details and click Finish
Step Four: Wait for Intellij to download all SBT dependencies and to construct the folder structure and to index.
Step Five: Begin hacking!

Related

IntelliJ: Scala worksheet don't pick up code changes without restart

If I have the contents of an object imported at the top of a Scala worksheet in Intellij, when I add new things to the object, they cannot be found in the worksheet. Even after I build the project and reevaluate the worksheet, it can't find the new property. Autocomplete works. If I quit and restart IntelliJ it works.
This is the error:
java.lang.NoSuchMethodError: Arith$.foo()Ljava/lang/String;
at #worksheet#.get$$instance$$res6(ArithWS.sc:15)
at A$A1$.main(ArithWS.sc:41)
at #worksheet#.#worksheet#(ArithWS.sc)
Weirdly, it seems to compile, but fails at runtime. This is a normal SBT project. IntelliJ 2017.2.5, Scala 2.12.3. Selecting the "Make project" checkbox doesn't help. Actually no combination of checkboxes makes it work. No amount of rebuilding the project or the worksheet helps either. Only restarting IntelliJ will get the worksheet to pick up the new code.
Example project code in Arith.scala:
object Arith {
val foo = "foo"
}
Example worksheet ArithWS.sc:
import Arith._
foo
This seems to fix it, though I would love to know why the default setting is broken, or what this undocumented setting does: Go to File -> Settings -> Languages & Frameworks -> Scala -> Worksheet (tab) . Unselect "Run worksheet in the compiler process".
In case others end up here as bewildered as I was and find that the above doesn't resolve the issue, try unchecking the "Use external compile server for scala" tick box in Preferences > Build, Execution, Deployment > Compiler > Scala Compiler > Scala Compile Server (you can also get to this view from the speedometer-like icon in the bottom right of Intellij).
Combined with "Make project" on the Scala worksheet view, this seems to ensure that changes are properly compiled and visible from within the worksheet.

Scala: Intellij shows compile errors on Import statements

I'm trying to teach myself scala and I'm using IntelliJ IDEA 2016.1.3. The editor does not recognize import commands. Errors appear. I have downloaded libraries, but the problem persists. Am I doing something wrong?
Try this,
1.) I created a scala project:
In your console:
$ activator new my-first-app play-scala
Please bear in mind that I just automate the creation of scala project which follow the conventional project structure. I am using the Play framework and activator command, fyi, but this is out of topic.
2.) I open the project folder in IntelliJ by
a. Go to File -> New -> Project from existing source
b. find the "my-first-app" folder
c. double click the "build.sbt"
d. a prompt appear and check the "Use auto-import" checkbox
e. make sure the project sdk is a JDK 1.8
f. Click OK.
g. Select the modules data as root. As is.
h. Go to Settings (Ctrl + Alt + s)
I. Under "Languages & Frameworks", go to "Play2"
j. In the Play2 section, check the "Use SBT watcher" and "Use Play 2 compiler for this project"
h. Click "apply" then click "ok"
g. if still not work, refresh the project by going to SBT child window of intelliJ and click the refresh button.
Good luck! :D

How to open SBT Tool Window in Intellij?

This question may sound silly for you, but I have been searching the Internet for a while and couldn't find the answer. How can I open SBT Tool Window in Intellij (scala and SBT plugins obviously installed)?
In Menu: View -> Tool Windows there should be two entries:
SBT
SBT Console
The first you can use to refresh IDEA project structure from SBT and view dependencies.
The second gives you a console where you can run sbt commands.
Update:
As Justin already aded:
As of IntelliJ IDEA 2017.1 Build #IC-171.3780.52 there is a new and (imho) better way:
(If you are working with an SBT project, that is)
When you open the SBT Tool Window, you will find a new scala terminal icon:
This will open an sbt console session with your sbt project import settings.
One major benefit is that you will actually (finally) get links to your code lines for compiler warnings and errors.
To be able to open SBT Console from View -> Tool Windows you first need to install the plugin required for it.
You can find the SBT plugin by searching for it under Settings -> Plugins -> Browse repositories
Plugin homepage: https://github.com/orfjackal/idea-sbt-plugin
In IntelliJ 2017.1, an sbt shell is integrated with the Scala plugin. You can open it from an icon on the sbt project view toolwindow, or from the "Tools" menu
In Intellij 16 SBT Console menu item no longer exists and in Debian 7 (XFCE) Alt-F12 doesn't work.
To run SBT console:
select View -> Tool Windows -> Terminal (for Alt-F12)
type sbt and console works
You can either use above or simply use Alt+F12
I had an issue where the sbt shell launcher was not on the bottom of the IDE and the sbt was not listed as an option under Tools. Discovered that the issue was after expanding a zip file of a scala project to my workspace, the build.sbt file was not in the root folder of the project structure, but nested one subfolder down. When importing this project into intellij it was not properly configuring it as an sbt project. After removing the project, exploding the zip in such a way that the build.sbt was in the root of the project and reimporting the issue was resolved.
I had a similar, but slightly different issue. First, I didn't have the Scala plugin installed on my version of IntelliJ. Once done, I restarted the app. Then created a new project (even though I was trying to open an existing one). When creating the project I made sure to put the root directory of the project as the root folder of the project I wanted to import (the one that has the sbt file). IntelliJ synched my new project with the existing one and I was good to go.

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

Attaching sources in IntelliJ IDEA for scala project

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