Nothing Special   »   [go: up one dir, main page]

Custom sublime text 3 plugin --- php file format

Last time I wrote about the sublime text 3 custom plugin steps, can only be said to play a role in the introduction of jade, does not have much practical function, this time I will combine the needs of recent work, how to put the php code format tool
php cs fixer Integrated sublime
text 3, format the php code as your plugin

First we need to installphp cs fixer,
downloadphp-cs-fixer.phar

wget http://get.sensiolabs.org/php-cs-fixer.phar -O php-cs-fixer
sudo mv php-cs-fixer.phar /usr/local/bin/phpcs
sudo chmod +x /usr/local/bin/phpcs

Next, verify that the installation is successful:

phpcs

display:

Usage:
  help [options] [--] [<command_name>]

That means anything OK

Say the goal of this plugin:

  1. Automatically format php files when saving files
  2. Combination key formatting php file

First need a sublime command,

class MikeCommand(sublime_plugin.TextCommand):
    def run(self, edit):

Inrun Method, callphpcsCommand formatting 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 combination is configured in the keymap file:

[
    {
        "keys": [
            "ctrl+alt+k"
        ],
        "command": "mike"
    }
]

So when I usectrl+alt+k The code is automatically formatted

So how do you auto-save a command to execute a response? Sublime text 3 api providesEventListener, so we define the class to inherit fromsublime_plugin.EventListener, then listen for the response to the event:

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 toEventListener

Up to now, both goals have been achieved in the beginning, we can look at the effect:

No code style php file:

<?php

function getSpecsFullPermutation($spces) {
$fullPermutation = fullPerm($spces);
foreach ($fullPermutation as $key => $value) {
        $ids = explode(',', $value);
    asort($ids);
        $fullPermutation[$key] = md5(implode(',', $ids));
    }
    return $fullPermutation;
}

ctrl+alt+k Orctrl+sAfter the file:

<?php

function getSpecsFullPermutation($spces)
{
    $fullPermutation = fullPerm($spces);
    foreach ($fullPermutation as $key => $value) {
        $ids = explode(',', $value);
        asort($ids);
        $fullPermutation[$key] = md5(implode(',', $ids));
    }

    return $fullPermutation;
}

Php-cs defaults to followPSR-2 Encoding specification, but you can also set the code style by specifying 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 the extra thread 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 listenerctrl+s Implementation:

class AutoAlign(sublime_plugin.EventListener):
    def on_post_save(self, view):
        thread = HandlerThread(view)
        thread.start()

Because the sublime text 3 api is written in pyhton 3, so our implementation is using python 3.
is in usethreading.Thread, os.path.splitext, os.systemRemember to introduce the appropriate package.

import os
import os.path
import threading

At this point, all the work has been completed, and you can write php happily!

Many times language is just a tool, the important thing is the idea, the idea.
We know that we can create a lot of novelty, but we don't know where the idea comes from.
Only after continuous accumulation of learning, after you have broadened your horizons, you will see and think more. In order to create value that belongs to us~

Intelligent Recommendation

Sublime Text 3 plugin settings

First, Package Control Installation (1) Install online Ctrl + ~ Open Command Input Interface Copy the python code below to the command input box (2) Local installation 1. Click on the menu Preferences...

Sublime Text 3 plugin installation

2019 Unicorn Enterprise Heavy Recruitment Python Engineer Standard >>> NodeJS plugin installation Reprinted on: https: //my.oschina.net/u/117966/blog/1511040...

Custom Sublime Text 3 editor

Original address:Custom Sublime Text 3 editor Sublime Text has a beautiful user interface and powerful features, such as code thumbnails, Python plugins, code snippets, etc. You can also customize key...

Sublime Text 3 compile Sass - Sublime Text plugin installed Sass

1, we must first install sass, the installation process: http://www.w3cplus.com/sassguide/install.html 2, sublime text Installation Package Control (already installed Package Control friends can skip ...

Sublime Text custom plugin for the current time

It's strange why the powerful Sublime editor doesn't add shortcuts for the current time, but fortunately Sublime can do whatever you want by customizing the plugin.   1. Create a plugin:   T...

More Recommendation

Install Sublime text 3 plugin on Mac

First, go to sublime official website to download software (1), friendship link:http://www.sublimetext.com/ The software is very small, I use the latest version of text3, you can use the current stabl...

Python development Sublime Text 3 plugin

When using Sublime Text 3 to write Python code, we need to install some plugins, which makes it easier to write programs. Installing Package Control allows you to easily install plugins. installation ...

Sublime Text 3 Chinese plugin installation tutorial

Reference link: Install Chinese plugin in sublime text3 The most complete Sublime Text Chinese and plug-in installation collection in history Use Sublime Text editor, never used, learn. Hanhua is carr...

Sublime Text 3 common plugin summary

Package Control install plugin method: Press Ctrl+Shift+P to bring up the command panel Enter install to bring up the Install Package option and press Enter, then select the plugin to install in the l...

Copyright  DMCA © 2018-2025 - All Rights Reserved - www.programmersought.com  User Notice

Top