コード例 #1
0
ファイル: PyCodeScanner.java プロジェクト: rolaechea/Pydev
    /** Check if we are still in the number */
    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 == '.';
      }
    }
コード例 #2
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
 /**
  * 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;
 }
コード例 #3
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
 private void rightTrimIfNeeded(FormatStd std, FastStringBuffer buf) {
   if (std.trimLines) {
     char tempC;
     while (buf.length() > 0 && ((tempC = buf.lastChar()) == ' ' || tempC == '\t')) {
       buf.deleteLast();
     }
   }
 }
コード例 #4
0
  /**
   * @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 boolen 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);
  }
コード例 #5
0
 /**
  * @param buffer
  * @param name
  */
 private void entrySetToString(FastStringBuffer buffer, Set<Entry<String, List<IInfo>>> name) {
   synchronized (lock) {
     for (Entry<String, List<IInfo>> entry : name) {
       List<IInfo> value = entry.getValue();
       for (IInfo info : value) {
         buffer.append(info.toString());
         buffer.append("\n");
       }
     }
   }
 }
コード例 #6
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
  /**
   * 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) {
    while (i < cs.length - 1 && (cs[i + 1]) == ' ') {
      i++;
    }

    if (std.spaceAfterComma) {
      buf.append(", ");
    } else {
      buf.append(',');
    }
    return i;
  }
コード例 #7
0
  /** ThreadRun event processing */
  private void processThreadRun(String payload) {
    try {
      Tuple<String, String> threadIdAndReason = getThreadIdAndReason(payload);
      int resumeReason = DebugEvent.UNSPECIFIED;
      try {
        int raw_reason = Integer.parseInt(threadIdAndReason.o2);
        if (raw_reason == AbstractDebuggerCommand.CMD_STEP_OVER)
          resumeReason = DebugEvent.STEP_OVER;
        else if (raw_reason == AbstractDebuggerCommand.CMD_STEP_RETURN)
          resumeReason = DebugEvent.STEP_RETURN;
        else if (raw_reason == AbstractDebuggerCommand.CMD_STEP_INTO)
          resumeReason = DebugEvent.STEP_INTO;
        else if (raw_reason == AbstractDebuggerCommand.CMD_RUN_TO_LINE)
          resumeReason = DebugEvent.UNSPECIFIED;
        else if (raw_reason == AbstractDebuggerCommand.CMD_SET_NEXT_STATEMENT)
          resumeReason = DebugEvent.UNSPECIFIED;
        else if (raw_reason == AbstractDebuggerCommand.CMD_THREAD_RUN)
          resumeReason = DebugEvent.CLIENT_REQUEST;
        else {
          PydevDebugPlugin.log(IStatus.ERROR, "Unexpected resume reason code", null);
          resumeReason = DebugEvent.UNSPECIFIED;
        }
      } catch (NumberFormatException e) {
        // expected, when pydevd reports "None"
        resumeReason = DebugEvent.UNSPECIFIED;
      }

      String threadID = threadIdAndReason.o1;
      PyThread t = (PyThread) findThreadByID(threadID);
      if (t != null) {
        t.setSuspended(false, null);
        fireEvent(new DebugEvent(t, DebugEvent.RESUME, resumeReason));

      } else {
        FastStringBuffer buf = new FastStringBuffer();
        for (PyThread thread : threads) {
          if (buf.length() > 0) {
            buf.append(", ");
          }
          buf.append("id: " + thread.getId());
        }
        String msg = "Unable to find thread: " + threadID + " available: " + buf;
        PydevDebugPlugin.log(IStatus.ERROR, msg, new RuntimeException(msg));
      }
    } catch (CoreException e1) {
      Log.log(e1);
    }
  }
