예제 #1
0
    public EditList getEditList() {
      try {
        BaseDocument doc = context.doc;

        Node bodyNode = ifNode.getThenBody();
        boolean isIf = bodyNode != null;
        if (bodyNode == null) {
          bodyNode = ifNode.getElseBody();
        }
        ParserResult info = context.parserResult;
        OffsetRange bodyRange = AstUtilities.getRange(bodyNode);
        bodyRange = LexUtilities.getLexerOffsets(info, bodyRange);
        if (bodyRange == OffsetRange.NONE) {
          return null;
        }

        String body = doc.getText(bodyRange.getStart(), bodyRange.getLength()).trim();
        if (body.endsWith(";")) {
          body = body.substring(0, body.length() - 1);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(body);
        sb.append(" ");
        sb.append(isIf ? "if" : "unless"); // NOI18N
        sb.append(" ");
        OffsetRange range = AstUtilities.getRange(ifNode.getCondition());
        range = LexUtilities.getLexerOffsets(info, range);
        if (range == OffsetRange.NONE) {
          return null;
        }
        sb.append(doc.getText(range.getStart(), range.getLength()));

        OffsetRange ifRange = AstUtilities.getRange(ifNode);
        ifRange = LexUtilities.getLexerOffsets(info, ifRange);
        if (ifRange == OffsetRange.NONE) {
          return null;
        }

        return new EditList(doc)
            .replace(ifRange.getStart(), ifRange.getLength(), sb.toString(), false, 0);
      } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        return null;
      }
    }
예제 #2
0
  /**
   * Locates AnnotateLine associated with given line. The line is translated to Element that is used
   * as map lookup key. The map is initially filled up with Elements sampled on annotate() method.
   *
   * <p>Key trick is that Element's identity is maintained until line removal (and is restored on
   * undo).
   *
   * @param line
   * @return found AnnotateLine or <code>null</code>
   */
  private AnnotateLine getAnnotateLine(int line) {
    StyledDocument sd = (StyledDocument) doc;
    int lineOffset = NbDocument.findLineOffset(sd, line);
    Element element = sd.getParagraphElement(lineOffset);
    AnnotateLine al = elementAnnotations.get(element);

    if (al != null) {
      int startOffset = element.getStartOffset();
      int endOffset = element.getEndOffset();
      try {
        int len = endOffset - startOffset;
        String text = doc.getText(startOffset, len - 1);
        String content = al.getContent();
        if (text.equals(content)) {
          return al;
        }
      } catch (BadLocationException e) {
        Mercurial.LOG.log(Level.INFO, "HG.AB: can not locate line annotation."); // NOI18N
      }
    }

    return null;
  }
예제 #3
0
  public void run(RubyRuleContext context, List<Hint> result) {
    Node node = context.node;
    ParserResult info = context.parserResult;

    IfNode ifNode = (IfNode) node;
    if (ifNode.getCondition() == null) {
      // Can happen for this code:
      //   if ()
      //   end
      // (typically while editing)
      return;
    }
    Node body = ifNode.getThenBody();
    Node elseNode = ifNode.getElseBody();

    if (body != null && elseNode != null) {
      // Can't convert if-then-else conditionals
      return;
    }

    if (body == null && elseNode == null) {
      // Can't convert empty conditions
      return;
    }

    // Can't convert if !x/elseif blocks
    if (ifNode.getElseBody() != null && ifNode.getElseBody().getNodeType() == NodeType.IFNODE) {
      return;
    }

    int start = ifNode.getPosition().getStartOffset();
    if (!RubyHints.isNullOrInvisible(body)
        && (
        // Can't convert blocks with multiple statements
        body.getNodeType() == NodeType.BLOCKNODE
            ||
            // Already a statement modifier?
            body.getPosition().getStartOffset() <= start)) {
      return;
    } else if (!RubyHints.isNullOrInvisible(elseNode)
        && (elseNode.getNodeType() == NodeType.BLOCKNODE
            || elseNode.getPosition().getStartOffset() <= start)) {
      return;
    }

    BaseDocument doc = context.doc;
    try {
      int keywordOffset = ConvertIfToUnless.findKeywordOffset(context, ifNode);
      if (keywordOffset == -1 || keywordOffset > doc.getLength() - 1) {
        return;
      }

      char k = doc.getText(keywordOffset, 1).charAt(0);
      if (!(k == 'i' || k == 'u')) {
        return; // Probably ternary operator, ?:
      }
    } catch (BadLocationException ble) {
      Exceptions.printStackTrace(ble);
    }

    // If statement that is not already a statement modifier
    OffsetRange range = AstUtilities.getRange(node);

    if (RubyUtils.isRhtmlDocument(doc) || RubyUtils.isYamlDocument(doc)) {
      // Make sure that we're in a single contiguous Ruby section; if not, this won't work
      range = LexUtilities.getLexerOffsets(info, range);
      if (range == OffsetRange.NONE) {
        return;
      }

      try {
        doc.readLock();
        TokenHierarchy th = TokenHierarchy.get(doc);
        TokenSequence ts = th.tokenSequence();
        ts.move(range.getStart());
        if (!ts.moveNext() && !ts.movePrevious()) {
          return;
        }

        if (ts.offset() + ts.token().length() < range.getEnd()) {
          return;
        }
      } finally {
        doc.readUnlock();
      }
    }

    ConvertToModifier fix = new ConvertToModifier(context, ifNode);

    if (fix.getEditList() == null) {
      return;
    }

    List<HintFix> fixes = Collections.<HintFix>singletonList(fix);

    String displayName = NbBundle.getMessage(ConvertConditionals.class, "ConvertConditionals");
    Hint desc = new Hint(this, displayName, RubyUtils.getFileObject(info), range, fixes, 500);
    result.add(desc);
  }