自定義sublime text 3插件 --- php文件格式化

上次寫了關(guān)于sublime text 3自定義插件的步驟, 只能說起到一個拋磚引玉的作用, 并不具備多少實際功能, 這次我將結(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

接下來驗證一下是否安裝成功:

phpcs

顯示:

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

則表明anything OK

說一下這個插件的目標:

  1. 保存文件時自動格式化php文件
  2. 組合鍵格式化php文件

首先需要一個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);

因為在keymap文件中配置過組合鍵:

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

所以當(dāng)我 使用 ctrl+alt+k 時代碼便自動格式化

那么如何在自動保存是去執(zhí)行響應(yīng)的命令呢? sublime text 3 api 提供了EventListener, 因此我們定義class 繼承自sublime_plugin.EventListener, 然后監(jiān)聽響應(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)于更多事件類型, 請參考EventListener

到現(xiàn)在, 一開始的兩個目標都已經(jīng)實現(xiàn)了, 我們可以看一下效果:

沒有代碼風(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+kctrl+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 默認是遵循 PSR-2 的編碼規(guī)范的, 但是也可以通過指定參數(shù)設(shè)置代碼風(fēng)格

但是這里存在另外一個問題, 所有的執(zhí)行都是在主線程里面, 那么整體就會很卡, 我們需要在這種計算提取到額外的線程里面.

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)聽 ctrl+s 的實現(xiàn)方式:

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

因為sublime text 3的api是用pyhton 3寫的, 所以我們的實現(xiàn)都是使用的python 3.
在使用 threading.Thread, os.path.splitext, os.system時要記得引入相應(yīng)的package.

import os
import os.path
import threading

至此所有的工作皆已完成, 又可以愉快的寫php了!

很多時候語言只是一種工具, 重要的還是想法,思路.
我們知道我們可以創(chuàng)造很多新奇的東西, 但是我們卻不知道idea從何而來.
只有不斷的積累學(xué)習(xí), 眼界開闊之后, 你才會看到和想到更多.才能創(chuàng)造屬于我們的價值~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容