/**
  * @return the contents that were obtained from this instance since it was started or since the
  *     last call to this method.
  */
 public String getAndClearContents() {
   synchronized (lock) {
     String string = contents.toString();
     contents.clear();
     return string;
   }
 }
  public void run() {
    try {
      InputStreamReader in;
      if (encoding != null) {
        in = new InputStreamReader(is, encoding);

      } else {
        in = new InputStreamReader(is);
      }
      int c;

      // small buffer because we may want to see contents as it's being written.
      // (still better than char by char).
      char[] buf = new char[80];

      if (synchronize) {
        while ((c = in.read(buf)) != -1) {
          synchronized (lock) {
            contents.append(buf, 0, c);
          }
        }
      } else {
        while ((c = in.read(buf)) != -1) {
          contents.append(buf, 0, c);
        }
      }
    } catch (Exception e) {
      // that's ok
    }
  }
Exemple #3
0
 /** @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char) */
 @Override
 public boolean isWordStart(char c) {
   isInHexa = false;
   buffer.clear();
   buffer.append(c);
   return Character.isDigit(c);
 }
Exemple #4
0
 /**
  * We just want to trim whitespaces, not newlines!
  *
  * @param locBuf the buffer to be trimmed
  * @return the same buffer passed as a parameter
  */
 private FastStringBuffer trim(FastStringBuffer locBuf) {
   while (locBuf.length() > 0 && (locBuf.firstChar() == ' ' || locBuf.firstChar() == '\t')) {
     locBuf.deleteCharAt(0);
   }
   rtrim(locBuf);
   return locBuf;
 }
Exemple #5
0
    /** Check if we are still in the number */
    @Override
    public boolean isWordPart(char c) {
      // ok, we have to test for scientific notation e.g.: 10.9e10

      if ((c == 'x' || c == 'X') && buffer.length() == 1 && buffer.charAt(0) == '0') {
        // it is an hexadecimal
        buffer.append(c);
        isInHexa = true;
        return true;
      } else {
        buffer.append(c);
      }

      if (isInHexa) {
        return Character.isDigit(c)
            || c == 'a'
            || c == 'A'
            || c == 'b'
            || c == 'B'
            || c == 'c'
            || c == 'C'
            || c == 'd'
            || c == 'D'
            || c == 'e'
            || c == 'E'
            || c == 'f'
            || c == 'F';

      } else {
        return Character.isDigit(c) || c == 'e' || c == '.';
      }
    }
 private void checkExpected(int expected) {
   FastStringBuffer buf = new FastStringBuffer(40 * comps.length);
   for (IToken t : comps) {
     buf.append(t.getRepresentation());
     buf.append(", ");
   }
   String msg = "Expected " + expected + ". Found: " + buf.toString();
   assertEquals(msg, expected, comps.length);
 }
 public static String getRunningName(IPath[] paths) {
   FastStringBuffer buf = new FastStringBuffer(20 * paths.length);
   for (IPath p : paths) {
     if (buf.length() > 0) {
       buf.append(" - ");
     }
     buf.append(p.lastSegment());
   }
   return buf.toString();
 }
