summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/console/PythonConsoleView.java
blob: 1378bff616e7a515a383ffbd8fc3259632931e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.console;

import com.google.common.collect.Maps;
import com.intellij.application.options.RegistryManager;
import com.intellij.execution.console.LanguageConsoleImpl;
import com.intellij.execution.filters.OpenFileHyperlinkInfo;
import com.intellij.execution.impl.ConsoleViewUtil;
import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.execution.ui.ObservableConsoleView;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.TransactionGuard;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.FoldingModelEx;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.JBPopupListener;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.intellij.openapi.ui.popup.util.MinimizeButton;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.ui.JBColor;
import com.intellij.ui.JBSplitter;
import com.intellij.ui.TitlePanel;
import com.intellij.ui.content.Content;
import com.intellij.ui.popup.AbstractPopup;
import com.intellij.util.TimeoutUtil;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.impl.frame.XStandaloneVariablesView;
import com.intellij.xdebugger.impl.ui.tree.nodes.XDebuggerTreeNode;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PythonLanguage;
import com.jetbrains.python.console.actions.CommandQueueForPythonConsoleService;
import com.jetbrains.python.console.actions.CommandQueueListener;
import com.jetbrains.python.console.completion.PythonConsoleAutopopupBlockingHandler;
import com.jetbrains.python.console.pydev.ConsoleCommunication;
import com.jetbrains.python.console.pydev.ConsoleCommunicationListener;
import com.jetbrains.python.console.pythonCommandQueue.PythonCommandQueuePanel;
import com.jetbrains.python.debugger.PyDebugValueDescriptor;
import com.jetbrains.python.debugger.PyDebuggerEditorsProvider;
import com.jetbrains.python.debugger.PyStackFrame;
import com.jetbrains.python.debugger.PyStackFrameInfo;
import com.jetbrains.python.highlighting.PyHighlighter;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import com.jetbrains.python.testing.PyTestsSharedKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static com.jetbrains.python.console.PydevConsoleRunner.CONSOLE_COMMUNICATION_KEY;

public class PythonConsoleView extends LanguageConsoleImpl implements ObservableConsoleView, PyCodeExecutor {
  public static final Key<Boolean> CONSOLE_KEY = new Key<>("PYDEV_CONSOLE_KEY");
  private static final Logger LOG = Logger.getInstance(PythonConsoleView.class);
  private final boolean myTestMode;

  private PythonConsoleExecuteActionHandler myExecuteActionHandler;
  private PyConsoleSourceHighlighter mySourceHighlighter;
  private boolean myIsIPythonOutput;
  private final PyHighlighter myPyHighlighter;
  private final EditorColorsScheme myScheme;
  private boolean myHyperlink;

  private XStandaloneVariablesView mySplitView;
  private final ActionCallback myInitialized = new ActionCallback();
  private boolean isShowVars;
  @Nullable private String mySdkHomePath;
  private PydevConsoleRunner myRunner;

  private final Map<String, Map<String, PyDebugValueDescriptor>> myDescriptorsCache = Maps.newConcurrentMap();

  private final PythonCommandQueuePanel myCommandQueuePanel = new PythonCommandQueuePanel(this);
  private JBPopup myCommandQueue;
  private Dimension commandQueueDimension;
  private boolean isShowQueue;

  private ActionToolbar myToolbar;
  private boolean myIsToolwindowHorizontal = true;

  /**
   * @param testMode this console will be used to display test output and should support TC messages
   */
  public PythonConsoleView(final Project project, final String title, @Nullable final Sdk sdk, final boolean testMode) {
    super(project, title, PythonLanguage.getInstance());
    myTestMode = testMode;
    isShowVars = PyConsoleOptions.getInstance(project).isShowVariableByDefault();
    VirtualFile virtualFile = getVirtualFile();
    PythonLanguageLevelPusher.specifyFileLanguageLevel(virtualFile, PythonSdkType.getLanguageLevelForSdk(sdk));
    virtualFile.putUserData(CONSOLE_KEY, true);
    // Mark editor as console one, to prevent autopopup completion
    getConsoleEditor().putUserData(PythonConsoleAutopopupBlockingHandler.REPL_KEY, new Object());
    getHistoryViewer().putUserData(ConsoleViewUtil.EDITOR_IS_CONSOLE_HISTORY_VIEW, true);
    super.setPrompt(null);
    setUpdateFoldingsEnabled(false);
    LanguageLevel languageLevel = LanguageLevel.getDefault();
    if (sdk != null) {
      final PythonSdkFlavor sdkFlavor = PythonSdkFlavor.getFlavor(sdk);
      if (sdkFlavor != null) {
        languageLevel = sdkFlavor.getLanguageLevel(sdk);
      }
      mySdkHomePath = sdk.getHomePath();
    }
    myPyHighlighter = new PyHighlighter(languageLevel);
    myScheme = getConsoleEditor().getColorsScheme();
    addToolwindowPositionListener(project);
  }

