@Override
 protected IStatus run(IProgressMonitor monitor) {
   IParserObserver observer = new RenameInFileParserObserver();
   PyParser parser = pyEdit.getParser();
   parser.addParseListener(observer); // it will analyze when the next parse is finished
   parser.forceReparse();
   return Status.OK_STATUS;
 }
Esempio n. 2
0
 private List<FoldingEntry> getMarks(Document doc, int grammarVersion) {
   ParseOutput r = PyParser.reparseDocument(new PyParser.ParserInfo(doc, grammarVersion, null));
   List<FoldingEntry> marks = CodeFoldingSetter.getMarks(doc, (SimpleNode) r.ast, true);
   if (DEBUG) {
     for (FoldingEntry entry : marks) {
       System.out.println(entry);
     }
   }
   return marks;
 }
Esempio n. 3
0
  /**
   * @param request this is the request for the completion
   * @param theList OUT - returned completions are added here. (IToken instances)
   * @param getOnlySupers whether we should only get things from super classes (in this case, we
   *     won't get things from the current class)
   * @param checkIfInCorrectScope if true, we'll first check if we're in a scope that actually has a
   *     method with 'self' or 'cls'
   * @return true if we actually tried to get the completions for self or cls.
   * @throws MisconfigurationException
   */
  @SuppressWarnings("unchecked")
  public static boolean getSelfOrClsCompletions(
      CompletionRequest request,
      List theList,
      ICompletionState state,
      boolean getOnlySupers,
      boolean checkIfInCorrectScope,
      String lookForRep)
      throws MisconfigurationException {

    SimpleNode s =
        PyParser.reparseDocument(
                new PyParser.ParserInfo(request.doc, true, request.nature, state.getLine()))
            .o1;
    if (s != null) {
      FindScopeVisitor visitor = new FindScopeVisitor(state.getLine(), 0);
      try {
        s.accept(visitor);
        if (checkIfInCorrectScope) {
          boolean scopeCorrect = false;

          FastStack<SimpleNode> scopeStack = visitor.scope.getScopeStack();
          for (Iterator<SimpleNode> it = scopeStack.topDownIterator();
              scopeCorrect == false && it.hasNext(); ) {
            SimpleNode node = it.next();
            if (node instanceof FunctionDef) {
              FunctionDef funcDef = (FunctionDef) node;
              if (funcDef.args != null
                  && funcDef.args.args != null
                  && funcDef.args.args.length > 0) {
                // ok, we have some arg, let's check for self or cls
                String rep = NodeUtils.getRepresentationString(funcDef.args.args[0]);
                if (rep != null && (rep.equals("self") || rep.equals("cls"))) {
                  scopeCorrect = true;
                }
              }
            }
          }
          if (!scopeCorrect) {
            return false;
          }
        }
        if (lookForRep.equals("self")) {
          state.setLookingFor(ICompletionState.LOOKING_FOR_INSTANCED_VARIABLE);
        } else {
          state.setLookingFor(ICompletionState.LOOKING_FOR_CLASSMETHOD_VARIABLE);
        }
        getSelfOrClsCompletions(visitor.scope, request, theList, state, getOnlySupers);
      } catch (Exception e1) {
        PydevPlugin.log(e1);
      }
      return true;
    }
    return false;
  }
Esempio n. 4
0
  /**
   * This function creates the module given that you have a document (that will be parsed)
   *
   * @throws MisconfigurationException
   */
  public static SourceModule createModuleFromDoc(
      String name,
      File f,
      IDocument doc,
      IPythonNature nature,
      boolean checkForPath,
      IGrammarVersionProvider grammarVersionProvider)
      throws MisconfigurationException {
    // for doc, we are only interested in python files.

    if (f != null) {
      if (!checkForPath || PythonPathHelper.isValidSourceFile(f.getName())) {
        ParseOutput obj =
            PyParser.reparseDocument(new PyParser.ParserInfo(doc, grammarVersionProvider, name, f));
        return new SourceModule(name, f, (SimpleNode) obj.ast, obj.error, nature);
      }
    } else {
      ParseOutput obj =
          PyParser.reparseDocument(new PyParser.ParserInfo(doc, grammarVersionProvider, name, f));
      return new SourceModule(name, f, (SimpleNode) obj.ast, obj.error, nature);
    }
    return null;
  }
