It feels like I am blind. Maybe one of you can help me out.
I try to create a command which opens the new command pallet and lists all package settings files except existing syntax-settings. But somehow it keeps throwing an exception.
>>> sublime.run_command("my_input")
Traceback (most recent call last):
File "C:\Apps\Sublime3\3156\sublime_plugin.py", line 934, in run_
return self.run()
TypeError: run() missing 1 required positional argument: 'base_file'
It looks like the input()
method of the MyInputCommand
is not executed at all.
Not working piece of code
import re
import os
import sublime
import sublime_plugin
# import Default.settings
PACKAGES = "${packages}/"
# The pattern to extract the path and basename of a file without OS pattern.
SETTINGS_RE = re.compile(
r"(?i)Packages/(.+/)(.+?)(?: \(.+\))?(\.sublime-settings)")
SYNTAX_RE = re.compile(
r"(?i)Packages/(.+/)(.+?)(?: \(.+\))?(\.sublime-syntax)")
class BaseFileInputHandler(sublime_plugin.ListInputHandler):
def name(self):
return "base_file"
def placeholder(self):
return "Settings File"
def preview(self, value):
if value:
return sublime.Html("<b>File:</b> " + value[len(PACKAGES):])
else:
return None
def list_items(self):
syntaxes = set()
for f in sublime.find_resources('*.sublime-syntax'):
_, name, _ = SYNTAX_RE.match(f).groups()
syntaxes.add(name)
items = set()
for f in sublime.find_resources('*.sublime-settings'):
path, name, ext = SETTINGS_RE.match(f).groups()
if 'User' not in path and name not in syntaxes:
items.add(("📑 " + name, "".join((PACKAGES, path, name, ext))))
items = list(items)
items.sort()
return items
class MyInputCommand(sublime_plugin.ApplicationCommand):
def input_description(self):
return "Preferences"
def input(self, args):
if "base_file" not in args:
return BaseFileInputHandler()
else:
return None
def run(self, base_file):
print(base_file)
There is another function to override the syntax specific settings which works fine.
class SyntaxSettingsInputHandler(sublime_plugin.ListInputHandler):
def __init__(self, syntax):
self.syntax = syntax
def name(self):
return "syntax"
def placeholder(self):
return "Syntax Name"
def list_items(self):
items = set()
selected = None
for f in sublime.find_resources('*.sublime-syntax'):
path, name, ext = SYNTAX_RE.match(f).groups()
if name == self.syntax:
caption = "📑 " + name + " (this view)"
else:
caption = "📑 " + name
item = (caption, name)
if name == self.syntax:
selected = item
items.add(item)
items = list(items)
items.sort()
return (items, items.index(selected)) if selected else items
class EditSyntaxSettingsCommand(Default.settings.EditSyntaxSettingsCommand):
def input_description(self):
return "Syntax Settings"
def input(self, args):
if "syntax" not in args:
try:
settings = self.window.active_view().settings()
syntax, _ = os.path.splitext(os.path.basename(settings.get('syntax')))
except:
syntax = None
return SyntaxSettingsInputHandler(syntax)
else:
return None
def run(self, syntax):
self.window.run_command(
'edit_settings',
{
'base_file': '${packages}/Default/Preferences.sublime-settings',
'user_file': os.path.join(sublime.packages_path(), 'User', syntax + '.sublime-settings'),
'default': (
'// These settings override both User and Default settings '
'for the %s syntax\n{\n\t$0\n}\n') % syntax
})
def is_enabled(self):
return True