Home Download Buy Blog Forum Support

Cycle edit positions

Cycle edit positions

Postby agibsonsw on Sun Mar 04, 2012 9:11 pm

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 :D

Code: Select all
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)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Cycle edit positions

Postby agibsonsw on Sun Mar 04, 2012 10:41 pm

Whilst it is not easy to make this 'bullet-proof' I quite like this new/alternative version 8-). 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: Select all
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)

Is there an easier way than:

Code: Select all
view.substr(sublime.Region(0,curr_pos))[-1] == '\n'

to discover the key that was pressed (in an event)? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests

cron