Sublime Text 3 API:从文件中获取所有文本 [英] Sublime Text 3 API : Get all text from a file

查看:375
本文介绍了Sublime Text 3 API:从文件中获取所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试为Sublime Text 3创建一个插件.
目前,我只能选择一个窗口中的所有文本,然后将其复制到另一个窗口中.
代码:


I am trying to create a plugin for sublime text 3.
For now I only get possible to select all text in a window and copy it in another window.
Code :

import sublime, sublime_plugin

class PrintCodeCommand(sublime_plugin.WindowCommand):
    def run(self):
        # for each caracter, add it to a string with the substr method
        s = ""
        for x in range(0,self.window.active_view().size()):
            s += self.window.active_view().substr(x)
        newFile = self.window.new_file()
        newFile.run_command("test",{"textBuffer": s})

class Test(sublime_plugin.TextCommand):
    def run(self, edit, textBuffer):
        self.view.insert(edit, 0, textBuffer)

您知道这样做的更好/更简单的方法吗?
谢谢!

Do you know a better/simpler method to do so ?
Thanks!

推荐答案

您可以通过以下方式获取当前文档的内容:

You can get the contents of the current doc with:

contents = self.view.substr(sublime.Region(0, self.view.size()))

这篇关于Sublime Text 3 API:从文件中获取所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