def testFindPYSMELLDICT(self): # include everything that starts with PYSMELLTAGS if os.path.exists('PYSMELLTAGS'): os.remove('PYSMELLTAGS') import pysmell.idehelper oldTryReadPSD = pysmell.idehelper.tryReadPYSMELLDICT oldListDir = pysmell.idehelper.listdir TRPArgs = [] def mockTRP(direct, fname, dictToUpdate): TRPArgs.append((direct, fname)) dictToUpdate.update({'PYSMELLTAGS.django': { 'django': True }}.get(fname, {})) listdirs = [] def mockListDir(dirname): listdirs.append(dirname) return { 'random': ['something'], os.path.join('random', 'dirA'): ['PYSMELLTAGS.django'], }.get(dirname, []) pysmell.idehelper.tryReadPYSMELLDICT = mockTRP pysmell.idehelper.listdir = mockListDir try: self.assertEquals( findPYSMELLDICT(os.path.join('a', 'random', 'path', 'andfile')), None, 'should not find PYSMELLTAGS') self.assertEquals( listdirs, [ os.path.join('a', 'random', 'path'), os.path.join('a', 'random'), 'a' ], # two '' because of root again 'did not list dirs correctly: %s' % listdirs) self.assertEquals(TRPArgs, [], 'did not read tags correctly') listdirs = [] TRPArgs = [] tags = findPYSMELLDICT(os.path.join('random', 'dirA', 'file')) self.assertEquals(tags, None, 'should not find pysmelltags') self.assertEquals(listdirs, [os.path.join('random', 'dirA'), 'random'], 'did not list dirs correctly: %s' % listdirs) self.assertEquals( TRPArgs, [(os.path.join('random', 'dirA'), 'PYSMELLTAGS.django')], 'did not read tags correctly: %s' % TRPArgs) finally: pysmell.idehelper.tryReadPYSMELLDICT = oldTryReadPSD pysmell.idehelper.listdir = oldListDir
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 testFindPYSMELLDICT(self): # include everything that starts with PYSMELLTAGS if os.path.exists('PYSMELLTAGS'): os.remove('PYSMELLTAGS') import pysmell.idehelper oldTryReadPSD = pysmell.idehelper.tryReadPYSMELLDICT oldListDir = pysmell.idehelper.listdir TRPArgs = [] def mockTRP(direct, fname, dictToUpdate): TRPArgs.append((direct, fname)) dictToUpdate.update({'PYSMELLTAGS.django': {'django': True}}.get(fname, {})) listdirs = [] def mockListDir(dirname): listdirs.append(dirname) return {'random': ['something'], os.path.join('random', 'dirA'): ['PYSMELLTAGS.django'], }.get(dirname, []) pysmell.idehelper.tryReadPYSMELLDICT = mockTRP pysmell.idehelper.listdir = mockListDir try: self.assertEquals(findPYSMELLDICT(os.path.join('a', 'random', 'path', 'andfile')), None, 'should not find PYSMELLTAGS') self.assertEquals(listdirs, [os.path.join('a', 'random', 'path'), os.path.join('a', 'random'), 'a'], # two '' because of root again 'did not list dirs correctly: %s' % listdirs) self.assertEquals(TRPArgs, [], 'did not read tags correctly') listdirs = [] TRPArgs = [] tags = findPYSMELLDICT(os.path.join('random', 'dirA', 'file')) self.assertEquals(tags, None, 'should not find pysmelltags') self.assertEquals(listdirs, [os.path.join('random', 'dirA'), 'random'], 'did not list dirs correctly: %s' % listdirs) self.assertEquals(TRPArgs, [(os.path.join('random', 'dirA'), 'PYSMELLTAGS.django')], 'did not read tags correctly: %s' % TRPArgs) finally: pysmell.idehelper.tryReadPYSMELLDICT = oldTryReadPSD pysmell.idehelper.listdir = oldListDir
def testPackageA(self): subprocess.call(["pysmell", "PackageA"], cwd='TestData') self.assertTrue(os.path.exists('TestData/PYSMELLTAGS')) PYSMELLDICT = eval(open('TestData/PYSMELLTAGS').read()) expectedDict = self.packageA self.assertDictsEqual(PYSMELLDICT, expectedDict) foundDict = idehelper.findPYSMELLDICT(os.path.join('TestData', 'PackageA', 'something')) self.assertDictsEqual(foundDict, expectedDict)
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 _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):])