コード例 #8
0
  /**
   * Recalculates indent prefixes based upon preferences
   *
   * <p>we hold onto the same array SourceViewer has, and write directly into it. This is because
   * there is no way to tell SourceViewer that indent prefixes have changed. And we need this
   * functionality when user resets the tabs vs. spaces preference
   */
  public void resetIndentPrefixes() {
    IPreferenceStore prefs = PydevPlugin.getDefault().getPreferenceStore();
    int tabWidth = DefaultIndentPrefs.getStaticTabWidth();
    FastStringBuffer spaces = new FastStringBuffer(8);

    for (int i = 0; i < tabWidth; i++) {
      spaces.append(" ");
    }

    boolean spacesFirst =
        prefs.getBoolean(PydevEditorPrefs.SUBSTITUTE_TABS)
            && !(getPyAutoIndentStrategy()).getIndentPrefs().getForceTabs();

    if (spacesFirst) {
      indentPrefixes[0] = spaces.toString();
      indentPrefixes[1] = "\t";
    } else {
      indentPrefixes[0] = "\t";
      indentPrefixes[1] = spaces.toString();
    }
  }
コード例 #9
0
ファイル: PyToggleComment.java プロジェクト: unnch/spell-sat
  @Override
  public Tuple<Integer, Integer> perform(final PySelection ps) throws BadLocationException {
    ps.selectCompleteLine();

    final boolean shouldAddCommentSign = PyToggleComment.allLinesStartWithCommentSign(ps) == false;
    String endLineDelim = ps.getEndLineDelim();
    int endLineIndex = ps.getEndLineIndex();
    int startLineIndex = ps.getStartLineIndex();

    final FastStringBuffer sb =
        new FastStringBuffer(ps.getSelLength() + (endLineIndex - startLineIndex) + 10);

    for (int i = startLineIndex, n = endLineIndex; i <= n; i++) {
      final String line = ps.getLine(i);
      if (shouldAddCommentSign) {
        sb.append("#");
        sb.append(line);
      } else { // remove comment sign
        sb.append(line.replaceFirst("#", ""));
      }
      // add a new line if we're not in the last line.
      sb.append((i < endLineIndex ? endLineDelim : ""));
    }

    final int start = ps.getStartLine().getOffset();
    final String replacement = sb.toString();

    ps.getDoc().replace(start, ps.getSelLength(), replacement);
    return new Tuple<Integer, Integer>(start, replacement.length());
  }
コード例 #10
0
  @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();
    }
  }
コード例 #11
0
 public String toString() {
   FastStringBuffer buf = new FastStringBuffer();
   buf.append(
       FullRepIterable.getLastPart(
           super
               .toString())); // something as org.eclipse.ui.internal.WorkingSet@2813 will become
                              // WorkingSet@2813
   buf.append(" (");
   buf.append(this.getActualObject().toString());
   buf.append(")");
   return buf.toString();
 }
コード例 #12
0
  private static String getQuotedString(char quote, String string) {
    // Implemented using SimpleRunner.getArgumentsAsStr as a starting point
    if (string == null || string.length() == 0) return "" + quote + quote;
    FastStringBuffer buf = new FastStringBuffer();

    buf.append(quote);
    char[] characters = string.toCharArray();
    for (int j = 0; j < characters.length; j++) {
      char character = characters[j];
      if (character == quote) {
        buf.append('\\');
      } else if (character == '\\') {
        buf.append('\\');
      }
      buf.append(character);
    }

    buf.append(quote);

    return buf.toString();
  }
コード例 #13
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
 /**
  * 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 rtrim(FastStringBuffer locBuf) {
   while (locBuf.length() > 0 && (locBuf.lastChar() == ' ' || locBuf.lastChar() == '\t')) {
     locBuf.deleteLast();
   }
   return locBuf;
 }
コード例 #14
0
ファイル: PyCodeScanner.java プロジェクト: rolaechea/Pydev
 /** @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char) */
 public boolean isWordStart(char c) {
   isInHexa = false;
   buffer.clear();
   buffer.append(c);
   return Character.isDigit(c);
 }
コード例 #15
0
ファイル: CtxParticipant.java プロジェクト: aparo/Pydev
  private void fillNatureCompletionsForConsole(
      IScriptConsoleViewer viewer,
      int requestOffset,
      List<ICompletionProposal> completions,
      String qual,
      boolean addAutoImport,
      int qlen,
      String lowerQual,
      IPythonNature nature,
      boolean getSystem) {
    AbstractAdditionalInterpreterInfo additionalInfoForProject;

    if (getSystem) {
      try {
        additionalInfoForProject =
            AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(
                PydevPlugin.getInterpreterManager(nature),
                nature.getProjectInterpreter().getExecutableOrJar());
      } catch (Exception e) {
        PydevPlugin.log(e);
        return;
      }
    } else {
      try {
        additionalInfoForProject =
            AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
      } catch (Exception e) {
        PydevPlugin.log(e);
        return;
      }
    }

    List<IInfo> tokensStartingWith =
        additionalInfoForProject.getTokensStartingWith(
            qual, AbstractAdditionalInterpreterInfo.TOP_LEVEL);

    FastStringBuffer realImportRep = new FastStringBuffer();
    FastStringBuffer displayString = new FastStringBuffer();
    FastStringBuffer tempBuf = new FastStringBuffer();
    for (IInfo info : tokensStartingWith) {
      // there always must be a declaringModuleName
      String declaringModuleName = info.getDeclaringModuleName();
      boolean hasInit = false;
      if (declaringModuleName.endsWith(".__init__")) {
        declaringModuleName =
            declaringModuleName.substring(
                0, declaringModuleName.length() - 9); // remove the .__init__
        hasInit = true;
      }

      String rep = info.getName();
      String lowerRep = rep.toLowerCase();
      if (!lowerRep.startsWith(lowerQual)) {
        continue;
      }

      if (addAutoImport) {
        realImportRep.clear();
        realImportRep.append("from ");
        realImportRep.append(
            AutoImportsPreferencesPage.removeImportsStartingWithUnderIfNeeded(
                declaringModuleName, tempBuf));
        realImportRep.append(" import ");
        realImportRep.append(rep);
      }

      displayString.clear();
      displayString.append(rep);
      displayString.append(" - ");
      displayString.append(declaringModuleName);
      if (hasInit) {
        displayString.append(".__init__");
      }

      PyConsoleCompletion proposal =
          new PyConsoleCompletion(
              rep,
              requestOffset - qlen,
              qlen,
              realImportRep.length(),
              AnalysisPlugin.getImageForAutoImportTypeInfo(info),
              displayString.toString(),
              (IContextInformation) null,
              "",
              lowerRep.equals(lowerQual)
                  ? IPyCompletionProposal.PRIORITY_LOCALS_1
                  : IPyCompletionProposal.PRIORITY_GLOBALS,
              realImportRep.toString(),
              viewer);

      completions.add(proposal);
    }
  }
コード例 #16
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
  /**
   * 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();
    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 comments or multiline comments...
          i = parsingUtils.eatLiterals(buf, i);
          break;

        case '#':
          i = parsingUtils.eatComments(buf, i);
          break;

        case ',':
          i = formatForComma(std, cs, buf, i);
          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
              FastStringBuffer localBufToCheckWildImport = new FastStringBuffer();
              while (Character.isJavaIdentifierPart(localC)) {
                localBufToCheckWildImport.append(localC);
                j--;
                if (j < 0) {
                  break; // break while
                }
                localC = buf.charAt(j);
              }
              if (!localBufToCheckWildImport.reverse().toString().equals("import")) {
                isOperator = true;
              }
            }
            if (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
            FastStringBuffer localBufToCheckNumber = new FastStringBuffer();
            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 == '.') {
                localBufToCheckNumber.append(localC);
              } else {
                break; // break for
              }
            }
            boolean isExponential = true;
            String partialNumber = localBufToCheckNumber.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();
            }
            rightTrimIfNeeded(std, buf);
          }
          buf.append(c);
      }
      lastChar = c;
    }
    if (parensLevel == 0) {
      rightTrimIfNeeded(std, buf);

      if (std.addNewLineAtEndOfFile) {
        char tempC;
        if (buf.length() == 0 || ((tempC = buf.lastChar()) != '\r' && tempC != '\n')) {
          buf.append(delimiter);
        }
      }
    }
    return buf.toString();
  }
コード例 #17
0
  /**
   * Change the pythonpath (used for both: system and project)
   *
   * @param project: may be null
   * @param defaultSelectedInterpreter: may be null
   */
  public void changePythonPath(
      String pythonpath, final IProject project, IProgressMonitor monitor) {
    pythonPathHelper.setPythonPath(pythonpath);
    ModulesFoundStructure modulesFound = pythonPathHelper.getModulesFoundStructure(monitor);

    // now, on to actually filling the module keys
    ModulesKeyTreeMap<ModulesKey, ModulesKey> keys =
        new ModulesKeyTreeMap<ModulesKey, ModulesKey>();
    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();
      File f = entry.getKey();
      String m = entry.getValue();

      if (j % 15 == 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);
      }

      if (m != null) {
        // we don't load them at this time.
        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())) {
            // 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);
          }
        }
      }
    }

    for (ZipContents zipContents : modulesFound.zipContents) {
      if (monitor.isCanceled()) {
        break;
      }
      for (String filePathInZip : zipContents.foundFileZipPaths) {
        String modName = StringUtils.stripExtension(filePathInZip).replace('/', '.');
        if (DEBUG_ZIP) {
          System.out.println("Found in zip:" + modName);
        }
        ModulesKey k = new ModulesKeyForZip(modName, zipContents.zipFile, filePathInZip, true);
        keys.put(k, k);

        if (zipContents.zipContentsType == ZipContents.ZIP_CONTENTS_TYPE_JAR) {
          // folder modules are only created for jars (because for python files, the __init__.py is
          // required).
          for (String s :
              new FullRepIterable(
                  FullRepIterable.getWithoutLastPart(
                      modName))) { // the one without the last part was already added
            k = new ModulesKeyForZip(s, zipContents.zipFile, s.replace('.', '/'), false);
            keys.put(k, k);
          }
        }
      }
    }

    onChangePythonpath(keys);

    // assign to instance variable
    this.setModules(keys);
  }
