/**
   * Determines the position in the model that is closest to the given view location in the row
   * below. The component given must have a size to compute the result. If the component doesn't
   * have a size a value of -1 will be returned.
   *
   * @param c the editor
   * @param offs the offset in the document >= 0
   * @param x the X coordinate >= 0
   * @return the position >= 0 if the request can be computed, otherwise a value of -1 will be
   *     returned.
   * @exception BadLocationException if the offset is out of range
   */
  public static final int getPositionBelow(RSyntaxTextArea c, int offs, float x, TabExpander e)
      throws BadLocationException {

    TokenOrientedView tov = (TokenOrientedView) e;
    Token token = tov.getTokenListForPhysicalLineBelow(offs);
    if (token == null) return -1;

    // A line containing only Token.NULL is an empty line.
    else if (token.type == Token.NULL) {
      int line = c.getLineOfOffset(offs); // Sure to be > c.getLineCount()-1 ??
      return c.getLineStartOffset(line + 1);
    } else {
      return token.getListOffset(c, e, 0, x);
    }
  }
Ejemplo n.º 2
0
    private void insertBreakInMLC(ActionEvent e, RSyntaxTextArea textArea, int line) {

      Matcher m = null;
      int start = -1;
      int end = -1;
      try {
        start = textArea.getLineStartOffset(line);
        end = textArea.getLineEndOffset(line);
        String text = textArea.getText(start, end - start);
        m = p.matcher(text);
      } catch (BadLocationException ble) { // Never happens
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        ble.printStackTrace();
        return;
      }

      if (m.lookingAt()) {

        String leadingWS = m.group(1);
        String mlcMarker = m.group(2);

        // If the caret is "inside" any leading whitespace or MLC
        // marker, move it to the end of the line.
        int dot = textArea.getCaretPosition();
        if (dot >= start && dot < start + leadingWS.length() + mlcMarker.length()) {
          // If we're in the whitespace before the very start of the
          // MLC though, just insert a normal newline
          if (mlcMarker.charAt(0) == '/') {
            handleInsertBreak(textArea, true);
            return;
          }
          textArea.setCaretPosition(end - 1);
        }

        boolean firstMlcLine = mlcMarker.charAt(0) == '/';
        boolean nested = appearsNested(textArea, line, start + leadingWS.length() + 2);
        String header = leadingWS + (firstMlcLine ? " * " : "*") + m.group(3);
        textArea.replaceSelection("\n" + header);
        if (nested) {
          dot = textArea.getCaretPosition(); // Has changed
          textArea.insert("\n" + leadingWS + " */", dot);
          textArea.setCaretPosition(dot);
        }

      } else {
        handleInsertBreak(textArea, true);
      }
    }