sublime插件

资料

sublime 插件和创建代码片段

https://blog.csdn.net/weixin_43649647/article/details/123439190

Sublime Text进阶(二) API的使用:TextCommand - 简书

https://www.jianshu.com/p/9c0a41adf0f8

手把手教你写Sublime中的Snippet - 简书

https://www.jianshu.com/p/356bd7b2ea8e

前言

开发某种语言的插件,先确定是否有必要,确认是否以及有人开发过

搜索方式:https://github.com/search?q=

修改已有语言的插件

插件的形式有两种,文件夹模式或者sublime-package模式

  • 文件夹模式:

    • C:\Users\xdd\AppData\Roaming\Sublime Text\Packages\ANSYS
    • E:\Sublime Text\Packages\Python
  • sublime-package模式:

    • E:\Sublime Text\Packages\Python.sublime-package

img

添加 print.sublime-snippet

将Python.sublime-package修改为Python.rar --- 解压 --- 打开文件夹Snippets --- 新建print.sublime-snippet --- 内容如下

img

  • 结果

img

修改插件名称与适用的文件类型

img

  • ANSYS.tmLanguage控制:

    • 设置插件在sublime中显示的名字
    • 设置哪些关键字高亮

添加全局代码片段

  • 工具->插件开发->新建代码片段
  • 把下面代码粘贴进去,修改字符串和Tab对应的单词,
  • 保存为C:\Users\xdd\AppData\Roaming\Sublime Text\Packages\User\Author.sublime-snippet
  • 在Sublime文件中输入author按Tab,即可插入指定字符串
<snippet>
	<content><![CDATA[
"""
@Author:		xdd2026@qq.com
@CreateData:	[ sublime: Ctrl+Shift+alt+d ]
@EditData:		***

@filename:		***
@Purpose:
	****
@InputParameter
	a:***
"""


]]></content>
	<tabTrigger>Author</tabTrigger>
</snippet>

菜单栏开发

需要两个文件即可:

img

Xdd.py文件内容

import sublime
import sublime_plugin
import datetime
import os

class insert_time(sublime_plugin.TextCommand):
	def run(self, edit):
		view = self.view
		selection = view.sel()
		nowStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		self.view.insert(edit, selection[0].begin(), nowStr)
		print(nowStr)

class insert_author(sublime_plugin.TextCommand):
	def run(self, edit):
		view = self.view
		selection = view.sel()
		nowStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		try:
			Filename = os.path.basename(view.file_name())
		except:
			Filename = ''

		author_info = '# -*- coding: utf-8 -*-\n# @Author    : xdd2026@qq.com\n# @CreateData: {}\n# @Filename  : {}\n# @Purpose   : \n\n'.format(nowStr,Filename)
		self.view.insert(edit, selection[0].begin(), author_info)

	

Main.sublime-menu 文件内容

[
    {   
        "id":"date",
        "caption": "date",
        "command": "insert_time"
    },

    {
        "caption": "其他(XDD)",
        "id": "XDD",
        "children":
        [
            { "command": "insert_author", "caption": "插入作者信息" },
            { "command": "create_py_file", "caption": "新建Python文件" },

        ]
    },

]

(可有可无)快捷键

新建文件Default (Windows).sublime-keymap,文件内容为

[
	{ "keys": ["ctrl+shift+m"], "command": "insert_time" },
	{ "keys": ["ctrl+shift+alt+a"], "command": "insert_author" },
]
posted @ 2022-11-10 18:47  xdd1997  阅读(35)  评论(0编辑  收藏  举报