コード例 #18
0
ファイル: CtxParticipant.java プロジェクト: aparo/Pydev
  private Collection<CtxInsensitiveImportComplProposal> getThem(
      CompletionRequest request, ICompletionState state, boolean addAutoImport)
      throws MisconfigurationException {

    ArrayList<CtxInsensitiveImportComplProposal> completions =
        new ArrayList<CtxInsensitiveImportComplProposal>();
    if (request.isInCalltip) {
      return completions;
    }

    HashSet<String> importedNames = getImportedNames(state);

    String qual = request.qualifier;
    if (qual.length()
        >= CodeCompletionPreferencesPage
            .getCharsForContextInsensitiveGlobalTokensCompletion()) { // at least n characters
                                                                      // required...
      String lowerQual = qual.toLowerCase();

      String initialModule = request.resolveModule();

      List<IInfo> tokensStartingWith =
          AdditionalProjectInterpreterInfo.getTokensStartingWith(
              qual, request.nature, AbstractAdditionalInterpreterInfo.TOP_LEVEL);

      FastStringBuffer realImportRep = new FastStringBuffer();
      FastStringBuffer displayString = new FastStringBuffer();
      FastStringBuffer tempBuf = new FastStringBuffer();

      for (IInfo info : tokensStartingWith) {
        // there always must be a declaringModuleName
        String declaringModuleName = info.getDeclaringModuleName();
        if (initialModule != null && declaringModuleName != null) {
          if (initialModule.equals(declaringModuleName)) {
            continue;
          }
        }
        boolean hasInit = false;
        if (declaringModuleName.endsWith(".__init__")) {
          declaringModuleName =
              declaringModuleName.substring(
                  0, declaringModuleName.length() - 9); // remove the .__init__
          hasInit = true;
        }

        String rep = info.getName();
        String lowerRep = rep.toLowerCase();
        if (!lowerRep.startsWith(lowerQual) || importedNames.contains(rep)) {
          continue;
        }

        if (addAutoImport) {
          realImportRep.clear();
          realImportRep.append("from ");
          realImportRep.append(
              AutoImportsPreferencesPage.removeImportsStartingWithUnderIfNeeded(
                  declaringModuleName, tempBuf));
          realImportRep.append(" import ");
          realImportRep.append(rep);
        }

        displayString.clear();
        displayString.append(rep);
        displayString.append(" - ");
        displayString.append(declaringModuleName);
        if (hasInit) {
          displayString.append(".__init__");
        }

        CtxInsensitiveImportComplProposal proposal =
            new CtxInsensitiveImportComplProposal(
                rep,
                request.documentOffset - request.qlen,
                request.qlen,
                realImportRep.length(),
                AnalysisPlugin.getImageForAutoImportTypeInfo(info),
                displayString.toString(),
                (IContextInformation) null,
                "",
                lowerRep.equals(lowerQual)
                    ? IPyCompletionProposal.PRIORITY_LOCALS_1
                    : IPyCompletionProposal.PRIORITY_GLOBALS,
                realImportRep.toString());

        completions.add(proposal);
      }
    }
    return completions;
  }
