LoginSignup
0
0

More than 5 years have passed since last update.

変数のシンボルを追加するプラグイン (Sublime Text)解説

Last updated at Posted at 2015-07-29

はじめに

PHPで変数を書いたりコピペした後に$を書くのって、面倒ですよね。これをコマンド一発で実行するプラグインを書きましたのでざっくり紹介します。

なお、以前サブライムのスニペットを使い倒すという記事も書いています。こちらではプラグインでスニペットを呼ぶ方法なども書かれているので、もし興味があれば見て下さい。

この記事を読むと、プラグインに関して大体以下の事が分かります。

  • キーバインディングでシンボルに変数を渡す方法
  • カーソル位置の単語の位置と単語の文字列を取得する方法
  • 上記単語の頭とお尻に任意の文字列を入力する方法

なお、PHP以外にも応用できるので、好きなように変更してお使い下さい。

プログラム解説

まずは全文を書いてしまいます。

import sublime, sublime_plugin
import re

class AddVariableSymbol(sublime_plugin.TextCommand):
    regs = {'PHP':'[a-zA-Z0-9_]+'}
    symbols = {'PHP':'$'}

    def run(self, edit, **args):
        #get current word
        current_word_region = self.view.word(self.view.sel()[0].begin())
        current_word = self.view.substr(current_word_region)

        #add symbol
        if (self.checkIfVariable(current_word,args['language'])):
            self.addSymbol(edit,current_word_region,args['language'])

    #chck if the string is variable
    def checkIfVariable(self,string,lang):
        reg = self.regs[lang]
        if (re.match(reg,string)):
            return True
        else:
            return False

    #add symbol to the top of word
    def addSymbol(self,edit,current_word_region,lang):
        symbol = self.symbols[lang]
        add_point = current_word_region.begin()
        self.view.insert(edit, add_point, symbol)

このプラグインを呼ぶキーバインディングは次のようになっています。


    { "keys": ["alt+4"], "command": "add_variable_symbol", "args": {"language": "PHP"}},

argsでプラグインに値を渡す事が出来ます。

以下に細かく解説していきますが、SublimeのAPI Referenceを参照すると分かりやすいかもしれません。

メイン関数

メイン関数は次のように定義します。


    def run(self, edit, **args):

self,editはお約束なので必ず定義して下さい。**argsにプラグインに渡された値が格納されています。

単語の取得

単語の取得は次の2行で行っています。self.view.sel()[0].begin()で現在のカーソル位置を取得して、self.view.word()でその単語のRegionを取得しています。これをself.view.substr()に入力する事で選択中の単語をcurrent_wordに入力しています。


        #get current word
        current_word_region = self.view.word(self.view.sel()[0].begin())
        current_word = self.view.substr(current_word_region)

シンボルの追加

次にシンボルの追加です。まず念のため、checkIfVariable()で選択ワードが変数かチェックしています。


        if (self.checkIfVariable(current_word,args['language'])):
            self.addSymbol(edit,current_word_region,args['language'])

選択ワードが変数かのチェックです。正規表現を使っています。regが不定の場合のエラー処理はさぼってます。すいません。


    def checkIfVariable(self,string,lang):
        reg = self.regs[lang]
        if (re.match(reg,string)):
            return True
        else:
            return False

次にシンボル追加を行います。


    def addSymbol(self,edit,current_word_region,lang):
        symbol = self.symbols[lang]
        add_point = current_word_region.begin()
        self.view.insert(edit, add_point, symbol)

ここで、第三引数に渡しているのが、current_wordではなくcurrent_word_regionなのがポイントです。これはRegion.begin()関数を使って、シンボルを追加する位置を取得する為です。位置を取得したらself.view.insert()でシンボル追加です。なお、Region.end()を使えば単語の後に好きな文字列を追加できるはずです。

まとめ

ざっくりの説明でしたが、いかがでしょうか?Sublimeのプラグインは、初めはよく分からなかったのですが、プログラムの例を色々と見てなんとなく分かってきました。この記事もそういった例になれると嬉しいです。この記事を書いていて気付いたのですが、現在書いているスクリプトがPHPかどうかって、自動的に判断できそうですね、、、。時間が出来たら試してみます。

0
0
1

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
0
0