简体   繁体   English

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

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

I am using Sublime Text 3 and running OSX Mavericks. 我正在使用Sublime Text 3并正在运行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 我正在使用Sublime REPL软件包,并且已经对该软件包的设置进行了调整,以使我拥有“ show_transferred_text”:true

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 . 当打开Python REPL窗口时,我有一个不错的选择,可以使用Ctrl +,,s从编辑器向其中发送代码。 But, doing so doesn't display any of the output of my commands, unless I include a print command. 但是,除非包含print命令,否则不会显示命令的任何输出。 Eg If I write the following 例如,如果我写以下内容

x = 2.5

type(x)

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

Is there any way to get this functionality within Sublime Text? 有什么办法可以在Sublime Text中获得此功能?

[UPDATE] [UPDATE]

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://gist.github.com/dantonnoriega/46c40275a93bab74cff6的此插件的要点。

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 但是,要保持最新状态,请遵循以下回购协议: 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 https://stackoverflow.com/a/14091739/3987905

Is it possible to chain key binding commands in sublime text 2? 是否可以将键绑定命令链接到sublime text 2中?

How to pass a line to the console in sublime text 2 editor 如何在Sublime Text 2编辑器中将行传递到控制台

Creating the Plugin 创建插件

The following plugin requires that you open repl in the same window as your code but as a separate group. 以下插件要求您在与代码相同的窗口中打开repl,但将其作为单独的组。 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

The code above sends the selected line(s) to REPL, focuses the group where REPL is contained, executes REPL (ie hits 'enter') then focuses back to the code window. 上面的代码将选定的行发送到REPL,将包含REPL的组聚焦,执行REPL(即命中“ enter”),然后聚焦回代码窗口。

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. 对我来说,这是关键,因为如果我在python中选择了一个功能块,我仍然必须切换到REPL并按Enter键才能完成该功能。 The while loop part fixes that. while循环部分解决了该问题。

Using the Plugin 使用插件

To run the following plugin, I put the following in my user key bindings (which I learned from the 3rd link above)... 要运行以下插件,我将以下内容放入我的用户键绑定(这是我从上面的第3个链接中学到的)...

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

And that should work! 那应该工作!

Summary 摘要

Remember, you need to 记住,你需要

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

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. 如果有人知道如何制作一个更优雅的版本,可以在活动窗口和包含REPL视图的任何位置(例如另一个窗口)之间切换,那么我欢迎您提出建议。 I worked through sublimerepl.py but could not figure out how to use all the active_window() etc. commands. 我通过sublimerepl.py工作,但无法弄清楚如何使用所有active_window()等命令。

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

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