コード例 #19
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
  /**
   * Handles having an operator
   *
   * @param std the coding standard to be used
   * @param cs the contents of the string
   * @param buf the buffer where the contents should be added
   * @param parsingUtils helper to get the contents
   * @param i current index
   * @param c current char
   * @return the new index after handling the operator
   */
  private int handleOperator(
      FormatStd std, char[] cs, FastStringBuffer buf, ParsingUtils parsingUtils, int i, char c) {
    // let's discover if it's an unary operator (~ + -)
    boolean isUnaryWithContents = true;

    boolean isUnary = false;
    boolean changeWhitespacesBefore = true;
    if (c == '~' || c == '+' || c == '-') {
      // could be an unary operator...
      String trimmedLastWord = buf.getLastWord().trim();
      isUnary =
          trimmedLastWord.length() == 0
              || PySelection.ALL_STATEMENT_TOKENS.contains(trimmedLastWord);

      if (!isUnary) {
        for (char itChar : buf.reverseIterator()) {
          if (itChar == ' ' || itChar == '\t') {
            continue;
          }
          if (itChar == '=' || itChar == ',') {
            isUnary = true;
          }

          switch (itChar) {
            case '[':
            case '{':
              changeWhitespacesBefore = false;

            case '(':
            case ':':
              isUnaryWithContents = false;

            case '>':
            case '<':

            case '-':
            case '+':
            case '~':

            case '*':
            case '/':
            case '%':
            case '!':
            case '&':
            case '^':
            case '|':
            case '=':
              isUnary = true;
          }
          break;
        }
      } else {
        isUnaryWithContents = buf.length() > 0;
      }
    }

    if (!isUnary) {
      // We don't want to change whitespaces before in a binary operator that is in a new line.
      for (char ch : buf.reverseIterator()) {
        if (!Character.isWhitespace(ch)) {
          break;
        }
        if (ch == '\r' || ch == '\n') {
          changeWhitespacesBefore = false;
          break;
        }
      }
    }

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

    boolean surroundWithSpaces = std.operatorsWithSpace;

    if (changeWhitespacesBefore) {
      // add spaces before
      if (isUnaryWithContents && surroundWithSpaces) {
        buf.append(' ');
      }
    }

    char localC = c;
    char prev = '\0';
    boolean backOne = true;
    while (isOperatorPart(localC, prev)) {
      buf.append(localC);
      prev = localC;
      i++;
      if (i == cs.length) {
        break;
      }
      localC = cs[i];
      if (localC == '=') {
        // when we get to an assign, we have found a full stmt (with assign) -- e.g.: a \\=  a += a
        // ==
        buf.append(localC);
        backOne = false;
        break;
      }
    }
    if (backOne) {
      i--;
    }

    // add space after only if it's not unary
    if (!isUnary && surroundWithSpaces) {
      buf.append(' ');
    }

    i = parsingUtils.eatWhitespaces(null, i + 1);
    return i;
  }
コード例 #20
0
 @Override
 public String toString() {
   FastStringBuffer buffer = new FastStringBuffer();
   buffer.append("CompletionRequest[");
   buffer.append(" editorFile:");
   buffer.appendObject(editorFile);
   buffer.append(" activationToken:");
   buffer.append(activationToken);
   buffer.append(" qualifier:");
   buffer.append(qualifier);
   buffer.append(" isInCalltip:");
   buffer.append(isInCalltip);
   buffer.append(" alreadyHasParams:");
   buffer.append(alreadyHasParams);
   buffer.append("]");
   return buffer.toString();
 }
