Home Download Buy Blog Forum Support

How is this plugin triggered?

How is this plugin triggered?

Postby pete on Sat Feb 02, 2013 11:50 pm

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?


https://github.com/LewisW/SublimeAutoSe ... miColon.py

Code: Select all
import sublime_plugin
import sublime

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

        # Insert the semi colon
        edit_init = self.view.begin_edit('insert')
            # 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, ';')

        # Create a new edit point
        edit_last = self.view.begin_edit('auto_semi_colon')
            # 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().add(sublime.Region(last, last))
Posts: 16
Joined: Wed Jan 09, 2013 4:24 am

Re: How is this plugin triggered?

Postby facelessuser on Sun Feb 03, 2013 12:24 am

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.
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: How is this plugin triggered?

Postby pete on Sun Feb 03, 2013 1:05 am

Ahh, Cool!, Thanks for that.
Posts: 16
Joined: Wed Jan 09, 2013 4:24 am

Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests