@Override
 public void setUp() throws Exception {
   super.setUp();
   baseDir = FileUtils.getTempFileAt(new File("."), "data_temp_additional_info_integrity_test");
   if (baseDir.exists()) {
     FileUtils.deleteDirectoryTree(baseDir);
   }
   baseDir.mkdir();
 }
  public void testIntegrityInModuleHasNoFile() throws MisconfigurationException {
    IntegrityInfo info = AdditionalInfoIntegrityChecker.checkIntegrity(nature, monitor, false);
    assertTrue(info.desc.toString(), info.allOk);

    File f = FileUtils.getTempFileAt(baseDir, "integrity_no_file", ".py");
    FileUtils.writeStrToFile("", f);
    addFooModule(new Module(new stmtType[0]), f);
    info = AdditionalInfoIntegrityChecker.checkIntegrity(nature, monitor, false);
    assertFalse(info.allOk);
    assertEquals(1, info.modulesNotInDisk.size());
    assertEquals(info.modulesNotInDisk.get(0), new ModulesKey("foo", null));

    fixAndCheckAllOk(info);
  }
Example #3
0
  /** Gets completions for jedi library (https://github.com/davidhalter/jedi) */
  public List<CompiledToken> getJediCompletions(
      File editorFile, PySelection ps, String charset, List<String> pythonpath) throws Exception {

    FastStringBuffer read = null;
    String str =
        StringUtils.join(
            "|",
            new String[] {
              String.valueOf(ps.getCursorLine()),
              String.valueOf(ps.getCursorColumn()),
              charset,
              FileUtils.getFileAbsolutePath(editorFile),
              StringUtils.replaceNewLines(ps.getDoc().get(), "\n")
            });

    str = URLEncoder.encode(str, ENCODING_UTF_8);

    try (AutoCloseable permit = acquire("getJediCompletions")) {
      internalChangePythonPath(pythonpath);
      read = this.writeAndGetResults("@@MSG_JEDI:", str, "\nEND@@");
    }

    Tuple<String, List<String[]>> theCompletions = ShellConvert.convertStringToCompletions(read);
    ArrayList<CompiledToken> lst = new ArrayList<>(theCompletions.o2.size());
    for (String[] s : theCompletions.o2) {
      // new CompiledToken(rep, doc, args, parentPackage, type);
      lst.add(new CompiledToken(s[0], s[1], "", "", Integer.parseInt(s[3])));
    }
    return lst;
  }
Example #4
0
  public void run(ItemPointer p, IProject project) {
    editor = null;
    Object file = p.file;
    String zipFilePath = p.zipFilePath;

    if (zipFilePath != null) {
      // currently, only open zip file
      editor = PyOpenEditor.doOpenEditor((File) file, zipFilePath);

    } else if (file instanceof IFile) {
      IFile f = (IFile) file;
      editor = PyOpenEditor.doOpenEditor(f);

    } else if (file instanceof IPath) {
      IPath path = (IPath) file;
      editor = PyOpenEditor.doOpenEditor(path, project);

    } else if (file instanceof File) {
      String absPath = FileUtils.getFileAbsolutePath((File) file);
      IPath path = Path.fromOSString(absPath);
      editor = PyOpenEditor.doOpenEditor(path, project);
    }

    if (editor instanceof ITextEditor && p.start.line >= 0) {
      EditorUtils.showInEditor((ITextEditor) editor, p.start, p.end);
    }
  }
 @Override
 public void tearDown() throws Exception {
   if (baseDir.exists()) {
     FileUtils.deleteDirectoryTree(baseDir);
   }
   super.tearDown();
 }
Example #6
0
  @Override
  protected Throwable exec(File f) {
    System.out.println(org.python.pydev.shared_core.string.StringUtils.format("Running: %s", f));
    Tuple<String, String> output =
        new SimpleIronpythonRunner()
            .runAndGetOutput(
                new String[] {
                  TestDependent.IRONPYTHON_EXE,
                  "-u",
                  IInterpreterManager.IRONPYTHON_DEFAULT_INTERNAL_SHELL_VM_ARGS,
                  FileUtils.getFileAbsolutePath(f)
                },
                f.getParentFile(),
                null,
                null,
                "utf-8");

    System.out.println(
        org.python.pydev.shared_core.string.StringUtils.format(
            "stdout:%s\nstderr:%s", output.o1, output.o2));

    if (output.o2.toLowerCase().indexOf("failed") != -1
        || output.o2.toLowerCase().indexOf("traceback") != -1) {
      throw new AssertionError(output.toString());
    }
    return null;
  }
