Esempio n. 1
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;
  }
Esempio n. 2
0
 /**
  * @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;
   }
 }
Esempio n. 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);
 }
Esempio n. 4
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();
      }
    }
  }
Esempio n. 5
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);
          }
        }
      }
    }
  }
Esempio n. 6
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));
      }
    }
  }
Esempio n. 7
0
  /** Handles the case where we found a '#' in the code. */
  private int handleComment(
      FormatStd std,
      char[] cs,
      FastStringBuffer buf,
      FastStringBuffer tempBuf,
      ParsingUtils parsingUtils,
      int i) {
    if (std.spacesBeforeComment != FormatStd.DONT_HANDLE_SPACES) {
      for (int j = i - 1; j >= 0; j--) {
        char cj = cs[j];
        if (cj == '\t' || cj == ' ') {
          continue;
        }
        // Ok, found a non-whitespace -- if it's not a new line, we're after some
        // code, in which case we have to put the configured amount of spaces.
        if (cj != '\r' && cj != '\n') {
          buf.rightTrim();
          buf.appendN(' ', std.spacesBeforeComment);
        }
        break;
      }
    }

    tempBuf.clear();
    i = parsingUtils.eatComments(tempBuf, i);
    if (std.trimLines) {
      String endLine = "";
      if (tempBuf.endsWith("\r\n")) {
        endLine = "\r\n";
        tempBuf.deleteLastChars(2);
      } else if (tempBuf.endsWith('\r') || tempBuf.endsWith('\n')) {
        endLine += tempBuf.lastChar();
        tempBuf.deleteLast();
      }
      tempBuf.rightTrim();
      tempBuf.append(endLine);
    }

    formatComment(std, tempBuf);

    buf.append(tempBuf);
    return i;
  }
Esempio n. 8
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();
  }
  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);
    }
  }
Esempio n. 10
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;
  }