コード例 #21
0
ファイル: PyFormatStd.java プロジェクト: alecmunro/Pydev
  /**
   * Formats the contents for when a parenthesis is found (so, go until the closing parens and
   * format it accordingly)
   *
   * @param throwSyntaxError
   * @throws SyntaxErrorException
   */
  private int formatForPar(
      final ParsingUtils parsingUtils,
      final char[] cs,
      final int i,
      final FormatStd std,
      final FastStringBuffer buf,
      final int parensLevel,
      final String delimiter,
      boolean throwSyntaxError)
      throws SyntaxErrorException {
    char c = ' ';
    FastStringBuffer locBuf = new FastStringBuffer();

    int j = i + 1;
    int start = j;
    int end = start;
    while (j < cs.length && (c = cs[j]) != ')') {

      j++;

      if (c == '\'' || c == '"') { // ignore comments or multiline comments...
        j = parsingUtils.eatLiterals(null, j - 1) + 1;
        end = j;

      } else if (c == '#') {
        j = parsingUtils.eatComments(null, j - 1) + 1;
        end = j;

      } else if (c == '(') { // open another par.
        if (end > start) {
          locBuf.append(cs, start, end - start);
          start = end;
        }
        j =
            formatForPar(
                    parsingUtils,
                    cs,
                    j - 1,
                    std,
                    locBuf,
                    parensLevel + 1,
                    delimiter,
                    throwSyntaxError)
                + 1;
        start = j;

      } else {
        end = j;
      }
    }
    if (end > start) {
      locBuf.append(cs, start, end - start);
      start = end;
    }

    if (c == ')') {
      // Now, when a closing parens is found, let's see the contents of the line where that parens
      // was found
      // and if it's only whitespaces, add all those whitespaces (to handle the following case:
      // a(a,
      //  b
      //   ) <-- we don't want to change this one.
      char c1;
      FastStringBuffer buf1 = new FastStringBuffer();

      if (locBuf.indexOf('\n') != -1 || locBuf.indexOf('\r') != -1) {
        for (int k = locBuf.length();
            k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n' && c1 != '\r';
            k--) {
          buf1.insert(0, c1);
        }
      }

      String formatStr =
          formatStr(trim(locBuf).toString(), std, parensLevel, delimiter, throwSyntaxError);
      FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10));

      String closing = ")";
      if (buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())) {
        formatStrBuf.append(buf1);

      } else if (std.parametersWithSpace) {
        closing = " )";
      }

      if (std.parametersWithSpace) {
        if (formatStrBuf.length() == 0) {
          buf.append("()");

        } else {
          buf.append("( ");
          buf.append(formatStrBuf);
          buf.append(closing);
        }
      } else {
        buf.append('(');
        buf.append(formatStrBuf);
        buf.append(closing);
      }
      return j;
    } else {
      if (throwSyntaxError) {
        throw new SyntaxErrorException("No closing ')' found.");
      }
      // we found no closing parens but we finished looking already, so, let's just add anything
      // without
      // more formatting...
      buf.append('(');
      buf.append(locBuf);
      return j;
    }
  }
