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); } }
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; }
/** @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); }
/** * 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); }
/** * @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; }