def testDetectModuleCompletionInitial(self): source = dedent("""\ from Nested.Package.Mo """) line = "from Nested.Package.Mo" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module='Nested.Package', showMembers=False) self.assertEquals(options, expected) source = dedent("""\ from Module. """) line = "from Module." options = detectCompletionType('Module.py', source, 1, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.MODULE, module='Module', showMembers=False) self.assertEquals(options, expected) source = dedent("""\ from Mo """) line = "from Mo" options = detectCompletionType('Module.py', source, 1, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.MODULE, module='Mo', showMembers=False) self.assertEquals(options, expected)
def testDetectModuleCompletionTwo(self): source = dedent("""\ from Nested.Package import """) line = "from Nested.Package import " options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested.Package", showMembers=True) self.assertEquals(options, expected) source = dedent("""\ from Nested import """) line = "from Nested import " options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested", showMembers=True) self.assertEquals(options, expected) source = dedent("""\ from Nested import Pack """) line = "from Nested import Pack" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested", showMembers=True) self.assertEquals(options, expected)
def testDetectModuleCompletionInitial(self): source = dedent("""\ from Nested.Package.Mo """) line = "from Nested.Package.Mo" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module='Nested.Package', showMembers=False) self.assertEquals(options, expected) source = dedent("""\ from Module. """) line = "from Module." options = detectCompletionType('Module.py', source, 1, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.MODULE, module='Module', showMembers=False) self.assertEquals(options, expected) source = dedent("""\ from Mo """) line = "from Mo" options = detectCompletionType('Module.py', source, 1, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.MODULE, module='Mo', showMembers=False) self.assertEquals(options, expected)
def testDetectModuleCompletionTwo(self): source = dedent("""\ from Nested.Package import """) line = "from Nested.Package import " options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Nested.Package", showMembers=True) self.assertEquals(options, expected) source = dedent("""\ from Nested import """) line = "from Nested import " options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Nested", showMembers=True) self.assertEquals(options, expected) source = dedent("""\ from Nested import Pack """) line = "from Nested import Pack" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Nested", showMembers=True) self.assertEquals(options, expected)
def main(): cur_file = os.environ.get("TM_FILEPATH") source = sys.stdin.read() line_no = int(os.environ.get("TM_LINE_NUMBER")) cur_col = int(os.environ.get("TM_LINE_INDEX")) PYSMELLDICT = idehelper.findPYSMELLDICT(cur_file) line = source.splitlines()[line_no - 1] index = vimhelper.findBase(line, cur_col) base = line[index:cur_col] options = idehelper.detectCompletionType(cur_file, source, line_no, cur_col, base, PYSMELLDICT) completions = idehelper.findCompletions(base, PYSMELLDICT, options) if not completions: write('No completions found') sys.exit(206) #magic code for tooltip if len(completions) == 1: new_word = completions[0]['word'] write(new_word) elif len(completions) > 1: dialogTuples = [ ( "%s - %s" % (comp.get('abbr', comp['word']), comp.get('menu', '')), index) for index, comp in enumerate(completions) ] compIndex = dialog.menu(dialogTuples) if compIndex is not None: write(completions[compIndex]['word'])
def testDetectCompleteArgumentListFunction(self): source = dedent("""\ def a(): b( """) line = ' b(' options = detectCompletionType('path', source, 2, len(line), 'b(', self.pysmelldict) expected = CompletionOptions(Types.FUNCTION, name='b', rindex=None) self.assertEquals(options, expected)
def testDetectCompleteArgumentListMethod(self): line = 'salf.bm(' options = detectCompletionType('path', line, 1, len(line), 'bm(', self.pysmelldict) expected = CompletionOptions(Types.METHOD, klass=None, parents=[], name='bm', rindex=None) self.assertEquals(options, expected)
def testDetectSimpleClass(self): source = dedent("""\ class aClass(object): def sth(self): self. """) line = "%sself." % (' ' * 8) options = detectCompletionType('Module.py', source, 3, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.INSTANCE, klass='Module.aClass', parents=['object']) self.assertEquals(options, expected)
def testDetectModuleAttrLookup(self): source = dedent("""\ from Nested.Package import Module as mod mod. """) line = "mod." options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 3, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Nested.Package.Module", showMembers=True) self.assertEquals(options, expected)
def testDetectModuleAttrLookup3(self): source = dedent("""\ from Nested import Package funct(Package.Module., arg) """) line = "funct(Package.Module." options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 3, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested.Package.Module", showMembers=True) self.assertEquals(options, expected)
def testDetectModuleAttrLookupWithBase(self): source = dedent("""\ from Nested.Package import Module as mod func(mod.some, arg) """) line = "func(mod.some" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 3, len(line), 'some', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested.Package.Module", showMembers=True) self.assertEquals(options, expected)
def testDetectCompleteArgumentListFunctionClosingParen(self): source = dedent("""\ def a(): b() """) line = ' b()' options = detectCompletionType('path', source, 2, len(line) - 1, 'b(', self.pysmelldict) expected = CompletionOptions(Types.FUNCTION, name='b', rindex=-1) # module? self.assertEqual(options, expected)
def testModuleCompletionThree(self): source = dedent("""\ import Nested.Package. """) line = "import Nested.Package." options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Nested.Package", showMembers=False) self.assertEquals(options, expected) source = dedent("""\ import Ne """) line = "import Ne" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.MODULE, module="Ne", showMembers=False) self.assertEquals(options, expected)
def testDetectClassCreation(self): source = dedent("""\ from Module import aClass thing = aClass() thing. """) line = "thing." options = detectCompletionType('apath', source, 4, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.INSTANCE, klass='Module.aClass', parents=['object', 'ForeignModule.alien']) self.assertEquals(options, expected)
def testDetectDeepClass(self): source = dedent("""\ class Class(object): def sth(self): self. """) line = "%sself." % (' ' * 8) options = detectCompletionType(os.path.join('Nested', 'Package', 'Module.py'), source, 3, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.INSTANCE, klass='Nested.Package.Module.Class', parents=['object']) self.assertEquals(options, expected)
def testDetectDeepClass(self): source = dedent("""\ class Class(object): def sth(self): self. """) line = "%sself." % (' ' * 8) options = detectCompletionType(os.path.join('Nested', 'Package', 'Module.py'), source, 3, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.INSTANCE, klass='Nested.Package.Module.Class', parents=['object']) self.assertEquals(options, expected)
def testDetectParentsOfUnknownClass(self): source = dedent("""\ from Nested.Package.Module import Class class Other(Class): def sth(self): self. """) line = "%sself." % (' ' * 8) options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 4, len(line), '', NESTEDDICT) expected = CompletionOptions(Types.INSTANCE, klass='PackageA.Module.Other', parents=['Nested.Package.Module.Class']) self.assertEquals(options, expected)
def testDetectParentsOfUnknownClass(self): source = dedent("""\ from Nested.Package.Module import Class class Other(Class): def sth(self): self. """) line = "%sself." % (' ' * 8) options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 4, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.INSTANCE, klass='PackageA.Module.Other', parents=['Nested.Package.Module.Class']) self.assertEquals(options, expected)
def testDetectClassCreationLocal(self): source = dedent("""\ class aClass(object): pass thing = aClass() thing. """) line = "thing." options = detectCompletionType(os.path.abspath('Module.py'), source, 5, len(line), '', self.pysmelldict) expected = CompletionOptions(Types.INSTANCE, klass='Module.aClass', parents=['object']) self.assertEquals(options, expected)
def testDetectModuleAttrLookupWithBase2(self): print '--------' source = dedent("""\ from Nested.Package import Module as mod class Some(object): def init(self): self.func(mod.EVT_, self.something) """) line = "%sself.func(mod.EVT_" % (" " * 8) options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 5, len(line), 'EVT_', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested.Package.Module", showMembers=True) self.assertEquals(options, expected)
def get_completions(fullPath, origSource, lineNo, origCol, matcher): """arguments: fullPath, origSource, lineNo, origCol, matcher When visiting the file at fullPath, with edited source origSource, find a list of possible completion strings for the symbol located at origCol on orgLineNo using matching mode matcher""" PYSMELLDICT = idehelper.findPYSMELLDICT(fullPath) if not PYSMELLDICT: return origLine = origSource.splitlines()[lineNo - 1] base = split("[,.\-+/|\[\]]", origLine[:origCol].strip())[-1] options = idehelper.detectCompletionType(fullPath, origSource, lineNo, origCol, base, PYSMELLDICT) completions = [completion['word'] for completion in idehelper.findCompletions(base, PYSMELLDICT, options, matcher)] completions = list(_uniquify(completions)) return completions
def testModuleCompletionThree(self): source = dedent("""\ import Nested.Package. """) line = "import Nested.Package." options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Nested.Package", showMembers=False) self.assertEqual(options, expected) source = dedent("""\ import Ne """) line = "import Ne" options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 1, len(line), '', NESTEDDICT, update=False) expected = CompletionOptions(Types.MODULE, module="Ne", showMembers=False) self.assertEqual(options, expected)
def testInferShouldUpdatePYSMELLDICT(self): source = dedent("""\ from Nested.Package.Module import Class class FreshClass(Class): something = 1 def sth(self): self. """) line = "%sself." % (' ' * 8) copiedDict = copy.deepcopy(self.pysmelldict) assert copiedDict == self.pysmelldict options = detectCompletionType(os.path.join('TestData', 'PackageA', 'Module.py'), source, 5, len(line), '', copiedDict) expected = CompletionOptions(Types.INSTANCE, klass='PackageA.Module.FreshClass', parents=['Nested.Package.Module.Class']) self.assertEquals(options, expected) #sanity klass = copiedDict['CLASSES']['PackageA.Module.FreshClass'] self.assertEquals(klass['bases'], ['Nested.Package.Module.Class']) self.assertEquals(klass['properties'], ['something']) self.assertEquals(klass['methods'], [('sth', [], "")])
def _main(cur_file, line_no, cur_col): if not cur_file: write('No filename - is the file saved?') return TOOLTIP source = sys.stdin.read() PYSMELLDICT = idehelper.findPYSMELLDICT(cur_file) if PYSMELLDICT is None: write('No PYSMELLTAGS found - you have to generate one.') return TOOLTIP line = source.splitlines()[line_no - 1] index = idehelper.findBase(line, cur_col) base = line[index:cur_col] options = idehelper.detectCompletionType(cur_file, source, line_no, cur_col, base, PYSMELLDICT) completions = idehelper.findCompletions(base, PYSMELLDICT, options) if not completions: write('No completions found') return TOOLTIP if len(completions) == 1: new_word = completions[0]['word'] write(new_word[len(base):]) elif len(completions) > 1: dialogTuples = [ ("%s - %s" % (comp.get('abbr', comp['word']), comp.get('menu', '')), index) for index, comp in enumerate(completions) ] try: compIndex = tm_dialog.menu(dialogTuples) except Exception as e: import traceback write(traceback.format_exc(e)) return TOOLTIP if compIndex is not None: write(completions[compIndex]['word'][len(base):])
def _main(cur_file, line_no, cur_col): if not cur_file: write('No filename - is the file saved?') return TOOLTIP source = sys.stdin.read() PYSMELLDICT = idehelper.findPYSMELLDICT(cur_file) if PYSMELLDICT is None: write('No PYSMELLTAGS found - you have to generate one.') return TOOLTIP line = source.splitlines()[line_no - 1] index = idehelper.findBase(line, cur_col) base = line[index:cur_col] options = idehelper.detectCompletionType(cur_file, source, line_no, cur_col, base, PYSMELLDICT) completions = idehelper.findCompletions(base, PYSMELLDICT, options) if not completions: write('No completions found') return TOOLTIP if len(completions) == 1: new_word = completions[0]['word'] write(new_word[len(base):]) elif len(completions) > 1: dialogTuples = [ ( "%s - %s" % (comp.get('abbr', comp['word']), comp.get('menu', '')), index) for index, comp in enumerate(completions) ] try: compIndex = tm_dialog.menu(dialogTuples) except Exception, e: import traceback write(traceback.format_exc(e)) return TOOLTIP if compIndex is not None: write(completions[compIndex]['word'][len(base):])
def testDetectGlobalLookup(self): options = detectCompletionType('path', 'b', 1, 1, 'b', self.pysmelldict) expected = CompletionOptions(Types.TOPLEVEL) self.assertEquals(options, expected)
def testDetectAttrLookup(self): line = 'somethign.a' options = detectCompletionType('path', line, 1, len(line), 'a', self.pysmelldict) expected = CompletionOptions(Types.INSTANCE, klass=None, parents=[]) self.assertEquals(options, expected)
def testDetectCompleteArgumentListMethod(self): line = 'salf.bm(' options = detectCompletionType('', line, 1, len(line), 'bm(', self.pysmelldict) expected = CompletionOptions(Types.METHOD, klass=None, parents=[], name='bm', rindex = None) self.assertEquals(options, expected)
def testDetectCompleteArgumentListFunctionClosingParen(self): line = ' b()' options = detectCompletionType('', line, 1, len(line) - 1, 'b(', self.pysmelldict) expected = CompletionOptions(Types.FUNCTION, name='b', rindex=-1)# module? self.assertEquals(options, expected)
def testDetectCompleteArgumentListFunction(self): line = ' b(' options = detectCompletionType('', line, 1, len(line), 'b(', self.pysmelldict) expected = CompletionOptions(Types.FUNCTION, name='b', rindex=None) self.assertEquals(options, expected)
def testDetectGlobalLookup(self): options = detectCompletionType('', 'b', 1, 1, 'b', self.pysmelldict) expected = CompletionOptions(Types.TOPLEVEL) self.assertEquals(options, expected)
def testDetectAttrLookup(self): line = 'somethign.a' options = detectCompletionType('', line, 1, len(line), 'a', self.pysmelldict) expected = CompletionOptions(Types.INSTANCE, klass=None, parents=[]) self.assertEquals(options, expected)