SHARE
TWEET

Untitled

a guest Aug 17th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sublime
  2. import sublime_plugin
  3.  
  4. # The global scope ensures that the settings can
  5. # be easily accessed from within all the classes.
  6. global settings
  7.  
  8. def plugin_loaded():
  9.     """
  10.     This module level function is called on ST startup when the API is ready.
  11.     """
  12.    
  13.     global settings
  14.     settings = Settings()
  15.     settings.load_all()
  16.  
  17. def plugin_unloaded():
  18.     """
  19.     This module level function is called just before the plugin is unloaded.
  20.     """
  21.  
  22.     settings.remove_callbacks()
  23.  
  24. class Settings:
  25.     """
  26.     Handles all the settings. A callback method is added for each setting, it
  27.     gets called by ST if that setting is changed in the settings file.
  28.     """
  29.  
  30.     def __init__(self):
  31.  
  32.         FILENAME = "YourPluginName.sublime-settings"
  33.         self.settings = sublime.load_settings(FILENAME)
  34.  
  35.         # User configurable settings.
  36.         self.max_display_length = None
  37.         self.monospace_font     = None
  38.         self.ellipsis_symbols   = None
  39.         self.prefix_custom      = None
  40.         self.command_names      = None
  41.         # ...
  42.  
  43.     def load_all(self):
  44.         self.init_setting("max_display_length", self.set_max_display_length)
  45.         self.init_setting("monospace_font", self.set_monospace_font)
  46.         self.init_setting("ellipsis_symbols", self.set_ellipsis_symbols)
  47.         self.init_setting("prefix_custom", self.set_prefix_custom)
  48.         self.init_setting("command_names", self.set_command_names)
  49.         # ...
  50.  
  51.     def init_setting(self, setting_name, setting_method):
  52.         """
  53.         Calls the setting_method to set the setting's value and registers the
  54.         setting_method as a callback so that it will be called by ST if the
  55.         setting's value is changed by the user.
  56.         """
  57.  
  58.         setting_method()
  59.         self.settings.add_on_change(setting_name, setting_method)
  60.  
  61.     def remove_callbacks(self):
  62.         self.settings.clear_on_change("max_display_length")
  63.         self.settings.clear_on_change("monospace_font")
  64.         self.settings.clear_on_change("ellipsis_symbols")
  65.         self.settings.clear_on_change("prefix_custom")
  66.         self.settings.clear_on_change("command_names")
  67.         # ...
  68.  
  69.     # Methods for the user configurable settings.
  70.  
  71.     def set_max_display_length(self):
  72.         DEFAULT = 70
  73.         MIN_LENGTH = 50
  74.         value = self.integer_setting("max_display_length", DEFAULT, MIN_LENGTH)
  75.         self.max_display_length = value
  76.  
  77.     def set_monospace_font(self):
  78.         DEFAULT = True
  79.         value = self.boolean_setting("monospace_font", DEFAULT)
  80.         self.monospace_font = value
  81.  
  82.     def set_ellipsis_symbols(self):
  83.         DEFAULT = "…"
  84.         value = self.string_setting("ellipsis_symbols", DEFAULT)
  85.         self.ellipsis_symbols = value
  86.  
  87.     def set_prefix_custom(self):
  88.         DEFAULT = ""
  89.         # This setting may be set to a string or a list of strings;
  90.         # if it is a list then ensure all list elements are strings.
  91.         value = self.list_or_string_setting("prefix_custom", DEFAULT)
  92.         if isinstance(value, list):
  93.             for index, item in enumerate(value):
  94.                 if not isinstance(item, str):
  95.                     value[index] = str(item)
  96.         self.prefix_custom = value
  97.  
  98.     def set_command_names(self):
  99.         DEFAULT = []
  100.         value = self.list_setting("command_names", DEFAULT)
  101.         self.command_names = value
  102.  
  103.     # Methods for settings retrieval; all will return a
  104.     # setting of the required type or the default value.
  105.  
  106.     def string_setting(self, setting, default):
  107.         return self.setting_of_type(setting, default, str)
  108.  
  109.     def list_setting(self, setting, default):
  110.         return self.setting_of_type(setting, default, list)
  111.  
  112.     def boolean_setting(self, setting, default):
  113.         return self.setting_of_type(setting, default, bool)
  114.  
  115.     def list_or_string_setting(self, setting, default):
  116.         return self.setting_of_type(setting, default, (str, list))
  117.  
  118.     def setting_of_type(self, setting, default, required_type):
  119.         value = self.settings.get(setting, None)
  120.         return value if isinstance(value, required_type) else default
  121.  
  122.     # Special case.
  123.  
  124.     def integer_setting(self, setting, default, min_value):
  125.         value = self.settings.get(setting, None)
  126.         return value if self.is_integer(value) and value >= min_value else default
  127.  
  128.     def is_integer(self, value):
  129.         # Bool is a subclass of int; isinstance(False, int) == True.
  130.         return isinstance(value, int) and not isinstance(value, bool)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top