These files are an excerpt of the Input Handler guide on https://docs.sublime-text.io/.
/Input Handlers.sublime-commands Secret
Last active
June 24, 2021 21:32
Star
You must be signed in to star a gist
Input Handlers guide code files
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
Show hidden characters
[ | |
{ "caption": "Simple Command", "command": "simple" }, | |
{ "caption": "Insert Html Entity", "command": "insert_html_entity" }, | |
{ "caption": "Multiply", "command": "multiply" }, | |
] |
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
from html.entities import html5 | |
import sublime_plugin | |
class InsertHtmlEntityCommand(sublime_plugin.TextCommand): | |
def run(self, edit, entity): | |
for region in self.view.sel(): | |
self.view.replace(edit, region, "&" + entity) | |
def input(self, args): | |
return EntityInputHandler() | |
class EntityInputHandler(sublime_plugin.ListInputHandler): | |
def list_items(self): | |
return sorted(html5.keys()) | |
def preview(self, value): | |
return "Character: {}".format(html5.get(value)) |
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_plugin | |
class MultiplyCommand(sublime_plugin.TextCommand): | |
def run(self, edit, operand1, operand2): | |
result = float(operand1) * float(operand2) | |
for region in self.view.sel(): | |
self.view.replace(edit, region, str(result)) | |
def input(self, args): | |
names = [name for name in ['operand1', 'operand2'] | |
if name not in args] | |
if names: | |
return MultiNumberInputHandler(names) | |
class MultiNumberInputHandler(sublime_plugin.TextInputHandler): | |
def __init__(self, names): | |
self._name, *self.next_names = names | |
def name(self): | |
return self._name | |
def placeholder(self): | |
return "Number" | |
def next_input(self, args): | |
if self.next_names: | |
return MultiNumberInputHandler(self.next_names) | |
def validate(self, text): | |
try: | |
float(text) | |
except ValueError: | |
return False | |
else: | |
return True |
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_plugin | |
class SimpleCommand(sublime_plugin.TextCommand): | |
def run(self, edit, text): | |
for region in self.view.sel(): | |
self.view.replace(edit, region, text) | |
def input(self, args): | |
return MyTextInputHandler(self.view) | |
class MyTextInputHandler(sublime_plugin.TextInputHandler): | |
def __init__(self, view): | |
self.view = view | |
def name(self): | |
return "text" | |
def placeholder(self): | |
return "Text to insert" | |
def preview(self, text): | |
return ("Selections: {}, Characters: {}" | |
.format(len(self.view.sel()), len(text))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment