Sublime text3 plugin compilation tutorial _ Lesson 1

Source: Internet
Author: User
Tags define definition

Today, we will share with you the process of compiling a sublime text3 plug-in and a practical problem solved by using the plug-in.

 

I. prerequisites for developing plug-ins

  • The sublime plug-in is developed using the Python language. Therefore, you must understand the basic syntax of the Python language. Learn Python language, it is recommended to learn Liao Xuefeng Python tutorial (http://www.liaoxuefeng.com /).
  • Many plug-ins use regular expressions to process text. Try to understand the regular expression syntax. Learn regular expressions, regular expressions recommended 30 minutes getting started tutorial (http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html ).

II, New plug-in

1. Open an initialized plug-in editing file through tools> New plugin.

The initialized plug-in has the following content:

2. Open the packages folder through preferences-> browse packages... and create a sub-folder under the folder. The name of the plug-in you want to develop, for example, definereplace.

 

Return to the initialization editor page of plug-in development, save the file through Ctrl + S, and put it in the created sub-Folder. The file name is like definereplace. py.

3. Press Ctrl + 'or view-> show console to open the sublimetext console.

 

Run the following command in the input box: view. run_command ('example ')

The execution result is as follows: Hello, world! is inserted in the first line! :

4. Change the examplecommand class name to the plug-in name you want, for example, definereplacecommand (the name earlier than command is in lower case). Then you can develop the corresponding functions of the plug-in, of course, the input command also becomes view. run_command ('definereid.

 

5, through the official API documentation to find the interface you need, see the document: http://www.sublimetext.com/docs/3/api_reference.html

IICompile plug-ins

Next I will share with you a plug-in I have written. The function of this plug-in is to replace all define definitions in the Tilde file with actual values.

 

Replace the macro definition in the file with the actual value:

 

The following will share the source code of my plug-in and provide detailed comments. You can refer to the API documentation to understand the content of my code:

Import sublime, sublime_pluginimport re, osclass definereplacecommand (sublime_plugin.textcommand): def run (self, edit): # Find the include file in the document to be replaced, add their file paths to a list with starts = self. view. find_all (''include \ s + "(. +) "', 0) includes_file = [] # The regular expression matches the file name in include. Example: 'include ". /define_cpu.v "pattern = ''include \ s + "(. +) "'For start in starts: # capture the matched string include_str = self. view. substr (sublime. region (start. a, start. b) If re. match (pattern, include_str): match_include = Re. search (pattern, include_str) include_file = match_include.group (1) includes_file.append (include_file) If Len (includes_file) <1: Return ### then open all include files, read the file content and save the define definition in it into a dictionary # Get the text of the current file File Path file_name = self. view. file_name () file_path = OS. path. dirname (file_name) define_value = {}# the regular expression matches the define value defined in The include file, for example, in define_cpu.v, # 'define p_c_addr_1 9 'b0110 _ partition _0 pattern = ''define \ s + (\ s +) \ s 'For Your de_file in your des_file: # obtain the complete file path file_full_path = OS. path. join (file_path, include_file) with open (file_full_path, 'rb') as filereader: content = filereader. read () Try: Conte Nt = content. decode ('utf-8') encoding t: content = content. decode ('gbk') matchs_def_val = Re. findall (pattern, content) print (matchs_def_val) for def_val in matchs_def_val: Define = def_val [0] value = def_val [1] If Len (define. strip ()> 0 and Len (value. strip ()> 0: define_value [define. strip ()] = value. strip () ### replace all define definitions in the open document with actual values # first, find the macro definition pattern = ''(\ W +) referenced in the document) 'finds = self. view. find_all (patt Ern, 0) startpoint = 0 for I in range (LEN (finds): Start = self. view. find (pattern, startpoint) content = self. view. substr (sublime. region (start. a, start. B )). replace (''', '') If define_value.get (content )! = None: # locate the macro definition location and find the actual value from the stored define_value dictionary and replace it. Self. View. Replace (edit, start, define_value [content]) else: startpoint = start. B

3., Bind shortcuts

Create a new ing file in the definereplace folder. The file name is fixed.

These two files correspond to different platforms, Windows and Linux respectively. Of course, the Mac platform is default (OSX). sublime-keymap. The file content is as follows:

[{ "keys": ["ctrl+shift+r"], "command": "definereplace"}]

Keys indicates the corresponding shortcut key, and command indicates the command to be executed.

Thu, Running Effect

Before running:

CTRL + Shift + R after running:

  

 

  

Today's sharing is here, and the next article continues the analysis. If you think it is okay, rememberRecommendationYo.

Welcome to support my public account:

This article is an original work. You are welcome to share it with us. Respect Original, reprinted please indicate from: seven night story http://www.cnblogs.com/qiyeboy/
 

 

Sublime text3 plugin compilation tutorial _ Lesson 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.