Sublime Forum

How is this plugin triggered?

#1

Hi guys!,
I’m just trying to get into plugins for sublime so I’ve been pulling appart other people’s scripts to see how things work and I was wondering if someone could help me out with this one.
This script is triggered when the user enters a semi colon into a line of code, but I cant find how that triggers the code to run. Can anyone point me in the right direction here?

Thanks
Pete

github.com/LewisW/SublimeAutoSe … miColon.py

[code]import sublime_plugin
import sublime

class AutoSemiColonCommand(sublime_plugin.TextCommand):
def run(self, edit):
# End the edit it passes
#del edit
self.view.end_edit(edit)

    # Insert the semi colon
    edit_init = self.view.begin_edit('insert')
    try:
        # Loop through and add the semi colon
        for sel in self.view.sel():
            # The last letter we've dealt with
            first = sel.end()
            self.view.insert(edit_init, first, ';')
    finally:
        self.view.end_edit(edit_init)

    # Create a new edit point
    edit_last = self.view.begin_edit('auto_semi_colon')
    try:
        # Loop through and add move it to the end
        for sel in self.view.sel():
            last = last_bracket = first = sel.end()
            # Find the last bracket
            while (self.view.substr(last) in ' ', ')']):
                print self.view.substr(last) == ')'
                if (self.view.substr(last) == ')'):
                    last_bracket = last + 1
                last += 1 

            if (last_bracket < last):
                last = last_bracket

            # Can we insert the semi colon elsewhere?
            if last > first:
                self.view.erase(edit_last, sublime.Region(first - 1, first))
                # Delete the old semi colon
                self.view.insert(edit_last, last - 1, ';')
                # Move the cursor
                self.view.sel().clear()
                self.view.sel().add(sublime.Region(last, last))
    finally:
        self.view.end_edit(edit_last)[/code]
0 Likes

#2

Via the condition in the keymaps.

Look at the keymap files. You will notice a shortcut is assigned to the “;” key, but it only executes a command when a certain condition is met.

0 Likes

#3

Ahh, Cool!, Thanks for that.

0 Likes