private void autoClose()

in ruta-ep-ide-ui/src/main/java/org/apache/uima/ruta/ide/ui/text/RutaAutoEditStrategy.java [631:718]


  private void autoClose(IDocument d, DocumentCommand c) {
    if (c.offset == -1)
      return;
    try {
      if (d.getChar(c.offset - 1) == '\\')
        return;
    } catch (BadLocationException e1) {
    }
    if ('\"' == c.text.charAt(0) && !closeStrings())
      return;
    if ('\'' == c.text.charAt(0) && !closeStrings())
      return;
    if (!closeBrackets()
            && ('[' == c.text.charAt(0) || '(' == c.text.charAt(0) || '{' == c.text.charAt(0)))
      return;
    try {

      switch (c.text.charAt(0)) {
        case '\"':
        case '\'':
          // if we close existing quote, do nothing
          if ('\"' == c.text.charAt(0) && c.offset > 0 && "\"".equals(d.get(c.offset - 1, 1)))
            return;

          if ('\'' == c.text.charAt(0) && c.offset > 0 && "\'".equals(d.get(c.offset - 1, 1)))
            return;

          if (c.offset != d.getLength() && c.text.charAt(0) == d.get(c.offset, 1).charAt(0))
            c.text = "";
          else {
            c.text += c.text;
            c.length = 0;
          }

          c.shiftsCaret = false;
          c.caretOffset = c.offset + 1;
          break;
        case '(':
        case '{':
        case '[':
          // check partition
          if (getRegionType(d, c.offset) != IDocument.DEFAULT_CONTENT_TYPE)
            return;
          if (c.offset != d.getLength() && c.text.charAt(0) == d.get(c.offset, 1).charAt(0))
            return;

          try { // in class closing
            String regex = "^\\s*class\\s+.*";
            String regex2 = ".*\\(.*\\).*";
            int start = d.getLineOffset(d.getLineOfOffset(c.offset));
            String curLine = d.get(start, c.offset - start);
            if (Pattern.matches(regex, curLine) && !Pattern.matches(regex2, curLine)) {
              c.text = "():";
              c.shiftsCaret = false;
              c.caretOffset = c.offset + 1;
              return;
            }
          } catch (BadLocationException e) {
          }

          // add closing peer
          c.text = c.text + getBracePair(c.text.charAt(0));
          c.length = 0;

          c.shiftsCaret = false;
          c.caretOffset = c.offset + 1;
          break;
        case '}':
        case ']':
        case ')':
          // check partition
          if (getRegionType(d, c.offset) != IDocument.DEFAULT_CONTENT_TYPE)
            return;
          if (!closeBrackets())
            return;
          // if we already have bracket we should jump over it
          if (c.offset != d.getLength() && c.text.charAt(0) == d.get(c.offset, 1).charAt(0)) {
            c.text = "";
            c.shiftsCaret = false;
            c.caretOffset = c.offset + 1;
            return;
          }
          break;
      }
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }