Plugins ST3插件将所选内容附加到新文件的末尾

Plugins ST3插件将所选内容附加到新文件的末尾,plugins,sublimetext3,Plugins,Sublimetext3,我一直在尝试一些Sublime Text 3插件,我正在制作一个插件,将视图选择添加到新文件的末尾 import sublime import sublime_plugin class AddToCommand(sublime_plugin.TextCommand): def run(self, edit): # text = sublime.Region(0, view.size()) window =

我一直在尝试一些Sublime Text 3插件,我正在制作一个插件,将视图选择添加到新文件的末尾

    import sublime
    import sublime_plugin


    class AddToCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            # text = sublime.Region(0, view.size())
            window = self.view.window()
            view = self.view
            s = ''
            for region in view.sel():
                if not region.empty():
                    s += '\n' + view.substr(region)
            print(s)
            for v in window.views():
                print(v.name())
                if v.name() == 'untitled':
                    f = v
                    break
            else:  # if no break occurs --> no current view called [untitled]
                f = window.new_file()
            # print(self)
            # print(edit)
            # print(f)
            window.focus_view(f)
            f.run_command('insert_in_new_file', view.sel())


    class InsertInNewFileCommand(sublime_plugin.TextCommand):
        def run(self, edit, selection):
            self.count = 0  # not using enumerate as it goes through 2 loops
            for line in selection:
                for char in line:
                    self.insert(edit, self.count, char)
                    self.count += 1
                self.insert(edit, self.count, '\n')
                self.count += 1
到目前为止,它检查(以一种基本的方式)一个已经打开的文件,所以现在我尝试将它附加到这个“无标题”文件的末尾。但是,当我运行此代码时,我得到一个错误:

    f.run_command('insert_in_new_file', view.sel())
        File "C:\Users\ ... \Sublime Text 3\sublime.py", line 838, in run_command
            sublime_api.view_run_command(self.view_id, cmd, args)
    TypeError: Value required

我的InsertInNewFile类或调用它的.run命令似乎有问题,但缺少ST3插件文档意味着我找不到任何类似的问题或解决方案

运行命令要求将命令名作为第一个参数,将json字典(作为从关键字到值的映射)作为第二个参数

让我们从这个开始

f.run_命令('insert_in_new_file',view.sel())
您的第二个参数是
sublime.Selection
object,它既不兼容json,也不是dict。因此我们创建了一个dict:

f.run_命令('insert_in_new_file',{“selection”:view.sel()})
现在它是一个dict(并且键与run方法的形式参数中的键相同),但它与json不兼容,因此会引发错误。我们可以通过将选择转换为元组列表来解决此问题:

f.run_命令('insert_in_new_file',{“selection”:list((s.a,s.b)for s in view.sel()))
现在调用将成功,但您还需要正确处理参数。 根据那里的代码,您需要的是字符串列表,而不是文件位置列表。因此,我们可以再次更改参数,以传递选择的内容,而不是选择本身,并跳过空选择:

f.run_命令('insert_in_new_file',{“selection”:list(view.substr for s in view.sel()if len(s)))
然后用
self.view.insert(edit,self.count,char)
替换
self.insert(edit,self.count,char)
,它应该可以工作