Skip to content

Commit

Permalink
Retain multiple caret position after wrapping with abbreviation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Oct 27, 2020
1 parent 0f8cf51 commit 5e15d2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
22 changes: 22 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ def replace_with_snippet(view: sublime.View, edit: sublime.Edit, region: sublime
'contents': preprocess_snippet(snippet)
})

def multicursor_replace_with_snippet(view: sublime.View, edit: sublime.Edit, payload: list):
"Replaces multiple regions with snippets, maintaining final caret positions"
sels = []
doc_size = view.size()
for region, snippet in reversed(list(payload)):
replace_with_snippet(view, edit, region, snippet)

# Update locations of existing regions
next_size = view.size()
delta = next_size - doc_size
for r in sels:
r.a += delta
r.b += delta

doc_size = next_size
sels += list(view.sel())

s = view.sel()
s.clear()
s.add_all(sels)



def get_caret(view: sublime.View) -> int:
"Returns current caret position for single selection"
Expand Down
25 changes: 12 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .lib.remove_tag import remove_tag
from .lib.split_join_tag import split_join_tag
from .lib.update_image_size import update_image_size
from .lib.utils import get_caret, narrow_to_non_space, replace_with_snippet, go_to_pos
from .lib.utils import get_caret, narrow_to_non_space, replace_with_snippet, multicursor_replace_with_snippet
from .lib.telemetry import track_action, check_telemetry
from .lib.config import get_settings

Expand Down Expand Up @@ -338,14 +338,14 @@ def run(self, edit, wrap_abbreviation):
global last_wrap_abbreviation # pylint: disable=global-statement

if wrap_abbreviation:
entries = self.wrap_entries[:]
entries.reverse()
payload = []
last_wrap_abbreviation = wrap_abbreviation

for region, config in entries:
for region, config in self.wrap_entries:
snippet = emmet_sublime.expand(wrap_abbreviation, config)
replace_with_snippet(self.view, edit, region, snippet)
payload.append((region, snippet))

last_wrap_abbreviation = wrap_abbreviation
multicursor_replace_with_snippet(self.view, edit, payload)

track_action('Wrap With Abbreviation')

Expand Down Expand Up @@ -387,15 +387,14 @@ class EmmetWrapWithAbbreviationPreview(sublime_plugin.TextCommand):
"Internal command to preview abbreviation in text"

def run(self, edit: sublime.Edit, items: list):
items = items[:]
items.reverse()
r = None

payload = []
for begin, end, result in items:
r = sublime.Region(begin, end)
replace_with_snippet(self.view, edit, r, result)
payload.append((sublime.Region(begin, end), result))

multicursor_replace_with_snippet(self.view, edit, payload)

if r is not None:
if payload:
r = payload[0][0]
self.view.show_at_center(r.begin())


Expand Down

0 comments on commit 5e15d2f

Please sign in to comment.