Search in sources :

Example 1 with FullRepIterable

use of org.python.pydev.shared_core.string.FullRepIterable in project Pydev by fabioz.

the class FullRepIterableTest method testiterator2.

public void testiterator2() {
    Iterator iterator = new FullRepIterable("var1").iterator();
    assertTrue(iterator.hasNext());
    assertEquals("var1", iterator.next());
    assertFalse(iterator.hasNext());
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) Iterator(java.util.Iterator)

Example 2 with FullRepIterable

use of org.python.pydev.shared_core.string.FullRepIterable in project Pydev by fabioz.

the class FullRepIterableTest method testiterator3.

public void testiterator3() {
    Iterator iterator = new FullRepIterable("testlib.unittest.relative").iterator();
    assertTrue(iterator.hasNext());
    assertEquals("testlib", iterator.next());
    assertEquals("testlib.unittest", iterator.next());
    assertEquals("testlib.unittest.relative", iterator.next());
    assertFalse(iterator.hasNext());
    int i = 0;
    for (String dummy : new FullRepIterable("testlib.unittest.relative")) {
        i++;
    }
    assertEquals(3, i);
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) Iterator(java.util.Iterator)

Example 3 with FullRepIterable

use of org.python.pydev.shared_core.string.FullRepIterable in project Pydev by fabioz.

the class FullRepIterableTest method testiterator2Rev.

public void testiterator2Rev() {
    Iterator iterator = new FullRepIterable("var1", true).iterator();
    assertTrue(iterator.hasNext());
    assertEquals("var1", iterator.next());
    assertFalse(iterator.hasNext());
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) Iterator(java.util.Iterator)

Example 4 with FullRepIterable

use of org.python.pydev.shared_core.string.FullRepIterable in project Pydev by fabioz.

the class FullRepIterableTest method testiterator3Rev.

public void testiterator3Rev() {
    Iterator iterator = new FullRepIterable("testlib.unittest.relative", true).iterator();
    assertTrue(iterator.hasNext());
    assertEquals("testlib.unittest.relative", iterator.next());
    assertEquals("testlib.unittest", iterator.next());
    assertEquals("testlib", iterator.next());
    assertFalse(iterator.hasNext());
    int i = 0;
    for (String dummy : new FullRepIterable("testlib.unittest.relative")) {
        i++;
    }
    assertEquals(3, i);
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) Iterator(java.util.Iterator)

Example 5 with FullRepIterable

use of org.python.pydev.shared_core.string.FullRepIterable in project Pydev by fabioz.

the class UndefinedVariableQuickFixCreator method createImportQuickProposalsFromMarkerSelectedText.

public static void createImportQuickProposalsFromMarkerSelectedText(IAdaptable projectAdaptable, PySelection ps, int offset, IPythonNature initialNature, List<ICompletionProposalHandle> props, ICodeCompletionASTManager astManager, int start, int end, boolean forceReparseOnApply) throws BadLocationException {
    ps.setSelection(start, end);
    String markerContents = ps.getSelectedText();
    String fullRep = ps.getFullRepAfterSelection();
    IModulesManager projectModulesManager = astManager.getModulesManager();
    IModulesManager[] managersInvolved = projectModulesManager.getManagersInvolved(true);
    boolean doIgnoreImportsStartingWithUnder = AnalysisPreferences.doIgnoreImportsStartingWithUnder(projectAdaptable);
    // Use a single buffer to create all the strings
    FastStringBuffer buffer = new FastStringBuffer();
    // Helper so that we don't add the same module multiple times.
    Set<Tuple<String, String>> mods = new HashSet<Tuple<String, String>>();
    for (IModulesManager iModulesManager : managersInvolved) {
        Set<String> allModules = iModulesManager.getAllModuleNames(false, markerContents.toLowerCase());
        // when an undefined variable is found, we can:
        // - add an auto import (if it is a class or a method or some global attribute)
        // - declare it as a local or global variable
        // - change its name to some other global or local (mistyped)
        // - create a method or class for it (if it is a call)
        // 1. check if it is some module
        CompareContext compareContext = new CompareContext(iModulesManager.getNature());
        for (String completeName : allModules) {
            FullRepIterable iterable = new FullRepIterable(completeName);
            for (String mod : iterable) {
                if (fullRep.startsWith(mod)) {
                    if (// it does not only start with, but it is equal to it.
                    fullRep.length() == mod.length() || (fullRep.length() > mod.length() && fullRep.charAt(mod.length()) == '.')) {
                        buffer.clear();
                        String realImportRep = buffer.append("import ").append(mod).toString();
                        buffer.clear();
                        String displayString = buffer.append("Import ").append(mod).toString();
                        addProp(props, realImportRep, displayString, IInfo.USE_PACKAGE_ICON, offset, mods, compareContext, forceReparseOnApply);
                    }
                }
                String[] strings = FullRepIterable.headAndTail(mod);
                String packageName = strings[0];
                String importRep = strings[1];
                if (importRep.equals(markerContents)) {
                    if (packageName.length() > 0) {
                        buffer.clear();
                        String realImportRep = buffer.append("from ").append(packageName).append(" ").append("import ").append(strings[1]).toString();
                        buffer.clear();
                        String displayString = buffer.append("Import ").append(importRep).append(" (").append(packageName).append(")").toString();
                        addProp(props, realImportRep, displayString, IInfo.USE_PACKAGE_ICON, offset, mods, compareContext, forceReparseOnApply);
                    } else {
                        buffer.clear();
                        String realImportRep = buffer.append("import ").append(strings[1]).toString();
                        buffer.clear();
                        String displayString = buffer.append("Import ").append(importRep).toString();
                        addProp(props, realImportRep, displayString, IInfo.USE_PACKAGE_ICON, offset, mods, compareContext, forceReparseOnApply);
                    }
                }
            }
        }
    }
    // 2. check if it is some global class or method
    List<AbstractAdditionalTokensInfo> additionalInfo;
    try {
        additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfo(initialNature);
    } catch (MisconfigurationException e) {
        return;
    }
    FastStringBuffer tempBuf = new FastStringBuffer();
    for (AbstractAdditionalTokensInfo info : additionalInfo) {
        Collection<IInfo> tokensEqualTo = info.getTokensEqualTo(markerContents, AbstractAdditionalTokensInfo.TOP_LEVEL);
        for (IInfo found : tokensEqualTo) {
            // there always is a declaring module
            String name = found.getName();
            String declPackage = found.getDeclaringModuleName();
            String declPackageWithoutInit = declPackage;
            if (declPackageWithoutInit.endsWith(".__init__")) {
                declPackageWithoutInit = declPackageWithoutInit.substring(0, declPackageWithoutInit.length() - 9);
            }
            declPackageWithoutInit = AnalysisPreferences.removeImportsStartingWithUnderIfNeeded(declPackageWithoutInit, tempBuf, doIgnoreImportsStartingWithUnder);
            buffer.clear();
            String importDeclaration = buffer.append("from ").append(declPackageWithoutInit).append(" import ").append(name).toString();
            buffer.clear();
            String displayImport = buffer.append("Import ").append(name).append(" (").append(declPackage).append(")").toString();
            addProp(props, importDeclaration, displayImport, found.getType(), offset, mods, new CompareContext(found.getNature()), forceReparseOnApply);
        }
    }
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IModulesManager(org.python.pydev.core.IModulesManager) ICompareContext(org.python.pydev.shared_core.code_completion.IPyCompletionProposal.ICompareContext) CompareContext(org.python.pydev.ast.codecompletion.ProposalsComparator.CompareContext) IInfo(org.python.pydev.core.IInfo) FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Aggregations

FullRepIterable (org.python.pydev.shared_core.string.FullRepIterable)18 Iterator (java.util.Iterator)7 IToken (org.python.pydev.core.IToken)6 MisconfigurationException (org.python.pydev.core.MisconfigurationException)4 Found (com.python.pydev.analysis.visitors.Found)3 CompareContext (org.python.pydev.ast.codecompletion.ProposalsComparator.CompareContext)3 IModule (org.python.pydev.core.IModule)3 IModulesManager (org.python.pydev.core.IModulesManager)3 IterTokenEntry (org.python.pydev.core.IterTokenEntry)3 ICompareContext (org.python.pydev.shared_core.code_completion.IPyCompletionProposal.ICompareContext)3 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)3 HashSet (java.util.HashSet)2 IFilter (org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter)2 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)2 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)2 ModulesKey (org.python.pydev.core.ModulesKey)2 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)2 TokensList (org.python.pydev.core.TokensList)2 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)2 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)1