Example #7
0
  public void testRefEncoding() throws Exception {
    String validEncoding = FileUtils.getValidEncoding("latin-1", null);
    assertEquals("latin1", validEncoding);
    assertNull(FileUtils.getValidEncoding("utf-8-*-", null));

    // supported
    assertTrue(Charset.isSupported("latin1"));
    assertTrue(Charset.isSupported("utf8"));
    assertTrue(Charset.isSupported("utf_16"));
    assertTrue(Charset.isSupported("UTF-8"));
    assertTrue(Charset.isSupported("utf-8"));

    // not supported
    assertFalse(Charset.isSupported("latin-1"));
    assertFalse(
        Charset.isSupported(
            "utf_8")); // why utf_16 is supported and utf_8 is not is something that really amazes
                       // me.
  }
Example #8
0
  /** @param dir the directory that should have .py files found and parsed. */
  protected void parseFilesInDir(File dir, boolean recursive, boolean generateTree) {
    assertTrue("Directory " + dir + " does not exist", dir.exists());
    assertTrue(dir.isDirectory());

    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      File f = files[i];
      String name = f.getName().toLowerCase();
      if (name.endsWith(".py")) {
        // Used for stress-testing: parsing all files in Python installation.
        //            	try {
        //					if(name.equals("func_syntax_error.py")){
        //						continue;
        //					}
        //					if(name.equals("badsyntax_nocaret.py")){
        //						continue;
        //					}
        //					if(name.equals("py3_test_grammar.py") && parser.getGrammarVersion() <
        // IPythonNature.GRAMMAR_PYTHON_VERSION_3_0){
        //						continue;
        //					}
        //					String absolute = f.getAbsolutePath().toLowerCase();
        //					if(absolute.contains("pylint") && absolute.contains("test")){
        //						continue;
        //					}
        //					if(absolute.contains("port_v3")){
        //						continue;
        //					}
        //				} catch (MisconfigurationException e) {
        //					throw new RuntimeException(e);
        //				}
        if (generateTree) {
          parseLegalDocStr(FileUtils.getFileContents(f), f);
        } else {
          parseLegalDocStrWithoutTree(FileUtils.getFileContents(f), f);
        }

      } else if (recursive && f.isDirectory()) {
        parseFilesInDir(f, recursive, generateTree);
      }
    }
  }
Example #9
0
 public void testGetEncoding2() {
   String s =
       ""
           + "#test.py\n"
           + "# handles encoding and decoding of xmlBlaster socket protocol \n"
           + "\n"
           + "\n"
           + "";
   CharArrayReader reader = new CharArrayReader(s.toCharArray());
   String encoding = FileUtils.getPythonFileEncoding(reader, null);
   assertEquals(null, encoding);
 }
