Ejemplo n.º 1
0
  private void processCompletionKeys(final KeyEvent event) {
    char keyChar = event.getKeyChar();
    int keyCode = event.getKeyCode();
    if (keyChar == KeyEvent.VK_ENTER
        || keyChar == KeyEvent.VK_ESCAPE
        || keyChar == KeyEvent.VK_TAB
        || (event.getID() == KeyEvent.KEY_RELEASED
            && keyCode != KeyEvent.VK_LEFT
            && keyCode != KeyEvent.VK_RIGHT)) {
      // ignore
    } else if (keyChar == ')') {
      // https://github.com/processing/processing/issues/2741
      hideSuggestion();

    } else if (keyChar == '.') {
      if (JavaMode.codeCompletionsEnabled) {
        Messages.log(
            "[KeyEvent]" + KeyEvent.getKeyText(event.getKeyCode()) + "  |Prediction started");
        fetchPhrase();
      }
    } else if (keyChar == ' ') { // Trigger on Ctrl-Space
      if (!Platform.isMacOS()
          && JavaMode.codeCompletionsEnabled
          && (event.isControlDown() || event.isMetaDown())) {
        // Provide completions only if it's enabled
        if (JavaMode.codeCompletionsEnabled) {
          // Removed for https://github.com/processing/processing/issues/3847
          // try {
          //  getDocument().remove(getCaretPosition() - 1, 1); // Remove the typed space
          Messages.log("[KeyEvent]" + event.getKeyChar() + "  |Prediction started");
          fetchPhrase();
          // } catch (BadLocationException e) {
          //  e.printStackTrace();
          // }
        }
      } else {
        hideSuggestion(); // hide on spacebar
      }
    } else {
      if (JavaMode.codeCompletionsEnabled) {
        prepareSuggestions(event);
      }
    }
  }
Ejemplo n.º 2
0
  /** Handles KeyEvents for TextArea (code completion begins from here). */
  public void processKeyEvent(KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
      if (suggestion != null) {
        if (suggestion.isVisible()) {
          Messages.log("esc key");
          hideSuggestion();
          evt.consume();
          return;
        }
      }

    } else if (evt.getKeyCode() == KeyEvent.VK_ENTER && evt.getID() == KeyEvent.KEY_PRESSED) {
      if (suggestion != null
          && suggestion.isVisible()
          && suggestion.insertSelection(CompletionPanel.KEYBOARD_COMPLETION)) {
        evt.consume();
        // Still try to show suggestions after inserting if it's
        // the case of overloaded methods. See #2755
        if (suggestion.isVisible()) {
          prepareSuggestions(evt);
        }
        return;
      }
    }

    if (evt.getID() == KeyEvent.KEY_PRESSED) {
      switch (evt.getKeyCode()) {
        case KeyEvent.VK_DOWN:
          if (suggestion != null)
            if (suggestion.isVisible()) {
              // log("KeyDown");
              suggestion.moveDown();
              return;
            }
          break;
        case KeyEvent.VK_UP:
          if (suggestion != null)
            if (suggestion.isVisible()) {
              // log("KeyUp");
              suggestion.moveUp();
              return;
            }
          break;
        case KeyEvent.VK_BACK_SPACE:
          Messages.log("BK Key");
          break;
        case KeyEvent.VK_SPACE:
          if (suggestion != null) {
            if (suggestion.isVisible()) {
              Messages.log("Space bar, hide completion list");
              suggestion.setInvisible();
            }
          }
          break;
      }
    }
    super.processKeyEvent(evt);

    // code completion disabled if Java tabs present
    if (!editor.hasJavaTabs()) {
      if (evt.getID() == KeyEvent.KEY_TYPED) {
        processCompletionKeys(evt);
      } else if (!Platform.isMacOS() && evt.getID() == KeyEvent.KEY_RELEASED) {
        processCompletionKeys(evt);
      } else if (Platform.isMacOS() && evt.getID() == KeyEvent.KEY_RELEASED) {
        processControlSpace(evt);
      }
    }
  }