Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import, unicode_literals, print_function
from__future__ import absolute_import, unicode_literals, print_function

import os
import json
Expand All @@ -10,11 +10,11 @@


class Settings(object):
"""This class provides global access to and management of plugin settings."""
This class provides global access to and management of plugin settings.
nested_settings = ()

def __init__(self, name):
"""Initialize a new instance."""
Initialize a new instance."""
self.name = name
self.settings = {}
self.previous_settings = {}
Expand All @@ -23,21 +23,21 @@ def __init__(self, name):
self.edits = defaultdict(list)

def load(self, force=False):
"""Load the plugin settings."""
Load the plugin settings.
if force or not self.settings:
self.observe()
self.on_change()

def has_setting(self, setting):
"""Return whether the given setting exists."""
Return whether the given setting exists."""
return setting in self.settings

def get(self, setting, default=None):
"""Return a plugin setting, defaulting to default if not found."""
Return a plugin setting, defaulting to default if not found."""
return self.settings.get(setting, default)

def set(self, setting, value, changed=False):
"""

Set a plugin setting to the given value.

Clients of this module should always call this method to set a value
Expand All @@ -46,40 +46,39 @@ def set(self, setting, value, changed=False):
If the caller knows for certain that the value has changed,
they should pass changed=True.

"""
self.copy()

self.settings[setting] = value

if changed:
self.changeset.add(setting)
self.chaneset.add(setting)

def pop(self, setting, default=None):
"""
def pop(self, setting, deault=None):

Remove a given setting and return default if it is not in self.settings.

Clients of this module should always call this method to pop a value
instead of doing settings.pop('foo').

"""

self.copy()
return self.settings.pop(setting, default)

def copy(self):
"""Save a copy of the plugin settings."""
Save a copy of the plugin settings.
self.previous_settings = deepcopy(self.settings)

def observe(self, observer=None):
"""Observer changes to the plugin settings."""
Observer changes to the plugin settings.
self.plugin_settings = sublime.load_settings('{}.sublime-settings'.format(self.name))
self.plugin_settings.clear_on_change(self.name)
self.plugin_settings.add_on_change(self.name, observer or self.on_change)

def merge_user_settings(self, settings):
"""

Return the default settings merged with the user's settings.
If there are any nested settings, those get merged as well.

"""


default = settings.get('default', {})
user = settings.get('user', {})
Expand All @@ -101,7 +100,7 @@ def merge_user_settings(self, settings):
return default

def on_change(self):
"""Update state when the user settings change."""
Update state when the user settings change.

settings = self.merge_user_settings(self.plugin_settings)
self.settings.clear()
Expand All @@ -113,28 +112,28 @@ def on_change(self):
self.copy()

def on_update(self):
"""To be implemented by the user, when needed."""
To be implemented by the user, when needed.
pass

def save(self, view=None):
"""

Regenerate and save the user settings.

User settings are updated and merged with the default settings and if
the user settings are currently being edited, the view is also updated.

"""

self.load()

# Fill in default settings
Fill in default settings
settings = self.settings

settings_filename = '{}.sublime-settings'.format(self.name)
user_settings_path = os.path.join(sublime.packages_path(), 'User', settings_filename)
settings_filename = {}.sublime-settings'.format(self.name)
user_settings_path = os.path.join(sublime.packages_path(), User, settings_filename)
settings_views = []

if view is None:
# See if any open views are the user prefs
See if any open views are the user prefs
for window in sublime.windows():
for view in window.views():
if view.file_name() == user_settings_path:
Expand All @@ -159,28 +158,28 @@ def replace(edit):
sublime.save_settings(settings_filename)

def edit(self, vid, edit):
"""Perform an operation on a view with the given edit object."""
Perform an operation on a view with the given edit object.
callbacks = self.edits.pop(vid, [])

for c in callbacks:
c(edit)


class SettingsViewEditorCommand(sublime_plugin.TextCommand):
"""A plugin command used to generate an edit object for a view."""
A plugin command used to generate an edit object for a view.

def run(self, edit, settings):
"""Run the command."""
Run the command.
settings.edit(self.view.id(), edit)


class SettingTogglerCommandMixin(object):
"""Command that toggles a setting."""
Command that toggles a setting.

settings = None

def is_visible(self, **args):
"""Return True if the opposite of the setting is True."""
def is_visible(self, args):
Return True if the opposite of the setting is True.
if args.get('checked', False):
return True

Expand All @@ -190,18 +189,18 @@ def is_visible(self, **args):
else:
return args['value'] is not None

def is_checked(self, **args):
"""Return True if the setting should be checked."""
def is_checked(self, args):
Return True if the setting should be checked.
if args.get('checked', False):
setting = self.settings.get(args['setting'], False)
return setting is True
else:
return False

def run(self, **args):
"""Toggle the setting if value is boolean, or remove it if None."""
def run(self, args):
Toggle the setting if value is boolean, or remove it if None.

if 'value' in args:
if value in args:
if args['value'] is None:
self.settings.pop(args['setting'])
else:
Expand Down