Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created August 24, 2020 05:33
Show Gist options
  • Select an option

  • Save OdatNurd/bdcab6b90171c81efa0f4203affacd8e to your computer and use it in GitHub Desktop.

Select an option

Save OdatNurd/bdcab6b90171c81efa0f4203affacd8e to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.ListInputHandler):
def list_items(self):
return [
("First", 0),
("Second", 1),
("Third", 2)
]
class ExampleFiveCommand(sublime_plugin.TextCommand):
def run(self, edit, string):
sublime.message_dialog(str(string))
def input(self, args):
if 'string' not in args:
return StringInputHandler()
def input_description(self):
return 'Sample 5'
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.ListInputHandler):
def list_items(self):
return [
"First",
"Second",
"Third"
]
class ExampleFourCommand(sublime_plugin.TextCommand):
def run(self, edit, string):
sublime.message_dialog(string)
def input(self, args):
if 'string' not in args:
return StringInputHandler()
def input_description(self):
return 'Sample 4'
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.TextInputHandler):
pass
class ExampleOneCommand(sublime_plugin.TextCommand):
def run(self, edit, string):
sublime.message_dialog(string)
def input(self, args):
if 'string' not in args:
return StringInputHandler()
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.TextInputHandler):
def validate(self, text):
return text and not text.isspace()
def confirm(self, text):
self.text = text
def next_input(self, args):
if 'operation' not in args:
return Operation(self.text)
class Operation(sublime_plugin.ListInputHandler):
def __init__(self, string):
self.string = string
def list_items(self):
return [("Upper Case", 0), ("Lower Case", 1)]
def preview(self, value):
return self.string.upper() if value == 0 else self.string.lower()
class ExampleSixCommand(sublime_plugin.TextCommand):
def run(self, edit, string, operation):
sublime.message_dialog(string + ', ' + str(operation))
def input(self, args):
if 'string' not in args:
return StringInputHandler()
def input_description(self):
return 'Sample 6'
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.TextInputHandler):
def __init__(self, arg_name, next_arg=None):
self.arg_name = arg_name
self.next_arg = next_arg
def name(self):
return self.arg_name
def next_input(self, args):
if self.next_arg is not None and self.next_arg not in args:
return StringInputHandler(self.next_arg)
class ExampleThreeCommand(sublime_plugin.TextCommand):
def run(self, edit, string1, string2):
sublime.message_dialog(string1 + ', ' + string2)
def input(self, args):
if 'string1' not in args:
return StringInputHandler('string1', 'string2')
def input_description(self):
return 'Sample 3'
import sublime
import sublime_plugin
import json
class StringInputHandler(sublime_plugin.TextInputHandler):
def __init__(self, last_string):
self.last_string = last_string
def initial_text(self):
return self.last_string or ""
class ExampleTwoCommand(sublime_plugin.TextCommand):
last_string = None
def run(self, edit, string):
sublime.message_dialog(string)
self.last_string = string
def input(self, args):
if 'string' not in args:
return StringInputHandler(self.last_string)
def input_description(self):
return 'Sample 2'
[
{ "caption": "Input Handlers: Sample 1", "command": "example_one" },
{ "caption": "Input Handlers: Sample 2", "command": "example_two" },
{ "caption": "Input Handlers: Sample 3", "command": "example_three" },
{ "caption": "Input Handlers: Sample 4", "command": "example_four" },
{ "caption": "Input Handlers: Sample 5", "command": "example_five" },
{ "caption": "Input Handlers: Sample 6", "command": "example_six" },
]
@OdatNurd

Copy link
Copy Markdown
Author

These plugins and the associated commands file demonstrate 6 different ways that you can use Input Handlers in your Sublime Text plugins to gather input in advanced ways.

To use them, drop these files into your User package, or create a new folder in the Packages folder and place them inside.

How they work is described in more detail in my video on Taking your Input Handlers to the next level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment