Skip to content

Using Frege in Intellij IDEA

Dierk König edited this page Jul 1, 2020 · 17 revisions

This page is for Java developers with prior exposure to Intellij IDEA.

For those who prefer working in an IDE, there is special Frege support for eclipse. However, Intellij IDEA can also be set up to compile and run Frege code smoothly even though the support is much less advanced. If you would like to see better Frege support in IDEA, please upvote the Frege issue.

This page describes how you can

  • edit Frege code with basic syntax highlighting and general editing support (commenting, search/replace, hippy-completion)
  • automatically compile Frege code whenever you change a .fr file
  • run Frege code
  • run QuickCheck
  • start the Frege REPL with your code on the classpath.

Prerequites

Make sure you have a recent version of IntellijIDEA. Any 13.x version or higher should be fine. The free community edition is totally sufficient. Some basic knowledge of this IDE is assumed below.

Make sure you have a Java 1.8 SDK or higher installed.

Install the following plugins from the JetBrains plugin site if you don't have them yet:

  • a Haskell plugin like HaskForce (for syntax highlighting). You can disable it. The highlighting works anyway.
  • the File Watcher plugin (to get quick compiler messages)

Setup

Syntax Highlighting for *.fr files

Register Frege files as having the Haskell file type such that the Haskell plugin will do the syntax highlighting. Go to Preferences -> Editor -> File Types and add *.fr to the list of Haskell file extensions.

FrAsHaskellFileType

Build Setup (with Gradle)

Strictly speaking, one does not need to have any kind of build automation with Frege. You can happily just call the compiler and use all the other tools (test, doc, repl) from the command line.

But Frege is supposed to work with your Java projects and that means you are likely to have some build automation in place. There are many options (Maven, Gradle, Bazel, Leinigen, SBT) and the likes but for our purpose here we assume that you use Gradle.

For starters, you can just copy the build.gradle file from the HelloFrege project. This provides the gradle tasks as visible in the Intellij Gradle tool

FrGradleTasks

You best have a look at the build.gradle file before you use it to get comfortable with its capabilities. Many lines are commented out such that you can see all options and easily change setting by changing the lines.

You can specify the Frege release that you want to use. It even gets downloaded and installed if you run the fregeInit task - in this case please comment out line 53 which relies on Frege being already available.

That's it. You can now compile, run, test, document, and repl your Frege code.

There are some more options to make the experience more fluent, though.

Automatic compilation with the file watcher

IntellijIdea allows to watch for file changes and start tools whenever it detects such a change.

You can set those in the preferences as below:

FrCompileFileWatcher

This will trigger frege to compile the currently edited file whenever you save it. (Some prefer to also compile on automatic saves but I find this distracting)

File watchers are configured per project and stored in .idea/watcherTasks.xml. You can copy/paste this file from here.

Note that there also is an output filter that detects file names and lines in the output to allow clicking on compile errors and jumping directly to the respective line.

Selective Tasks as External Tools

The Preferences of IntellijIdea also provide a way to register "External Tasks". We can use this feature to register Frege's Gradle tasks and make them more selective, for example running the current file.

Set up like this: FrRun

Now you can run the current Frege file (provided that it has a main function and a package) by selecting the module name (including the package name), right click, choose frege -> frege run <selected> and the file will be compiled and started in the IntellijIdea Run view.

There it remains and can be re-started any time you want.

In case you want to test a selected Frege QuickCheck test case, you can do the analogous setup like so:

FrTest

Finally, there is the Frege Repl that is really nice to work with interactively while programming. This as well, can be configured as an external tool:

FrFrepl

When started, it prints commands to the run window that you can copy (all three lines at once) and paste into the Terminal window. This triggers the Repl to start, load the current file, and browse the selected module.

All project dependencies and project classes are properly on the classpath such that they are available to works with. Only the current file is removed from the classpath. This facilitates that you can change the code in the editor, reload in the repl via :r, and work with the changes.

Note that the repl has a history of commands that you can cycle through with the arrow keys.

Caveat: When working with the repl this way, you best disable the frege-compile file watcher for the time being or the compiled class can get in the way of the interactive reload.

Loading a frege file in the Repl and using the :type and :browse commands along with just calling functions gives an interactive feeling that is almost as good as a proper IDE plugin.

Tips & Tricks

Make sure you have UTF-8 as your encoding!

Make sure you have Tabs-to-spaces set for your editor!

The file watcher sometimes needs some extra motivation to pick up a change and start the the compiler. Just add a space after some line and wait for half a second or so.

Some code suggestions by the Haskell plugin do not apply to Frege, e.g. True vs true.

The Haskell support for commenting lines or blocks also works fine for Frege (Cmd-/).

You can use "Hippi-Completion"! This feature is much more useful than one might think. After having started to write an identifier, hit Ctrl-Space and IDEA will try to find a token with the same beginning in the file and offer completions.

All the typical text-editing goodies are available. I've since recently come the love the Ctrl-G to select many occurrences of the same word and change all occurrences at once.

It is helpful to have the Frege project itself opened in a separate IDEA window - just for being able to search via Ctrl-Shift-F for module and function names in the prelude or in the examples.

It goes without saying that you can add additional Java modules to that project and let them call into the Frege code.


Have a lot of fun using Frege in IntelljIDEA!

Clone this wiki locally