Example #10
0
 /**
  * This function creates a module and resolves the module name (use this function if only the file
  * is available).
  *
  * @throws MisconfigurationException
  */
 public static IModule createModuleFromDoc(File file, IDocument doc, IPythonNature pythonNature)
     throws MisconfigurationException {
   IModulesManager projModulesManager = pythonNature.getAstManager().getModulesManager();
   String moduleName = null;
   if (file != null) {
     moduleName = projModulesManager.resolveModule(FileUtils.getFileAbsolutePath(file));
   }
   if (moduleName == null) {
     moduleName = MODULE_NAME_WHEN_FILE_IS_UNDEFINED;
   }
   IModule module = createModuleFromDoc(moduleName, file, doc, pythonNature, false);
   return module;
 }
  /**
   * @return true if the given marker is from an external file, and the editor in which this action
   *     is being executed is in this same editor.
   */
  protected static boolean isInSameExternalEditor(
      IMarker marker, IEditorInput externalFileEditorInput) throws CoreException {
    if (marker == null || externalFileEditorInput == null) {
      return false;
    }

    String attribute = (String) marker.getAttribute(PyBreakpoint.PY_BREAK_EXTERNAL_PATH_ID);
    if (attribute != null) {
      File file = PydevFileEditorInput.getFile(externalFileEditorInput);
      if (file == null) {
        return false;
      }
      if (attribute.equals(FileUtils.getFileAbsolutePath(file))) {
        return true;
      }
    }
    return false;
  }
 @Override
 public String getOutgoing() {
   if (file != null) {
     return makeCommand(
         AbstractDebuggerCommand.CMD_IGNORE_THROWN_EXCEPTION_AT,
         sequence,
         StringUtils.join("|", FileUtils.getFileAbsolutePath(file), this.lineNumber));
   } else {
     // Bulk-creation
     Collection<String> ignoreThrownExceptions =
         PyExceptionBreakPointManager.getInstance()
             .ignoreCaughtExceptionsWhenThrownFrom
             .getIgnoreThrownExceptions();
     return makeCommand(
         AbstractDebuggerCommand.CMD_IGNORE_THROWN_EXCEPTION_AT,
         sequence,
         "REPLACE:" + StringUtils.join("||", ignoreThrownExceptions));
   }
 }
Example #13
0
  /**
   * @param paths the paths to be added
   * @return a String suitable to be added to the PYTHONPATH environment variable.
   */
  public static String makePythonPathEnvFromPaths(Collection<String> inPaths) {
    ArrayList<String> paths = new ArrayList<String>(inPaths);
    try {
      // whenever we launch a file from pydev, we must add the sitecustomize to the pythonpath so
      // that
      // the default encoding (for the console) can be set.
      // see:
      // http://sourceforge.net/tracker/index.php?func=detail&aid=1580766&group_id=85796&atid=577329

      paths.add(
          0,
          FileUtils.getFileAbsolutePath(PydevPlugin.getScriptWithinPySrc("pydev_sitecustomize")));
    } catch (CoreException e) {
      Log.log(e);
    }

    String separator = getPythonPathSeparator();
    return org.python.pydev.shared_core.string.StringUtils.join(separator, paths);
  }
Example #14
0
 public void testGetEncoding3() {
   // silent it in the tests
   FileUtils.LOG_ENCODING_ERROR = false;
   try {
     String s =
         ""
             + "#coding: foo_1\n"
             + // not valid encoding... will show in log but will not throw error
             "# handles encoding and decoding of xmlBlaster socket protocol \n"
             + "\n"
             + "\n"
             + "";
     CharArrayReader reader = new CharArrayReader(s.toCharArray());
     String encoding = FileUtils.getPythonFileEncoding(reader, null);
     assertEquals(null, encoding);
   } finally {
     FileUtils.LOG_ENCODING_ERROR = true;
   }
 }
        @Override
        public void onChangedIInterpreterInfo(InfoTracker infoTracker, File file) {
          if (SynchSystemModulesManager.DEBUG) {
            System.out.println(
                "File changed :" + file + " starting track of: " + infoTracker.info.getNameForUI());
          }
          job.addToTrack(infoTracker.manager, infoTracker.info);
          if (file.exists() && file.isDirectory()) {
            // If it's a directory, it may be a copy operation, so, check until the copy finishes
            // (i.e.:
            // poll for changes and when there are no changes anymore scheduleLater it right away).

            job.scheduleLater(1000);
            long lastFound = 0;
            while (true) {
              long lastModified =
                  FileUtils.getLastModifiedTimeFromDir(
                      file, filter, dirFilter, EventsStackerRunnable.LEVELS_TO_GET_MODIFIED_TIME);
              if (lastFound == lastModified) {
                break;
              }
              lastFound = lastModified;

              // If we don't have a change in the directory structure for 500 millis, stop it.
              synchronized (this) {
                try {
                  this.wait(500);
                } catch (InterruptedException e) {
                  // Ignore
                }
              }
              if (lastFound == 0) {
                return; // I.e.: found no interesting file.
              }
              job.scheduleLater(1000);
            }

          } else {
            job.scheduleLater(5 * 1000); // 5 seconds
          }
        }
Example #16
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);
        }
      }
    }
  }
