1

查看 sublime 插件示例的文档(我用谷歌搜索的东西)我得到了 . 来自崇高网站的链接

首先,我收到了无模块错误“ImportError:没有名为 sublimeplugin 的模块”, import sublime, sublimeplugin尽管出现了错误,class Rot13Command(sublimeplugin.TextCommand): 但运行view.run_command('rot13')仍然有效(或者尽管现在没有,但仍然可以运行)。

然后我添加了一个 _ 因为我在他们的论坛上读到(不是特别活跃),它现在应该有一个下划线链接

然后,这摆脱了“无模块......”错误

但是当我在控制台中输入此命令时 -view.run_command('rot13') 我收到此错误"TypeError: run() takes exactly 3 arguments (2 given)"

下面是我刚刚从该链接获取的代码,但添加了下划线,我该如何解决该错误?

http://www.sublimetext.com/docs/plugin-examples
CODE: SELECT ALL
import sublime, sublime_plugin  

class Rot13Command(sublime_plugin.TextCommand):  
    def run(self, view, args):  
        for region in view.sel():  
            if not region.empty():  
                # Get the selected text  
                s = view.substr(region)  
                # Transform it via rot13  
                s = s.encode('rot13')  
                # Replace the selection with transformed text  
                view.replace(region, s)  
4

2 回答 2

3

该文档似乎符合原始的Sublime Text API,而不是Sublime Text 2 API
打印给定的参数,run很明显既没有view也没有args通过。相反,它收到一个单独的Edit对象。

import sublime, sublime_plugin

class RotCommand(sublime_plugin.TextCommand):
    def run(self, *args):
        for arg in args:
            print type(arg)

#later, in the console:
>>> view.run_command('rot')
<class 'sublime.Edit'>

幸运的是,您仍然可以访问该view对象。它是 的成员self。在进行更改时,将edit参数添加到view.replace.

class RotCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                # Get the selected text
                s = self.view.substr(region)
                # Transform it via rot13
                s = s.encode('rot13')
                # Replace the selection with transformed text
                self.view.replace(edit, region, s)

立即运行view.run_command('rot')会翻译您选择的文本。hello I am some sample text变成uryyb V nz fbzr fnzcyr grkg.

于 2013-10-15T19:49:35.710 回答
1

对于任何在 2019 年查看此内容的人:

import sublime
import sublime_plugin
import codecs

class Rot13Command(sublime_plugin.TextCommand):
  """docstring for Rot13Command"""
  def run(self, edit):
    for region in self.view.sel():
      if not region.empty():
        # Get the selected text
        s = self.view.substr(region)
        # Transform it via rot13
        s = codecs.encode(s, "rot-13")
        # Replace the selection with transformed text
        self.view.replace(edit, region, s)
于 2019-01-06T09:39:38.947 回答