/** When a console page is initialized, */
  public void init(IPageBookViewPage page, final IConsole console) {
    if (!(console instanceof ProcessConsole)) {
      return;
    }
    ProcessConsole processConsole = (ProcessConsole) console;
    IProcess process = processConsole.getProcess();
    if (process == null) {
      return;
    }
    if (!PyCodeCompletionPreferencesPage.useCodeCompletion()
        || !PyCodeCompletionPreferencesPage.useCodeCompletionOnDebug()) {
      return;
    }
    String attribute = process.getAttribute(Constants.PYDEV_DEBUG_IPROCESS_ATTR);
    if (!Constants.PYDEV_DEBUG_IPROCESS_ATTR_TRUE.equals(attribute)) {
      // Only provide code-completion for pydev debug processes.
      return;
    }
    Control control = page.getControl();
    if (page instanceof IOConsolePage) {

      // Note that completions on "all letters and '_'" are already activated just by installing
      // the content assist, but the completions on the default keybinding is not, so, we have to
      // call it ourselves here.
      control.addKeyListener(
          new KeyListener() {
            public void keyPressed(KeyEvent e) {

              if (KeyBindingHelper.matchesContentAssistKeybinding(e)) {
                contentAssist.showPossibleCompletions();
              }
            }

            public void keyReleased(KeyEvent e) {}
          });

      IOConsolePage consolePage = (IOConsolePage) page;
      TextConsoleViewer viewer = consolePage.getViewer();

      contentAssist =
          new PyContentAssistant() {
            public String showPossibleCompletions() {
              // Only show completions if we're in a suspended console.
              if (getCurrentSuspendedPyStackFrame(console) == null) {
                return null;
              }
              return super.showPossibleCompletions();
            };
          };
      contentAssist.setInformationControlCreator(
          PyContentAssistant.createInformationControlCreator(viewer));
      ILaunch launch = process.getLaunch();
      IDebugTarget debugTarget = launch.getDebugTarget();
      IInterpreterInfo projectInterpreter = null;
      if (debugTarget instanceof PyDebugTarget) {
        PyDebugTarget pyDebugTarget = (PyDebugTarget) debugTarget;
        PythonNature nature = PythonNature.getPythonNature(pyDebugTarget.project);
        if (nature != null) {
          try {
            projectInterpreter = nature.getProjectInterpreter();
          } catch (Throwable e1) {
            Log.log(e1);
          }
        }
      }
      contentAssist.install(new ScriptConsoleViewerWrapper(viewer, projectInterpreter));

      PydevConsoleInterpreter interpreter = new PydevConsoleInterpreter();
      interpreter.setConsoleCommunication(new GetCompletionsInDebug());

      IContentAssistProcessor processor =
          new PydevConsoleCompletionProcessor(interpreter, contentAssist);
      contentAssist.setContentAssistProcessor(processor, IOConsolePartition.INPUT_PARTITION_TYPE);
      contentAssist.setContentAssistProcessor(processor, IOConsolePartition.OUTPUT_PARTITION_TYPE);

      contentAssist.enableAutoActivation(true);
      contentAssist.enableAutoInsert(false);
      contentAssist.setAutoActivationDelay(PyCodeCompletionPreferencesPage.getAutocompleteDelay());
    }
  }
示例#2
0
  /**
   * This function adds the input listener extension point, so that plugins that only care about the
   * input in the console can know about it.
   */
  @SuppressWarnings({"unchecked"})
  public void addConsoleInputListener() {
    IConsole console = DebugUITools.getConsole(this.getProcess());
    if (console instanceof ProcessConsole) {
      final ProcessConsole c = (ProcessConsole) console;
      final List<IConsoleInputListener> participants =
          ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_DEBUG_CONSOLE_INPUT_LISTENER);
      final AbstractDebugTarget target = this;
      // let's listen the doc for the changes
      c.getDocument()
          .addDocumentListener(
              new IDocumentListener() {

                public void documentAboutToBeChanged(DocumentEvent event) {
                  // only report when we have a new line
                  if (event.fText.indexOf('\r') != -1 || event.fText.indexOf('\n') != -1) {
                    try {
                      ITypedRegion partition = event.fDocument.getPartition(event.fOffset);
                      if (partition instanceof IOConsolePartition) {
                        IOConsolePartition p = (IOConsolePartition) partition;

                        // we only communicate about inputs (because we only care about what the
                        // user writes)
                        if (p.getType().equals(IOConsolePartition.INPUT_PARTITION_TYPE)) {
                          if (event.fText.length() <= 2) {
                            // the user typed something
                            final String inputFound = p.getString();
                            for (IConsoleInputListener listener : participants) {
                              listener.newLineReceived(inputFound, target);
                            }
                          }
                        }
                      }
                    } catch (Exception e) {
                      Log.log(e);
                    }
                  }
                }

                public void documentChanged(DocumentEvent event) {
                  // only report when we have a new line
                  if (event.fText.indexOf('\r') != -1 || event.fText.indexOf('\n') != -1) {
                    try {
                      ITypedRegion partition = event.fDocument.getPartition(event.fOffset);
                      if (partition instanceof IOConsolePartition) {
                        IOConsolePartition p = (IOConsolePartition) partition;

                        // we only communicate about inputs (because we only care about what the
                        // user writes)
                        if (p.getType().equals(IOConsolePartition.INPUT_PARTITION_TYPE)) {
                          if (event.fText.length() > 2) {
                            // the user pasted something
                            for (IConsoleInputListener listener : participants) {
                              listener.pasteReceived(event.fText, target);
                            }
                          }
                        }
                      }
                    } catch (Exception e) {
                      Log.log(e);
                    }
                  }
                }
              });
    }
  }