[PYTHON] Plugin to add variable symbols (Sublime Text) Description

Introduction

Writing $ after writing or copying variables in PHP is a hassle, isn't it? I wrote a plugin that executes this with a single command, so I will briefly introduce it.

In addition, I also wrote an article Use up sublime snippets before. It also describes how to call snippets with plugins, so if you are interested, please have a look.

After reading this article, you'll find out about the following about plugins:

--How to pass variables to symbols with key bindings --How to get the position of the word at the cursor position and the character string of the word --How to enter an arbitrary character string at the beginning and end of the above word

In addition, it can be applied to other than PHP, so please change it as you like.

Program explanation

First of all, I will write the full text.

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)

The key bindings that call this plugin are as follows:


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

You can pass values to the plugin with ʻargs`.

I will explain in detail below, but it may be easier to understand if you refer to Sublime's API Reference.

Main function

The main function is defined as follows.


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

Be sure to define self and ʻeditas they are promises. The value passed to the plugin is stored in** args`.

Get the word

The word is acquired in the following two lines. self.view.sel () [0] .begin () gets the current cursor position, and self.view.word () gets the Region for that word. By entering this in self.view.substr (), the selected word is entered in current_word.


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

Add symbol

Next is the addition of symbols. First of all, just in case, checkIfVariable () checks whether the selected word is a variable.


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

Check if the selected word is a variable. I'm using a regular expression. Error handling when reg is indefinite is skipped. Excuse me.


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

Next, add a symbol.


    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)

Here, the point is that what is passed to the third argument is not current_word but current_word_region. This is to get the position to add the symbol using the Region.begin () function. After getting the position, add the symbol with self.view.insert (). You should be able to add your favorite string after the word by using Region.end ().

Summary

It was a rough explanation, but how about it? I wasn't sure about the Sublime plugin at first, but I managed to understand it by looking at various program examples. I hope this article can be an example of that. As I was writing this article, I noticed that it seems that I can automatically determine whether the script I am writing is PHP. I will try it when I have time.

Recommended Posts

Plugin to add variable symbols (Sublime Text) Description
Added achievement function to Sublime Text
Create a plugin that allows you to search Sublime Text 3 tabs in Python
"Inline" Sublime Text plugin callbacks with a generator
Customize Jupyter notebook shortcuts to look like sublime text
Links to do what you want with Sublime Text
Let's add it to the environment variable from the command ~