Exemplo n.º 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;
      }
    }
Exemplo n.º 2
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);
  }