  public void setCommandQueueTitle(String title) {
    if (myCommandQueue != null) {
      myCommandQueue.setCaption(PyBundle.message(
        "python.console.command.queue.add.title", title));
    }
  }

  public void setConsoleCommunication(final ConsoleCommunication communication) {
    getFile().putCopyableUserData(CONSOLE_COMMUNICATION_KEY, communication);

    if (isShowVars && communication instanceof PydevConsoleCommunication) {
      myIsToolwindowHorizontal = isToolwindowHorizontal(PythonConsoleToolWindow.getInstance(getProject()).getToolWindow());
      showVariables((PydevConsoleCommunication)communication);
    }
    if (RegistryManager.getInstance().is("python.console.CommandQueue")){
      if (communication instanceof PydevConsoleCommunication || communication instanceof PythonDebugConsoleCommunication) {
        myCommandQueuePanel.setCommunication(communication);
        ApplicationManager.getApplication().getService(CommandQueueForPythonConsoleService.class)
          .addListener(communication, new CommandQueueListener() {
          @Override
          public void removeCommand(ConsoleCommunication.@NotNull ConsoleCodeFragment command) {
            ApplicationManager.getApplication().invokeLater(() -> {
              myCommandQueuePanel.removeCommand(command);
            });
          }

          @Override
          public void addCommand(ConsoleCommunication.@NotNull ConsoleCodeFragment command) {
            myCommandQueuePanel.addCommand(command);
          }

          @Override
          public void removeAll() {
            myCommandQueuePanel.removeAllCommands();
          }
        });
      }
    }
  }

  /**
   * Add folding to Console view
   *
   * @param addOnce If true, folding will be added once when an appropriate area is found.
   *                Otherwise folding can be expanded by newly added text.
   */
  @Nullable
  private PyConsoleStartFolding createConsoleFolding(boolean addOnce) {
    PyConsoleStartFolding startFolding = new PyConsoleStartFolding(this, addOnce);
    myExecuteActionHandler.getConsoleCommunication().addCommunicationListener(startFolding);
    Editor editor = getEditor();
    if (editor == null) {
      return null;
    }
    editor.getDocument().addDocumentListener(startFolding);
    ((FoldingModelEx)editor.getFoldingModel()).addListener(startFolding, this);
    return startFolding;
  }

  public void addConsoleFolding(boolean isDebugConsole, boolean addOnce) {
    try {
      if (isDebugConsole && myExecuteActionHandler != null && getEditor() != null) {
        PyConsoleStartFolding folding = createConsoleFolding(addOnce);
        if (folding != null) {
          // in debug console we should add folding from the place where the folding was turned on
          folding.setStartLineOffset(getEditor().getDocument().getTextLength());
          folding.setNumberOfCommandToStop(2);
        }
      }
      else {
        myInitialized.doWhenDone(() -> createConsoleFolding(addOnce));
      }
    }
    catch (Exception e) {
      LOG.error(e.getMessage());
    }
  }

  public void setExecutionHandler(@NotNull PythonConsoleExecuteActionHandler consoleExecuteActionHandler) {
    myExecuteActionHandler = consoleExecuteActionHandler;
  }

  public PythonConsoleExecuteActionHandler getExecuteActionHandler() {
    return myExecuteActionHandler;
  }

  public void setConsoleEnabled(boolean flag) {
    if (myExecuteActionHandler != null) {
      myExecuteActionHandler.setEnabled(flag);
    }
    else {
      myInitialized.doWhenDone(() -> myExecuteActionHandler.setEnabled(flag));
    }
  }

  public void inputRequested() {
    if (myExecuteActionHandler != null) {
      final ConsoleCommunication consoleCommunication = myExecuteActionHandler.getConsoleCommunication();
      if (consoleCommunication instanceof PythonDebugConsoleCommunication) {
        consoleCommunication.notifyInputRequested();
      }
    }
  }

  public void inputReceived() {
    // If user's input was entered while debug console was turned off, we shouldn't wait for it anymore
    if (myExecuteActionHandler != null) {
      myExecuteActionHandler.getConsoleCommunication().notifyInputReceived();
    }
  }

  @Override
  public void requestFocus() {
    myInitialized.doWhenDone(() ->
                               IdeFocusManager.getGlobalInstance().requestFocus(getConsoleEditor().getContentComponent(), true));
  }

