@jurjanpaul/codemirror6-clojure-smart-indent
v0.1.1
Published
Clojure Smart Indent extension for CodeMirror 6
Maintainers
Readme
codemirror6-clojure-smart-indent
Clojure Smart Indentation extension for CodeMirror 6.
The indentation follows the Clojure Styleguide in case the last line of code before the cursor contains any unmatched open or close delimiter character, i.e. (, [, {, }, ] or ). Otherwise, the current (custom) indentation is followed. Comments are skipped and no smart indentation is applied within strings.
For simplicity's sake no distinction is made between the different types of open and closing delimiters. (A form of structural editing is assumed - tested with Parinfer - that forces each open delimiter to be properly closed with a matching closing delimiter.) The use of tab characters is not supported: they are counted as single spaces.
Usage
This package provides a CodeMirror 6 extension for smart Clojure indentation.
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { clojure } from "@nextjournal/lang-clojure";
import { indentService } from "@codemirror/language";
import { clojureSmartIndentExtension } from "@jurjanpaul/codemirror6-clojure-smart-indent";
new EditorView({
state: EditorState.create({
doc: "(defn foo [bar]\n (println bar))",
extensions: [
clojure(),
clojureSmartIndentExtension(indentService)
],
}),
parent: document.getElementById("editor")
});Note the need to pass in the indentService (to prevent ending up with separate instances).
Demo
A demo editor is provided in demo/index.html. (Run npm run build first.)
Alternatively, see it in action in the Away from Preferred Editor ClojureScript Playground or in the demo page for codemirror6-parinfer.
Motivation
After upgrading Away from Preferred Editor ClojureScript Playground from CodeMirror 5 to CodeMirror 6 (and making Parinfer work), it became clear that the de facto standard Clojure language extension by Nextjournal did not provide Smart Indentation the way the original Clojure mode had. With a fork I got it to somewhat work, but given the fact that these days a number of competing indentation styles are in use within the Clojure community it always seemed better to make this into its own extension.
Future
Nothing planned, but if the need arises I can imagine adding an option that allows specifying custom 'body form' symbols.
Approach
Indentation is determined by scanning characters in the text before the cursor.
Originally, I hoped to be able to take advantage of the existing Clojure Lezer parser. This worked quite well in a fork of @nextjournal/lang-clojure, but I wanted this to be a separate extension and in that context using the existing parser and syntax tree to determine smart indentation proved a lot more involved than I expected. Perhaps I will revisit the approach at some point.
