Skip to content
Austin Guest edited this page Jan 11, 2015 · 7 revisions

n# Getting Started with IntelliJ

TOC

## Docs ## Config * Install these plugins from the Configure->Plugins dropdown on the IntelliJ welcome screen. You'll need to browse non-bundled repositories to find them: * Scala * SBT * Restart IntelliJ when prompted. ## Project setup

Below getting started steps are culled from this tutorial and this video from the Functional Programming in Scala Coursera

An example project that resulted from my implementing the below steps can be found in here on the TLC Github

Create project (File/New Project)

  • Click File/New Project from menu
  • Select Scala from left pane, SBT in main pane, click Next
  • name project, select location, set JDK version (if not pre-filled), click Finish

Create a class to export in a package

  • in the lefthand pane (Project), navigate to the src/main/scala folder and select it
  • create new package by:
    • either clicking Ctrl-N or right-clicking then mousing over New in the contextual menu, then:
    • selecting New Package
    • name it something descriptive (main or example)

Create a package with some classes in it

  • right click-on package you just created
  • select New/Scala Class from contextual menu
  • name the class with the same name as the file its in
  • give the class some methods (for example Talker.sayHello)
    • eg: file named Talker.scala with class Talker and method sayHello

Run from a main loop object

  • right click on package (or use Ctrl-N) create Scala class
  • change the class declaration to an object declaration
  • add extends App to class declaration (ie: object Main extends App {
  • add some code you want to execute to the body of the object (ie: import example.Talker; val talker = new Talker; talker.sayHello)
  • assuming main loop object is named Main, run the main loop by either:
    • right clicking anywhere in the window of the main loop declaration and selecting run Main from the contextual menu
    • selecting Main from the dropdown in the upper right-hand corner, then clicking the play-shaped run button next to the dropdown
    • clicking Shift-Fn-F10

Run from a test suite

Import test suite

  • add test suite dependency to build.sbt file
    • for standard scala test library, insert: libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test"
    • for specs2 library(recommended), insert:
    libraryDependencies ++= Seq(
      "org.specs2" %% "specs2-core" % "2.4.15" % "test"
    )
    scalacOptions in Test ++= Seq("-Yrangepos")
    // Read here for optional jars and dependencies:
    // http://etorreborre.github.io/specs2/guide/org.specs2.guide.Runners.html#Dependencies
    resolvers ++= Seq("snapshots", "releases").map(Resolver.sonatypeRepo)
    
    • this will prompt you to re-import the project
    • after you do: you should be able to click on External Libraries in the left-hand pane and see the test libraries

Creating & running tests

  • create new test placing cursor in class you wish to test then either:
    • pressing Cmd-Shift-T
    • selecting Navigate/Test from the menu
  • name the test
  • select the testing library you want to use
    • default is ScalaTest, others might have more fluent syntax
    • optionally: select a method you want a test generated for
  • run the test by either:
    • pressing Ctrl-Shift-Fn-F10
    • right-clicking anywhere in file and selecting run <test class name> from contextual menu

Run by sending snippet to the console

  • start up a Scala console with Cmd-Shift-D
  • select a snippet you'd like to run
  • right-click and select send to Scala Console from contextual menu
  • if more code is required to invoke your snippet, enter it
  • click Cmd-Enter to run your code and see a return value printed to the screen
  • (unlike in scala REPL from the command line, Enter won't trigger evaluation, you have to hit Cmd-Enter to do that)

Run by composing in a worksheet

  • select the location in the right-hand nav pane where you'd like to worksheet to live
  • use Ctrl-N to bring up a generation menu, select "Scala Worksheet" from dropdown
  • enter in code you'd like to evaluate in the file that is created
  • as you type, every line that can be evaluated will be evaluated in another pane on the right-hand side of the screen

Keyboard Shortcuts

refactoring

  • renaming stuff

    • Alt-F7 rename
    • Shift-F6 rename everywhere
  • extracting stuff

    • extract variable Ctrl-Cmd-V
    • extract field
    • extract param

editing & manipulation

  • selection

    • Cmd-D duplicate selection
    • Ctrl-G add next occurrence to selection
    • Cmd-Alt-Shift-J select all occurences (& create multiple cursors)
    • Ctrl-Ctrl <up,down> enter multiple cursor mode
    • Cmd-Alt-I auto-correct indentation of selection
    • Cmd-W expand selection by 1 tree syntax pair
    • Shift-Ctrl-W shrink selection by 1 tree synatax pair
  • find & replace

    • find in path Shift-Cmd-F
    • replace in path Shift-Cmd-R
  • Cmd-Y kill line

  • move stuff up/down

    • Cmd-Shift-Up/Down move selection up

tell me stuff you know intelligent jay!

  • Ctrl-Shift P show type of highlighted expression
  • Ctrl-J show API docs for selected Type
  • Cmd <hover> will produce hyperlink to source code, definition of Class, package, etc.
  • Ctrl-Space invoke auto-complete

running stuff

  • Shift-Cmd-T create new test
  • Shift-Fn-F10 run executable object
  • Shift-Cmd-Fn-F9 compile current file### testing
  • use specs2 library

project structure

  • menu: File/Project Structure
    • this is just a view on your .sbt file. don't edit it because those changes won't stick!