Skip to content

Commit 1bb5607

Browse files
committed
Pretty print selection (closes #123)
1 parent 96b970d commit 1bb5607

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
### WIP
22

3-
- Remove bg on quoted strings inside metadata
4-
- Handle eval of `#_` forms in nREPL JVM
3+
- Pretty print selection #123
54
- Execute code from inside top-level `; ...` and `#_...` #124
5+
- Remove background color on quoted strings inside metadata
6+
- Better handle eval of `#_` forms in nREPL JVM
67

78
### 4.2.2 - Sep 27, 2024
89

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
"caption": "Clojure Sublimed: Reindent Buffer",
7777
"command": "clojure_sublimed_reindent_buffer"
7878
},
79+
{
80+
"caption": "Clojure Sublimed: Pretty-print selection",
81+
"command": "clojure_sublimed_pretty_print"
82+
},
7983
{
8084
"caption": "Clojure Sublimed: Insert Newline",
8185
"command": "clojure_sublimed_insert_newline"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ To enable correct indentations as you type code, rebind `Enter` to `Clojure Subl
7777
{"key": "panel_has_focus", "operator": "equal", "operand": false}]}
7878
```
7979

80+
Indent will fix indentation, but will not create new lines. To format a completely unformatted source, use `Pretty-print selection` command:
81+
82+
<img src="https://raw.github.com/tonsky/Clojure-Sublimed/master/screenshots/pretty_print.gif" width="830" height="550" alt="Pretty print">
83+
8084

8185
# REPL clients
8286

cs_indent.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sublime, sublime_plugin
2-
from . import cs_cljfmt, cs_common, cs_parser
2+
from . import cs_cljfmt, cs_common, cs_parser, cs_printer
33

44
def search_path(node, pos):
55
"""
@@ -145,6 +145,19 @@ def run(self, edit):
145145
else:
146146
view.run_command('clojure_sublimed_reindent_lines')
147147

148+
class ClojureSublimedPrettyPrintCommand(sublime_plugin.TextCommand):
149+
def run(self, edit):
150+
view = self.view
151+
change_id = view.change_id()
152+
for region in [r for r in view.sel()]:
153+
region = view.transform_region_from(region, change_id)
154+
if region.empty():
155+
region = cs_parser.topmost_form(view, region.begin())
156+
form = view.substr(region)
157+
node = cs_parser.parse(form)
158+
formatted = cs_printer.format(form, node, limit = cs_common.wrap_width(view))
159+
view.replace(edit, region, formatted)
160+
148161
def cljfmt_indent(view, point):
149162
i = None
150163
try:

screenshots/pretty_print.gif

729 KB
Loading

0 commit comments

Comments
 (0)