Exemple #8
0
 @Override
 public String toString() {
   if (file != null) {
     FastStringBuffer ret = new FastStringBuffer(name, 40);
     ret.append(" - ");
     ret.appendObject(file);
     return ret.toString();
   }
   return name;
 }
  /**
   * @param lastMayBeMethod if true, it gets the path and accepts a method (if it is the last in the
   *     stack) if false, null is returned if a method is found.
   * @param tempStack is a temporary stack object (which may be cleared)
   * @return a tuple, where the first element is the path where the entry is located (may return
   *     null). and the second element is a boolean that indicates if the last was actually a method
   *     or not.
   */
  private Tuple<String, Boolean> getPathToRoot(
      ASTEntry entry, boolean lastMayBeMethod, boolean acceptAny, FastStack<SimpleNode> tempStack) {
    if (entry.parent == null) {
      return null;
    }
    // just to be sure that it's empty
    tempStack.clear();

    boolean lastIsMethod = false;
    // if the last 'may be a method', in this case, we have to remember that it will actually be the
    // first one
    // to be analyzed.

    // let's get the stack
    while (entry.parent != null) {
      if (entry.parent.node instanceof ClassDef) {
        tempStack.push(entry.parent.node);

      } else if (entry.parent.node instanceof FunctionDef) {
        if (!acceptAny) {
          if (lastIsMethod) {
            // already found a method
            return null;
          }

          if (!lastMayBeMethod) {
            return null;
          }

          // ok, the last one may be a method... (in this search, it MUST be the first one...)
          if (tempStack.size() != 0) {
            return null;
          }
        }

        // ok, there was a class, so, let's go and set it
        tempStack.push(entry.parent.node);
        lastIsMethod = true;

      } else {
        return null;
      }
      entry = entry.parent;
    }

    // now that we have the stack, let's make it into a path...
    FastStringBuffer buf = new FastStringBuffer();
    while (tempStack.size() > 0) {
      if (buf.length() > 0) {
        buf.append(".");
      }
      buf.append(NodeUtils.getRepresentationString(tempStack.pop()));
    }
    return new Tuple<String, Boolean>(buf.toString(), lastIsMethod);
  }
 /**
  * @param buffer
  * @param name
  */
 private void entrySetToString(FastStringBuffer buffer, Set<Entry<String, Set<IInfo>>> name) {
   synchronized (lock) {
     for (Entry<String, Set<IInfo>> entry : name) {
       Set<IInfo> value = entry.getValue();
       for (IInfo info : value) {
         buffer.append(info.toString());
         buffer.append("\n");
       }
     }
   }
 }
Exemple #11
0
  public static void buildKeysForRegularEntries(
      IProgressMonitor monitor,
      ModulesFoundStructure modulesFound,
      PyPublicTreeMap<ModulesKey, ModulesKey> keys,
      boolean includeOnlySourceModules) {
    String[] dottedValidSourceFiles = FileTypesPreferencesPage.getDottedValidSourceFiles();

    int j = 0;
    FastStringBuffer buffer = new FastStringBuffer();
    // now, create in memory modules for all the loaded files (empty modules).
    for (Iterator<Map.Entry<File, String>> iterator =
            modulesFound.regularModules.entrySet().iterator();
        iterator.hasNext() && monitor.isCanceled() == false;
        j++) {
      Map.Entry<File, String> entry = iterator.next();
      String m = entry.getValue();

      if (m != null) {
        if (j % 20 == 0) {
          // no need to report all the time (that's pretty fast now)
          buffer.clear();
          monitor.setTaskName(buffer.append("Module resolved: ").append(m).toString());
          monitor.worked(1);
        }

        // we don't load them at this time.
        File f = entry.getKey();

        if (includeOnlySourceModules) {
          // check if we should include only source modules
          if (!PythonPathHelper.isValidSourceFile(f.getName())) {
            continue;
          }
        }
        ModulesKey modulesKey = new ModulesKey(m, f);

        // no conflict (easy)
        if (!keys.containsKey(modulesKey)) {
          keys.put(modulesKey, modulesKey);

        } else {
          // we have a conflict, so, let's resolve which one to keep (the old one or this one)
          if (PythonPathHelper.isValidSourceFile(f.getName(), dottedValidSourceFiles)) {
            // source files have priority over other modules (dlls) -- if both are source, there is
            // no real way to resolve
            // this priority, so, let's just add it over.
            keys.put(modulesKey, modulesKey);
          }
        }
      }
    }
  }
