Skip to content

Commit

Permalink
Merge pull request #2537 from BetterThanTomorrow/2533-pretty-print-co…
Browse files Browse the repository at this point in the history
…mmand-option

2533 pretty print command option
  • Loading branch information
PEZ authored Apr 25, 2024
2 parents 3538608 + 17bb0c7 commit 4c57851
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes to Calva.

## [Unreleased]

- Fix: [Replace Current Form with Pretty Printed Form also sorts map entries alphabetically by key](https://github.com/BetterThanTomorrow/calva/issues/2535)

## [2.0.447] - 2024-04-24

- [Make the inspector sort maps on the keys](https://github.com/BetterThanTomorrow/calva/issues/2534)
Expand Down
22 changes: 22 additions & 0 deletions docs/site/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ Unlike with the ”real” Calva Formatter, which never breaks up lines, this on
!!! Note "Applies to the other Current Form"
Unlike the other Format commands, which applies to the current _enclosing_ form, this one applies to the [Current Form, same as with evaluations](evaluation.md#current-form). That is because this is not really part of the Calva formatter, but rather is a convenience command for tidying up code or data.

#### Provide zprint config via keyboard shortcuts (or Joyride)

The default options for the command are to not sort maps and to not insert commas between map entries. However, the command takes a map as an argument, expecting this map to be a [zprint configuration]((https://github.com/kkinnear/zprint/blob/main/doc/reference.md)) map. You can provide the argument map via keyboard shortcuts. In VS Code settings, you use JSON, but it will be converted to EDN before handed to zprint.

Say you want to have maps sorted by their keys, and generally use community standard formating + justify maps and bindings. This keyboard shortcut does that.

```json
{
"key": "ctrl+alt+p enter",
"command": "calva.prettyPrintReplaceCurrentForm",
"args": {
"style": ["community", "justified"],
"map": {
"comma?": false,
"sort?": true,
}
}
}
```

Note that we have to specify `"comma?": false` here, because this argument will replace the default options maps for the command.

## Configuration

You can adjust the above mentioned defaults, and the default indents, by configuring the formatting using [cljfmt's configuration EDN](https://github.com/weavejester/cljfmt#configuration).
Expand Down
15 changes: 11 additions & 4 deletions src/cljs-lib/src/calva/pprint/printer.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns calva.pprint.printer
(:require [zprint.core :as zprint]
[calva.js-utils :refer [jsify cljify]]
[clojure.string]))
[clojure.string]
[clojure.walk :as walk]))

(def colors {:brace :white,
:bracket :white,
Expand Down Expand Up @@ -39,17 +40,23 @@
(zprint/zprint-file-str s "Calva" (assoc opts :color-map colors))}
(catch js/Error e
{:value s
:error (str "Plain printing, b/c pprint failed. (" (.-message e) ")")}))]
:error (str "Pretty print failed. (" (.-message e) ")")}))]
result))

(defn string-to-keyword [x]
(if (string? x)
(keyword x)
x))

(defn strings->keywords [data]
(walk/postwalk string-to-keyword data))

(defn pretty-print-js [s {:keys [maxLength, maxDepth, map-commas?] :as all-opts}]
(println "all-opts" all-opts)
(let [opts (into {}
(remove (comp nil? val)
(-> all-opts
(dissoc :maxLength :maxDepth :printEngine :enabled :map-commas?)
(update :style (partial mapv keyword))
strings->keywords
(merge {:max-length maxLength
:max-depth maxDepth}))))
opts (if (nil? map-commas?)
Expand Down
12 changes: 8 additions & 4 deletions src/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ export function replace(
);
}

export function prettyPrintReplaceCurrentForm(options = { enabled: true }) {
export function prettyPrintReplaceCurrentForm(
options = { map: { 'sort?': false, 'comma?': false } }
) {
const editor = util.getActiveTextEditor();
const document = editor.document;
const selection = editor.selections[0];
const range = selection.isEmpty
? select.getFormSelection(document, selection.active, false)
: selection;
const text = document.getText(range);
const result = printer.prettyPrint(text, { ...options, 'map-commas?': false });
if (result) {
return replace(editor, range, result.value);
const result = printer.prettyPrint(text, options);
if (result.error) {
void vscode.window.showErrorMessage(`${result.error}`, 'OK');
return;
}
return replace(editor, range, result.value);
}

0 comments on commit 4c57851

Please sign in to comment.