Home Download Buy Blog Forum Support

Starting a new plugin

Re: Starting a new plugin

Postby agibsonsw on Sun Mar 04, 2012 2:42 pm

viewtopic.php?f=4&t=2112

This versions now works for whichever view you are in. That is, it cycles through the last five edit locations for the current view.

Code: Select all
import sublime, sublime_plugin

POSITIONS = {}

class LastEditLineCommand(sublime_plugin.TextCommand):
   posn = 0
   def run(self, edit):
      vid = self.view.id()
      if not POSITIONS.has_key(vid): return
      if len(POSITIONS[vid]) <= self.posn + 1:
         self.posn = 0
      self.view.sel().clear()
      self.view.show(POSITIONS[vid][-(self.posn+1)])
      self.view.sel().add(self.view.line(POSITIONS[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 POSITIONS.has_key(vid):
         POSITIONS[vid] = [curr_line, sel.begin()]
      elif POSITIONS[vid][0] != curr_line:
         POSITIONS[vid].append(sel.begin())
         POSITIONS[vid][0] = curr_line
      if len(POSITIONS[vid]) > 6: POSITIONS[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: Starting a new plugin

Postby khrizt on Mon Mar 05, 2012 9:44 pm

I've done some improvements, now the plugin is multi-file safe and swtiches the view if the last edit position was in another opened file. Please if someone could take a look at the code and tell me if you see anything wrong:

Code: Select all
import sublime
import sublime_plugin


class LastEditLineCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        if (len(CaptureEditing.last_view) > 0 and self.view.id() != CaptureEditing.last_view[-1] and CaptureEditing.last_view[-1] > 0):
            sublime.active_window().focus_view(CaptureEditing.view_refs[CaptureEditing.last_view[-1]])
            CaptureEditing.last_view.pop(-1)
        if len(CaptureEditing.last_line[self.view.id()]) > CaptureEditing.posn[self.view.id()]:
            self.view.show(self.view.line(CaptureEditing.last_line[self.view.id()][-(CaptureEditing.posn[self.view.id()] + 1)]))
            self.view.sel().clear()
            self.view.sel().add(self.view.line(CaptureEditing.last_line[self.view.id()][-(CaptureEditing.posn[self.view.id()] + 1)]))
            CaptureEditing.posn[self.view.id()] = (CaptureEditing.posn[self.view.id()] + 1) % 5
        else:
            CaptureEditing.posn[self.view.id()] = 0


class CaptureEditing(sublime_plugin.EventListener):
    last_line = {}
    prev_line = {}
    posn = {}
    last_view = []
    view_refs = {}

    def on_modified(self, view):
        sel = view.sel()[0]
        curr_line, _ = view.rowcol(sel.begin())
        CE = CaptureEditing
        if CE.prev_line[view.id()] == -1 or curr_line != CE.prev_line[view.id()]:
            CE.prev_line[view.id()] = curr_line
            CE.last_line[view.id()].append(sel.begin())
            if len(CE.last_line[view.id()]) > 10:
                CE.last_line[view.id()].pop(0)
            CE.last_view.append(view.id())
            if len(CE.last_view) > 10:
                CE.last_view.pop(0)

    def on_close(self, view):
        CE = CaptureEditing
        CE.last_line.remove(view.id())
        CE.prev_line.remove(view.id())
        CE.posn.remove(view.id())
        CE.view_refs.remove(view.id())

    def on_new(self, view):
        CE = CaptureEditing
        CE.last_line[view.id()] = []
        CE.prev_line[view.id()] = -1
        CE.posn[view.id()] = 0
        CE.view_refs[view.id()] = view

    def on_activated(self, view):
        CE = CaptureEditing
        if view.id() not in CE.last_line:
            CE.last_line[view.id()] = []
        if view.id() not in CE.prev_line:
            CE.prev_line[view.id()] = -1
        if view.id() not in CE.posn:
            CE.posn[view.id()] = 0
        if view.id() not in CE.view_refs:
            CE.view_refs[view.id()] = view


Thanks a lot
khrizt
 
Posts: 14
Joined: Thu Feb 23, 2012 8:08 am

Previous

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], mwb1100, wkirby and 20 guests