Example #17
0
 public static String getRunFilesScript() throws CoreException {
   return FileUtils.getFileAbsolutePath(PydevDebugPlugin.getScriptWithinPySrc("runfiles.py"));
 }
Example #18
0
 /**
  * @return
  * @throws CoreException
  */
 public static String getCoverageScript() throws CoreException {
   return FileUtils.getFileAbsolutePath(
       PydevDebugPlugin.getScriptWithinPySrc("pydev_coverage.py"));
 }
Example #19
0
 /**
  * @see
  *     org.python.pydev.core.IProjectModulesManager#resolveModule(org.eclipse.core.resources.IResource,
  *     org.eclipse.core.resources.IProject)
  */
 public String resolveModule(IResource member, IProject container) {
   File inOs = member.getRawLocation().toFile();
   return resolveModule(FileUtils.getFileAbsolutePath(inOs));
 }
Example #20
0
  public void saveToFile(File workspaceMetadataFile) {
    if (workspaceMetadataFile.exists() && !workspaceMetadataFile.isDirectory()) {
      try {
        FileUtils.deleteFile(workspaceMetadataFile);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    if (!workspaceMetadataFile.exists()) {
      workspaceMetadataFile.mkdirs();
    }

    File modulesKeysFile = new File(workspaceMetadataFile, "modulesKeys");
    File pythonpatHelperFile = new File(workspaceMetadataFile, "pythonpath");
    FastStringBuffer buf;
    HashMap<String, Integer> commonTokens = new HashMap<String, Integer>();

    synchronized (modulesKeysLock) {
      buf = new FastStringBuffer(this.modulesKeys.size() * 50);
      buf.append(MODULES_MANAGER_V2);

      for (Iterator<ModulesKey> iter = this.modulesKeys.keySet().iterator(); iter.hasNext(); ) {
        ModulesKey next = iter.next();
        buf.append(next.name);
        if (next.file != null) {
          buf.append("|");
          if (next instanceof ModulesKeyForZip) {
            ModulesKeyForZip modulesKeyForZip = (ModulesKeyForZip) next;
            if (modulesKeyForZip.zipModulePath != null) {
              String fileStr = next.file.toString();
              Integer t = commonTokens.get(fileStr);
              if (t == null) {
                t = commonTokens.size();
                commonTokens.put(fileStr, t);
              }
              buf.append(t);
              buf.append("|");

              buf.append(modulesKeyForZip.zipModulePath);
              buf.append("|");
              buf.append(modulesKeyForZip.isFile ? '1' : '0');
            }
          } else {
            buf.append(next.file.toString());
          }
        }
        buf.append('\n');
      }
    }
    if (commonTokens.size() > 0) {
      FastStringBuffer header = new FastStringBuffer(buf.length() + (commonTokens.size() * 50));
      header.append(MODULES_MANAGER_V2);
      header.append("--COMMON--\n");
      for (Map.Entry<String, Integer> entries : commonTokens.entrySet()) {
        header.append(entries.getValue());
        header.append('=');
        header.append(entries.getKey());
        header.append('\n');
      }
      header.append("--END-COMMON--\n");
      header.append(buf);
      buf = header;
    }
    FileUtils.writeStrToFile(buf.toString(), modulesKeysFile);

    this.pythonPathHelper.saveToFile(pythonpatHelperFile);
  }
Example #21
0
  /**
   * @param systemModulesManager
   * @param workspaceMetadataFile
   * @throws IOException
   */
  public static void loadFromFile(ModulesManager modulesManager, File workspaceMetadataFile)
      throws IOException {
    if (workspaceMetadataFile.exists() && !workspaceMetadataFile.isDirectory()) {
      throw new IOException("Expecting: " + workspaceMetadataFile + " to be a directory.");
    }
    File modulesKeysFile = new File(workspaceMetadataFile, "modulesKeys");
    File pythonpatHelperFile = new File(workspaceMetadataFile, "pythonpath");
    if (!modulesKeysFile.isFile()) {
      throw new IOException("Expecting: " + modulesKeysFile + " to exist (and be a file).");
    }
    if (!pythonpatHelperFile.isFile()) {
      throw new IOException("Expecting: " + pythonpatHelperFile + " to exist (and be a file).");
    }

    String fileContents = FileUtils.getFileContents(modulesKeysFile);
    if (!fileContents.startsWith(MODULES_MANAGER_V2)) {
      throw new RuntimeException(
          "Could not load modules manager from " + modulesKeysFile + " (version changed).");
    }

    HashMap<Integer, String> intToString = new HashMap<Integer, String>();
    fileContents = fileContents.substring(MODULES_MANAGER_V2.length());
    if (fileContents.startsWith("--COMMON--\n")) {
      String header = fileContents.substring("--COMMON--\n".length());
      header = header.substring(0, header.indexOf("--END-COMMON--\n"));
      fileContents =
          fileContents.substring(
              fileContents.indexOf("--END-COMMON--\n") + "--END-COMMON--\n".length());

      for (String line : StringUtils.iterLines(header)) {
        line = line.trim();
        List<String> split = StringUtils.split(line, '=');
        if (split.size() == 2) {
          try {
            int i = Integer.parseInt(split.get(0));
            intToString.put(i, split.get(1));
          } catch (NumberFormatException e) {
            Log.log(e);
          }
        }
      }
      if (fileContents.startsWith(MODULES_MANAGER_V2)) {
        fileContents = fileContents.substring(MODULES_MANAGER_V2.length());
      }
    }

    handleFileContents(modulesManager, fileContents, intToString);

    if (modulesManager.pythonPathHelper == null) {
      throw new IOException(
          "Pythonpath helper not properly restored. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }
    modulesManager.pythonPathHelper.loadFromFile(pythonpatHelperFile);

    if (modulesManager.pythonPathHelper.getPythonpath() == null) {
      throw new IOException(
          "Pythonpath helper pythonpath not properly restored. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }

    if (modulesManager.pythonPathHelper.getPythonpath().size() == 0) {
      throw new IOException(
          "Pythonpath helper pythonpath restored with no contents. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }

    if (modulesManager.modulesKeys.size()
        < 2) { // if we have few modules, that may indicate a problem...
      // if the project is really small, modulesManager will be fast, otherwise, it'll fix the
      // problem.
      // Note: changed to a really low value because we now make a check after it's restored
      // anyways.
      throw new IOException(
          "Only "
              + modulesManager.modulesKeys.size()
              + " modules restored in I/O. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }
  }
Example #22
0
 public IPath getLocation() {
   return Path.fromOSString(FileUtils.getFileAbsolutePath(this.projectRoot));
 }
Example #23
0
 public void testGetEncoding() {
   String loc = TestDependent.TEST_PYSRC_LOC + "testenc/encutf8.py";
   String encoding = FileUtils.getPythonFileEncoding(new File(loc));
   assertEquals("UTF-8", encoding);
 }
Example #24
0
 public void testGetEncoding10() {
   String s = "" + "#coding: latin1\n" + "\n" + "";
   CharArrayReader reader = new CharArrayReader(s.toCharArray());
   String encoding = FileUtils.getPythonFileEncoding(reader, null);
   assertEquals("latin1", encoding);
 }
Example #25
0
 public void testGetEncoding5() {
   String s = "" + "#-*- coding: utf-8; -*-\n" + "\n" + "";
   CharArrayReader reader = new CharArrayReader(s.toCharArray());
   String encoding = FileUtils.getPythonFileEncoding(reader, null);
   assertEquals("utf-8", encoding);
 }
  @Override
  public void addProps(
      MarkerAnnotationAndPosition markerAnnotation,
      IAnalysisPreferences analysisPreferences,
      String line,
      PySelection ps,
      int offset,
      IPythonNature nature,
      PyEdit edit,
      List<ICompletionProposal> props)
      throws BadLocationException, CoreException {
    if (nature == null) {
      return;
    }

    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
      return;
    }

    if (markerAnnotation.position == null) {
      return;
    }
    IMarker marker = markerAnnotation.markerAnnotation.getMarker();
    Integer id = (Integer) marker.getAttribute(AnalysisRunner.PYDEV_ANALYSIS_TYPE);
    int start = markerAnnotation.position.offset;
    int end = start + markerAnnotation.position.length;
    ps.setSelection(start, end);
    String markerContents;
    try {
      markerContents = ps.getSelectedText();
    } catch (Exception e1) {
      return; // Selection may be wrong.
    }

    IDocument doc = ps.getDoc();
    List<String> parametersAfterCall = ps.getParametersAfterCall(end);

    switch (id) {
      case IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE:
        addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall);

        addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall);
        break;

      case IAnalysisPreferences.TYPE_UNDEFINED_IMPORT_VARIABLE:
        // Say we had something as:
        // import sys
        // sys.Bar
        // in which case 'Bar' is undefined
        // in this situation, the activationTokenAndQual would be "sys." and "Bar"
        // and we want to get the definition for "sys"
        String[] activationTokenAndQual = ps.getActivationTokenAndQual(true);

        if (activationTokenAndQual[0].endsWith(".")) {
          ArrayList<IDefinition> selected = findDefinitions(nature, edit, start - 2, doc);

          for (IDefinition iDefinition : selected) {

            IModule module = iDefinition.getModule();
            if (module.getFile() != null) {
              Definition definition = (Definition) iDefinition;
              File file = module.getFile();
              if (definition.ast == null) {
                // if we have no ast in the definition, it means the module itself was found (global
                // scope)

                // Add option to create class at the given module!
                addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);

                addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
              } else if (definition.ast instanceof ClassDef) {
                ClassDef classDef = (ClassDef) definition.ast;
                // Ok, we should create a field or method in this case (accessing a classmethod or
                // staticmethod)
                PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                String className = NodeUtils.getNameFromNameTok(classDef.name);
                pyCreateMethod.setCreateInClass(className);
                pyCreateMethod.setCreateAs(PyCreateMethodOrField.CLASSMETHOD);
                addCreateClassmethodOption(
                    ps,
                    edit,
                    props,
                    markerContents,
                    parametersAfterCall,
                    pyCreateMethod,
                    file,
                    className);
              }
            }
          }
        }
        break;

      case IAnalysisPreferences.TYPE_UNRESOLVED_IMPORT:
        // This case is the following: from other_module4 import Foo
        // with 'Foo' being undefined.
        // So, we have to suggest creating a Foo class/method in other_module4
        PyImportsHandling importsHandling = new PyImportsHandling(ps.getDoc(), false);
        int offsetLine = ps.getLineOfOffset(start);
        String selectedText = ps.getSelectedText();

        Tuple<IModule, String> found = null;
        String foundFromImportStr = null;
        boolean isImportFrom = false;
        OUT:
        for (ImportHandle handle : importsHandling) {
          if (handle.startFoundLine == offsetLine
              || handle.endFoundLine == offsetLine
              || (handle.startFoundLine < offsetLine && handle.endFoundLine > offsetLine)) {
            List<ImportHandleInfo> importInfo = handle.getImportInfo();
            for (ImportHandleInfo importHandleInfo : importInfo) {
              String fromImportStr = importHandleInfo.getFromImportStr();
              List<String> importedStr = importHandleInfo.getImportedStr();
              for (String imported : importedStr) {
                if (selectedText.equals(imported)) {
                  if (fromImportStr != null) {
                    foundFromImportStr = fromImportStr + "." + imported;
                    isImportFrom = true;
                  } else {
                    // if fromImportStr == null, it's not a from xxx import yyy (i.e.: simple
                    // import)
                    foundFromImportStr = imported;
                  }
                  try {
                    String currentModule = nature.resolveModule(edit.getEditorFile());
                    ICompletionState state =
                        CompletionStateFactory.getEmptyCompletionState(
                            nature, new CompletionCache());

                    found =
                        nature
                            .getAstManager()
                            .findModule(
                                foundFromImportStr,
                                currentModule,
                                state,
                                new SourceModule(
                                    currentModule, edit.getEditorFile(), edit.getAST(), null));
                  } catch (Exception e) {
                    Log.log(e);
                  }
                  break OUT;
                }
              }
            }
            break OUT;
          }
        }

        boolean addOptionToCreateClassOrMethod = isImportFrom;

        if (found != null && found.o1 != null) {
          // Ok, we found a module, now, it may be that we still have to create some intermediary
          // modules
          // or just create a class or method at the end.
          if (found.o1 instanceof SourceModule) {

            // if all was found, there's nothing left to create.
            if (found.o2 != null && found.o2.length() > 0) {
              SourceModule sourceModule = (SourceModule) found.o1;
              File file = sourceModule.getFile();

              if (found.o2.indexOf('.') != -1) {
                // We have to create some intermediary structure.
                if (!addOptionToCreateClassOrMethod) {

                  // Cannot create class or method from the info (only the module structure).
                  if (sourceModule.getName().endsWith(".__init__")) {
                    File f = getFileStructure(file.getParentFile(), found.o2);
                    addCreateModuleOption(ps, edit, props, markerContents, f);
                  }

                } else {
                  // Ok, the leaf may be a class or method.
                  if (sourceModule.getName().endsWith(".__init__")) {
                    String moduleName = FullRepIterable.getWithoutLastPart(sourceModule.getName());
                    String withoutLastPart = FullRepIterable.getWithoutLastPart(found.o2);
                    moduleName += "." + withoutLastPart;

                    String classOrMethodName = FullRepIterable.getLastPart(found.o2);

                    File f = getFileStructure(file.getParentFile(), withoutLastPart);
                    addCreateClassInNewModuleOption(
                        ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                    addCreateMethodInNewModuleOption(
                        ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                  }
                }

              } else {
                // Ok, it's all there, we just have to create the leaf.
                if (!addOptionToCreateClassOrMethod
                    || sourceModule.getName().endsWith(".__init__")) {
                  // Cannot create class or method from the info (only the module structure).
                  if (sourceModule.getName().endsWith(".__init__")) {
                    File f =
                        new File(
                            file.getParent(),
                            found.o2 + FileTypesPreferencesPage.getDefaultDottedPythonExtension());
                    addCreateModuleOption(ps, edit, props, markerContents, f);
                  }
                } else {
                  // Ok, the leaf may be a class or method.
                  addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                  addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                }
              }
            }
          }

        } else if (foundFromImportStr != null) {
          // We couldn't find anything there, so, we have to create the modules structure as needed
          // and
          // maybe create a class or module at the end (but only if it's an import from).
          // Ok, it's all there, we just have to create the leaf.

          // Discover the source folder where we should create the structure.

          File editorFile = edit.getEditorFile();
          String onlyProjectPythonPathStr =
              nature.getPythonPathNature().getOnlyProjectPythonPathStr(false);
          List<String> split =
              StringUtils.splitAndRemoveEmptyTrimmed(onlyProjectPythonPathStr, '|');
          for (int i = 0; i < split.size(); i++) {
            String fullPath = FileUtils.getFileAbsolutePath(split.get(i));
            fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
            split.set(i, fullPath);
          }
          HashSet<String> projectSourcePath = new HashSet<String>(split);
          if (projectSourcePath.size() == 0) {
            return; // No source folder for editor... this shouldn't happen (code analysis wouldn't
            // even run on it).
          }
          String fullPath = FileUtils.getFileAbsolutePath(editorFile);
          fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
          String foundSourceFolderFullPath = null;
          if (projectSourcePath.size() == 1) {
            foundSourceFolderFullPath = projectSourcePath.iterator().next();
          } else {
            for (String string : projectSourcePath) {
              if (fullPath.startsWith(string)) {
                // Use this as the source folder
                foundSourceFolderFullPath = string;
                break;
              }
            }
          }
          if (foundSourceFolderFullPath != null) {

            if (!addOptionToCreateClassOrMethod) {
              // Cannot create class or method from the info (only the module structure).

              File f = getFileStructure(new File(foundSourceFolderFullPath), foundFromImportStr);
              addCreateModuleOption(ps, edit, props, foundFromImportStr, f);

            } else {
              // Ok, the leaf may be a class or method.
              String moduleName = FullRepIterable.getWithoutLastPart(foundFromImportStr);
              File file = getFileStructure(new File(foundSourceFolderFullPath), moduleName);
              String lastPart = FullRepIterable.getLastPart(foundFromImportStr);
              addCreateClassInNewModuleOption(
                  ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
              addCreateMethodInNewModuleOption(
                  ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
            }
          }
        }
        break;
    }
  }