Skip to content

julianhaslinger/sublime-clojure

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clojure support for Sublime Text 4

This package provides Clojure support for Sublime Text and includes:

  • Clojure and EDN syntax grammars (Sublime Text 3+)
  • Clojure nREPL client (Sublime Text 4075+, nREPL 0.8+)

Clojure syntax

Unfortunately, Clojure syntax that is shipped with Sublime Text handles many edge cases badly:

Syntaxes

Unlike default Clojure syntax, this package is:

  • pedantic as per EDN spec and Clojure Reader,
  • rigorously tested,
  • detects unbalanced brackets and incorrect escape sequences efficiently,
  • punctuation and validation inside regexps.,
  • quoted and unquoted regions are marked for highlighting,
  • semantically correct tokenization, perfect for fonts with ligatures,
  • unicode-friendly (supports unicode letters in symbols/keywords),
  • has separate EDN syntax, same way JSON is separate from JavaScript in Sublime Text.

Want to put your parser to test? Check out syntax_test_edn.edn and syntax_test_clojure.cljc.

nREPL Client

Sublime Clojure nREPL client enables interactive development from the comfort of your editor.

Principles:

  • Minimal distraction. Display evaluation results inline.
  • Decomplected. Eval code and nothing more.
  • Server-agnostic. We work with any nREPL socket, local or over network.

Features:

  • evaluate code,
  • display evaluation results inline.
  • display stack traces inline,
  • interrupt evaluation,
  • eval multiple forms at once (parallel evaluation),
  • lookup symbol info,
  • show evaluation time,
  • bind keys to eval arbitrary code.

We intentionally excluded following features:

  • Autocomplete. Static analysis is much simpler and much more reliable than requiring an always-live connection to the working app.

Why nREPL and not Socket Server REPL/pREPL/unREPL?

  • nREPL has the widest adoption,
  • nREPL is machine-friendly,
  • nREPL comes with batteries included (interrupt, load-file, sideload),
  • nREPL is extensible via middleware,
  • nREPL serialization is easier to access from Python than EDN.

Differences from Tutkain:

  • nREPL instead of Socket Server REPL
  • Does not have separate REPL panel
  • Keeps multiple eval results on a screen simultaneously
  • Can show stack traces inline in editor
  • Can eval several forms in parallel
  • Can eval non well-formed forms (e.g. (+ 1 2)
  • Can eval infinite sequences
  • Redirects all *out*/*err* to System.out/System.err

Installation

Install package from source:

git clone https://github.com/tonsky/sublime-clojure.git
ln -s `pwd`/sublime-clojure ~/Library/Application\ Support/Sublime\ Text/Packages/Sublime\ Clojure

Assign syntax to Clojure files:

  • open any clj/cljc/cljs file,
  • run ViewSyntaxOpen all with current extension as...Sublime ClojureClojure (Sublime Clojure).

How to use

Important! Make sure you switched your syntax to Clojure (Sublime Clojure).

  1. Run nREPL server.
  2. Run Clojure REPL: Connect command.

Evaluating code from buffer

From here you have three options:

Clojure REPL: Evaluate without selection evaluates topmost form around your cursor:

Evaluate Topmost

Clojure REPL: Evaluate with selection evaluates selected text:

Evaluate Selection

Clojure REPL: Evaluate Buffer will evaluate the entire file:

Evaluate Buffer

You don’t have to wait for one form to finish evaluating to evaluate something else. Multiple things can be executed in parallel:

Evaluate in Parallel

By default, Sublime Clojure will also print evaluation time if it took more than 100 ms:

Elapsed time

Interrupting

If your evaluation runs too long and you want to interrupt it, run Clojure REPL: Interrupt Pending Evaluations:

Interrupt

Opening stacktrace

If your evaluation failed, put your cursor inside failed region and run Clojure REPL: Toggle Stacktrace:

Toggle Stacktrace

Sublime Clojure will display stacktraces in a Clojure-friendly way. Compare with the default REPL:

Stacktraces

Looking up symbol

To show symbol info, run Clojure REPL: Toggle Symbol Info:

Toggle Symbol Info

Universal Clojure REPL: Toggle Info command acts as either Toggle Stacktrace or Toggle Symbol Info, depending on context.

Binding keys to eval code

Every project is different, and sometimes it’s convenient to run a piece of code so often you’d want it on a shortcut. It might be a namespace reload, test execution, database reconnect, linter, formatter — possibilities are endless.

To support such use cases, Sublime Clojure allows you to bind arbitrary piece of code to a keyboard shortcut. Run Preferences: Sublime Clojure Key Bindings and add something like this:

{"keys": ["ctrl+t"],
 "command": "sublime_clojure_eval_code",
 "args": {"code": "(clojure.test/run-all-tests)"}}

Then, whenever you press Ctrl + T, you’ll see the result in the status bar, like this:

Eval Code

Clearing results

Finally, to clear evaluation results run Clojure REPL: Clear Evaluation Results.

Editing settings

To edit settings, run Preferences: Sublime Clojure Settings command.

Default Key Bindings

Sublime Clojure comes with no keybindings enabled by default to guarantee they won’t conflict with any other extension.

This is the recommended keymap:

Command macOS Windows/Linux Mnemonic
Evaluate Ctrl Enter Ctrl Alt Enter
Evaluate Buffer Ctrl B Ctrl Alt B [B]uffer
Interrupt Pending Evaluations Ctrl C Ctrl Alt C [C]ancel
Toggle Info Ctrl I Ctrl Alt I [I]nfo
Clear Evaluation Results Ctrl L Ctrl Alt L c[L]ear

To set it up, run Preferences: Sublime Clojure Key Bindings command and copy example keybindings to your local Key Bindings file.

Frequently Asked Questions

Q: Why no Package Control?

A: They seem to have a shortage of reviewers, track progress.

Q: REPL/eval doesn’t work

A: Make sure you are using nREPL 0.8 or later. A: Make sure you have assigned Clojure (Sublime Clojre) syntax to the file.

Credits

Made by Niki Tonsky.

See also

Writer Color Scheme: A color scheme optimized for long-form writing.

Alabaster Color Scheme: Minimal color scheme for coding.

Sublime Profiles: Profile switcher.

License

MIT License

About

Clojure support for Sublime Text 4

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 62.9%
  • Clojure 37.1%