Created
November 6, 2013 19:45
-
-
Save anonymous/7342901 to your computer and use it in GitHub Desktop.
test
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
''' | |
author: Dealga McArdle, 2013. | |
http://www.sublimetext.com/docs/2/api_reference.html | |
Installation | |
- place iteration_notation.py in Data/Packages/User | |
- place dictionary entry in Keybindings, User | |
{ "keys": ["ctrl+shift+i"], "command": "iteration_notation" } | |
Usage | |
select a sequence of space separated words then press the key combo | |
one two three four -> ctrl+shift+[ -> ['one', 'two', 'three', 'four'] | |
''' | |
import sublime, sublime_plugin | |
def check_is_loopform(istr): | |
# returns non None only if everything seems ok. | |
# does not support tabs, but could. I don't use tabs so meh. | |
# some preprocessing, count spaces to the left, store this | |
restr = istr.lstrip() | |
spaces = len(istr) - len(restr) | |
restr = restr.strip() | |
if (not restr.startswith("for ")): | |
print("must start with keyword for") | |
return | |
elements = restr.split() | |
if (not len(elements) == 4): | |
print("must take form: for i in some_array:") | |
print("got: {} instead".format(restr)) | |
return | |
if (not elements[2] == "in"): | |
print("missing the in keyword") | |
return | |
if (not restr.endswith(":")): | |
print("must end with colon") | |
return | |
return [spaces, elements] | |
def perform_replacement(istr): | |
content = check_is_loopform(istr) | |
if not content: | |
return | |
ostr = """\ | |
{0}for(0 => int {1}; {1}<{2}.cap(); {1}++){{ | |
{0} {2}[{1}];\n{0}}}""" | |
amount_space = " "*content[0] | |
print('{} spaces, do: {}'.format(*content)) | |
a = ostr.format(amount_space, content[1][1], content[1][3][:-1]) | |
print(a) | |
return a | |
def nothing_selected(): | |
sublime.status_message('nothing selected, fool') | |
class Iternotate(sublime_plugin.TextCommand): | |
def run(self, edit): | |
sublime.status_message('waaay') | |
if not self.enabled(): | |
nothing_selected() | |
return | |
view = self.view | |
sel = view.sel()[0] | |
# just selected region | |
selection = view.substr(sel) | |
final = perform_replacement(selection) | |
if not final: | |
self.view.insert(edit, 0, "Hello, World!") | |
return | |
edit = view.begin_edit() # stick onto undo stack | |
view.replace(edit, sel, final) | |
# finalize this edit, use as one undo level | |
view.end_edit(edit) | |
def enabled(self): | |
'''only allow 1 selection for version 0.1''' | |
sels = self.view.sel() # lists regions, | |
nsels = len(sels) # dir(sels[0]) for methods | |
fsel = sels[0] # first selection | |
if nsels == 1 and not fsel.empty(): | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment