LoginSignup
2
2

More than 5 years have passed since last update.

SublimeText3用、ファイル種別に対応して異なるprintfデバッグをできるようにするプラグイン

Posted at

JSとPHPを並行開発していた際、ファイルの種類によってconsole.logとvar_dumpを自動で切り替えて呼び出せる機能が欲しかったので、作りました。

PHPファイル上で実行すると選択範囲の文字列をvar_dump();でラップします。それ以外であればconsole.log();でラップします。

ConsoleDebugEasilizer.py
import sublime, sublime_plugin

class ConsoleDebugEasilizerCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        scopeName = self.view.scope_name(self.view.sel()[0].begin())
        if ('source.php' in scopeName):
            source = 'php'
        elif ('source.coffee' in scopeName):
            source = 'coffee'
        else:
            source = 'js'

        self.view.run_command('insert_debug', {'arg': source})


class InsertDebug(sublime_plugin.TextCommand):
  def run(self, edit, arg):
    if (arg == 'php'):
        for region in self.view.sel():
            self.view.insert(edit, region.end(), ');')
            self.view.insert(edit, region.begin(), 'var_dump(' )

    elif (arg == 'coffee'):
        for region in self.view.sel():
            self.view.insert(edit, region.end(), ' ')
            self.view.insert(edit, region.begin(), 'console.log ' )

    else:
        for region in self.view.sel():
            self.view.insert(edit, region.end(), ');')
            self.view.insert(edit, region.begin(), 'console.log(' )

PackageControllerからは追加できないので、手動でインストール(パッケージフォルダにpyファイルをコピー)してください。
Key Bindings - User あたりに { "keys": ["ctrl+shift+space"], "command": "console_debug_easilizer"}, などと追加しておけば良いかと思います。
Pythonはこれが2回目です。変なところありましたらご教示いただけると幸いです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2