-
Notifications
You must be signed in to change notification settings - Fork 4
Add SettingsListener. #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thom1729
wants to merge
13
commits into
master
Choose a base branch
from
settings-listener
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d2d4efd
Add SettingsListener.
Thom1729 9aa7da2
Use get_selector.
Thom1729 11b67fd
More tests and fix for GlobalSettingsListener.
Thom1729 0adac86
Merge branch 'master' into settings-listener
Thom1729 3377d2f
Merge branch 'master' into settings-listener
Thom1729 7ab35d0
First draft of docs.
Thom1729 1f7bc82
First draft of docs.
Thom1729 15803a8
Don't manually call __del__
Thom1729 f441ec0
Merge branch 'master' into settings-listener
Thom1729 50a2a01
Add error handling when SETTINGS_NAME is missing.
Thom1729 2571d55
Fix line length.
Thom1729 031f49b
Add projection documentation.
Thom1729 508d685
Move string case to top of projection docs.
Thom1729 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,12 +52,33 @@ def _on_settings_changed(self) -> None: | |
|
|
||
|
|
||
| class GlobalSettingsListener(BaseSettingsListener, sublime_plugin.EventListener): | ||
| """ | ||
| A subclass of :class:`sublime_plugin.EventListener` | ||
| that also listens for changes in a global settings object. | ||
|
|
||
| Subclasses must set the `SETTINGS_NAME` class variable to the name of the global settings. | ||
|
|
||
| Example usage: | ||
|
|
||
| .. code-block:: python | ||
| from sublime_lib import GlobalSettingsListener, on_setting_changed | ||
|
|
||
| class JsCustomConfigurationsListener(GlobalSettingsListener) | ||
| SETTINGS_NAME = 'JS Custom.sublime-settings' | ||
|
|
||
| @on_setting_changed('configurations') | ||
| def configurations_changed(self, new_configuration, old_configuration): | ||
| if self.settings.get('auto_build', False): | ||
| sublime.active_window().run_command('build_js_custom_syntaxes') | ||
| """ | ||
| SETTINGS_NAME = '' | ||
|
|
||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| super().__init__(sublime.load_settings(self.SETTINGS_NAME), *args, **kwargs) | ||
|
|
||
| def _on_settings_changed(self) -> None: | ||
| # Necessary because Sublime keeps EventListener objects alive unnecessarily. | ||
| # See https://github.com/sublimehq/sublime_text/issues/4078. | ||
| if self in sublime_plugin.all_callbacks['on_new']: | ||
| super()._on_settings_changed() | ||
| else: | ||
|
|
@@ -68,6 +89,31 @@ def on_new(self, view: sublime.View) -> None: | |
|
|
||
|
|
||
| class ViewSettingsListener(BaseSettingsListener, sublime_plugin.ViewEventListener): | ||
| """ | ||
| A subclass of :class:`sublime_plugin.ViewEventListener` | ||
| that also listens for changes in the view settings. | ||
|
|
||
| Example: | ||
|
|
||
| .. code-block:: python | ||
| from sublime_lib import ViewSettingsListener, on_setting_changed | ||
|
|
||
| class TabSizeListener(ViewSettingsListener): | ||
| @classmethod | ||
| def is_applicable(cls, settings): | ||
| return settings.get('translate_tabs_to_spaces', False) | ||
|
|
||
| @classmethod | ||
| def applies_to_primary_view_only(cls): | ||
| return True | ||
|
|
||
| @on_setting_changed('tab_size') | ||
| def tab_size_changed(self, new_value, old_value): | ||
| self.view.run_command('resize_existing_tabs', { | ||
| 'new_size': new_value, | ||
| 'old_size': old_value, | ||
| }) | ||
| """ | ||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| view = args[0] | ||
| assert isinstance(view, sublime.View) | ||
|
|
@@ -81,6 +127,21 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: | |
| def on_setting_changed( | ||
| selector: Union[str, Callable[[sublime.Settings], Selected]] | ||
| ) -> Callable[[OnChangeListener], OnChangeListener]: | ||
| """ | ||
| A decorator function to mark a method | ||
| of a :class:`GlobalSettingsListener` or :class:`ViewSettingsListener` | ||
| as a setting change handler. | ||
|
|
||
| The handler is not called every time the `settings` object changes, | ||
| but only when the value derived using the `selector` argument changes. | ||
| If `selector` is callable, then the derived value is ``selector(self)``. | ||
| If `selector` is a :class:`str`, | ||
FichteFoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| then the derived value is ``self.get(selector, None)``. | ||
| Otherwise, the derived value is ``projection(self, selector)``. | ||
|
||
|
|
||
| The handler should accept two arguments: | ||
| the new derived value and the previous derived value. | ||
| """ | ||
| def decorator(function: OnChangeListener) -> OnChangeListener: | ||
| setattr(function, OPTIONS_ATTRIBUTE, OnChangedOptions(get_selector(selector))) | ||
| return function | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.