@@ -40,7 +40,6 @@ def run(self, edit):
4040 self .run (edit )
4141
4242
43-
4443class 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+
104104class 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
146185class SelectionListener (sublime_plugin .EventListener ):
147186
0 commit comments