Exemple #12
0
 /** Gets the reader content as a String */
 private static String getString(Reader reader) {
   FastStringBuffer buf = new FastStringBuffer();
   char[] buffer = new char[1024];
   int count;
   try {
     while ((count = reader.read(buffer)) != -1) {
       buf.append(buffer, 0, count);
     }
   } catch (IOException e) {
     return null;
   }
   return buf.toString();
 }
  public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
    boolean isZipModule = key instanceof ModulesKeyForZip;
    ModulesKeyForZip modulesKeyForZip = null;
    if (isZipModule) {
      modulesKeyForZip = (ModulesKeyForZip) key;
    }

    Object doc;
    if (isZipModule) {
      doc =
          FileUtilsFileBuffer.getCustomReturnFromZip(
              modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);

    } else {
      doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
    }

    char[] charArray;
    int len;
    if (doc instanceof IDocument) {
      IDocument document = (IDocument) doc;
      charArray = document.get().toCharArray();
      len = charArray.length;

    } else if (doc instanceof FastStringBuffer) {
      FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
      // In this case, we can actually get the internal array without doing any copies (and just
      // specifying the len).
      charArray = fastStringBuffer.getInternalCharsArray();
      len = fastStringBuffer.length();

    } else if (doc instanceof String) {
      String str = (String) doc;
      charArray = str.toCharArray();
      len = charArray.length;

    } else if (doc instanceof char[]) {
      charArray = (char[]) doc;
      len = charArray.length;

    } else {
      throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
    }

    SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len);
    if (node == null) {
      return null;
    }

    return addAstInfo(node, key, generateDelta);
  }
Exemple #14
0
  /**
   * Creates the errors that are related to a bad indentation (number of space chars is not ok).
   *
   * @param monitor
   */
  private static void createBadIndentForSpacesMessages(
      IDocument doc,
      IAnalysisPreferences analysisPrefs,
      IIndentPrefs indentPrefs,
      ArrayList<IMessage> ret,
      List<Tuple3<String, Integer, Boolean>> validsAre,
      IProgressMonitor monitor) {

    int tabWidth = indentPrefs.getTabWidth();
    // if we're analyzing the spaces, let's mark invalid indents (tabs are not searched for those
    // because
    // a tab always marks a full indent).

    FastStringBuffer buffer = new FastStringBuffer();
    for (Tuple3<String, Integer, Boolean> indentation : validsAre) {
      if (monitor.isCanceled()) {
        return;
      }

      if (!indentation
          .o3) { // if it does not have more contents (its only whitespaces), let's keep on going!
        continue;
      }
      String indentStr = indentation.o1;
      if (indentStr.indexOf("\t") != -1) {
        continue; // the ones that appear in tabs and spaces should not be analyzed here (they'll
        // have their own error messages).
      }

      int lenFound = indentStr.length();
      int extraChars = lenFound % tabWidth;
      if (extraChars != 0) {

        Integer offset = indentation.o2;
        int startLine = PySelection.getLineOfOffset(doc, offset) + 1;
        int startCol = 1;
        int endCol = startCol + lenFound;

        buffer.clear();
        ret.add(
            new Message(
                IAnalysisPreferences.TYPE_INDENTATION_PROBLEM,
                buffer.append("Bad Indentation (").append(lenFound).append(" spaces)").toString(),
                startLine,
                startLine,
                startCol,
                endCol,
                analysisPrefs));
      }
    }
  }
  public void run(IAction action) {
    FastStringBuffer buf = new FastStringBuffer();
    try {
      PyEdit pyEdit = getPyEdit();

      PySelection pySelection = new PySelection(pyEdit);

      IPythonNature nature = pyEdit.getPythonNature();
      File editorFile = pyEdit.getEditorFile();

      if (editorFile != null) {
        if (nature != null) {
          String mod = nature.resolveModule(editorFile);
          if (mod != null) {
            buf.append(mod);

          } else {
            // Support for external files (not in PYTHONPATH).
            buf.append(FullRepIterable.getFirstPart(editorFile.getName()));
          }
        } else {
          buf.append(FullRepIterable.getFirstPart(editorFile.getName()));
        }
      }

      List<stmtType> path =
          FastParser.parseToKnowGloballyAccessiblePath(
              pySelection.getDoc(), pySelection.getStartLineIndex());
      for (stmtType stmtType : path) {
        if (buf.length() > 0) {
          buf.append('.');
        }
        buf.append(NodeUtils.getRepresentationString(stmtType));
      }

    } catch (MisconfigurationException e1) {
      Log.log(e1);
      return;
    }

    Transfer[] dataTypes = new Transfer[] {TextTransfer.getInstance()};
    Object[] data = new Object[] {buf.toString()};

    Clipboard clipboard = new Clipboard(EditorUtils.getShell().getDisplay());
    try {
      clipboard.setContents(data, dataTypes);
    } catch (SWTError e) {
      if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
        throw e;
      }
      MessageDialog.openError(
          EditorUtils.getShell(), "Error copying to clipboard.", e.getMessage());
    } finally {
      clipboard.dispose();
    }
  }
  /**
   * The line delimiters must match the platform for the bolds to be correct, so, in this function
   * we remove the ones existing and add the ones dependent on the platform
   */
  protected String correctLineDelimiters(String str) {
    FastStringBuffer buf = new FastStringBuffer();
    for (String s : StringUtils.splitInLines(str)) {

      boolean found = false;
      while (s.endsWith("\r") || s.endsWith("\n")) {
        found = true;
        s = s.substring(0, s.length() - 1);
      }
      buf.append(s);
      if (found) {
        buf.append(LINE_DELIM);
      }
    }
    str = buf.toString();
    return str;
  }
  protected void saveTo(OutputStreamWriter writer, FastStringBuffer tempBuf, File pathToSave)
      throws IOException {
    synchronized (lock) {
      if (DEBUG_ADDITIONAL_INFO) {
        System.out.println(
            "Saving info "
                + this.getClass().getName()
                + " to file (size = "
                + getAllTokens().size()
                + ") "
                + pathToSave);
      }

      Map<String, Integer> dictionary = new HashMap<String, Integer>();
      tempBuf.append("-- START TREE 1\n");
      TreeIO.dumpTreeToBuffer(this.topLevelInitialsToInfo, tempBuf, dictionary);

      tempBuf.append("-- START TREE 2\n");
      TreeIO.dumpTreeToBuffer(this.innerInitialsToInfo, tempBuf, dictionary);

      FastStringBuffer buf2 = new FastStringBuffer(50 * (dictionary.size() + 4));
      TreeIO.dumpDictToBuffer(dictionary, buf2);

      // Write the dictionary before the actual trees.
      writer.write(buf2.getInternalCharsArray(), 0, buf2.length());
      buf2 = null;

      // Note: tried LZFFileInputStream from https://github.com/ning/compress
      // and Snappy from https://github.com/dain/snappy checking to see if by writing less we'd
      // get a better time but it got a bit slower (gzip was slowest, then snappy and the faster was
      // LZFFileInputStream)
      writer.write(tempBuf.getInternalCharsArray(), 0, tempBuf.length());
    }
  }