Esempio n. 11
0
  /**
   * This method formats a string given some standard.
   *
   * @param str the string to be formatted
   * @param std the standard to be used
   * @param parensLevel the level of the parenthesis available.
   * @return a new (formatted) string
   * @throws SyntaxErrorException
   */
  private String formatStr(
      String str, FormatStd std, int parensLevel, String delimiter, boolean throwSyntaxError)
      throws SyntaxErrorException {
    char[] cs = str.toCharArray();
    FastStringBuffer buf = new FastStringBuffer();

    // Temporary buffer for some operations. Must always be cleared before it's used.
    FastStringBuffer tempBuf = new FastStringBuffer();

    ParsingUtils parsingUtils = ParsingUtils.create(cs, throwSyntaxError);
    char lastChar = '\0';
    for (int i = 0; i < cs.length; i++) {
      char c = cs[i];

      switch (c) {
        case '\'':
        case '"':
          // ignore literals and multi-line literals, including comments...
          i = parsingUtils.eatLiterals(buf, i, std.trimMultilineLiterals);
          break;

        case '#':
          i = handleComment(std, cs, buf, tempBuf, parsingUtils, i);
          break;

        case ',':
          i = formatForComma(std, cs, buf, i, tempBuf);
          break;

        case '(':
          i =
              formatForPar(
                  parsingUtils, cs, i, std, buf, parensLevel + 1, delimiter, throwSyntaxError);
          break;

          // Things to treat:
          // +, -, *, /, %
          // ** // << >>
          // <, >, !=, <>, <=, >=, //=, *=, /=,
          // & ^ ~ |
        case '*':
          // for *, we also need to treat when it's used in varargs, kwargs and list expansion
          boolean isOperator = false;
          for (int j = buf.length() - 1; j >= 0; j--) {
            char localC = buf.charAt(j);
            if (Character.isWhitespace(localC)) {
              continue;
            }
            if (localC == '(' || localC == ',') {
              // it's not an operator, but vararg. kwarg or list expansion
            }
            if (Character.isJavaIdentifierPart(localC)) {
              // ok, there's a chance that it can be an operator, but we still have to check
              // the chance that it's a wild import
              tempBuf.clear();
              while (Character.isJavaIdentifierPart(localC)) {
                tempBuf.append(localC);
                j--;
                if (j < 0) {
                  break; // break while
                }
                localC = buf.charAt(j);
              }
              String reversed = tempBuf.reverse().toString();
              if (!reversed.equals("import") && !reversed.equals("lambda")) {
                isOperator = true;
              }
            }
            if (localC == '\'' || localC == ')' || localC == ']') {
              isOperator = true;
            }

            // If it got here (i.e.: not whitespace), get out of the for loop.
            break;
          }
          if (!isOperator) {
            buf.append('*');
            break; // break switch
          }
          // Otherwise, FALLTHROUGH

        case '+':
        case '-':
          if (c == '-' || c == '+') { // could also be *

            // handle exponentials correctly: e.g.: 1e-6 cannot have a space
            tempBuf.clear();
            boolean started = false;

            for (int j = buf.length() - 1; ; j--) {
              if (j < 0) {
                break;
              }
              char localC = buf.charAt(j);
              if (localC == ' ' || localC == '\t') {
                if (!started) {
                  continue;
                } else {
                  break;
                }
              }
              started = true;
              if (Character.isJavaIdentifierPart(localC) || localC == '.') {
                tempBuf.append(localC);
              } else {
                break; // break for
              }
            }
            boolean isExponential = true;
            String partialNumber = tempBuf.reverse().toString();
            int partialLen = partialNumber.length();
            if (partialLen < 2 || !Character.isDigit(partialNumber.charAt(0))) {
              // at least 2 chars: the number and the 'e'
              isExponential = false;
            } else {
              // first char checked... now, if the last is an 'e', we must leave it together no
              // matter what
              if (partialNumber.charAt(partialLen - 1) != 'e'
                  && partialNumber.charAt(partialLen - 1) != 'E') {
                isExponential = false;
              }
            }
            if (isExponential) {
              buf.rightTrim();
              buf.append(c);
              // skip the next whitespaces from the buffer
              int initial = i;
              do {
                i++;
              } while (i < cs.length && (c = cs[i]) == ' ' || c == '\t');
              if (i > initial) {
                i--; // backup 1 because we walked 1 too much.
              }
              break; // break switch
            }
            // Otherwise, FALLTHROUGH
          }

        case '/':
        case '%':
        case '<':
        case '>':
        case '!':
        case '&':
        case '^':
        case '~':
        case '|':
          i = handleOperator(std, cs, buf, parsingUtils, i, c);
          c = cs[i];
          break;

          // check for = and == (other cases that have an = as the operator should already be
          // treated)
        case '=':
          if (i < cs.length - 1 && cs[i + 1] == '=') {
            // if == handle as if a regular operator
            i = handleOperator(std, cs, buf, parsingUtils, i, c);
            c = cs[i];
            break;
          }

          while (buf.length() > 0 && buf.lastChar() == ' ') {
            buf.deleteLast();
          }

          boolean surroundWithSpaces = std.operatorsWithSpace;
          if (parensLevel > 0) {
            surroundWithSpaces = std.assignWithSpaceInsideParens;
          }

          // add space before
          if (surroundWithSpaces) {
            buf.append(' ');
          }

          // add the operator and the '='
          buf.append('=');

          // add space after
          if (surroundWithSpaces) {
            buf.append(' ');
          }

          i = parsingUtils.eatWhitespaces(null, i + 1);
          break;

        default:
          if (c == '\r' || c == '\n') {
            if (lastChar == ',' && std.spaceAfterComma && buf.lastChar() == ' ') {
              buf.deleteLast();
            }
            if (std.trimLines) {
              buf.rightTrim();
            }
          }
          buf.append(c);
      }
      lastChar = c;
    }
    if (parensLevel == 0 && std.trimLines) {
      buf.rightTrim();
    }
    return buf.toString();
  }