toggle_user_setting.py
, my custom plugin:
import sublime
import sublime_plugin
import os
import json
class ToggleUserSettingCommand(sublime_plugin.ApplicationCommand):
def run(self, setting):
settings_file = "Preferences.sublime-settings"
settings_path = os.path.join(sublime.packages_path(), "User", settings_file)
# Load current user settings directly from disk
try:
with open(settings_path, "r", encoding="utf-8") as f:
user_settings = json.load(f)
except Exception:
user_settings = {}
# Toggle the setting
new_value = not user_settings.get(setting, False)
user_settings[setting] = new_value
# Write updated settings back to disk
with open(settings_path, "w", encoding="utf-8") as f:
json.dump(user_settings, f, indent=4)
# Let Sublime pick up the change
settings_obj = sublime.load_settings(settings_file)
settings_obj.set(setting, new_value)
sublime.save_settings(settings_file)
print(f"Toggled setting: {setting} = {new_value}")
sublime.status_message(f"{setting} = {new_value}")
Main.sublime-menu
:
[
{
"caption": "custom", "id": "custom", "children":
[
{ "caption": "auto_match_enabled", "command": "toggle_user_setting", "args": { "setting": "auto_match_enabled" } },
{ "caption": "close_windows_when_empty", "command": "toggle_user_setting", "args": { "setting": "close_windows_when_empty" } },
{ "caption": "detect_indentation", "command": "toggle_user_setting", "args": { "setting": "detect_indentation" } },
{ "caption": "save_on_focus_lost", "command": "toggle_user_setting", "args": { "setting": "save_on_focus_lost" } },
{ "caption": "show_line_endings", "command": "toggle_user_setting", "args": { "setting": "show_line_endings" } }
]
}
]
If I put both files in Packages/User
, the plugin and its menu works fine: I can click a menu item and this will adjusts my Preferences.sublime-settings
file.
But if I put them in a custom folder, for example Packages/toggle_setting
, menu items are grayed out and cannot be clicked. Why it that?