Sublime text3 使用phpcs 格式化代码
Sublime text3 使用phpcs.phar格式化代码
-
首先我们需要下载 php cs fixer
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 查看是否安装成功
-
下一步我们需要创建一个插件
我们在run方法中调用phpcs命令格式化代码
import os
import os.path
import sublime
import sublime_plugin
class FormatCommand(sublime_plugin.TextCommand):
def run(self, edit):
file_name = self.view.file_name()
print(file_name)
suffix = os.path.splitext(file_name)[1][1:]
print(suffix)
if suffix == 'php':
fixphp(file_name)
def fixphp(file_name):
if not os.path.exists(file_name):
return
command = 'phpcs fix ' + file_name
print(command)
os.system(command)
print('done')
插件位置:~/Library/Application Support/Sublime Text 3/Packages/User
然后新建一个热键用来出发格式化工功能
sublime text->preference->热键设置 添加如下代码
[
{ "keys": ["ctrl+alt+w"], "command": "toggle_setting", "args": {"setting": "word_wrap"}}, //切换自动换行
{ "keys": ["super+alt+l"], "command": "format"}, //格式化php代码
]
脚本插件就是一个python脚本,可以在sublime里面输入 ctrl + ` 打开console面板,输入 sublime.log_commands(True) 开启log模式. 之后的每一次操作,都会在console里面显示出来