Created
November 19, 2020 19:00
-
-
Save OdatNurd/ef7a9867b36d926f233cda0ced72f7a8 to your computer and use it in GitHub Desktop.
Defining a custom settings key context for Sublime Text that can compare any setting and not just booleans
This file contains 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
import sublime | |
import sublime_plugin | |
class CustomContextEventListener(sublime_plugin.EventListener): | |
""" | |
This event is raised when Sublime sees a key binding context key it does not | |
recognize. | |
""" | |
def on_query_context(self, view, key, operator, operand, match_all): | |
# Determine if we understand the key. If we don't we should return None | |
# to allow Subliem to ask another listener. | |
if not key.startswith('view_setting.'): | |
return None | |
# Obtain the actual setting by removing the common key prefix, and then | |
# get the value of the setting for us in our comparison. | |
setting = key[len('view_setting.'):] | |
lhs = view.settings().get(setting) | |
# The operator tells us what sort of comparison to do and the operand | |
# is the information that comes from the key binding. | |
# | |
# The full list of options is: | |
# OP_EQUAL, OP_NOT_EQUAL, OP_REGEX_MATCH, OP_NOT_REGEX_MATCH, | |
# OP_REGEX_CONTAINS, OP_NOT_REGEX_CONTAINS | |
if operator == sublime.OP_EQUAL: | |
return lhs == operand | |
elif operator == sublime.OP_NOT_EQUAL: | |
return lhs != operand | |
# We understand the key, but someone used an operator that we don't | |
# understand, so default to saying that the binding doesn't apply. | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A demonstration of how this works is available in my YouTube video on defining custom contexts with on_query_context in Sublime, which is part of my Plugin 101 playlist.