Rant: In Sublime, Window Commands != Application Commands
Consider this diff:
--- Unsaved view (172)
+++ /Users/drew/.local/share/chezmoi/dot_config/sublime-text/Packages/User/dwm_tab.py
@@ -182,5 +182,5 @@
variables = collect_env_variables()
variables.update(collect_sublime_variables(self.window))
dirs = [sublime.expand_variables(dn, variables) for dn in dirs]
- sublime.run_command("open_dir", {"dir": dirs[0]}, )
+ self.window.run_command("open_dir", {"dir": dirs[0]}, )
The red text doesn't work, but the green text does.
proceeds to throw computer out of window
The Sublime Text API Reference only ever refers to 'commands', but in the actual
API there are classes such as ApplicationCommand
, WindowCommand
,
TextCommand
, etc. that all subclass a common Command
. The names of these
commands are (bizarrely) derived from mutating the class name, so
MyCoolApplicationCommand
gets turned into my_cool_application
when referencing from other places. There are a few theories here:
open_dir
is not a validApplicationCommand
, andsublime.run_command
only runs application commands. So callingsublime.run_command ("open_dir", ...)
is invalid becauseopen_dir
isn't actually a 'real' command, it can't find theOpenDirCommand
class you're referring to. Essentially a namespace error.There are extra special arguments that get passed to the command when running it from a window that you don't see. This causes the first invocation to fail while the second succeeds.
Either way, the original code fails silently, not even giving you console
output or any return value indicating that something is wrong. Also, enabling
command logging with sublime.log_commands()
shows that the invocation for
both lines is exactly the same!
I had to discover this by randomly trying things I saw in example files. That sucks!
EDIT: After re-reading the API reference, it
does actually say that sublime.run_command
only runs ApplicationCommand
s.
So (1) was correct. However, I can't find anywhere a list of valid application
commands, only this out-of-date page that doesn't list open_dir
.
Cool!