DIY Development Sublime plug-ins (1)

Source: Internet
Author: User

The recent increase in the intimacy of sublime, previously just used it to write the web, now found to use it to write C + + is also very convenient, especially its intrusion-free mode is too good, so I simply got a C + + sublime build system, so in sublime can also compile C + +, in addition to not debugging, has been considered a development environment.

However, yesterday, I found that I used to use the visual assist in the sublime can not find a similar plug-in. In particular, ALT + up, ALT + down shortcut keys, in the VC is jump to the class or function definition, is one of my most commonly used shortcut keys. Well, since I can't find it, I'll make one myself.

In fact, it is not difficult to do plug-ins for sublime, Sublime is a script-driven Python, plug-ins are often used in python. On the Internet to read some information, and then look at the Sublime3\package\default directory inside a few py, I probably understand the idea of plug-ins.

First, in the Sublime3 installation directory \data\packages directory under the new directory, the name can be arbitrary, I named Jumptag.

Then, because I want to catch the shortcut keys, there will be. sublime-keymap files; To use Python, you must have a. py file. So create several files in this Jumptag directory: Default (Linux). Sublime-keymap, Default (OSX). Sublime-keymap, Default (Windows). Sublime-keymap and

Of these, three. sublime-keymap files are the same:

[  "Keys": ["Alt+up"], "command": "Jump_tag_prev"},  "keys": ["Alt+down"], "command": "Jump_ Tag_next "}]

In fact, it's JSON format, meaning to call the command Jump_tag_prev,alt+down call Jump_tag_next when you press Alt+up.

So where does the Jump_tag_prev, Jump_tag_next command be implemented? In the. py file, of course:

1 ImportSublime, Sublime_plugin2 ImportRe3 4 5_datas = {"view_id"70A"Recorder": None}6 7 classJumptagrecorder (Sublime_plugin. EventListener):8     defOn_modified_async (self, view):9_datas["view_id"] ();Ten_datas["Recorder"]=None; One  A     defOn_activated_async (self, view): -_datas["view_id"] =0; -_datas["Recorder"]=None; the  -  - defgen_recorder (view): -     if(_datas["Recorder"] isNoneor_datas["view_id"] ! ()): +_datas["view_id"] () -_datas["Recorder"]= View.find_all (r"(^(?! [\t]*?\b (if|while|for|try) \b) [\w \t*&]*?\ ([^ ()]*?\] (? =[\w\s]*?\{)) | (\bclass\b|\bstruct\b) [\w \t:,<>]+? (? =[\s]*?\{)") +  A     return_datas["Recorder"] at  -  - classJumptagprevcommand (Sublime_plugin. Textcommand): #注意看类的名字, corresponds to the Jump_tag_prev_command command -     defRun (self, edit): -Reg_list =Gen_recorder (Self.view) -  in         ifReg_list isNone: -             return to  +          forRegioninchSelf.view.sel (): -PT = Region the              Break *  $Closest_num = 999999999Panax NotoginsengClosest_region =Sublime. Region (0,0) -  the          forRegioninchreg_list: +             ifRegion.end () <Pt.begin (): An = pt.begin ()-region.end () the                 ifN <Closest_num: +Closest_num =N -Closest_region = Region $  $         ifclosest_region.empty (): -             return -  the Self.view.sel (). Clear () - Self.view.sel (). Add (closest_region)Wuyi  the (Closest_region.begin ()) -  Wu classJumptagnextcommand (Sublime_plugin. Textcommand): #这个类的名字对应了 jump_tag_next_command Command -     defRun (self, edit): About  $Reg_list =Gen_recorder (Self.view) -  -         ifReg_list isNone: -             return A  +          forRegioninchSelf.view.sel (): thePT = Region -              Break $  theClosest_num = 999999999 theClosest_region =Sublime. Region (0,0) the  the          forRegioninchreg_list: -             ifRegion.begin () >pt.end (): inn = region.begin ()-pt.end () the                 ifN <Closest_num: theClosest_num =N AboutClosest_region = Region the  the         ifclosest_region.empty (): the             return +  - Self.view.sel (). Clear () the Self.view.sel (). Add (closest_region)Bayi (Closest_region.begin ())

A little bit of this code: because we want to jump between function definition and class definition, we have to find out the location of these definitions first, the function Gen_recorder is the job, I admit that the name of the letter is not a good drop. It calls a api:find_all of sublime, finds all the function definitions and class definitions by regular expressions, and I don't know if the regular expression is optimal, but the functionality is implemented.

Now that we have a function for defining the location, then when do we need to find the location of these definitions? You should know that full-text lookup is a relatively time-consuming operation and should not be called every time you press a shortcut key. We can save the defined position and look it up again when the document changes. Therefore, it is necessary to listen to the document modification events. It's also easy to define a listener:

classJumptagrecorder (Sublime_plugin. EventListener):#Note that the brackets must be sublime_plugin. EventListener    defOn_modified_async (self, view):#Note the name of the function_datas["view_id"] (); _datas["Recorder"]=None; defOn_activated_async (self, view): _datas["view_id"] =0; _datas["Recorder"]= None;

The name of the class above is arbitrary, but note that the parentheses must be sublime_plugin. EventListener, the method of the class must follow the API documentation

Finally, is the implementation of Jump_tag_prev, Jump_tag_next command, the code is very simple, I believe that no one can not understand. Note that the command corresponds to the class, can not be arbitrarily named, you must change the command name to "Hump", but also command, and must have a run method.

Simple, just realize a small plug-in! Yes, let me influence the more deep is sublime is hot load plug-in code, that is, once the plugin code is modified, sublime immediately reload, this is really convenient.

DIY Development Sublime plug-ins (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.