  @Override
  public void executeCode(final @Nullable String code, @Nullable final Editor editor) {
    myInitialized.doWhenDone(
      () -> {
        if (code != null) {
          if (RegistryManager.getInstance().is("python.console.CommandQueue")) {
            executeInConsole(code);
          } else {
            ProgressManager.getInstance().run(new Task.Backgroundable(null, PyBundle.message("console.executing.code.in.console"), true) {
              @Override
              public void run(@NotNull final ProgressIndicator indicator) {
                while (!myExecuteActionHandler.isEnabled() || !myExecuteActionHandler.canExecuteNow()) {
                  if (indicator.isCanceled()) {
                    break;
                  }
                  TimeoutUtil.sleep(300);
                }
                if (!indicator.isCanceled()) {
                  executeInConsole(code);
                }
              }
            });
          }
        }
        else {
          requestFocus();
        }
      }
    );
  }


  public void executeInConsole(@NotNull final String code) {
    CountDownLatch latch = new CountDownLatch(1);

    TransactionGuard.submitTransaction(this, () -> {
      final String codeToExecute = code.endsWith("\n") || myExecuteActionHandler.checkSingleLine(code) ? code : code + "\n";
      DocumentEx document = getConsoleEditor().getDocument();
      String oldText = document.getText();
      ApplicationManager.getApplication().runWriteAction(() -> {
        setInputText(codeToExecute);
        PsiDocumentManager.getInstance(getProject()).commitDocument(document);
        PsiFile psiFile = PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
        if (psiFile != null) {
          CommandProcessor.getInstance().runUndoTransparentAction(() ->
                                                                    CodeStyleManager.getInstance(getProject())
                                                                                    .adjustLineIndent(psiFile,
                                                                                                      new TextRange(0, psiFile
                                                                                                        .getTextLength())));
        }
        int oldOffset = getConsoleEditor().getCaretModel().getOffset();
        getConsoleEditor().getCaretModel().moveToOffset(document.getTextLength());
        myExecuteActionHandler.runExecuteAction(this);

        if (!StringUtil.isEmpty(oldText)) {
          ApplicationManager.getApplication().runWriteAction(() -> setInputText(oldText));
          getConsoleEditor().getCaretModel().moveToOffset(oldOffset);
        }
      });

      latch.countDown();
    });

    try {
      latch.await(1, TimeUnit.MINUTES);
    }
    catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }
  }

  public void executeStatement(@NotNull String statement, @NotNull final Key attributes) {
    print(statement, outputTypeForAttributes(attributes));
    myExecuteActionHandler.processLine(statement);
  }

  public void printText(String text, final ConsoleViewContentType outputType) {
    super.print(text, outputType);
  }

  public void print(String text, @NotNull final Key attributes) {
    print(text, outputTypeForAttributes(attributes));
  }

  @Override
  public void print(@NotNull String text, @NotNull final ConsoleViewContentType outputType) {
    if (myTestMode) {
      text = PyTestsSharedKt.processTCMessage(text);
    }
    detectIPython(text, outputType);
    if (PyConsoleUtil.detectIPythonEnd(text)) {
      myIsIPythonOutput = false;
      mySourceHighlighter = null;
    }
    else if (PyConsoleUtil.detectIPythonStart(text)) {
      myIsIPythonOutput = true;
    }
    else {
      if (mySourceHighlighter == null || outputType == ConsoleViewContentType.ERROR_OUTPUT) {
        if (myHyperlink) {
          printHyperlink(text, outputType);
        }
        else {
          //Print text normally with converted attributes
          super.print(text, outputType);
        }
        myHyperlink = detectHyperlink(text);
        if (mySourceHighlighter == null && myIsIPythonOutput && PyConsoleUtil.detectSourcePrinting(text)) {
          mySourceHighlighter = new PyConsoleSourceHighlighter(this, myPyHighlighter);
        }
      }
      else {
        try {
          mySourceHighlighter.printHighlightedSource(text);
        }
        catch (Exception e) {
          LOG.error(e);
        }
      }
    }
  }

  public void detectIPython(String text, final ConsoleViewContentType outputType) {
    VirtualFile file = getVirtualFile();
    if (PyConsoleUtil.detectIPythonImported(text, outputType)) {
      PyConsoleUtil.markIPython(file);
      PythonConsoleExecuteActionHandler handler = getExecuteActionHandler();
      if (handler != null) {
        handler.updateConsoleState();
      }
    }
    if (PyConsoleUtil.detectIPythonAutomagicOn(text)) {
      PyConsoleUtil.setIPythonAutomagic(file, true);
    }
    if (PyConsoleUtil.detectIPythonAutomagicOff(text)) {
      PyConsoleUtil.setIPythonAutomagic(file, false);
    }
  }

  private boolean detectHyperlink(@NotNull String text) {
    return myIsIPythonOutput && text.startsWith("File:");
  }

  private void printHyperlink(@NotNull String text, @NotNull ConsoleViewContentType contentType) {
    if (!StringUtil.isEmpty(text)) {
      VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(text.trim());

      if (vFile != null) {
        OpenFileHyperlinkInfo hyperlink = new OpenFileHyperlinkInfo(getProject(), vFile, -1);

        super.printHyperlink(text, hyperlink);
      }
      else {
        super.print(text, contentType);
      }
    }
  }

  public ConsoleViewContentType outputTypeForAttributes(Key attributes) {
    final ConsoleViewContentType outputType;
    if (attributes == ProcessOutputTypes.STDERR) {
      outputType = ConsoleViewContentType.ERROR_OUTPUT;
    }
    else if (attributes == ProcessOutputTypes.SYSTEM) {
      outputType = ConsoleViewContentType.SYSTEM_OUTPUT;
    }
    else {
      outputType = ConsoleViewContentType.getConsoleViewType(attributes);
    }

    return outputType;
  }

  public void setSdk(Sdk sdk) {
    getFile().putCopyableUserData(PydevConsoleRunner.CONSOLE_SDK, sdk);
  }

  public void showVariables(PydevConsoleCommunication consoleCommunication) {
    Project project = getProject();
    PyStackFrame stackFrame = new PyStackFrame(project, consoleCommunication, new PyStackFrameInfo("", "", "", null), null);
    stackFrame.restoreChildrenDescriptors(myDescriptorsCache);
    final XStandaloneVariablesView view = new XStandaloneVariablesView(project, new PyDebuggerEditorsProvider(), stackFrame);
    consoleCommunication.addCommunicationListener(new ConsoleCommunicationListener() {
      @Override
      public void commandExecuted(boolean more) {
        view.rebuildView();
      }

      @Override
      public void inputRequested() {
      }
    });
    mySplitView = view;
    Disposer.register(this, view);
    splitWindow();
  }

  private static boolean isToolwindowHorizontal(ToolWindow toolWindow) {
    return toolWindow.getAnchor() == ToolWindowAnchor.BOTTOM || toolWindow.getAnchor() == ToolWindowAnchor.TOP;
  }

  private void addToolwindowPositionListener(Project project) {
    MessageBusConnection busConnection = project.getMessageBus().connect(this);
    busConnection.subscribe(ToolWindowManagerListener.TOPIC, new ToolWindowManagerListener() {
      @Override
      public void stateChanged(@NotNull ToolWindowManager toolWindowManager) {
        ToolWindow consoleToolWindow = PythonConsoleToolWindow.getInstance(project).getToolWindow();
        if (myIsToolwindowHorizontal != isToolwindowHorizontal(consoleToolWindow)) {
          myIsToolwindowHorizontal = !myIsToolwindowHorizontal;
          if (isShowVars) {
            restoreWindow();
            ConsoleCommunication communication = getFile().getCopyableUserData(CONSOLE_COMMUNICATION_KEY);
            if (communication instanceof PydevConsoleCommunication) {
              showVariables((PydevConsoleCommunication)communication);
            }
          }
        }
      }
    });
  }

  //the main function for drawing the queue
  public void showQueue() {
    JBPopupListener listener = new JBPopupListener() {

      @Override
      public void onClosed(@NotNull LightweightWindowEvent event) {
        isShowQueue = false;
      }
    };
    String commandQueueName = getConsoleDisplayName(getProject());
    myCommandQueue = JBPopupFactory.getInstance()
      .createComponentPopupBuilder(myCommandQueuePanel, null)
      .setMovable(true)
      .setResizable(true)
      .setShowShadow(true)
      .setCancelOnClickOutside(false)
      .setTitle(PyBundle.message(
        "python.console.command.queue.add.title",
        commandQueueName != null ? commandQueueName : "Python Console"))
      .setCancelButton(new MinimizeButton(IdeBundle.message("tooltip.hide")))
      .addListener(listener)
      .setBorderColor(JBColor.background())
      .setCancelOnOtherWindowOpen(true)
      .createPopup();

    var title = (TitlePanel)((AbstractPopup)myCommandQueue).getTitle();
    title.getLabel().setForeground(JBColor.foreground());
    title.setActive(true);
    ((AbstractPopup)myCommandQueue).addResizeListener(this::commandQueueWasResized, this);

    if (commandQueueDimension != null) {
      myCommandQueue.setSize(commandQueueDimension);
    }
    var editor = getConsoleEditor();
    if (UIUtil.isShowing(editor.getContentComponent())) {
      myCommandQueue.showInBestPositionFor(getConsoleEditor());
    }

    Disposer.register(this, myCommandQueue);
  }

  private void commandQueueWasResized() {
    commandQueueDimension = myCommandQueue.getSize();
  }

  @NotNull
  @Override
  protected JComponent createCenterComponent() {
    //workaround for extra lines appearing in the console
    JComponent centerComponent = super.createCenterComponent();
    getHistoryViewer().getSettings().setAdditionalLinesCount(0);
    getHistoryViewer().getSettings().setUseSoftWraps(false);
    getConsoleEditor().getGutterComponentEx().setBackground(getConsoleEditor().getBackgroundColor());
    getConsoleEditor().getGutterComponentEx().revalidate();
    getConsoleEditor().getColorsScheme().setColor(EditorColors.GUTTER_BACKGROUND, getConsoleEditor().getBackgroundColor());

    // settings.set
    return centerComponent;
  }

  private void splitWindow() {
    Component console = getComponent(0);
    removeAll();
    JBSplitter p = new JBSplitter(!myIsToolwindowHorizontal, 2f / 3);
    p.setFirstComponent((JComponent)console);
    p.setSecondComponent(mySplitView.getPanel());
    p.setShowDividerControls(true);
    p.setHonorComponentsMinimumSize(true);

    add(p, BorderLayout.CENTER);
    validate();
    repaint();
  }

  // helper function for drawing the CommandQueue
  public void restoreQueueWindow(boolean removeCommand) {
    if (myCommandQueue != null) {
      ApplicationManager.getApplication().invokeLater(() -> Disposer.dispose(myCommandQueue));
    }
    if (removeCommand) {
      ApplicationManager.getApplication().getService(CommandQueueForPythonConsoleService.class)
        .removeCommand(myRunner.getPydevConsoleCommunication(), true);
    }
  }

  public void restoreWindow() {
    Component component = getComponent(0);
    if (mySplitView != null && component instanceof JBSplitter) {
      JBSplitter pane = (JBSplitter)component;
      removeAll();
      Disposer.dispose(mySplitView);
      mySplitView = null;
      add(pane.getFirstComponent(), BorderLayout.CENTER);
      validate();
      repaint();
    }
  }

  @Nullable
  public String getSdkHomePath() {
    return mySdkHomePath;
  }

  public boolean isInitialized() {
    return myInitialized.isDone();
  }

  public void initialized() {
    myInitialized.setDone();
  }

  public boolean isShowVars() {
    return isShowVars;
  }

  public void setShowVars(boolean showVars) {
    isShowVars = showVars;
  }

  public boolean isShowQueue() {
    return isShowQueue;
  }

  public void setShowQueue(boolean showQueue) {
    isShowQueue = showQueue;
  }

  public void whenInitialized(Runnable runnable) {
    myInitialized.doWhenDone(runnable);
  }

  public void setRunner(PydevConsoleRunner runner) {
    myRunner = runner;
  }

  public PydevConsoleRunner getRunner() {
    return myRunner;
  }

  @Nullable
  @TestOnly
  public XDebuggerTreeNode getDebuggerTreeRootNode() {
    return mySplitView.getTree().getRoot();
  }

  @Override
  public void dispose() {
    super.dispose();
    if (RegistryManager.getInstance().is("python.console.CommandQueue")) {
      ConsoleCommunication communication = getFile().getCopyableUserData(CONSOLE_COMMUNICATION_KEY);
      if (communication != null) {
        ApplicationManager.getApplication().getService(CommandQueueForPythonConsoleService.class).removeListener(communication);
      }
    }
    var editor = myCommandQueuePanel.getQueueEditor();
    commandQueueDimension = null;
    if (!editor.isDisposed()) {
      EditorFactory.getInstance().releaseEditor(editor);
    }
  }

  public void setToolbar(ActionToolbar toolbar) {
    myToolbar = toolbar;
  }

  public ActionToolbar getToolbar() {
    return myToolbar;
  }

  // needs for correctly display the queue's name after renaming
  @Nullable
  private static String getConsoleDisplayName(@NotNull Project project) {
    ToolWindow window = PythonConsoleToolWindow.getInstance(project).getToolWindow();
    if (window == null) return null;
    final Content content = window.getContentManager().getSelectedContent();
    if (content == null) return null;
    return content.getDisplayName();
  }
}