/** 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; }
/** * Saves the current list of commands in the preferences, adding the one passed as a parameter if * it is not null. */ private void saveCurrentCommands(String newCommand) { ArrayList<String> newCommands = new ArrayList<String>(input); if (newCommand != null && !input.contains(newCommand)) { newCommands.add(newCommand); } newCommands.remove(NEW_ENTRY_TEXT); // never save this entry. preferenceStore.setValue( preferenceKey, org.python.pydev.shared_core.string.StringUtils.join("|", newCommands)); }
@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)); } }
/** * @return list with tuples: new String[]{token, description} * @throws CoreException */ public Tuple<String, List<String[]>> getImportCompletions(String str, List<String> pythonpath) throws Exception { FastStringBuffer read = null; str = URLEncoder.encode(str, ENCODING_UTF_8); try (AutoCloseable permit = acquire(StringUtils.join("", "getImportCompletions: ", str))) { internalChangePythonPath(pythonpath); read = this.writeAndGetResults("@@IMPORTS:", str, "\nEND@@"); } return ShellConvert.convertStringToCompletions(read); }
/** @param pythonpath */ private void internalChangePythonPath(List<String> pythonpath) throws Exception { if (finishedForGood) { throw new RuntimeException( "Shells are already finished for good, so, it is an invalid state to try to change its dir."); } synchronized (lockLastPythonPath) { String pythonpathStr = StringUtils.join("|", pythonpath.toArray(new String[pythonpath.size()])); if (lastPythonPath != null && lastPythonPath.equals(pythonpathStr)) { return; } // Note: ignore results writeAndGetResults( "@@CHANGE_PYTHONPATH:", URLEncoder.encode(pythonpathStr, ENCODING_UTF_8), "\nEND@@"); lastPythonPath = pythonpathStr; } }
/** * @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); }
private FastStringBuffer writeAndGetResults(String... str) throws CoreException { try { synchronized (ioLock) { this.write(StringUtils.join("", str)); FastStringBuffer read = this.read(); return read; } } catch (NullPointerException e) { // still not started... restartShell(); return null; } catch (Exception e) { if (DebugSettings.DEBUG_CODE_COMPLETION) { Log.log(IStatus.ERROR, "ERROR getting completions.", e); } restartShell(); return null; } }
/** * This method returns the module that corresponds to the path passed as a parameter. * * @param name the name of the module we're looking for (e.g.: mod1.mod2) * @param dontSearchInit is used in a negative form because initially it was isLookingForRelative, * but it actually defines if we should look in __init__ modules too, so, the name matches the * old signature. * <p>NOTE: isLookingForRelative description was: when looking for relative imports, we don't * check for __init__ * @return the module represented by this name */ protected IModule getModule( boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit) { synchronized (lockTemporaryModules) { SortedMap<Integer, IModule> map = temporaryModules.get(name); if (map != null && map.size() > 0) { if (DEBUG_TEMPORARY_MODULES) { System.out.println("Returning temporary module: " + name); } return map.get(map.lastKey()); } } AbstractModule n = null; ModulesKey keyForCacheAccess = new ModulesKey(null, null); if (!dontSearchInit) { if (n == null) { keyForCacheAccess.name = (String) StringUtils.join(".", new String[] {name, "__init__"}, null); n = cache.getObj(keyForCacheAccess, this); if (n != null) { name = keyForCacheAccess.name; } } } if (n == null) { keyForCacheAccess.name = name; n = cache.getObj(keyForCacheAccess, this); } if (n instanceof SourceModule) { // ok, module exists, let's check if it is synched with the filesystem version... SourceModule s = (SourceModule) n; if (!s.isSynched()) { // change it for an empty and proceed as usual. n = (AbstractModule) addModule(createModulesKey(s.getName(), s.getFile())); } } if (n instanceof EmptyModule) { EmptyModule e = (EmptyModule) n; if (e.f != null) { if (!e.f.exists()) { // if the file does not exist anymore, just remove it. keyForCacheAccess.name = name; keyForCacheAccess.file = e.f; doRemoveSingleModule(keyForCacheAccess); n = null; } else { // file exists n = checkOverride(name, nature, n); if (n instanceof EmptyModule) { // ok, handle case where the file is actually from a zip file... if (e instanceof EmptyModuleForZip) { EmptyModuleForZip emptyModuleForZip = (EmptyModuleForZip) e; if (emptyModuleForZip.pathInZip.endsWith(".class") || !emptyModuleForZip.isFile) { // handle java class... (if it's a class or a folder in a jar) n = JythonModulesManagerUtils.createModuleFromJar(emptyModuleForZip); n = decorateModule(n, nature); } else if (FileTypesPreferencesPage.isValidDll(emptyModuleForZip.pathInZip)) { // .pyd n = new CompiledModule(name, this); n = decorateModule(n, nature); } else if (PythonPathHelper.isValidSourceFile(emptyModuleForZip.pathInZip)) { // handle python file from zip... we have to create it getting the contents from the // zip file try { IDocument doc = FileUtilsFileBuffer.getDocFromZip( emptyModuleForZip.f, emptyModuleForZip.pathInZip); // NOTE: The nature (and so the grammar to be used) must be defined by this // modules // manager (and not by the initial caller)!! n = AbstractModule.createModuleFromDoc( name, emptyModuleForZip.f, doc, this.getNature(), false); SourceModule zipModule = (SourceModule) n; zipModule.zipFilePath = emptyModuleForZip.pathInZip; n = decorateModule(n, nature); } catch (Exception exc1) { Log.log(exc1); n = null; } } } else { // regular case... just go on and create it. try { // NOTE: The nature (and so the grammar to be used) must be defined by this modules // manager (and not by the initial caller)!! n = AbstractModule.createModule(name, e.f, this.getNature(), true); n = decorateModule(n, nature); } catch (IOException exc) { keyForCacheAccess.name = name; keyForCacheAccess.file = e.f; doRemoveSingleModule(keyForCacheAccess); n = null; } catch (MisconfigurationException exc) { Log.log(exc); n = null; } } } } } else { // ok, it does not have a file associated, so, we treat it as a builtin (this can // happen in java jars) n = checkOverride(name, nature, n); if (n instanceof EmptyModule) { if (acceptCompiledModule) { n = new CompiledModule(name, this); n = decorateModule(n, nature); } else { return null; } } } if (n != null) { doAddSingleModule(createModulesKey(name, e.f), n); } else { Log.log(("The module " + name + " could not be found nor created!")); } } if (n instanceof EmptyModule) { throw new RuntimeException("Should not be an empty module anymore: " + n); } if (n instanceof SourceModule) { SourceModule sourceModule = (SourceModule) n; // now, here's a catch... it may be a bootstrap module... if (sourceModule.isBootstrapModule()) { // if it's a bootstrap module, we must replace it for the related compiled module. n = new CompiledModule(name, this); n = decorateModule(n, nature); } } return n; }