gpt4 book ai didi

python - SublimeText3插件on_modified api实现

转载 作者:行者123 更新时间:2023-12-01 05:39:38 25 4
gpt4 key购买 nike

我正在尝试为 SublimeText3 开发一些插件。

该插件应该对文本的每次修改都起作用,因此请查看 api

http://www.sublimetext.com/docs/3/api_reference.html

on_modified_async(view) 是我猜想使用的。

所以代码是这样的:

#!/usr/bin/env python

import sublime, sublime_plugin, socket

class testCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World1!")
self.view.insert(edit, 0, "Hello, World2!")
def on_modified_async(self, view):
self.view.insert(edit, 0, "Hello, World3!")

此代码按照 Hello, world 1&2 的预期工作,但 3 从未被触发。

我对 SublimeText 插件和 python3.3 开发都很陌生。我想念什么?谢谢。//编辑

 MESSAGE = self.view.substr(0,50)
TypeError: substr() takes 2 positional arguments but 3 were given

看来我给出了 2 个位置参数,而不是 3 个。

<小时/>

我遇到错误:AttributeError:'MyEventListener'对象没有属性'view'......我的代码有什么问题(已编辑)

import sublime, sublime_plugin, socket

class testCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World1!")

class MyEventListener(sublime_plugin.EventListener):
def on_modified_async(self,view):
self.view.insert(edit, 0, "Hello, World2!")

--

Traceback (most recent call last):
File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 279, in on_modified_async
callback.on_modified_async(v)
File "/Users/ken/Library/Application Support/Sublime Text 3/Packages/test/test.py", line 11, in on_modified_async
self.view.insert(edit, 0, "Hello, World2!")
AttributeError: 'MyEventListener' object has no attribute 'view'
<小时/>

这是工作代码:

import sublime, sublime_plugin, socket

class MarkdownLiveCommand(sublime_plugin.TextCommand):
def run(self, edit):
tcp(self.view)

class MyEventListener1(sublime_plugin.EventListener):
def on_modified_async(self, view):
tcp(view)

def tcp(view):
#do the job

最佳答案

如文档中所述,您需要创建一个扩展 sublime_plugin.EventListener 的类,并在其中定义 on_modified_async 方法 - 在 run 中定义它 TextCommand 的 code> 方法实际上不执行任何操作。像这样的事情:

class MyEventListener(sublime_plugin.EventListener):
def on_modified_async(self, view):
edit = view.begin_edit()
view.insert(edit, 0, "Hello, World2!")
view.end_edit(edit)

关于substr方法的错误,view.substr采用一个sublime.Region实例,而不是两个点:

message = self.view.substr(sublime.Region(0, 50))

错误消息可能有点令人困惑,因为第一个参数是 View 本身;传递两个显式参数会导致使用三个参数调用该方法。

关于python - SublimeText3插件on_modified api实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17943802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com