Created
January 7, 2014 03:30
This file contains hidden or 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 sublime_plugin import EventListener | |
COMMENT_SCOPE = "comment.line.number-sign.nimrod" | |
FAIL_TOLERANCE = 3 | |
DEBUG = False | |
def debug(string): | |
if DEBUG: | |
print(string) | |
class CommentListener(EventListener): | |
def on_modified(self, view): | |
rowcol_set = [view.rowcol(s.a) for s in view.sel() if s.a == s.b] | |
for row, col in rowcol_set: | |
last_line = view.line(view.text_point(row - 1, 0)) | |
debug(view.scope_name(last_line.b)) | |
# Stage 2 | |
command, args, repeats = view.command_history(0, False) | |
if (command == "insert" and args["characters"] == '\n'): | |
debug("Stage 2 success - A") | |
elif (command == "paste"): | |
debug("Stage 2 success - B") | |
else: | |
debug("Stage 2 failure") | |
return | |
command, args, repeats = view.command_history(1, False) | |
if command == "continueComment": | |
return | |
# Stage 3 | |
current_line = view.line(view.text_point(row, col)) | |
if view.substr(current_line).isspace(): | |
debug("Stage 3 success") | |
else: | |
debug("Stage 3 failure") | |
return | |
# Stage 1 | |
if COMMENT_SCOPE in view.scope_name(last_line.b): | |
debug("Stage 1 success") | |
else: | |
debug("Stage 1 failure") | |
return | |
insertion_edit = view.begin_edit("continueComment") | |
view.insert(insertion_edit, view.text_point(row, col), "## ") | |
view.end_edit(insertion_edit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment