Skip to content

Commit

Permalink
Save folding with filename instead of view_index
Browse files Browse the repository at this point in the history
Saved folding will no longer apply in wrong files. Instead it uses full
filepath name which is not perfect but will make folding usable again.

Fixes #68
  • Loading branch information
danielmagnussons committed Mar 3, 2018
1 parent d3021e7 commit 0269d47
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions orgmode_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,47 +62,46 @@ def save(self, view, where='unknow'):
sublime.set_timeout(lambda: self.save(view, where), 100)
return

_id = self.view_index(view)
if _id not in self.db:
self.db[_id] = {}
_filename = view.file_name()
if _filename not in self.db:
self.db[_filename] = {}

# if the result of the new collected data is different
# from the old data, then will write to disk
# this will hold the old value for comparison
old_db = dict(self.db[_id])
old_db = dict(self.db[_filename])

# if the size of the view change outside the application skip
# restoration
self.db[_id]['id'] = int(view.size())
self.db[_filename]['id'] = int(view.size())

# marks
self.db[_id]['m'] = [[item.a, item.b]
self.db[_filename]['m'] = [[item.a, item.b]
for item in view.get_regions("mark")]
if self.debug:
log.debug('marks: ' + str(self.db[_id]['m']))
log.debug('marks: ' + str(self.db[_filename]['m']))

# previous folding save, to be able to refold
if 'f' in self.db[_id] and list(self.db[_id]['f']) != []:
self.db[_id]['pf'] = list(self.db[_id]['f'])
if 'f' in self.db[_filename] and list(self.db[_filename]['f']) != []:
self.db[_filename]['pf'] = list(self.db[_filename]['f'])

# folding
self.db[_id]['f'] = [[item.a, item.b] for item in view.folded_regions()]
self.db[_filename]['f'] = [[item.a, item.b] for item in view.folded_regions()]
if self.debug:
log.debug('fold: ' + str(self.db[_id]['f']))
log.debug('fold: ' + str(self.db[_filename]['f']))


if not self.db[_filename]['f'] and not self.db[_filename]['m']:
if self.debug:
log.debug("Nothing to save")
return

# write to disk only if something changed
if old_db != self.db[_id] or where == 'on_deactivated':
log.debug("Orgmode settings w path: " + self.store)
if old_db != self.db[_filename] or where == 'on_deactivated':
log.debug("Orgmode settings write path: " + self.store)
with GzipFile(self.store, 'wb') as f:
dump(self.db, f, -1)

def view_index(self, view):
window = view.window()
if not window:
window = sublime.active_window()
index = window.get_view_index(view)
return str(window.id()) + str(index)

def restore(self, view, where='unknow'):
if view is None or not view.file_name():
return
Expand All @@ -111,17 +110,15 @@ def restore(self, view, where='unknow'):
sublime.set_timeout(lambda: self.restore(view, where), 100)
return

_id = self.view_index(view)
if self.debug:
log.debug('-----------------------------------')
log.debug('RESTORING from: ' + where)
log.debug('file: ' + view.file_name())
log.debug('_id: ' + _id)

if _id in self.db:
_filename = view.file_name()
if _filename in self.db:
if self.debug:
log.debug('-----------------------------------')
log.debug('RESTORING from: ' + where)
log.debug('file: ' + view.file_name())
# fold
rs = []
for r in self.db[_id]['f']:
for r in self.db[_filename]['f']:
rs.append(sublime.Region(int(r[0]), int(r[1])))
if len(rs):
view.fold(rs)
Expand All @@ -130,13 +127,13 @@ def restore(self, view, where='unknow'):

# marks
rs = []
for r in self.db[_id]['m']:
for r in self.db[_filename]['m']:
rs.append(sublime.Region(int(r[0]), int(r[1])))
if len(rs):
view.add_regions(
"mark", rs, "mark", "dot", sublime.HIDDEN | sublime.PERSISTENT)
if self.debug:
log.debug('marks: ' + str(self.db[_id]['m']))
log.debug('marks: ' + str(self.db[_filename]['m']))


class OrgmodeFoldingCommand(sublime_plugin.TextCommand):
Expand Down

0 comments on commit 0269d47

Please sign in to comment.