Skip to content

Commit

Permalink
Fix issues with JumpBack function in ST3
Browse files Browse the repository at this point in the history
- Rename JumpBack to JumpPrev due to clash with ST3 standard command
- Change last buffer from array to deque to prevent need to manually
  manage length
  • Loading branch information
stephenfin committed Nov 25, 2013
1 parent 91dccd6 commit dd63989
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 44 deletions.
20 changes: 9 additions & 11 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
"caption": "-"
},
{
"command": "navigate_to_definition",
"args": {},
"command": "navigate_to_definition",
"args": {},
"caption": "Navigate to Definition"
},
},
{
"command": "jump_back",
"args": {},
"command": "jump_prev",
"args": {},
"caption": "Jump Back"
},
},
{
"command": "jump_back",
"args": {
"to": "last_modification"
},
"caption": "Jump to Last Modification"
"command": "jump_prev_to_last_modification",
"args": {},
"caption": "Jump Back to Last Modification"
}
]
6 changes: 3 additions & 3 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"keys": ["ctrl+t", "ctrl+y"]
},
{
"command": "jump_back",
"command": "jump_prev",
"keys": ["ctrl+t", "ctrl+b"]
},
{
"command": "jump_back",
"command": "jump_prev",
"keys": ["ctrl+shift+comma"]
},
{
"command": "jump_back_to_last_modification",
"command": "jump_prev_to_last_modification",
"keys": ["ctrl+t", "ctrl+m"]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Default.sublime-mousemap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"button": "button2",
"count": 1,
"modifiers": ["ctrl","shift"],
"command": "jump_back"
"command": "jump_prev"
}
]
2 changes: 1 addition & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"command": "navigate_to_definition"
},
{
"command": "jump_back"
"command": "jump_prev"
},
{
"command": "rebuild_tags"
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ Command Key Binding Alt Binding Mouse Binding
============================== ================ =========== ======================
rebuild_ctags ctrl+t, ctrl+r
navigate_to_definition ctrl+t, ctrl+t ctrl+> ctrl+shift+left_click
jump_back ctrl+t, ctrl+b ctrl+< ctrl+shift+right_click
jump_back to_last_modification ctrl+t, ctrl+m
jump_prev ctrl+t, ctrl+b ctrl+< ctrl+shift+right_click
jump_prev_to_last_modification ctrl+t, ctrl+m
show_symbols alt+s
show_symbols (multi) alt+shift+s
show_symbols (suffix) ctrl+alt+shift+s
Expand Down
55 changes: 29 additions & 26 deletions ctagsplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from itertools import chain
from operator import itemgetter as iget
from collections import defaultdict
from collections import defaultdict, deque

try:
import sublime
Expand Down Expand Up @@ -67,14 +67,15 @@


def get_settings():
"""Load settings
"""Load settings.
:returns: dictionary containing settings"""
:returns: dictionary containing settings
"""
return sublime.load_settings("CTags.sublime-settings")


def get_setting(key, default=None):
"""Load individual setting
"""Load individual setting.
:param key: setting key to get value for
:param default: default value to return if no value found
Expand Down Expand Up @@ -454,7 +455,7 @@ def get_current_file_suffix(path):
Sublime Commands
"""

"""Jumpback Commands"""
"""JumpPrev Commands"""


def different_mod_area(f1, f2, r1, r2):
Expand All @@ -463,7 +464,7 @@ def different_mod_area(f1, f2, r1, r2):
return not same_file or not same_region


class JumpBack(sublime_plugin.WindowCommand):
class JumpPrev(sublime_plugin.WindowCommand):
"""Provide ``jump_back`` command.
Command "jumps back" to the previous code point before a tag was navigated
Expand All @@ -472,35 +473,37 @@ class JumpBack(sublime_plugin.WindowCommand):
This is functionality supported natively by ST3 but not by ST2. It is
therefore included for legacy purposes.
"""
last = []
buf = deque(maxlen=100) # virtually a "ring buffer"

def is_enabled(self):
return len(self.last) > 0
# disable if nothing in the buffer
return len(self.buf) > 0

def is_visible(self):
return setting('show_context_menus')

def run(self):
if not JumpBack.last:
return status_message('JumpBack buffer empty')
if not self.buf:
return status_message('JumpPrev buffer empty')

f, sel = JumpBack.last.pop()
self.jump(f, eval(sel))
file_name, sel = self.buf.pop()
self.jump(file_name, sel)

def jump(self, fn, sel):
@on_load(fn, begin_edit=True)
def and_then(view):
select(view, sublime.Region(*sel))
select(view, sel)

@classmethod
def append(cls, view):
"""Append a file name to a a list"""
"""Append a code point to the list"""
fn = view.file_name()
if fn:
cls.last.append((fn, repr(view.sel()[0])))
sel = [s for s in view.sel()][0]
cls.buf.append((fn, sel))


class JumpBackToLastModification(sublime_plugin.WindowCommand):
class JumpPrevToLastModification(sublime_plugin.WindowCommand):
"""Provide ``jump_back_to_last_modification`` command.
Command "jumps back" to a previous modification point
Expand All @@ -527,7 +530,7 @@ def lastModifications(self):
cf = cv.file_name()

# very latest, s)tarting modification
sf, sr = JumpBackToLastModification.mods.pop(0)
sf, sr = JumpPrevToLastModification.mods.pop(0)

if sf is None:
return
Expand All @@ -538,18 +541,18 @@ def lastModifications(self):
# default j)ump f)ile and r)egion
jf, jr = sf, sr

if JumpBackToLastModification.mods:
for i, (f, r) in enumerate(JumpBackToLastModification.mods):
if JumpPrevToLastModification.mods:
for i, (f, r) in enumerate(JumpPrevToLastModification.mods):
region = eval(r)
if different_mod_area(sf, f, sr, region):
break

del JumpBackToLastModification.mods[:i]
del JumpPrevToLastModification.mods[:i]
if not in_different_mod_area:
jf, jr = f, region

if in_different_mod_area or not JumpBackToLastModification.mods:
JumpBackToLastModification.mods.insert(0, (jf, repr(jr)))
if in_different_mod_area or not JumpPrevToLastModification.mods:
JumpPrevToLastModification.mods.insert(0, (jf, repr(jr)))

self.jump(jf, jr)

Expand All @@ -559,7 +562,7 @@ def and_then(view):
select(view, sublime.Region(*sel))


class JumpBackListener(sublime_plugin.EventListener):
class JumpPrevListener(sublime_plugin.EventListener):
"""Maintain a list of edit points to jump back to.
Maintains a list of the last x edit points (points where a character is
Expand All @@ -572,9 +575,9 @@ class JumpBackListener(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()
if len(sel):
JumpBackToLastModification.mods.insert(
JumpPrevToLastModification.mods.insert(
0, (view.file_name(), repr(sel[0])))
del JumpBackToLastModification.mods[100:]
del JumpPrevToLastModification.mods[100:]


"""CTags commands"""
Expand All @@ -593,7 +596,7 @@ def show_tag_panel(view, result, jump_directly):

def on_select(i):
if i != -1:
JumpBack.append(view)
JumpPrev.append(view)
scroll_to_tag(view, args[i])

if jump_directly and len(args) == 1:
Expand Down

0 comments on commit dd63989

Please sign in to comment.