Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created November 19, 2020 19:00
Show Gist options
  • Save OdatNurd/ef7a9867b36d926f233cda0ced72f7a8 to your computer and use it in GitHub Desktop.
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
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
@OdatNurd
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment