【问题标题】:Display output from evaluating selection - Sublime Text Python REPL显示评估选择的输出 - Sublime Text Python REPL
【发布时间】:2014-05-31 15:47:24
【问题描述】:

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

当一个 Python REPL 窗口打开时,我可以使用 Ctrl + , , s 从编辑器中发送一段代码给它。但是,这样做不会显示我的命令的任何输出,除非我包含打印命令。例如。如果我写以下内容

x = 2.5

type(x)

并使用Ctrl +, , s 将其发送以进行评估,然后我会得到这些命令的显示,但我没有得到 type(x) 的输出显示,如果我这样做的话将命令复制/粘贴到 Mac 终端的 Python 解释器中。

有没有办法在 Sublime Text 中获得这个功能?

【问题讨论】:

    标签: python sublimetext sublimerepl


    【解决方案1】:

    [更新]

    以下代码现已弃用。如需最新、更好的工作版本,请访问此插件的要点https://gist.github.com/dantonnoriega/46c40275a93bab74cff6

    随意分叉和加星。随着代码的发展,我将添加对要点的任何更改。但是,为了保持最新,请关注以下 repo: https://github.com/dantonnoriega/sublime_text_plugins/blob/master/python_blocks_for_repl.py

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

    我想要相同的功能。我想出的作品是以下帖子的大杂烩:

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

    Is it possible to chain key binding commands in sublime text 2?

    How to pass a line to the console in sublime text 2 editor

    创建插件

    以下插件要求您在与您的代码相同的窗口中打开 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(即点击“输入”)然后聚焦回代码窗口。

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

    使用插件

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

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

    这应该有效!

    总结

    记住,你需要

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 2016-02-07
      • 2023-03-07
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多