繁体   English   中英

显示评估选择的输出-Sublime Text Python REPL

[英]Display output from evaluating selection - Sublime Text Python REPL

我正在使用Sublime Text 3并正在运行OSX Mavericks。 我正在使用Sublime REPL软件包,并且已经对该软件包的设置进行了调整,以使我拥有“ show_transferred_text”:true

当打开Python REPL窗口时,我有一个不错的选择,可以使用Ctrl +,,s从编辑器向其中发送代码。 但是,除非包含print命令,否则不会显示命令的任何输出。 例如,如果我写以下内容

x = 2.5

type(x)

并使用Ctrl + ,,s将其发送给要评估的对象,那么我的确得到了这些命令的显示,但是却没有得到type(x)的输出的显示,就像我复制/粘贴命令输入Mac终端中的Python解释器。

有什么办法可以在Sublime Text中获得此功能?

[UPDATE]

现在不建议使用以下代码。 有关最新的,更好的工作版本,请访问https://gist.github.com/dantonnoriega/46c40275a93bab74cff6的此插件的要点。

随意拨叉和加星标。 随着代码的发展,我将对要点进行任何更改。 但是,要保持最新状态,请遵循以下回购协议: https : //github.com/dantonnoriega/sublime_text_plugins/blob/master/python_blocks_for_repl.py

***************

我想要相同的功能。 我想到的是以下文章的大杂烩:

https://stackoverflow.com/a/14091739/3987905

是否可以将键绑定命令链接到sublime text 2中?

如何在Sublime Text 2编辑器中将行传递到控制台

创建插件

以下插件要求您在与代码相同的窗口中打开repl,但将其作为单独的组。 我使用上面的第一个链接学习了如何执行此操作。 为了发送您想要的文本然后执行文本,我使用了上面第二个链接中的想法,并编写了以下插件(工具->新插件...)

class ReplViewAndExecute(sublime_plugin.TextCommand):
    def run(self, edit):
        v = self.view
        ex_id = v.scope_name(0).split(" ")[0].split(".", 1)[1]
        lines = v.lines(self.view.sel()[0]) # get the region, ordered
        last_point = v.line(self.view.sel()[0]).b # get last point (must use v.line)
        last_line = v.line(last_point) # get region of last line

        for line in lines:
            self.view.sel().clear() # clear selection
            self.view.sel().add(line) # add the first region/line
            text = v.substr(line)
            print('%s' % text) # prints in console (ctrl+`)

            if not line.empty() and text[0] != '#': # ignore empty lines or comments
                v.window().run_command('focus_group', {"group": 1}) # focus REPL
                v.window().active_view().run_command("insert",
                    {"characters": text})
                v.window().run_command('repl_enter')

        # if the last line was empty, hit return
        # else, move the cursor down. check if empty, hit return once more
        if last_line.empty():
            self.view.sel().clear()
            self.view.sel().add(last_line)
            v.window().run_command('focus_group', {"group": 1}) # focus REPL
            v.window().run_command('repl_enter')
            v.window().run_command('focus_group', {"group": 0})
            v.window().run_command('move', {
                "by": "lines",
                "forward": True,
                "extend": False
            })
        else:
            v.window().run_command('focus_group', {"group": 0})
            v.window().run_command('move', {
                "by": "lines",
                "forward": True,
                "extend": False
            })
            if self.empty_space():
                v.window().run_command('focus_group', {"group": 1}) # focus REPL
                v.window().run_command('repl_enter')

        # move through empty space
        while self.empty_space() and self.eof():
            v.window().run_command('focus_group', {"group": 0})
            v.window().run_command('move', {
                "by": "lines",
                "forward": True,
                "extend": False
            })
    def eof(self):
        v = self.view
        s = v.sel()
        return True if v.line(s[0]).b < v.size() else False

    def empty_space(self):
        v = self.view
        s = v.sel()
        return True if v.line(s[0]).empty() else False

上面的代码将选定的行发送到REPL,将包含REPL的组聚焦,执行REPL(即命中“ enter”),然后聚焦回代码窗口。

插件现在将光标自动移动到下一行,因此您可以快速评估行。 另外,如果下一行恰好为空,则插件会自动为您自动点击“输入”,直到遇到非空行。 对我来说,这是关键,因为如果我在python中选择了一个功能块,我仍然必须切换到REPL并按Enter键才能完成该功能。 while循环部分解决了该问题。

使用插件

要运行以下插件,我将以下内容放入我的用户键绑定(这是我从上面的第3个链接中学到的)...

    //REPL send and evaluate
    { "keys": ["super+enter"], "command": "repl_view_and_execute"}

那应该工作!

摘要

记住,你需要

  1. 将sublimeREPL放在同一窗口中,但放在单独的组中(按照第一个链接使用pydev插件快速完成此pydev )。
  2. 创建'repl_view_and_execute'插件,然后将其绑定。

如果有人知道如何制作一个更优雅的版本,可以在活动窗口和包含REPL视图的任何位置(例如另一个窗口)之间切换,那么我欢迎您提出建议。 我通过sublimerepl.py工作,但无法弄清楚如何使用所有active_window()等命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM