Created
November 23, 2024 03:38
-
-
Save OdatNurd/009565250165d2244705c311d1c9551d to your computer and use it in GitHub Desktop.
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
[ | |
// NOTE: The filename in the example is "Default.sublime-keymap, but you | |
// should put this in your own user and platform specific key bindings, | |
// such as "Default (Linux).sublime-keymap" or such. | |
// | |
// That would be the file that opens when you use the | |
// "Preferences > Key Bindings" menu item. | |
// For more details on how this binding works, see: https://youtu.be/1CkAkpNs28M | |
// Here the binding is using the standard build key; this is different on MacOS, | |
// where it's "super+b" instead. If you were to use a custom key, then you probably | |
// also don't need the context lines either. | |
{ "keys": ["ctrl+b"], | |
"command": "build", | |
"args": { | |
"build_system": "Packages/User/CustomBuild.sublime-build", | |
"variant": "" | |
}, | |
"context": [ | |
// Choose one of the following options, depending on how you would like | |
// to match the file. | |
{ "key": "current_filename", "operator": "equal", "operand": "/home/odatnurd/sample.txt" }, | |
// { "key": "current_filename", "operator": "not_equal", "operand": "/home/odatnurd/sample.txt" }, | |
// { "key": "current_filename", "operator": "regex_match", "operand": "^.*sample\\.txt" }, | |
// { "key": "current_filename", "operator": "not_regex_match", "operand": "^.*sample\\.txt" }, | |
// { "key": "current_filename", "operator": "regex_contains", "operand": "sample\\.txt" }, | |
// { "key": "current_filename", "operator": "not_regex_contains", "operand": "sample\\.txt" }, | |
], | |
}, | |
] |
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 | |
import re | |
# Coded in response to a question on the video: | |
# https://youtu.be/ZZt7LUlVTss?si=IinLWWNMhbksDH1K | |
# Build up a list of the possible comparison operators that can be used with | |
# the key binding context to compare the filename of the currently active tab | |
# with the operand value provided in the key binding context. | |
# | |
# The comparison operators compare the (fully qualified) filename directly with | |
# the operand, while the regex options check to see if the regex in the | |
# operands does or does not match the filename. | |
# | |
# When using the MATCH items, the regex is only considered to match if it | |
# matches at the start of the filename, while for the CONTAINS items the regex | |
# can match anywhere within. | |
comparisons = { | |
sublime.OP_EQUAL: | |
lambda filename,operand: filename == operand, | |
sublime.OP_NOT_EQUAL: | |
lambda filename,operand: filename != operand, | |
sublime.OP_REGEX_MATCH: | |
lambda filename,operand: re.match(operand, filename) is not None, | |
sublime.OP_NOT_REGEX_MATCH: | |
lambda filename,operand: re.match(operand, filename) is None, | |
sublime.OP_REGEX_CONTAINS: | |
lambda filename,operand: re.search(operand, filename) is not None, | |
sublime.OP_NOT_REGEX_CONTAINS: | |
lambda filename,operand: re.search(operand, filename) is None, | |
} | |
class CheckFilenameContextEventListener(sublime_plugin.EventListener): | |
""" | |
This event listener listens for the on_query_context event and checks the | |
filename in the currently active file against the information provided in a | |
key context, allowing you to specify a key binding that triggers only in | |
specific files. | |
context name: "current_filename" | |
operators: "equal", "not_equal", "regex_match", "not_regex_match", | |
"regex_contains", "not_regex_contains" | |
When using "equal" and "not_equal" the name of the current file is directly | |
compared to the operand in the context; when using one of the regex options | |
the regular expression must either match or not match the filename. | |
Note that "regex_match" and "not_regex_match" anchor the regex at the start | |
of the input string, while the "contains" variants do not. | |
If the currently active view has no file name (i.e. it has not been saved | |
to disk yet) or an operator is used that is not one of the ones mentioned | |
above, the context will always evaluate to False. | |
""" | |
def on_query_context(self, view, key, operator, operand, match_all): | |
# If this is not a context key that we know how to deal with, then | |
# return None to indicate that Sublime should continue along the | |
# context handler chain until it finds one that applies. | |
if key != "current_filename": | |
return None | |
# If the current file has no filename, we can't match because we don't | |
# know what to match against. | |
if view.file_name() is None: | |
return False | |
# Get the appropriate handler for the provided operator, defaulting to | |
# one that will return False if we don't recognize it, then call it to | |
# get the result, giving it the name of the current file and the | |
# operand from the binding context. | |
handler = comparisons.get(operator, lambda lhs, rhs: False) | |
return handler(view.file_name(), operand) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is in response to a question on my YouTube Video on ways to select a custom build, where @Kyt0z wanted to be able to trigger on a specific filename rather than just all files that have the same name.
One way to do this is via a custom key binding on the
build
command, which is outlined in my YouTube Video on Supercharging Sublime Builds, which allows you to execute any build you want regardless of what is currently selected in the build menu.In order to make this seamless to the user, we can extend that functionality by reusing the standard build key but having it directly trigger a specific build in response to a specific filename, which can be done via the key binding context plugin defined above.