Esempio n. 5
0
  public static Module getRootNode(IDocument doc) throws ParseException {
    Tuple<SimpleNode, Throwable> objects =
        PyParser.reparseDocument(
            new PyParser.ParserInfo(doc, false, IPythonNature.LATEST_GRAMMAR_VERSION));
    Throwable exception = objects.o2;

    if (exception != null) {
      /* We try to get rid of the 'Throwable' exception, if possible */
      if (exception instanceof ParseException) {
        throw (ParseException) exception;
      } else if (exception instanceof TokenMgrError) {
        /* Error from Lexer */
        throw new ParseException(exception.toString());
      } else {
        throw new RuntimeException(exception);
      }
    }

    if (objects.o2 != null) throw new RuntimeException(objects.o2);
    return (Module) objects.o1;
  }
  /**
   * Restores the info for a module manager
   *
   * @param monitor a monitor to keep track of the progress
   * @param m the module manager
   * @param nature the associated nature (may be null if there is no associated nature -- as is the
   *     case when restoring system info).
   * @return the info generated from the module manager
   */
  public static AbstractAdditionalTokensInfo restoreInfoForModuleManager(
      IProgressMonitor monitor,
      IModulesManager m,
      String additionalFeedback,
      AbstractAdditionalTokensInfo info,
      IPythonNature nature,
      int grammarVersion) {
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }
    // TODO: Check if keeping a zip file open makes things faster...
    // Timer timer = new Timer();
    ModulesKey[] allModules = m.getOnlyDirectModules();
    int i = 0;

    FastStringBuffer msgBuffer = new FastStringBuffer();

    for (ModulesKey key : allModules) {
      if (monitor.isCanceled()) {
        return null;
      }
      i++;

      if (PythonPathHelper.canAddAstInfoFor(
          key)) { // otherwise it should be treated as a compiled module (no ast generation)

        if (i % 17 == 0) {
          msgBuffer.clear();
          msgBuffer.append("Creating ");
          msgBuffer.append(additionalFeedback);
          msgBuffer.append(" additional info (");
          msgBuffer.append(i);
          msgBuffer.append(" of ");
          msgBuffer.append(allModules.length);
          msgBuffer.append(") for ");
          msgBuffer.append(key.file.getName());
          monitor.setTaskName(msgBuffer.toString());
          monitor.worked(1);
        }

        try {
          if (info.addAstInfo(key, false) == null) {
            String str = "Unable to generate ast -- using %s.\nError:%s";
            ErrorDescription errorDesc = null;
            throw new RuntimeException(
                com.aptana.shared_core.string.StringUtils.format(
                    str,
                    PyParser.getGrammarVersionStr(grammarVersion),
                    (errorDesc != null && errorDesc.message != null)
                        ? errorDesc.message
                        : "unable to determine"));
          }

        } catch (Throwable e) {
          Log.log(IStatus.ERROR, "Problem parsing the file :" + key.file + ".", e);
        }
      }
    }
    // timer.printDiff("Time to restore additional info");
    return info;
  }
  private boolean ensureParsed(PyEdit edit) {
    // Ok, we have a little problem here: we have to ensure not that only a regular ast parse took
    // place, but
    // that the analysis of the related document is updated (so that the markers are in-place).
    // To do that, we ask for a reparse asking to analyze the results in that same thread (without
    // scheduling it).
    //
    // Maybe better would be having some extension that allowed to call the analysis of the document
    // directly
    // (so we don't have to rely on markers in place?)
    final PyParser parser = edit.getParser();
    final boolean[] notified = new boolean[] {false};
    final Object sentinel = new Object();

    parser.addPostParseListener(
        new IPostParserListener() {

          @Override
          public void participantsNotified(Object... argsToReparse) {
            synchronized (OrganizeImportsFixesUnused.this) {
              parser.removePostParseListener(this);
              if (argsToReparse.length == 2 && argsToReparse[1] == sentinel) {
                notified[0] = true;
                OrganizeImportsFixesUnused.this.notify();
              }
            }
          }
        });

    long initial = System.currentTimeMillis();
    while ((System.currentTimeMillis() - initial) < 5000) {

      if (parser.forceReparse(
          new Tuple<String, Boolean>(
              IMiscConstants.ANALYSIS_PARSER_OBSERVER_FORCE_IN_THIS_THREAD, true),
          sentinel)) {
        // ok, we were able to schedule it with our parameters, let's wait for its completion...
        synchronized (this) {
          try {
            wait(5000);
          } catch (InterruptedException e) {
          }
        }
        break;
      } else {
        synchronized (this) {
          try {
            wait(200);
          } catch (InterruptedException e) {
          }
        }
      }
    }

    // Commented out: in the worse case, we already waited 5 seconds, if because of a racing
    // condition we couldn't decide
    // that it worked, let's just keep on going hoping that the markers are in place...
    // if (!notified[0]) {
    //    return false;
    // }
    return true;
  }