Skip to content

Commit ed97948

Browse files
committed
implement a hopefully working workaround for #1; implement 'live' split selection (can be disabled via settings)
1 parent 5fb5b1e commit ed97948

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

Main.sublime-menu

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,54 @@
4545
"args": {
4646
"file": "${packages}/MultiEditUtils/Default (OSX).sublime-keymap",
4747
"platform": "OSX"
48-
},
48+
},
4949
"caption": "Key Bindings – Default"
5050
},
5151
{
5252
"command": "open_file",
5353
"args": {
5454
"file": "${packages}/MultiEditUtils/Default (Linux).sublime-keymap",
5555
"platform": "Linux"
56-
},
56+
},
5757
"caption": "Key Bindings – Default"
5858
},
5959
{
6060
"command": "open_file",
6161
"args": {
6262
"file": "${packages}/User/Default (Windows).sublime-keymap",
6363
"platform": "Windows"
64-
},
64+
},
6565
"caption": "Key Bindings – User"
6666
},
6767
{
6868
"command": "open_file",
6969
"args": {
7070
"file": "${packages}/User/Default (OSX).sublime-keymap",
7171
"platform": "OSX"
72-
},
72+
},
7373
"caption": "Key Bindings – User"
7474
},
7575
{
7676
"command": "open_file",
7777
"args": {
7878
"file": "${packages}/User/Default (Linux).sublime-keymap",
7979
"platform": "Linux"
80-
},
80+
},
8181
"caption": "Key Bindings – User"
82+
},
83+
{
84+
"command": "open_file",
85+
"args": {
86+
"file": "${packages}/MultiEditUtils/MultiEditUtils.sublime-settings"
87+
},
88+
"caption": "Settings - Default"
89+
},
90+
{
91+
"command": "open_file",
92+
"args": {
93+
"file": "${packages}/User/MultiEditUtils.sublime-settings"
94+
},
95+
"caption": "Settings - User"
8296
}
8397
]
8498
}

MultiEditUtils.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def run(self, edit):
4040
self.run(edit)
4141

4242

43-
4443
class CycleThroughRegionsCommand(sublime_plugin.TextCommand):
4544

4645
def run(self, edit):
@@ -101,27 +100,53 @@ def areRegionsNormalized(self, regions):
101100
return all(region.a < region.b for region in regions)
102101

103102

103+
104104
class SplitSelectionCommand(sublime_plugin.TextCommand):
105105

106106
def run(self, edit):
107107

108+
self.savedSelection = [r for r in self.view.sel()]
109+
onConfirm, onChange = self.getHandlers()
110+
108111
sublime.active_window().show_input_panel(
109112
"Separating character(s) for splitting the selection",
110113
"",
111-
self.splitSelection,
112-
None,
113-
None
114+
onConfirm,
115+
onChange,
116+
self.restoreSelection
114117
)
115118

116119

120+
def getHandlers(self):
121+
122+
settings = sublime.load_settings("MultiEditUtils.sublime-settings")
123+
live_split_selection = settings.get("live_split_selection")
124+
125+
if live_split_selection:
126+
onConfirm = None
127+
onChange = self.splitSelection
128+
else:
129+
onConfirm = self.splitSelection
130+
onChange = None
131+
132+
return (onConfirm, onChange)
133+
134+
135+
def restoreSelection(self):
136+
137+
selection = self.view.sel()
138+
selection.clear()
139+
selection.add_all(self.savedSelection)
140+
141+
self.workaroundForRefreshBug(self, self.view, selection)
142+
143+
117144
def splitSelection(self, separator):
118145

119146
view = self.view
120-
selection = view.sel()
121-
122147
newRegions = []
123148

124-
for region in selection:
149+
for region in self.savedSelection:
125150
currentPosition = region.begin()
126151
regionString = view.substr(region)
127152

@@ -139,9 +164,23 @@ def splitSelection(self, separator):
139164
newRegions.append(newRegion)
140165
currentPosition += len(subRegion) + len(separator)
141166

167+
selection = view.sel()
142168
selection.clear()
143169
selection.add_all(newRegions)
144170

171+
self.workaroundForRefreshBug(view, selection)
172+
173+
174+
def workaroundForRefreshBug(self, view, selection):
175+
# see:
176+
# https://github.com/code-orchestra/colt-sublime-plugin/commit/9e6ffbf573fc60b356665ff2ba9ced614c71120f
177+
178+
# work around sublime bug with caret position not refreshing
179+
bug = [s for s in selection]
180+
view.add_regions("bug", bug, "bug", "dot", sublime.HIDDEN | sublime.PERSISTENT)
181+
view.erase_regions("bug")
182+
183+
145184

146185
class SelectionListener(sublime_plugin.EventListener):
147186

0 commit comments

Comments
 (0)