Exemple #18
0
 public String getLastWord() {
   FastStringBuffer lastWordBuf = new FastStringBuffer(this.count);
   int i;
   // skip whitespaces in the end
   for (i = this.count - 1; i >= 0; i--) {
     if (!Character.isWhitespace(this.value[i])) {
       break;
     }
   }
   // actual word
   for (; i >= 0; i--) {
     if (Character.isWhitespace(this.value[i])) {
       break;
     }
     lastWordBuf.append(this.value[i]);
   }
   lastWordBuf.reverse();
   return lastWordBuf.toString();
 }
Exemple #19
0
  /**
   * When a comma is found, it's formatted accordingly (spaces added after it).
   *
   * @param std the coding standard to be used
   * @param cs the contents of the document to be formatted
   * @param buf the buffer where the comma should be added
   * @param i the current index
   * @return the new index on the original doc.
   */
  private int formatForComma(
      FormatStd std,
      char[] cs,
      FastStringBuffer buf,
      int i,
      FastStringBuffer formatForCommaTempBuf) {
    formatForCommaTempBuf.clear();
    char c = '\0';
    while (i < cs.length - 1 && (c = cs[i + 1]) == ' ') {
      formatForCommaTempBuf.append(c);
      i++;
    }

    if (c == '#') {
      // Ok, we have a comment after a comma, let's handle it according to preferences.
      buf.append(',');
      if (std.spacesBeforeComment == FormatStd.DONT_HANDLE_SPACES) {
        // Note: other cases we won't handle here as it should be handled when the start of
        // a comment is found.
        buf.append(formatForCommaTempBuf);
      }
    } else {
      // Default: handle it as usual.
      if (std.spaceAfterComma) {
        buf.append(", ");
      } else {
        buf.append(',');
      }
    }
    return i;
  }
Exemple #20
0
  /** Dict format is the following: -- START DICTIONARY dictionary size name=integer */
  public static void dumpDictToBuffer(Map<String, Integer> strToInt, FastStringBuffer buf2) {
    Iterator<Entry<String, Integer>> it = strToInt.entrySet().iterator();

    buf2.append("-- START DICTIONARY\n");
    buf2.append(strToInt.size());
    buf2.append('\n');
    while (it.hasNext()) {
      Entry<String, Integer> next = it.next();
      buf2.append(next.getValue());
      buf2.append('=');
      buf2.append(next.getKey());
      buf2.append('\n');
    }
    buf2.append("-- END DICTIONARY\n");
  }
Exemple #21
0
  public static Map<Integer, String> loadDictFrom(
      FastBufferedReader reader, FastStringBuffer buf, ObjectsPoolMap objectsPoolMap)
      throws IOException {
    int size = StringUtils.parsePositiveInt(reader.readLine());
    HashMap<Integer, String> map = new HashMap<Integer, String>(size + 5);

    FastStringBuffer line;
    int val = 0;
    while (true) {
      line = reader.readLine();
      if (line == null) {
        return map;

      } else if (line.startsWith("-- ")) {
        if (line.startsWith("-- END DICTIONARY")) {
          return map;
        }
        throw new RuntimeException("Unexpected line: " + line);

      } else {
        int length = line.length();
        // line is str=int
        for (int i = 0; i < length; i++) {
          char c = line.charAt(i);
          if (c == '=') {
            val = StringUtils.parsePositiveInt(buf);
            buf.clear();
          } else {
            buf.appendResizeOnExc(c);
          }
        }
        String bufStr = buf.toString();
        String interned = objectsPoolMap.get(bufStr);
        if (interned == null) {
          interned = bufStr;
          objectsPoolMap.put(bufStr, bufStr);
        }
        map.put(val, interned);
        buf.clear();
      }
    }
  }
Exemple #22
0
  /**
   * Adds spaces after the '#' according to the configured settings. The first char of the buffer
   * passed (which is also the output) should always start with a '#'.
   */
  public static void formatComment(FormatStd std, FastStringBuffer bufWithComment) {
    if (std.spacesInStartComment > 0) {
      Assert.isTrue(bufWithComment.charAt(0) == '#');
      int len = bufWithComment.length();

      char firstCharFound = '\n';
      String bufAsString = bufWithComment.toString();
      // handle cases where the code-formatting should not take place
      if (FileUtils.isPythonShebangLine(bufAsString)) {
        return;
      }

      int spacesFound = 0;
      String remainingStringContent = "";
      for (int j = 1; j < len; j++) { // start at 1 because 0 should always be '#'
        if ((firstCharFound = bufWithComment.charAt(j)) != ' ') {
          remainingStringContent = bufAsString.substring(j).trim();
          break;
        }
        spacesFound += 1;
      }
      if (firstCharFound != '\r'
          && firstCharFound != '\n') { // Only add spaces if it wasn't an empty line.

        // handle cases where the code-formatting should not take place
        for (String s : BLOCK_COMMENT_SKIPS) {
          if (remainingStringContent.endsWith(s) || remainingStringContent.startsWith(s)) {
            return;
          }
        }
        int diff = std.spacesInStartComment - spacesFound;
        if (diff > 0) {
          bufWithComment.insertN(1, ' ', diff);
        }
      }
    }
  }
  @Override
  public String toString() {
    synchronized (lock) {
      FastStringBuffer buffer = new FastStringBuffer();
      buffer.append("AdditionalInfo{");

      buffer.append("topLevel=[");
      entrySetToString(buffer, this.topLevelInitialsToInfo.entrySet());
      buffer.append("]\n");
      buffer.append("inner=[");
      entrySetToString(buffer, this.innerInitialsToInfo.entrySet());
      buffer.append("]");

      buffer.append("}");
      return buffer.toString();
    }
  }
