Custom Sublime Text 3 plugin

Source: Internet
Author: User
The last time I wrote about the sublime text 3 custom plug-in steps, can only be said to play a role, does not have much actual functionality, this time I will combine the needs of recent work, introduced how to put PHP code format tool PHP CS Fixer integration Sublimetext 3 To format the PHP code as your plugin

First we need to install PHP cs fixer, download Php-cs-fixer.phar

wget Http://get.sensiolabs.org/php-cs-fixer.phar-O Php-cs-fixersudo MV Php-cs-fixer.phar/usr/local/bin/phpcssudo chmod +x/usr/local/bin/phpcs

Next, verify that the installation is successful:

Phpcs

Show:

Usage: Help  [Options] [--] []

Indicates that the anything OK

Say the goal of this plugin:

    1. Automatically format php files when saving files

    2. Combining keys to format PHP files

First you need a sublime command,

Class Mikecommand (Sublime_plugin. Textcommand):    def run (self, edit):

In the Run method, call the PHPCS command to format the PHP file

Class Mikecommand (Sublime_plugin. Textcommand):    def run (self, edit):        view = Self.view;        FileName = View.file_name ();        suffix =  os.path.splitext (filename) [1][1:]        if suffix = = ' php ':            fix (filename) def fix (phpfile):    if not Os.path.exists (phpfile):        return;    Command = ' phpcs fix ' + phpfile;    Os.system (command);

Because the key combinations are configured in the Keymap file:

[    {        "keys": [            "Ctrl+alt+k"        ],        "command": "Mike"    }]

So the code is automatically formatted when I use Ctrl+alt+k

So how to automatically save is the command to execute the response? The sublime Text 3 API provides EventListener, so we define class inheritance from Sublime_plugin. EventListener, and then listen for the event of the response:

Class Autoalign (Sublime_plugin. EventListener):    def on_post_save (self, view):        FileName = View.file_name ();        suffix =  os.path.splitext (filename) [1][1:]        if suffix = = ' php ':            fix (filename)

For more event types, please refer to the EventListener

By now, the first two goals have been achieved, we can look at the effect:

No code-style PHP files:

$value) {        $ids = explode (', ', $value);    Asort ($ids);        $fullPermutation [$key] = MD5 (implode (', ', $ids));    }    return $fullPermutation;}

Files after ctrl+alt+k or ctrl+s:

$value) {        $ids = explode (', ', $value);        Asort ($ids);        $fullPermutation [$key] = MD5 (implode (', ', $ids));    }    return $fullPermutation;}

The Php-cs default is to follow the PSR-2 encoding specification, but you can also set the code style by specifying the parameters

But there is another problem here, all the execution is in the main thread, then the whole will be very card, we need to extract into the extra threads in this calculation.

Class Handlerthread (threading. Thread):    def __init__ (self, view):        Self.view = View        Threading. Thread.__init__ (self)    def run (self):        fileName = Self.view.file_name ();        suffix =  os.path.splitext (filename) [1][1:]        if suffix = = ' php ':            fix (filename)

Then change the way the listener Ctrl+s is implemented:

Class Autoalign (Sublime_plugin. EventListener):    def on_post_save (self, view):        thread = handlerthread (view)        Thread.Start ()

Because the API for sublime text 3 is written in Pyhton 3, our implementation is using Python 3. Use threading. Thread, Os.path.splitext, Os.system Remember to bring in the appropriate package.

Import Osimport Os.pathimport Threading

At this point all the work has been completed, but also happy to write PHP!

Most of the time, language is just a tool, important or idea, thinking. We know that we can create a lot of novelty, but we don't know where it comes from. Only by accumulating learning and broadening your horizons will you be able to see and think more. To create the value that belongs to US ~

Related articles:

Sublime Text 3 plugin most commonly used by 7 developers

The sample code for the 10 sublime text plug-ins that are required by the JavaScript developer in detail

Sublime TEXT3 shortcut Key Practical summary

  • 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: info-contact@alibabacloud.com 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.