显示评估选择的输出-Sublime Text Python REPL [英] Display output from evaluating selection - Sublime Text Python REPL

查看:61
本文介绍了显示评估选择的输出-Sublime Text Python REPL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sublime Text 3并运行OSX Mavericks.我正在使用Sublime REPL软件包,并且已经对该软件包的设置进行了调整,以便 "show_transferred_text":是

I am using Sublime Text 3 and running OSX Mavericks. I am using the Sublime REPL package and I have adjusted the settings for that package such that I have "show_transferred_text" : true

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

When a Python REPL window is opened, I have the nice option to send a chunk of code from the editor to it using Ctrl + , , s . But, doing so doesn't display any of the output of my commands, unless I include a print command. E.g. If I write the following

x = 2.5

type(x)

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

and use the Ctrl +, , s to send it to be evaluated, then I do get a display of these commands, but I don't get a display of the output from type(x) as I would if I copy/pasted the commands into the Python interpreter in the Mac Terminal.

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

Is there any way to get this functionality within Sublime Text?

推荐答案

[UPDATE]

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

The code below is now deprecated. For the newest, better working version, please visit the gist for this plugin at https://gist.github.com/dantonnoriega/46c40275a93bab74cff6.

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

Feel free to fork and star. I will add any changes to the gist as the code evolves. However, to stay the most up-to-date, please following the following repo: https://github.com/dantonnoriega/sublime_text_plugins/blob/master/python_blocks_for_repl.py

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

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

I wanted the same functionality. What I came up with works and is a hodgepodge of the following posts:

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

是否可以将键绑定命令链接到高级文本2中?

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

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

The following plugin requires that you open repl in the same window as your code but as a separate group. I learned how to do this using the 1st link above. In order to send the text you want and then execute the text, I used the idea from the 2nd link above and wrote the following plugin (Tools -> New Plugin...)

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"),然后聚焦回到代码窗口.

The code above sends the selected line(s) to REPL, focuses the group where REPL is contained, executes REPL (i.e. hits 'enter') then focuses back to the code window.

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

The plugin now moves the cursor to the next line automatically so you can evaluate lines quickly. Also, if the next line happens to be empty, the plugin keeps hitting 'enter' for you automatically until it encounters a non-empty line. This, for me, was key, since if I selected a function block in python, I would still have to switch over to REPL and hit enter for the function to complete. The while loop part fixes that.

要运行以下插件,请在用户按键绑定中添加以下内容(我是从上面的第3个链接中学到的)...

To run the following plugin, I put the following in my user key bindings (which I learned from the 3rd link above)...

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

那应该可行!

请记住,您需要

  1. 将sublimeREPL放在同一窗口中但在单独的组中(如下) 使用pydev插件快速执行此操作的第一个链接.
  2. 创建'repl_view_and_execute'插件,然后将其绑定.
  1. Put sublimeREPL in the same window but in a separate group (follow the first link to do this quickly with the pydev plugin).
  2. Create the 'repl_view_and_execute' plugin then bind it.

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

If anyone knows how to make a more elegant version that can switch between the active window and wherever the REPL view is contained (like another window), I welcome the advice. I worked through sublimerepl.py but could not figure out how to use all the active_window() etc. commands.

这篇关于显示评估选择的输出-Sublime Text Python REPL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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