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:
First need a sublime command,
class MikeCommand(sublime_plugin.TextCommand):
def run(self, edit):
Inrun Method, callphpcs
Command 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+s
After 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.system
Remember 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~
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...
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 ...
My settings My plugin...
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...
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...