Permalink
Browse files

Fix instant search (being edit, end edit mess), rename of files does …

…not carry changes, history objects
  • Loading branch information...
titoBouzout committed Jun 6, 2013
1 parent c4893e9 commit d37e6693d169a6d5a0eff8dcb86ca35e32c499e3
Showing with 178 additions and 99 deletions.
  1. +15 −0 CHANGELOG
  2. +76 −0 Edit.py
  3. +8 −8 SideBar.py
  4. +76 −0 sidebar/Edit.py
  5. +3 −91 sidebar/SideBarItem.py
View
@@ -2,6 +2,21 @@
SideBarEnhancements Changelog:
+ .2
+
+ BUGS FIXED
+
+ * fix instant search (begin edit, end edit mess)..
+ * rename of "dirty" view, does not carry changes.
+ * rename of view, does not carry undo/redo history.
+
+ TODO
+
+ * Use a real clipboard integrated with the OS
+ * fix canary
+ * fix.. reveal does not work with "," in path names
+ * copy and paste in background
+ * copy image cant' copy image sizes
.1
BUGS FIXED
View
76 Edit.py
@@ -0,0 +1,76 @@
+# edit.py
+# buffer editing for both ST2 and ST3 that "just works"
+
+import sublime
+import sublime_plugin
+from collections import defaultdict
+
+try:
+ sublime.edit_storage
+except AttributeError:
+ sublime.edit_storage = {}
+
+class EditStep:
+ def __init__(self, cmd, *args):
+ self.cmd = cmd
+ self.args = args
+
+ def run(self, view, edit):
+ if self.cmd == 'callback':
+ return self.args[0](view, edit)
+
+ funcs = {
+ 'insert': view.insert,
+ 'erase': view.erase,
+ 'replace': view.replace,
+ }
+ func = funcs.get(self.cmd)
+ if func:
+ func(edit, *self.args)
+
+
+class Edit:
+ defer = defaultdict(dict)
+
+ def __init__(self, view):
+ self.view = view
+ self.steps = []
+
+ def step(self, cmd, *args):
+ step = EditStep(cmd, *args)
+ self.steps.append(step)
+
+ def insert(self, point, string):
+ self.step('insert', point, string)
+
+ def erase(self, region):
+ self.step('erase', region)
+
+ def replace(self, region, string):
+ self.step('replace', region, string)
+
+ def callback(self, func):
+ self.step('callback', func)
+
+ def run(self, view, edit):
+ for step in self.steps:
+ step.run(view, edit)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ view = self.view
+ if sublime.version().startswith('2'):
+ edit = view.begin_edit()
+ self.run(edit)
+ view.end_edit(edit)
+ else:
+ key = str(hash(tuple(self.steps)))
+ sublime.edit_storage[key] = self.run
+ view.run_command('apply_edit', {'key': key})
+
+
+class apply_edit(sublime_plugin.TextCommand):
+ def run(self, edit, key):
+ sublime.edit_storage.pop(key)(self.view, edit)
View
@@ -8,6 +8,8 @@
from .sidebar.SideBarSelection import SideBarSelection
from .sidebar.SideBarProject import SideBarProject
+from .Edit import Edit as Edit
+
# needed for getting local app data path on windows
if sublime.platform() == 'windows':
import winreg
@@ -22,7 +24,7 @@ def expandVars(path):
# A "directory" for a user is a "folder"
def checkVersion():
- version = '2012.04.28.1605.1';
+ version = '2012.06.06.1611.2';
if s.get('version') != version:
SideBarItem(sublime.packages_path()+'/SideBarEnhancements/CHANGELOG', False).edit();
s.set('version', version);
@@ -316,10 +318,9 @@ def run(self, paths = []):
view.set_name('Instant File Search')
view.set_syntax_file('Packages/SideBarEnhancements/SideBar Results.hidden-tmLanguage')
view.set_scratch(True)
- edit = view.begin_edit()
view.settings().set('sidebar_instant_search_paths', paths)
- view.replace(edit, sublime.Region(0, view.size()), "Type to search: ")
- view.end_edit(edit)
+ with Edit(view) as edit:
+ edit.replace(sublime.Region(0, view.size()), "Type to search: ")
view.sel().clear()
view.sel().add(sublime.Region(16))
sidebar_instant_search += 1
@@ -390,14 +391,13 @@ def run(self):
def on_done(self):
if self.start_time == self.view.settings().get('sidebar_search_paths_start_time'):
view = self.view;
- edit = view.begin_edit()
sel = sublime.Region(view.sel()[0].begin(), view.sel()[0].end())
- view.replace(edit, sublime.Region(0, view.size()), self.match_result);
- view.end_edit(edit)
+ with Edit(view) as edit:
+ edit.replace(sublime.Region(0, view.size()), self.match_result);
view.erase_regions("sidebar_search_instant_highlight")
if self.total < 30000 and len(self.searchTerm) > 1:
regions = [item for item in view.find_all(self.searchTerm, sublime.LITERAL|sublime.IGNORECASE) if item.begin() >= self.highlight_from]
- view.add_regions("sidebar_search_instant_highlight", regions, 'string', sublime.DRAW_EMPTY|sublime.DRAW_OUTLINED|sublime.DRAW_EMPTY_AS_OVERWRITE)
+ view.add_regions("sidebar_search_instant_highlight", regions, '', '', sublime.DRAW_EMPTY|sublime.DRAW_OUTLINED|sublime.DRAW_EMPTY_AS_OVERWRITE)
view.sel().clear()
view.sel().add(sel)
View
@@ -0,0 +1,76 @@
+# edit.py
+# buffer editing for both ST2 and ST3 that "just works"
+
+import sublime
+import sublime_plugin
+from collections import defaultdict
+
+try:
+ sublime.edit_storage
+except AttributeError:
+ sublime.edit_storage = {}
+
+class EditStep:
+ def __init__(self, cmd, *args):
+ self.cmd = cmd
+ self.args = args
+
+ def run(self, view, edit):
+ if self.cmd == 'callback':
+ return self.args[0](view, edit)
+
+ funcs = {
+ 'insert': view.insert,
+ 'erase': view.erase,
+ 'replace': view.replace,
+ }
+ func = funcs.get(self.cmd)
+ if func:
+ func(edit, *self.args)
+
+
+class Edit:
+ defer = defaultdict(dict)
+
+ def __init__(self, view):
+ self.view = view
+ self.steps = []
+
+ def step(self, cmd, *args):
+ step = EditStep(cmd, *args)
+ self.steps.append(step)
+
+ def insert(self, point, string):
+ self.step('insert', point, string)
+
+ def erase(self, region):
+ self.step('erase', region)
+
+ def replace(self, region, string):
+ self.step('replace', region, string)
+
+ def callback(self, func):
+ self.step('callback', func)
+
+ def run(self, view, edit):
+ for step in self.steps:
+ step.run(view, edit)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ view = self.view
+ if sublime.version().startswith('2'):
+ edit = view.begin_edit()
+ self.run(edit)
+ view.end_edit(edit)
+ else:
+ key = str(hash(tuple(self.steps)))
+ sublime.edit_storage[key] = self.run
+ view.run_command('apply_edit', {'key': key})
+
+
+class apply_edit(sublime_plugin.TextCommand):
+ def run(self, edit, key):
+ sublime.edit_storage.pop(key)(self.view, edit)
View
@@ -6,6 +6,8 @@
from .SideBarProject import SideBarProject
+from .Edit import Edit as Edit
+
class Object():
pass
@@ -332,97 +334,7 @@ def _moveMoveViews(self, old, location):
active_view = self._moveMoveView(window, view, view.file_name().replace(old+'/', location+'/', 1), active_view)
def _moveMoveView(self, window, view, location, active_view):
- if active_view == view:
- is_active_view = True
- else:
- is_active_view = False
-
- options = Object()
-
- options.scroll = view.viewport_position()
-
- options.selections = [[item.a, item.b] for item in view.sel()]
-
- options.marks = [[item.a, item.b] for item in view.get_regions("mark")]
-
- options.bookmarks = [[item.a, item.b] for item in view.get_regions("bookmarks")]
-
- if int(sublime.version()) >= 2167:
- options.folds = [[item.a, item.b] for item in view.folded_regions()]
- else:
- options.folds = [[item.a, item.b] for item in view.unfold(sublime.Region(0, view.size()))]
-
- options.syntax = view.settings().get('syntax')
-
- try:
- _window = window or view.window() or sublime.active_window()
- options.position = _window.get_view_index(view)
- except:
- options.position = False
-
- window.focus_view(view)
- if view.is_dirty():
- options.content = view.substr(sublime.Region(0, view.size()))
- view.window().run_command('revert')
- else:
- options.content = False
-
- _view = view
- view = window.open_file(location)
- window.focus_view(_view)
- window.run_command('close')
-
- sublime.set_timeout(lambda: self._moveRestoreView(view, options, window), 200)
-
- if is_active_view:
- window.focus_view(view)
- return view
- else:
- window.focus_view(active_view)
- return active_view
-
- def _moveRestoreView(self, view, options, window):
- if view.is_loading():
- sublime.set_timeout(lambda: self._moveRestoreView(view, options, window), 100)
- else:
- if options.content != False:
- edit = view.begin_edit()
- view.replace(edit, sublime.Region(0, view.size()), options.content);
- view.sel().clear()
- view.sel().add(sublime.Region(0))
- view.end_edit(edit)
-
- if options.position != False:
- try:
- _window = window or view.window() or sublime.active_window()
- group, index = options.position
- _window.set_view_index(view, group, index)
- except:
- pass
-
- if options.syntax:
- view.settings().set('syntax', options.syntax);
-
- for r in options.folds:
- view.fold(sublime.Region(r[0], r[1]))
-
- view.sel().clear()
- for r in options.selections:
- view.sel().add(sublime.Region(r[0], r[1]))
-
- rs = []
- for r in options.marks:
- rs.append(sublime.Region(r[0], r[1]))
- if len(rs):
- view.add_regions("mark", rs, "mark", "dot", sublime.HIDDEN | sublime.PERSISTENT)
-
- rs = []
- for r in options.bookmarks:
- rs.append(sublime.Region(r[0], r[1]))
- if len(rs):
- view.add_regions("bookmarks", rs, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)
-
- view.set_viewport_position(options.scroll, False)
+ view.retarget(location)
def closeViews(self):
path = self.path()

0 comments on commit d37e669

Please sign in to comment.