Sublime Forum

Cycle edit positions

#1

I thought I might re-post the following here, as it seems to be working quite well. If you assign **last_edit_line **to a key binding it moves/cycles between the last five edited positions of the current view.

It’s not bullet-proof - it’s quite easy to distort it by deleting a few lines, undo-ing, duplicating a line etc. - but still quite handy :smiley:

[code]import sublime, sublime_plugin

POSNS = {}

class LastEditLineCommand(sublime_plugin.TextCommand):
posn = 0
def run(self, edit):
vid = self.view.id()
if not POSNS.has_key(vid): return
if len(POSNS[vid]) <= self.posn + 1:
self.posn = 0
self.view.sel().clear()
self.view.show(POSNS[vid]-(self.posn + 1)])
self.view.sel().add(self.view.line(POSNS[vid]-(self.posn + 1)]))
self.posn = (self.posn + 1) % 5

class CaptureEditing(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()[0]
vid = view.id()
curr_line, _ = view.rowcol(sel.begin())
if not POSNS.has_key(vid):
POSNS[vid] = [curr_line, sel.begin()]
elif POSNS[vid][0] != curr_line:
POSNS[vid].append(sel.begin())
POSNS[vid][0] = curr_line
if len(POSNS[vid]) > 6: POSNS[vid].pop(1)[/code]

0 Likes

#2

Whilst it is not easy to make this ‘bullet-proof’ I quite like this new/alternative version :sunglasses:. Pressing Enter is no longer regarded as an edit position, but any existing edit positions *beyond *the cursor are incremented (using a list comprehension, whe’hay!). That is, if you’ve pressed Enter a few times, using your shortcut to move to the edit positions is more likely to select the edited line (rather than a few lines before it).

[code]import sublime, sublime_plugin

POSNS = {}

class LastEditLineCommand(sublime_plugin.TextCommand):
posn = 0
def run(self, edit):
vid = self.view.id()
if not POSNS.has_key(vid): return
if len(POSNS[vid]) <= self.posn + 1:
self.posn = 0
self.view.sel().clear()
self.view.show(POSNS[vid]-(self.posn + 1)])
self.view.sel().add(self.view.line(POSNS[vid]-(self.posn + 1)]))
self.posn = (self.posn + 1) % 5

class CaptureEditing(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()[0]
vid = view.id()
curr_pos = sel.end()
curr_line, _ = view.rowcol(curr_pos)
if not POSNS.has_key(vid):
POSNS[vid] = [curr_line, curr_pos]
elif view.substr(sublime.Region(0,curr_pos))-1] == ‘\n’:
# if you’ve just pressed Enter…
new_list = [x + (x > curr_pos) for x in POSNS[vid][1:]]
POSNS[vid][1:] = new_list
elif POSNS[vid][0] != curr_line:
POSNS[vid].append(curr_pos)
POSNS[vid][0] = curr_line
if len(POSNS[vid]) > 6: POSNS[vid].pop(1)[/code]
Is there an easier way than:

view.substr(sublime.Region(0,curr_pos))-1] == '\n'

to discover the key that was pressed (in an event)? Andy.

0 Likes