Exemplo n.º 1
0
  /**
   * Perform the goto operation.
   *
   * @return whether the dialog should be made invisible or not
   */
  protected boolean performGoto() {
    JTextComponent c = EditorRegistry.lastFocusedComponent();
    if (c != null) {
      try {
        int line = Integer.parseInt(getGotoValueText());

        // issue 188976
        if (line == 0) line = 1;
        // end of issue 188976

        BaseDocument doc = Utilities.getDocument(c);
        if (doc != null) {
          int rowCount = Utilities.getRowCount(doc);
          if (line > rowCount) line = rowCount;

          // Obtain the offset where to jump
          int pos = Utilities.getRowStartFromLineOffset(doc, line - 1);

          BaseKit kit = Utilities.getKit(c);
          if (kit != null) {
            Action a = kit.getActionByName(ExtKit.gotoAction);
            if (a instanceof ExtKit.GotoAction) {
              pos = ((ExtKit.GotoAction) a).getOffsetFromLine(doc, line - 1);
            }
          }

          if (pos != -1) {
            Caret caret = c.getCaret();
            caret.setDot(pos);
          } else {
            c.getToolkit().beep();
            return false;
          }
        }
      } catch (NumberFormatException e) {
        c.getToolkit().beep();
        return false;
      }
    }
    return true;
  }
Exemplo n.º 2
0
  @Override
  public Map<String, List<OffsetRange>> folds(ParserResult info) {
    ASTNode root = ASTUtils.getRoot(info);

    if (root == null) {
      return Collections.emptyMap();
    }

    GroovyParserResult rpr = ASTUtils.getParseResult(info);
    AnalysisResult analysisResult = rpr.getStructure();

    Map<String, List<OffsetRange>> folds = new HashMap<String, List<OffsetRange>>();
    List<OffsetRange> codefolds = new ArrayList<OffsetRange>();
    folds.put("codeblocks", codefolds); // NOI18N

    final BaseDocument doc = LexUtilities.getDocument(rpr, false);
    if (doc == null) {
      return Collections.emptyMap();
    }

    final OffsetRange[] importsRange = new OffsetRange[1];
    final List<OffsetRange> commentsRanges = new ArrayList<OffsetRange>();

    doc.render(
        new Runnable() {
          @Override
          public void run() {
            TokenSequence<?> ts = LexUtilities.getGroovyTokenSequence(doc, 1);

            int importStart = 0;
            int importEnd = 0;

            boolean startSet = false;

            while (ts != null && ts.isValid() && ts.moveNext()) {
              Token t = ts.token();
              if (t.id() == GroovyTokenId.LITERAL_import) {
                int offset = ts.offset();
                if (!startSet) {
                  importStart = offset;
                  startSet = true;
                }
                importEnd = offset;
              } else if (t.id() == GroovyTokenId.BLOCK_COMMENT) {
                // does this Block comment (GSF_BLOCK_COMMENT) span
                // multiple lines? E.g. includes \n ?
                StringBuffer sb = new StringBuffer(t.text());

                if (sb.indexOf("\n") != -1) {
                  int offset = ts.offset();
                  commentsRanges.add(new OffsetRange(offset, offset + t.length()));
                }
              }
            }
            try {
              importEnd = Utilities.getRowEnd(doc, importEnd);
              importsRange[0] = new OffsetRange(importStart, importEnd);
            } catch (BadLocationException ble) {
              Exceptions.printStackTrace(ble);
            }
          }
        });

    if (!commentsRanges.isEmpty()) {
      folds.put("comments", commentsRanges); // NOI18N
    }

    try {
      if (importsRange[0] != null
          && Utilities.getRowCount(doc, importsRange[0].getStart(), importsRange[0].getEnd()) > 1) {
        folds.put("imports", Collections.singletonList(importsRange[0])); // NOI18N
      }
      addFolds(doc, analysisResult.getElements(), folds, codefolds);
    } catch (BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    }

    return folds;
  }