Exemple #24
0
 /** Inserts a string at a given position in the buffer. */
 public FastStringBuffer insert(int offset, FastStringBuffer str) {
   int len = str.length();
   int newCount = count + len;
   if (newCount > value.length) {
     int newCapacity = (value.length + 1) * 2;
     if (newCount > newCapacity) {
       newCapacity = newCount;
     }
     char newValue[] = new char[newCapacity];
     System.arraycopy(value, 0, newValue, 0, count);
     value = newValue;
   }
   System.arraycopy(value, offset, value, offset + len, count - offset);
   System.arraycopy(str.value, 0, value, offset, str.count);
   count = newCount;
   return this;
 }
  protected void save(File persistingLocation) {
    try {
      FileOutputStream stream = new FileOutputStream(persistingLocation);
      OutputStreamWriter writer = new OutputStreamWriter(stream);
      try {
        FastStringBuffer tempBuf = new FastStringBuffer();
        tempBuf.append("-- VERSION_");
        tempBuf.append(AbstractAdditionalTokensInfo.version);
        tempBuf.append('\n');
        writer.write(tempBuf.getInternalCharsArray(), 0, tempBuf.length());
        tempBuf.clear();

        saveTo(writer, tempBuf, persistingLocation);
      } finally {
        try {
          writer.close();
        } finally {
          stream.close();
        }
      }
    } catch (Exception e) {
      Log.log(e);
    }
  }
Exemple #26
0
  /** Tree is written as: line 1= tree size cub|2|CubeColourDialog!13&999@CUBIC!263@cube!202&999@ */
  public static void dumpTreeToBuffer(
      SortedMap<String, Set<IInfo>> tree, FastStringBuffer tempBuf, Map<String, Integer> strToInt) {
    Set<Entry<String, Set<IInfo>>> entrySet = tree.entrySet();
    Iterator<Entry<String, Set<IInfo>>> it = entrySet.iterator();
    tempBuf.append(entrySet.size());
    tempBuf.append('\n');
    while (it.hasNext()) {
      Entry<String, Set<IInfo>> next = it.next();
      tempBuf.append(next.getKey());
      Set<IInfo> value = next.getValue();

      tempBuf.append('|');
      tempBuf.append(value.size());
      tempBuf.append('|');

      Iterator<IInfo> it2 = value.iterator();
      Integer integer;
      while (it2.hasNext()) {
        IInfo info = it2.next();
        tempBuf.append(info.getName());
        tempBuf.append('!');

        String path = info.getPath();
        if (path != null) {
          integer = strToInt.get(path);
          if (integer == null) {
            integer = strToInt.size() + 1;
            strToInt.put(path, integer);
          }
          tempBuf.append(integer);
          tempBuf.append('&');
        }

        String modName = info.getDeclaringModuleName();

        integer = strToInt.get(modName);
        if (integer == null) {
          integer = strToInt.size() + 1;
          strToInt.put(modName, integer);
        }

        int v = integer << 3;
        v |= info.getType();
        tempBuf.append(v); // Write a single for name+type

        tempBuf.append('@');
      }
      tempBuf.append('\n');
    }
    tempBuf.append("-- END TREE\n");
  }
