Skip to content

Commit 3016612

Browse files
committed
4.4.0 New Align Cursors command
1 parent dd4901e commit 3016612

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
### WIP
1+
### 4.4.0 - Dec 30, 2024
22

3+
- New `Align Cursors` command
34
- Color scheme adjustments
45

56
### 4.3.2 - Dec 9, 2024

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
"caption": "Clojure Sublimed: Add Watch",
9797
"command": "clojure_sublimed_add_watch"
9898
},
99+
{
100+
"caption": "Clojure Sublimed: Align Cursors",
101+
"command": "clojure_sublimed_align_cursors_command"
102+
},
99103
{
100104
"caption": "Preferences: Clojure Sublimed Settings",
101105
"command": "edit_settings",

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ By default Sublime will try to use `;;` because it’s most similar to other lan
9494

9595
Clojure Sublimed offer `Toggle Comment` command that can be used instead of Sublime provided one in Clojure sources. See Keymap on how to enable.
9696

97+
# Align cursors command
98+
99+
If you select multiple cursors on different lines and run `Align cursors` command, Clojure Sublimed will align them in a column. Works with multiple columns, too:
100+
101+
<img src="https://raw.github.com/tonsky/Clojure-Sublimed/master/screenshots/align_cursors.gif" width="500" height="201" alt="Align cursors">
97102

98103
# REPL clients
99104

cs_indent.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import re
1+
import collections, re
22
import sublime, sublime_plugin
33
from . import cs_cljfmt, cs_common, cs_parser, cs_printer
44

@@ -195,3 +195,43 @@ def run(self, edit):
195195
view.replace(edit, region, string)
196196
# Add selection at the end of newly inserted region
197197
view.sel().add(sublime.Region(point, point))
198+
199+
class ClojureSublimedAlignCursorsCommand(sublime_plugin.TextCommand):
200+
def run(self, edit):
201+
view = self.view
202+
by_row = collections.defaultdict(list)
203+
for region in view.sel():
204+
row, col = view.rowcol(region.a)
205+
by_row[row].append(region)
206+
# print('by_row', by_row)
207+
cols = max(len(line_regions) for line_regions in by_row.values())
208+
# print('cols', cols)
209+
change_id = view.change_id()
210+
# upd = lambda r: view.transform_region_from(r, change_id)
211+
for col in range(0, cols):
212+
col_regions = [line_regions[col] for line_regions in by_row.values() if len(line_regions) > col]
213+
col_regions = [view.transform_region_from(r, change_id) for r in col_regions]
214+
col_regions.sort(key = lambda r: view.rowcol(r.a)[0])
215+
# print('col', col, col_regions)
216+
max_col = max(view.rowcol(r.a)[1] for r in col_regions)
217+
max_len = max(r.size() for r in col_regions)
218+
# print("max_col", max_col, "max_len", max_len)
219+
change_id_2 = view.change_id()
220+
for r in col_regions:
221+
r = view.transform_region_from(r, change_id_2)
222+
_, col = view.rowcol(r.begin())
223+
length = r.size()
224+
prepend = max_col - col
225+
append = max_len - length
226+
# print("r", r, "col", col, "len", length, "left", max_col - col, "right", max_len - length)
227+
view.replace(edit, sublime.Region(r.begin()), ' ' * prepend)
228+
# r = view.transform_region_from(r, change_id)
229+
# print("new r", r)
230+
view.replace(edit, sublime.Region(r.end() + prepend), ' ' * append)
231+
232+
233+
# change_id = view.change_id()
234+
# for region in list(view.sel()):
235+
# region = view.transform_region_from(region, change_id)
236+
# _, col = view.rowcol(region.a)
237+
# view.replace(edit, region, ' ' * (max_col - col))

screenshots/align_cursors.gif

215 KB
Loading

screenshots/syntaxes.png

-51.9 KB
Loading

test_repl/indent.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,10 @@ nil)
100100
y 4]
101101
(sum (square x)
102102
(square y))))
103+
104+
(let [x 1
105+
x+y 22
106+
x+y+z 333
107+
a {:a 1, :b 2, :c 3}
108+
bb {:a+b 123, :c+d 456, :e+f 789}
109+
ccc {:a+b+c 12345, :d+e+f 67890, :g+h+i 0xFFFFFF}])

0 commit comments

Comments
 (0)