Sublime文档,示例不工作,一个python错误然后另一个

时间:2013-10-15 19:48:46

标签: python sublimetext2

查看sublime插件示例的文档(我用Google搜索的东西),我得到了。 a link from the sublime site

首先,我得到了行模块import sublime, sublimepluginclass Rot13Command(sublimeplugin.TextCommand):的无模块错误“ImportError:没有名为sublimeplugin的模块” 虽然运行view.run_command('rot13')仍然有效,尽管有错误(或者之前做过但现在没有)。

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

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

但是当我在控制台中输入此命令时 - 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)  

2 个答案:

答案 0 :(得分:3)

文档似乎符合原始Sublime Text API,而不是Sublime Text 2 API 打印给run的参数,很明显,viewargs都没有通过。相反,它收到一个孤独的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

答案 1 :(得分:0)

对于在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)