Exemple #27
0
  public static PyPublicTreeMap<String, Set<IInfo>> loadTreeFrom(
      final FastBufferedReader reader,
      final Map<Integer, String> dictionary,
      FastStringBuffer buf,
      ObjectsPoolMap objectsPoolMap,
      IPythonNature nature)
      throws IOException {
    PyPublicTreeMap<String, Set<IInfo>> tree = new PyPublicTreeMap<String, Set<IInfo>>();
    final int size = StringUtils.parsePositiveInt(reader.readLine());

    try {

      final Entry[] entries = new Entry[size];
      // each line is something as: cub|CubeColourDialog!13&999@CUBIC!263@cube!202&999@
      // note: the path (2nd int in record) is optional
      for (int iEntry = 0; iEntry < size; iEntry++) {
        buf.clear();
        FastStringBuffer line = reader.readLine();
        if (line == null || line.startsWith("-- ")) {
          throw new RuntimeException("Unexpected line: " + line);
        }
        char[] internalCharsArray = line.getInternalCharsArray();
        int length = line.length();
        String key = null;
        String infoName = null;
        String path = null;

        int i = 0;

        OUT:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              key = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              i++;
              break OUT;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        int hashSize = 0;
        OUT2:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              hashSize = StringUtils.parsePositiveInt(buf);
              buf.clear();
              i++;
              break OUT2;
            default:
              buf.appendResizeOnExc(c);
          }
        }
        HashSet<IInfo> set = new HashSet<IInfo>(hashSize);

        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '!':
              infoName = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              break;

            case '&':
              path = dictionary.get(StringUtils.parsePositiveInt(buf));
              buf.clear();
              break;

            case '@':
              int dictKey = StringUtils.parsePositiveInt(buf);
              byte type = (byte) dictKey;
              type &=
                  0x07; // leave only the 3 least significant bits there (this is the type -- value
              // from 0 - 8).

              dictKey =
                  (dictKey
                      >> 3); // the entry in the dict doesn't have the least significant bits there.
              buf.clear();
              String moduleDeclared = dictionary.get(dictKey);
              if (moduleDeclared == null) {
                throw new AssertionError("Unable to find key: " + dictKey);
              }
              if (infoName == null) {
                throw new AssertionError("Info name may not be null. Line: " + line);
              }
              switch (type) {
                case IInfo.CLASS_WITH_IMPORT_TYPE:
                  set.add(new ClassInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.METHOD_WITH_IMPORT_TYPE:
                  set.add(new FuncInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
                  set.add(new AttrInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.NAME_WITH_IMPORT_TYPE:
                  set.add(new NameInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.MOD_IMPORT_TYPE:
                  set.add(new ModInfo(infoName, false, nature));
                  break;
                default:
                  Log.log("Unexpected type: " + type);
              }
              break;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        entries[iEntry] = new MapEntry(key, set);
      }

      tree.buildFromSorted(
          size,
          new Iterator() {
            private int iNext;

            @Override
            public boolean hasNext() {
              return iNext > size;
            }

            @Override
            public Object next() {
              Object o = entries[iNext];
              iNext++;
              return o;
            }

            @Override
            public void remove() {}
          },
          null,
          null);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    return tree;
  }
Exemple #28
0
  public List<IMessage> getMessages(
      SourceModule module,
      IDocument document,
      IProgressMonitor monitor,
      IAnalysisPreferences prefs) {
    try {

      if (prefs.getSeverityForType(IAnalysisPreferences.TYPE_PEP8) < IMarker.SEVERITY_WARNING) {
        return messages;
      }
      this.prefs = prefs;
      this.document = document;
      messageToIgnore = prefs.getRequiredMessageToIgnore(IAnalysisPreferences.TYPE_PEP8);
      File pep8Loc = JythonModules.getPep8Location();

      if (pep8Loc == null) {
        Log.log("Unable to get pep8 module.");
        return messages;
      }

      IAdaptable projectAdaptable = prefs.getProjectAdaptable();
      if (AnalysisPreferencesPage.useSystemInterpreter(projectAdaptable)) {
        String parameters = AnalysisPreferencesPage.getPep8CommandLineAsStr(projectAdaptable);
        String output =
            PyFormatStd.runWithPep8BaseScript(document.get(), parameters, "pep8.py", "");
        List<String> splitInLines = StringUtils.splitInLines(output, false);

        for (String line : splitInLines) {
          try {
            List<String> lst = StringUtils.split(line, ':', 4);
            int lineNumber = Integer.parseInt(lst.get(1));
            int offset = Integer.parseInt(lst.get(2)) - 1;
            String text = lst.get(3);
            this.reportError(lineNumber, offset, text, null);
          } catch (Exception e) {
            Log.log("Error parsing line: " + line, e);
          }
        }
        return messages;
      }

      String[] pep8CommandLine = AnalysisPreferencesPage.getPep8CommandLine(projectAdaptable);
      FastStringBuffer args = new FastStringBuffer(pep8CommandLine.length * 20);
      for (String string : pep8CommandLine) {
        args.append(',').append("r'").append(string).append('\'');
      }

      // It's important that the interpreter is created in the Thread and not outside the thread
      // (otherwise
      // it may be that the output ends up being shared, which is not what we want.)
      boolean useConsole = AnalysisPreferencesPage.useConsole(projectAdaptable);
      IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(useConsole, false);
      String file = StringUtils.replaceAllSlashes(module.getFile().getAbsolutePath());
      interpreter.set("visitor", this);

      List<String> splitInLines = StringUtils.splitInLines(document.get());
      interpreter.set("lines", splitInLines);
      PyObject tempReportError = reportError;
      if (tempReportError != null) {
        interpreter.set("ReportError", tempReportError);
      } else {
        interpreter.set("ReportError", Py.None);
      }
      PyObject pep8Module = JythonModules.getPep8Module(interpreter);
      interpreter.set("pep8", pep8Module);

      String formatted = StringUtils.format(EXECUTE_PEP8, file, args.toString(), file);
      interpreter.exec(formatted);
      if (reportError == null) {
        synchronized (lock) {
          if (reportError == null) {
            reportError = interpreter.get("ReportError");
          }
        }
      }

    } catch (Exception e) {
      Log.log("Error analyzing: " + module, e);
    }

    return messages;
  }
Exemple #29
0
  /**
   * @param operation
   * @return
   * @throws IOException
   */
  private FastStringBuffer read(IProgressMonitor monitor) throws IOException {
    synchronized (ioLock) {
      if (finishedForGood) {
        throw new RuntimeException(
            "Shells are already finished for good, so, it is an invalid state to try to read from it.");
      }
      if (inStart) {
        throw new RuntimeException(
            "The shell is still not completely started, so, it is an invalid state to try to read from it.");
      }
      if (!isConnected) {
        throw new RuntimeException(
            "The shell is still not connected, so, it is an invalid state to try to read from it.");
      }
      if (isInRead) {
        throw new RuntimeException(
            "The shell is already in read mode, so, it is an invalid state to try to read from it.");
      }
      if (isInWrite) {
        throw new RuntimeException(
            "The shell is already in write mode, so, it is an invalid state to try to read from it.");
      }

      isInRead = true;

      try {
        FastStringBuffer str = new FastStringBuffer(AbstractShell.BUFFER_SIZE);
        byte[] b = new byte[AbstractShell.BUFFER_SIZE];
        while (true) {

          int len = this.socket.getInputStream().read(b);
          if (len == 0) {
            break;
          }

          String s = new String(b, 0, len);
          str.append(s);

          if (str.indexOf("END@@") != -1) {
            break;
          } else {
            sleepALittle(10);
          }
        }

        str.replaceFirst("@@COMPLETIONS", "");
        // remove END@@
        try {
          if (str.indexOf("END@@") != -1) {
            str.setCount(str.indexOf("END@@"));
            return str;
          } else {
            throw new RuntimeException("Couldn't find END@@ on received string.");
          }
        } catch (RuntimeException e) {
          if (str.length() > 500) {
            str.setCount(499)
                .append("...(continued)..."); // if the string gets too big, it can crash Eclipse...
          }
          Log.log(IStatus.ERROR, ("ERROR WITH STRING:" + str), e);
          return new FastStringBuffer();
        }
      } finally {
        isInRead = false;
      }
    }
  }
Exemple #30
0
  /**
   * copied from org.eclipse.jdt.internal.launching.StandardVMRunner
   *
   * @param args - other arguments to be added to the command line (may be null)
   * @return
   */
  public static String getArgumentsAsStr(String[] commandLine, String... args) {
    if (args != null && args.length > 0) {
      String[] newCommandLine = new String[commandLine.length + args.length];
      System.arraycopy(commandLine, 0, newCommandLine, 0, commandLine.length);
      System.arraycopy(args, 0, newCommandLine, commandLine.length, args.length);
      commandLine = newCommandLine;
    }

    if (commandLine.length < 1) return ""; // $NON-NLS-1$
    FastStringBuffer buf = new FastStringBuffer();
    FastStringBuffer command = new FastStringBuffer();
    for (int i = 0; i < commandLine.length; i++) {
      if (commandLine[i] == null) {
        continue; // ignore nulls (changed from original code)
      }

      buf.append(' ');
      char[] characters = commandLine[i].toCharArray();
      command.clear();
      boolean containsSpace = false;
      for (int j = 0; j < characters.length; j++) {
        char character = characters[j];
        if (character == '\"') {
          command.append('\\');
        } else if (character == ' ') {
          containsSpace = true;
        }
        command.append(character);
      }
      if (containsSpace) {
        buf.append('\"');
        buf.append(command.toString());
        buf.append('\"');
      } else {
        buf.append(command.toString());
      }
    }
    return buf.toString();
  }