簡體   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