コード例 #22
0
ファイル: PyFileListing.java プロジェクト: pmahoney/Pydev
  /**
   * Returns the directories and python files in a list.
   *
   * @param addSubFolders indicates if sub-folders should be added
   * @param canonicalFolders used to know if we entered a loop in the listing (with symlinks)
   * @return An object with the results of making that listing.
   */
  private static PyFileListing getPyFilesBelow(
      PyFileListing result,
      File file,
      FileFilter filter,
      IProgressMonitor monitor,
      boolean addSubFolders,
      int level,
      boolean checkHasInit,
      String currModuleRep,
      Set<File> canonicalFolders) {

    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }

    if (file != null && file.exists()) {
      // only check files that actually exist

      if (file.isDirectory()) {
        if (level != 0) {
          FastStringBuffer newModuleRep = new FastStringBuffer(currModuleRep, 128);
          if (newModuleRep.length() != 0) {
            newModuleRep.append(".");
          }
          newModuleRep.append(file.getName());
          currModuleRep = newModuleRep.toString();
        }

        // check if it is a symlink loop
        try {
          File canonicalizedDir = file.getCanonicalFile();
          if (!canonicalizedDir.equals(file)) {
            if (canonicalFolders.contains(canonicalizedDir)) {
              return result;
            }
          }
          canonicalFolders.add(canonicalizedDir);
        } catch (IOException e) {
          PydevPlugin.log(e);
        }

        File[] files = null;

        if (filter != null) {
          files = file.listFiles(filter);
        } else {
          files = file.listFiles();
        }

        boolean hasInit = false;

        List<File> foldersLater = new LinkedList<File>();

        for (File file2 : files) {

          if (monitor.isCanceled()) {
            break;
          }

          if (file2.isFile()) {
            result.addPyFileInfo(new PyFileInfo(file2, currModuleRep));

            monitor.worked(1);
            monitor.setTaskName("Found:" + file2.toString());

            if (checkHasInit && hasInit == false) {
              // only check if it has __init__ if really needed
              if (PythonPathHelper.isValidInitFile(file2.getName())) {
                hasInit = true;
              }
            }

          } else {
            foldersLater.add(file2);
          }
        }

        if (!checkHasInit || hasInit || level == 0) {
          result.foldersFound.add(file);

          for (File folder : foldersLater) {

            if (monitor.isCanceled()) {
              break;
            }

            if (folder.isDirectory() && addSubFolders) {

              getPyFilesBelow(
                  result,
                  folder,
                  filter,
                  monitor,
                  addSubFolders,
                  level + 1,
                  checkHasInit,
                  currModuleRep,
                  canonicalFolders);

              monitor.worked(1);
            }
          }
        }

      } else if (file.isFile()) {
        result.addPyFileInfo(new PyFileInfo(file, currModuleRep));

      } else {
        throw new RuntimeException("Not dir nor file... what is it?");
      }
    }

    return result;
  }
コード例 #23
0
ファイル: PythonPathHelper.java プロジェクト: rcuza/Pydev
  /**
   * @param root the zip file to analyze
   * @param monitor the monitor, to keep track of what is happening
   * @return a list with the name of the found modules in the jar
   */
  protected static ModulesFoundStructure.ZipContents getFromZip(
      File root, IProgressMonitor monitor) {

    String fileName = root.getName();
    if (root.isFile()
        && FileTypesPreferencesPage.isValidZipFile(
            fileName)) { // ok, it may be a jar file, so let's get its contents and get the
      // available modules

      // the major difference from handling jars from regular python files is that we don't have to
      // check for __init__.py files
      ModulesFoundStructure.ZipContents zipContents = new ModulesFoundStructure.ZipContents(root);

      // by default it's a zip (for python) -- may change if a .class is found.
      zipContents.zipContentsType = ZipContents.ZIP_CONTENTS_TYPE_PY_ZIP;

      try {
        String zipFileName = root.getName();

        ZipFile zipFile = new ZipFile(root);
        try {
          Enumeration<? extends ZipEntry> entries = zipFile.entries();

          int i = 0;
          FastStringBuffer buffer = new FastStringBuffer();
          // ok, now that we have the zip entries, let's map them to modules
          while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory()) {
              if (isValidFileMod(name) || name.endsWith(".class")) {

                if (name.endsWith(".class")) {
                  zipContents.zipContentsType = ZipContents.ZIP_CONTENTS_TYPE_JAR;
                }

                // it is a valid python file
                if (i % 15 == 0) {
                  if (monitor.isCanceled()) {
                    return null;
                  }
                  buffer.clear();
                  monitor.setTaskName(
                      buffer
                          .append("Found in ")
                          .append(zipFileName)
                          .append(" module ")
                          .append(name)
                          .toString());
                  monitor.worked(1);
                }

                if (isValidInitFile(name)) {
                  zipContents.pyInitFilesLowerWithoutExtension.add(
                      StringUtils.stripExtension(name).toLowerCase());
                }
                zipContents.pyFilesLowerToRegular.put(name.toLowerCase(), name);
              }

            } else { // !isDirectory
              zipContents.pyfoldersLower.add(name.toLowerCase());
            }
            i++;
          }
        } finally {
          zipFile.close();
        }

        // now, on to actually filling the structure if we have a zip file (just add the ones that
        // are actually under
        // the pythonpath)
        zipContents.consolidatePythonpathInfo(monitor);

        return zipContents;

      } catch (Exception e) {
        // that's ok, it is probably not a zip file after all...
        PydevPlugin.log(e);
      }
    }
    return null;
  }