I have this plugin that has two text commands. For some reason, the commands don’t work until I open up the plugin file and then save it. I have the file saved in …/Packages/User, and I don’t have this issue with any other plugins. Is there something in the code that interferes with it loading automatically? Thanks for your help!
import sublime
import sublime_plugin
import re
import ex_location
def cursor_position(view):
return view.sel()[0]
def previous_line(view):
return view.line(cursor_position(view)).a - 1
def next_line(view):
return view.rowcol(view.line(cursor_position(view)).b + 1)[0]
def str_to_left(view):
sel_pt = cursor_position(view).a
left_pt = view.line(sel_pt).a
left_region = sublime.Region(left_pt, sel_pt)
return view.substr(left_region)
def str_to_right(view):
sel_pt = cursor_position(view).a
right_pt = view.line(sel_pt).b
right_region = sublime.Region(sel_pt, right_pt)
return view.substr(right_region)
def current_line(view):
line_region = view.line(cursor_position(view))
return view.substr(line_region)
def current_indent_level(view):
spaces = re.match(r"(\s*)", current_line(view))
return spaces.group(0)
def scan(view, direction = 1):
if re.match(r"^\s*$", str_to_left(view)) and re.match(r"^\s+\S+", str_to_right(view)):
search_str = "^ {0," + str(len(str_to_left(view))) + "}\S+"
else:
search_str = "^" + current_indent_level(view) + "\S"
if direction > 0:
return ex_location.search(view, search_str, next_line(view)) - 1
else:
return ex_location.reverse_search(view, search_str, 0, previous_line(view)) - 1
class NextIndentCommand(sublime_plugin.TextCommand):
def run(self, edit, extend_selection = False):
view = self.view
(current_row, current_col) = view.rowcol(view.sel()[0].begin())
scanned = scan(view, 1)
end_of_new_line = view.rowcol(view.line(view.text_point(scanned, 0)).b)[1]
if current_col > end_of_new_line:
new_col = end_of_new_line
else:
new_col = current_col
target = view.text_point(scanned, new_col)
if extend_selection:
view.run_command('set_mark')
view.sel().clear()
view.sel().add(sublime.Region(target))
view.show(target)
if extend_selection:
view.run_command('select_to_mark')
view.run_command("clear_bookmarks", {"name": "mark"})
class PrevIndentCommand(sublime_plugin.TextCommand):
def run(self, edit, extend_selection = False):
view = self.view
(current_row, current_col) = view.rowcol(view.sel()[0].begin())
scanned = scan(view, -1)
end_of_new_line = view.rowcol(view.line(view.text_point(scanned, 0)).b)[1]
if current_col > end_of_new_line:
new_col = end_of_new_line
else:
new_col = current_col
target = view.text_point(scanned, new_col)
if extend_selection:
view.run_command('set_mark')
view.sel().clear()
view.sel().add(sublime.Region(target))
view.show(target)
if extend_selection:
view.run_command('select_to_mark')
view.run_command("clear_bookmarks", {"name": "mark"})
new_selection = view.sel()[0]
view.sel().clear()
view.sel().add(sublime.Region(new_selection.b, new_selection.a))