上次寫(xiě)了關(guān)于sublime text 3自定義插件的步驟, 只能說(shuō)起到一個(gè)拋磚引玉的作用, 并不具備多少實(shí)際功能, 這次我將結(jié)合最近工作的需要, 介紹如何將php代碼格式工具
php cs fixer 集成sublime
text 3中, 作為你的插件格式化php代碼
首先我們需要安裝 php cs fixer,
下載 php-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
接下來(lái)驗(yàn)證一下是否安裝成功:
phpcs
顯示:
Usage:
help [options] [--] [<command_name>]
則表明anything OK
說(shuō)一下這個(gè)插件的目標(biāo):
- 保存文件時(shí)自動(dòng)格式化php文件
- 組合鍵格式化php文件
首先需要一個(gè)sublime command,
class MikeCommand(sublime_plugin.TextCommand):
def run(self, edit):
在 run 方法中, 調(diào)用 phpcs
命令格式化php文件
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);
因?yàn)樵趉eymap文件中配置過(guò)組合鍵:
[
{
"keys": [
"ctrl+alt+k"
],
"command": "mike"
}
]
所以當(dāng)我 使用 ctrl+alt+k
時(shí)代碼便自動(dòng)格式化
那么如何在自動(dòng)保存是去執(zhí)行響應(yīng)的命令呢? sublime text 3 api 提供了EventListener
, 因此我們定義class 繼承自sublime_plugin.EventListener
, 然后監(jiān)聽(tīng)響應(yīng)的事件即可:
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)
關(guān)于更多事件類型, 請(qǐng)參考EventListener
到現(xiàn)在, 一開(kāi)始的兩個(gè)目標(biāo)都已經(jīng)實(shí)現(xiàn)了, 我們可以看一下效果:
沒(méi)有代碼風(fēng)格的php文件:
<?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
或 ctrl+s
后的文件:
<?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 默認(rèn)是遵循 PSR-2
的編碼規(guī)范的, 但是也可以通過(guò)指定參數(shù)設(shè)置代碼風(fēng)格
但是這里存在另外一個(gè)問(wèn)題, 所有的執(zhí)行都是在主線程里面, 那么整體就會(huì)很卡, 我們需要在這種計(jì)算提取到額外的線程里面.
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)
那么更改監(jiān)聽(tīng) ctrl+s
的實(shí)現(xiàn)方式:
class AutoAlign(sublime_plugin.EventListener):
def on_post_save(self, view):
thread = HandlerThread(view)
thread.start()
因?yàn)閟ublime text 3的api是用pyhton 3寫(xiě)的, 所以我們的實(shí)現(xiàn)都是使用的python 3.
在使用 threading.Thread
, os.path.splitext
, os.system
時(shí)要記得引入相應(yīng)的package.
import os
import os.path
import threading
至此所有的工作皆已完成, 又可以愉快的寫(xiě)php了!
很多時(shí)候語(yǔ)言只是一種工具, 重要的還是想法,思路.
我們知道我們可以創(chuàng)造很多新奇的東西, 但是我們卻不知道idea從何而來(lái).
只有不斷的積累學(xué)習(xí), 眼界開(kāi)闊之后, 你才會(huì)看到和想到更多.才能創(chuàng)造屬于我們的價(jià)值~