2017-11-11 141 views
2

我已經IPython的5.3.0和當我在表達式中(光標被標記爲<cursor>),例如表達的執行力將這條線分成兩行。IPython的任何光標位置

In [69]: x = np.arange(      
    ...: 1, 21, 2).reshape(2, 5) 

但是,當我在其他地方光標例如:

In [69]: x = np.<cursor>arange(1, 21, 2).reshape(2, 5) 

它執行表達式。

哪個鍵盤快捷鍵強制執行表達式而不考慮光標位置?

我試過CTRL + ENTER或SHIFT + ENTER,但是沒有人爲第一個例子工作。

回答

1

This was fixed in IPython 5.4。此前,沒有辦法修補/ monkey-patching at startup,但有一個簡單的解決方法。

下面是5.4.1的相關邏輯,IPython/terminal/shortcuts.py。帶有評論指的是第一個鏈接上的問題的片段是修復。

def newline_or_execute_outer(shell): 
    def newline_or_execute(event): 
     """When the user presses return, insert a newline or execute the code.""" 
     b = event.current_buffer 
     d = b.document 

     if b.complete_state: 
      cc = b.complete_state.current_completion 
      if cc: 
       b.apply_completion(cc) 
      else: 
       b.cancel_completion() 
      return 

     # If there's only one line, treat it as if the cursor is at the end. 
     # See https://github.com/ipython/ipython/issues/10425 
     if d.line_count == 1: 
      check_text = d.text 
     else: 
      check_text = d.text[:d.cursor_position] 
     status, indent = shell.input_splitter.check_complete(check_text + '\n') 

     if not (d.on_last_line or 
       d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end() 
       ): 
      b.insert_text('\n' + (' ' * (indent or 0))) 
      return 

     if (status != 'incomplete') and b.accept_action.is_returnable: 
      b.accept_action.validate_and_handle(event.cli, b) 
     else: 
      b.insert_text('\n' + (' ' * (indent or 0))) 
    return newline_or_execute 

正如您所看到的,該操作取決於與代碼標記相關的遊標位置。

所以,如果你有一個完整的語句,你只需按下結束之前輸入強制執行。