Beispiel #1
0
  /**
   * @return a map with the messages separated by type (keys are the type)
   *     <p>the values are guaranteed to have size at least equal to 1
   */
  private Map<Integer, List<IMessage>> getMessagesByType(List<IMessage> l) {
    HashMap<Integer, List<IMessage>> messagesByType = new HashMap<Integer, List<IMessage>>();
    for (IMessage message : l) {

      List<IMessage> messages = messagesByType.get(message.getType());
      if (messages == null) {
        messages = new ArrayList<IMessage>();
        messagesByType.put(message.getType(), messages);
      }
      messages.add(message);
    }
    return messagesByType;
  }
Beispiel #2
0
 /**
  * @param result
  * @param message
  */
 private void addToResult(List<IMessage> result, IMessage message) {
   if (isUnusedImportMessage(message.getType())) {
     IToken generator = message.getGenerator();
     if (generator instanceof SourceToken) {
       String asAbsoluteImport = generator.getAsAbsoluteImport();
       if (asAbsoluteImport.indexOf("__future__.") != -1
           || asAbsoluteImport.indexOf("__metaclass__") != -1) {
         // do not add from __future__ import xxx
         return;
       }
     }
   }
   result.add(message);
 }
Beispiel #3
0
  public void testInconsistentIndent2() throws Exception {
    Document doc = new Document("" + "def m(b):\n" + "   pass\n" + "\n" + "");

    List<IMessage> messages =
        TabNanny.analyzeDoc(
            doc, this.prefs, "", new TestIndentPrefs(true, 4), new NullProgressMonitor());
    assertEquals(1, messages.size());
    IMessage m = messages.get(0);
    assertEquals("Bad Indentation (3 spaces)", m.getMessage());
    assertEquals(2, m.getStartLine(null));
    assertEquals(2, m.getEndLine(null));
    assertEquals(1, m.getStartCol(null));
    assertEquals(4, m.getEndCol(null));
  }
Beispiel #4
0
  public void testTabErrors1() throws Exception {
    Document doc = new Document("" + "aaa\n" + "\t\n" + "    \n" + "    \t\n" + "ccc\n" + "");

    List<IMessage> messages =
        TabNanny.analyzeDoc(
            doc, this.prefs, "", new TestIndentPrefs(true, 4), new NullProgressMonitor());
    for (IMessage m : messages) {
      assertEquals("Mixed Indentation: Tab found", m.getMessage());
      int startLine = m.getStartLine(null);

      if (startLine == 2) {
        assertEquals(1, m.getStartCol(null));
        assertEquals(2, m.getEndCol(null));

      } else if (startLine == 4) {
        assertEquals(5, m.getStartCol(null));
        assertEquals(6, m.getEndCol(null));

      } else {
        throw new RuntimeException("Unexpected line:" + startLine);
      }
    }
    assertEquals(2, messages.size());
  }
Beispiel #5
0
  /** @return the generated messages. */
  public List<IMessage> getMessages() {

    List<IMessage> result = new ArrayList<IMessage>();

    // let's get the messages
    for (List<IMessage> l : messages.values()) {
      if (l.size() < 1) {
        // we need at least one message
        continue;
      }

      Map<Integer, List<IMessage>> messagesByType = getMessagesByType(l);
      for (int type : messagesByType.keySet()) {
        l = messagesByType.get(type);

        // the values are guaranteed to have size at least equal to 1
        IMessage message = l.get(0);

        // messages are grouped by type, and the severity is set by type, so, this is ok...
        if (message.getSeverity() == IMarker.SEVERITY_INFO) {
          if (doIgnoreMessageIfJustInformational(message.getType())) {
            // ok, let's ignore it for real (and don't add it) as those are not likely to be
            // used anyways for other actions)
            continue;
          }
        }
        // we add even ignore messages because they might be used later in actions dependent on code
        // analysis

        if (l.size() == 1) {
          // don't add additional info: not being used
          //                    addAdditionalInfoToUnusedWildImport(message);
          addToResult(result, message);

        } else {
          // the generator token has many associated messages - the messages may have different
          // types,
          // so, we need to get them by types
          CompositeMessage compositeMessage =
              new CompositeMessage(message.getType(), message.getGenerator(), prefs);
          for (IMessage m : l) {
            compositeMessage.addMessage(m);
          }

          // don't add additional info: not being used
          //                    addAdditionalInfoToUnusedWildImport(compositeMessage);
          addToResult(result, compositeMessage);
        }
      }
    }

    for (IMessage message : independentMessages) {
      if (message.getSeverity() == IMarker.SEVERITY_INFO) {
        if (doIgnoreMessageIfJustInformational(message.getType())) {
          // ok, let's ignore it for real (and don't add it) as those are not likely to be
          // used anyways for other actions)
          continue;
        }
        // otherwise keep on and add it (needed for some actions)
      }

      addToResult(result, message);
    }

    return result;
  }