public boolean spansOffset(Node node, int offset) {
   if (node == null || node.getPosition() == null) {
     return false;
   }
   return node.getPosition().getStartOffset() <= offset
       && node.getPosition().getEndOffset() > offset;
 }
예제 #2
0
  public static List<Node> getArgumentNodesFromFunctionCall(IArgumentNode iVisited) {
    List<Node> arguments = new ArrayList<Node>();
    Node argsNode = iVisited.getArgsNode();

    Iterator<Node> iter = null;
    if (argsNode != null) {
      iter = argsNode.childNodes().iterator();
    } else {
      // block?
      Node iterNode = null;
      if (iVisited instanceof FCallNode) {
        iterNode = ((FCallNode) iVisited).getIterNode();
      } else if (iVisited instanceof CallNode) {
        iterNode = ((CallNode) iVisited).getIterNode();
      }

      if (iterNode == null) {
        return arguments;
      }
      if (iterNode instanceof IterNode) {
        // yup, it has a block
        IterNode yeah = (IterNode) iterNode;
        Node varNode = yeah.getVarNode();
        if (varNode instanceof DAsgnNode) {
          // single variable in the block
          arguments.add(varNode);
        } else if (varNode instanceof MultipleAsgnNode) {
          // multiple variables in the block
          MultipleAsgnNode multi = (MultipleAsgnNode) varNode;
          ListNode list = multi.getHeadNode();
          if (list != null) {
            iter = list.childNodes().iterator();
          } else {
            Node multiArgsNode = multi.getArgsNode();
            if (multiArgsNode instanceof DAsgnNode) {
              arguments.add(multiArgsNode);
            }
          }
        }
      }
    }
    if (iter == null) {
      return arguments;
    }
    while (iter.hasNext()) {
      arguments.add(iter.next());
    }
    return arguments;
  }
예제 #3
0
 public static String getStringRepresentation(Node node) {
   if (node == null) {
     return ""; //$NON-NLS-1$
   }
   if (node instanceof HashNode) {
     return "{}"; //$NON-NLS-1$
   }
   if (node instanceof SelfNode) {
     return "self"; //$NON-NLS-1$
   }
   if (node instanceof NilNode) {
     return "nil"; //$NON-NLS-1$
   }
   if (node instanceof TrueNode) {
     return "true"; //$NON-NLS-1$
   }
   if (node instanceof FalseNode) {
     return "false"; //$NON-NLS-1$
   }
   if (node instanceof SymbolNode) {
     return ':' + ((SymbolNode) node).getName();
   }
   if (node instanceof INameNode) {
     return ((INameNode) node).getName();
   }
   if (node instanceof ZArrayNode) {
     return "[]"; //$NON-NLS-1$
   }
   if (node instanceof FixnumNode) {
     return String.valueOf(((FixnumNode) node).getValue());
   }
   if (node instanceof StrNode) {
     return '"' + ((StrNode) node).getValue().toString() + '"';
   }
   if (node instanceof DStrNode) {
     List<Node> children = node.childNodes();
     StringBuilder text = new StringBuilder();
     text.append("\""); // $NON-NLS-1$
     for (Node child : children) {
       text.append(getStringRepresentation(child));
     }
     text.append("\""); // $NON-NLS-1$
     return text.toString();
   }
   return node.toString();
 }
예제 #4
0
 public static String getName(Node node) {
   if (node == null) {
     return EMPTY_STRING;
   }
   if (node instanceof ClassNode) {
     return getName(((ClassNode) node).getCPath());
   }
   if (node instanceof ModuleNode) {
     return getName(((ModuleNode) node).getCPath());
   }
   if (node instanceof INameNode) {
     return ((INameNode) node).getName();
   }
   // tries reflection
   try {
     Method getNameMethod = node.getClass().getMethod("getName", new Class[] {}); // $NON-NLS-1$
     Object name = getNameMethod.invoke(node, new Object[0]);
     return (String) name;
   } catch (Exception e) {
     return EMPTY_STRING;
   }
 }
 protected int spanLength(Node node) {
   if (node == null || node.getPosition() == null) {
     return 0;
   }
   return node.getPosition().getEndOffset() - node.getPosition().getStartOffset();
 }
예제 #6
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);
  }