Skip to content
jfacorro edited this page Dec 14, 2014 · 4 revisions

Clojure is the language for which Clojure Lab was originally built for, so much so that the development of the application was done using early versions of itself.

All files that have the extensions .clj, .cljs or .cljx are considered Clojure source code files.

The features associated with these Clojure files are the following:

  • Syntax Highlighting
  • Delimiter Matching
  • Rainbow Delimiters
  • Paredit
  • Auto-complete
  • nREPL
    • Macro-expand
    • Docstring Information

Syntax Highlighting

Clojure's syntax highlighting has full support for all of the language elements. Behind the scenes each language currently uses the parsley library to figure out how to process the source code in each file. Since parsley was built with Clojure in mind it fits perfectly for using it with this language.

Delimiter Matching

The Clojure language does a heavy use of delimiters and sometimes it is pretty hard to identify the pair that matches a given delimiter. This is why it is handy to highlight both delimiters whenever the cursor is positioned right next to one of them.

In the following screenshots you can see how both brackets are highlighted either when the cursor is before or after the closing bracket.

This feature is enabled by default for all Clojure files.

Rainbow Delimiters

Rainbow delimiters are a quick and visual way of identifying the nesting level associated with each pair of delimiters. Additionally this also helps figuring out where the code goes wrong when there is some unbalanced delimiter lying around.

Colors are assigned from a fixed set according to the nesting level, since the set has a limited amount of colors, once a deep enough level is reached, the assigned colors cycle form the beginning of the set. For example:

This feature is enabled by default for all Clojure files although you can disable it by pressing Ctrl + P from inside the editor.

Paredit

This feature comes from the the world of Emacs. Paredit is composed out of a set of commands that make editing Lisp lists a lot easier, therefore the name ( parenthesis editor ).

Although there a too many paredit commands to list here, these are the groups in which they are categorized:

  • Basic Insertion.
  • Deleting & Killing.
  • Movement & Navigation.
  • Depth-Changing Commands.
  • Barfage & Slurpage.

The main objective of these commands is to keep delimiters balanced, help the navigation between the different levels and assist in the correct modification of depth and/or size for each list.

Please check this reference card for a more detailed explanation of each command. Also remember you can check what are the available commands by pressing F1 from the editor.

Autocomplete

The auto-completion pop-up window is available by pressing the Ctrl + Space key combination. You need to position the cursor right after a word in order for the auto-complete to work, otherwise the pop-up won't even be displayed.

The information of all available symbols that could match the auto-complete is taken from two sources.

The first one is the local context from where the auto-complete is requested. In the example above the context would be the numbers function which defines both itself and the argument named n. Since the cursor is positioned right after the n then the possible options for auto-completion are both the function's name and the argument's name. The local context also includes the name of functions declared in the same file.

The second source of information is the live running environment provided by the REPL which will be explained in the next section.

REPL

A REPL is in its most basic form a console in which you interact with a running instance of your application. REPL is an acronym for Read-Eval-Print-Loop which describes the interaction between you and the console. The use of a REPL is a characteristic feature of hacking in a Lisp dialect and since Clojure is one of those, Clojure Lab includes a REPL for your programming pleasure.

IMPORTANT: This feature counts on Leiningen website being installed in your system which means different things depending on your operating system. Please check out the really simple installation instructions here.

In order to open a bare REPL that has only the standard Clojure libraries available you can either press the Ctrl + Alt + R key combination or click on the Clojure > nREPL > Start and Connect to REPL menu item. This will open a tab in the lower section of the application which is shown in the following screenshot.

There are two editor controls in that tab, one above the other. The upper one shows you the information returned from the REPL while in the lower you can type in the code you want to evaluate. After entering the code you can send it to the REPL by pressing the keys Ctrl + Enter . The following sequence shows the entering and evaluating of the expression (println "hello").

Both lines signaled by the arrows are the result of evaluating the code sent to the REPL. The first one is the output to stdout while the second one is the result of evaluating the expression.

The REPL editor where you enter the code keep a history of all code sent. You can browse through each of the snippets sent from this editor by pressing Ctrl + Up to get the previous entry in the history or Ctrl + Down for the next one.

Another way of sending code to the REPL is through the editor. Pressing Ctrl + Enter with no text selected while in the editor will send the whole content of the current file to the REPL. In the case where you have some text selected then only the selected text will be sent to the REPL.

Project REPL

When you have an existing project it is possible to start a REPL from that project by pressing Ctrl + R or clicking the menu item Clojure > nREPL > Start and Connect to Project. This will create a new tab in the lower section of the application with the same two editor mentioned previously, the difference is that you will be able to load every namespace present in your project and evaluate any function as well.

Current Namespace

The Clojure namespace in which code is evaluated on the REPl is taken from the ns declaration of the current file. If there is no ns declaration then the current namespace does not change. In order to determine what the current namespace is you can send *ns* to the REPL.

Autocomplete

As mentioned in the Auto-complete section the REPL is used as the second source of information when listing the auto-completion symbols. The way this works is by sending a piece of code to the REPL that returns all symbols that are accessible from the current namespace.

These symbols include:

  • Imported Java classes.
  • Required namespaces.
  • Used namespaces.
  • Referred symbols.

This allows to include in the auto-complete options a more accurate approach since the information is taken from a live running environment of the application.

Macro-expand

Having the REPL also enables you to get the macro-expansion of every expression in your code since getting it is as simple as evaluating macroexpand with the expression as its arguments.

To get the macro-expansion of an expression just press Ctrl + Alt + Enter and you will get a pop-up window with the expanded code.

This is what a macro-expansion on the expression (ns awesome) looks like:

Docstring Information

It is also possible to get the docstring information from any Var just as easily by pressing Ctrl + I .

Here is the pop-up with the docstring for the ns macro: