ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Clojure Sublimed

by tonsky ST4

Clojure support for Sublime Text 4

Labels clojure, repl, nrepl

Details

Installs

  • Total 2K
  • Win 394
  • Mac 1K
  • Linux 480
Jun 20 Jun 19 Jun 18 Jun 17 Jun 16 Jun 15 Jun 14 Jun 13 Jun 12 Jun 11 Jun 10 Jun 9 Jun 8 Jun 7 Jun 6 Jun 5 Jun 4 Jun 3 Jun 2 Jun 1 May 31 May 30 May 29 May 28 May 27 May 26 May 25 May 24 May 23 May 22 May 21 May 20 May 19 May 18 May 17 May 16 May 15 May 14 May 13 May 12 May 11 May 10 May 9 May 8 May 7 May 6
Windows 0 0 1 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
Mac 1 0 1 0 2 1 1 1 1 0 1 1 1 0 2 0 0 1 0 0 1 0 0 0 2 0 0 2 0 1 0 0 1 1 1 0 0 2 0 1 0 1 0 2 1 1
Linux 0 0 1 1 1 1 2 0 0 1 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 3 1 1 0 0 0 0 0 1 2 0 1 0 0 0 0 0 0 2 0

Readme

Source
raw.​githubusercontent.​com

This package provides Clojure support for Sublime Text and includes:

  • Clojure and EDN syntax grammars (Sublime Text 3+)
  • Code formatter/indenter (Sublime Text 4075+)
  • nREPL, Socket REPL, ShadowCLJS REPL clients (Sublime Text 4075+)

Installation

Package Control: Install PackageClojure Sublimed

Clojure syntax

Syntaxes

Clojure Sublimed ships with its own syntax definition for Clojure and EDN. Unlike default Clojure syntax, this package is:

  • slightly more pedantic as per EDN spec and Clojure Reader,
  • rigorously tested,
  • can be used to highlight rainbow parentheses,
  • punctuation and validation inside regexps,
  • quoted and unquoted regions are marked for highlighting,
  • semantically correct tokenization, perfect for fonts with ligatures,
  • has separate EDN syntax, same way JSON is separate from JavaScript in Sublime Text.

How to enable? Assign syntax to Clojure files:

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

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

Formatter/indenter

Clojure Sublimed includes optional support for Simple Clojure Formatting rules. It doesn’t require REPL connection, any Clojure runtime or external tools.

To reformat whole file, run Clojure Sublimed: Reindent Buffer.

To reindent only selected line(s), run Clojure Sublimed: Reindent Lines.

To enable reindenting/formatting on save, add format_on_save: true to settings. (See how to edit settings)

To enable correct indentations as you type code, rebind Enter to Clojure Sublimed: Insert Newline:

{"keys":    ["enter"],
 "command": "clojure_sublimed_insert_newline",
 "context": [{"key": "selector", "operator": "equal", "operand": "source.edn | source.clojure"},
             {"key": "auto_complete_visible", "operator": "equal", "operand": false},
             {"key": "panel_has_focus", "operator": "equal", "operand": false}]}

Best way to do it is through running Preferences: Clojure Sublimed Key Bindings.

REPL clients

Clojure Sublimed REPL clients enable interactive development from the comfort of your editor.

Principles:

  • Minimal distraction. Display evaluation results inline.
  • Decomplected. Eval code and nothing more.

Features:

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

Clojure Sublimed has four REPL clients:

  1. Raw nREPL: no extra middlewares, could work with any nREPL server.
  2. JVM nREPL: only works with JVM server.
  3. ShadowCLJS nREPL: works with ShadowCLJS
  4. JVM Socket REPL: works with raw Socket REPL on JVM

All four support same basic features (eval, lookup, interrupt on JVM), maybe with slightly different quality of implementation.

How to choose which REPL to use?

  1. Are you on JVM? Use Socket REPL.
  2. On JVM and only have nREPL? Use JVM nREPL.
  3. CLJS? Use ShadowCLJS REPL.
  4. Otherwise (e.g. babashka) use Raw nREPL.

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.

Look at Sublime LSP with Clojure LSP or SublimeLinter with clj-kondo if you need autocompletion.

How to use

For Clojure apps:

  1. Run Socket Server, e.g. with either
clj -X clojure.core.server/start-server :name repl :port 5555 :accept clojure.core.server/repl :server-daemon false

or

clj "-J-Dclojure.server.repl={:port 5555 :accept clojure.core.server/repl :server-daemon false}"
  1. Run Clojure Sublimed: Connect to Socket REPL command.

For Shadow-cljs apps:

  1. Run shadow-cljs watch app. (This starts a HTTP server and an nREPL)
  2. If you are building a web-app, open the http-server url (from step 1) in the browser. This connects the shadow server to JS runtime.
  3. Run Clojure Sublimed: Connect shadow-cljs command.

For other nREPL apps:

  1. Run nREPL server.
  2. Run Clojure Sublimed: Connect to raw nREPL command.

Evaluating code from buffer

From here you have three options:

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

Evaluate Topmost

Clojure Sublimed: Evaluate with selection evaluates selected text:

Evaluate Selection

Clojure Sublimed: 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, Clojure Sublimed will also print evaluation time if it takes more than 100 ms:

Elapsed time

Copying evaluation results

Sometimes you want to copy evaluation result. It is recommended to rebind Cmd+C/Ctrl+C from copy to sublime_clojure_copy. This will copy evaluation result if inside evaluated region and fallback to default copy otherwise.

Interrupting

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

Interrupt

Opening stacktrace

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

Toggle Stacktrace

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

Stacktraces

Watches

Watches are great alternative to debug prints: they allow you to monitor intermediate values during function execution right in the editor.

This is how they work:

  • Select a right-hand expression
  • Run Clojure Sublimed: Add Watch command
  • Now every time function is executed, for any reason, watched expressions will display values they evaluate to, in real time.

Watches are only supported in Socket REPL.

Looking up symbol

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

Toggle Symbol Info

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

Binding keys to eval custom 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, Clojure Sublimed allows you to bind arbitrary piece of code to a keyboard shortcut. Run Preferences: Clojure Sublimed Key Bindings and add something like this:

{"keys":    ["ctrl+t"],
 "command": "clojure_sublimed_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

Other possible use-cases (select key combinations to your linking):

Only run tests from current namespace:

{"keys":    ["ctrl+shift+t"],
 "command": "clojure_sublimed_eval_code",
 "args":    {"code": "(clojure.test/run-all-tests (re-pattern (str *ns*)))"}}

Reload code with clj-reload:

{"keys":    ["ctrl+r"],
 "command": "clojure_sublimed_eval_code",
 "args":    {"code": "(clj-reload.core/reload)"}}

Transforming code before eval

You can also modify eval to run a transformed version of code under cursor/inside selection.

For example, this will pretty-print result of your evaluation to stdout:

{"keys":    ["ctrl+p"],
 "command": "clojure_sublimed_eval",
 "args":    {"transform": "(doto %code clojure.pprint/pprint)"}}

transform is a format string that takes selected form, formats it according to described rules and then sends resulting code to evaluation.

If you now press ctrl+p on a form like (+ 1 2), the actual eval sent to REPL will be:

(doto (+ 1 2) clojure.pprint/pprint)

Which will pretty-print evaluation result to stdout. This pattern might be useful for large results that don’t fit inline.

We can implement macroexpand this way:

{"keys":    ["ctrl+e"],
 "command": "clojure_sublimed_eval",
 "args":    {"transform": "(macroexpand-1 '%code)",
             "expand": true}}

Another use-case might be “eval to buffer”:

{"keys":    ["ctrl+b"],
 "command": "chain",
 "args":    {
    "commands": [
      ["clojure_sublimed_eval", {"transform": "(with-open [w (clojure.java.io/writer \"/tmp/sublimed_output.edn\")] (doto %code (clojure.pprint/pprint w)))"}],
      ["open_file", {"file": "/tmp/sublimed_output.edn"}]
    ]
 }
}

Eval to buffer at work:

Inside transform you can also use %ns (current ns) and %symbol (if selected form is def-something, it will be replaced with defined name, otherwise nil).

This allows us to implement “run test under cursor”:

{"keys":    ["super+shift+t"],
 "command": "clojure_sublimed_eval",
 "args":    {"transform": "(clojure.test/run-test-var #'%symbol)"}}

Run test under cursor at work:

Clearing results

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

Editing settings

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

Session-wide settings

It is sometimes desirable to set dynamic Clojure vars for the whole session. To do that, edit "eval_shared" setting. For example:

"eval_shared": "(do (set! *warn-on-reflection* true) (set! *print-namespace-maps* false))"

This will be applied to every evaluation.

Default Key Bindings

Clojure Sublimed comes with no keybindings enabled by default to guarantee they won’t conflict with any other extension (Sublime Text’s limitation).

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
Copy Evaluation Results Command C Ctrl C [C]opy
Reindent Lines Ctrl F Ctrl Alt F [F]ormat
Reindent Buffer Ctrl Shift F Ctrl Alt Shift F Capital [F]ormat

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

stdout/stderr

Clojure Sublimed does things a little different when it comes to stdout. Normally REPL would show you all stdout/stderr that originated from your session. I find it confusing, because it doesn’t always work and you have to check two places for output. Moreover, there’s no output panel, so there’s no place to show stdout anyway.

So instead, Clojure Sublimed does not redirect neither stdout nor stderr. Check original console to see what was printed there.

Stdout redirect

Frequently Asked Questions

Q: REPL/eval doesn’t work

A: Make sure you are using nREPL 0.9 or later. A: Also check console (Cmd `) for errors


Q: How to connect to a Babashka REPL?

A: Use Raw nREPL.

Credits

Made by Niki Tonsky.

With contributions by Jaihindh Reddy and KgOfHedgehogs.

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