From fe63e3bc9796950d27af0167f632ddbd15824c83 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Sun, 28 Jul 2013 15:13:09 -0500 Subject: [PATCH] Add printing for all the different kinds of completions in sith.py run This kind of printing should probably go in the objects themselves, but I was too disgusted by the completion APIs to even begin to start extending the code for that (it needs to be cleaned up first). --- sith.py | 59 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/sith.py b/sith.py index 31c97e33..f75c81d3 100755 --- a/sith.py +++ b/sith.py @@ -108,16 +108,16 @@ class TestCase(object): column = random.randint(0, len(lines[line - 1])) return cls(operation, path, line, column) - def run(self, debugger, record=None, print_completions=False): + def run(self, debugger, record=None, print_result=False): try: with open(self.path) as f: self.file = f.read() self.script = jedi.Script(self.file, self.line, self.column, self.path) self.completions = getattr(self.script, self.operation)() - if print_completions: - self.show_location() - self.show_completions() + if print_result: + self.show_location(self.line, self.column) + self.show_operation() except jedi.NotFoundError: pass except Exception: @@ -135,20 +135,55 @@ class TestCase(object): pdb.post_mortem(einfo[2]) exit(1) - def show_location(self): + def show_location(self, lineno, column, show=3): # Three lines ought to be enough - show = 3 - lower = self.line - show if self.line - show > 0 else 0 - for i, line in enumerate(self.file.split('\n')[lower:self.line]): + lower = lineno - show if lineno - show > 0 else 0 + for i, line in enumerate(self.file.split('\n')[lower:lineno]): print(lower + i + 1, line) - print(' '*(self.column + len(str(self.line))), '^') + print(' '*(column + len(str(lineno))), '^') + + def show_operation(self): + print("%s:\n" % self.operation.capitalize()) + if self.operation == 'completions': + self.show_completions() + elif self.operation == 'goto_assignments': + self.show_goto_assignments() + elif self.operation == 'goto_definitions': + self.show_goto_definitions() + elif self.operation == 'usages': + self.show_usages() + elif self.operation == 'call_signatures': + self.show_call_signatures() def show_completions(self): - print("Completions:") - print() for completion in self.completions: print(completion.name) + # TODO: Support showing the location in other files + + # TODO: Move this printing to the completion objects themselves + def show_usages(self): + for completion in self.completions: + print(completion.description) + if os.path.abspath(completion.module_path) == os.path.abspath(self.path): + self.show_location(completion.line, completion.column) + + def show_call_signatures(self): + for completion in self.completions: + # This is too complicated to print. It really should be + # implemented in str() anyway. + print(completion) + # Can't print the location here because we don't have the module path + + + def show_goto_definitions(self): + for completion in self.completions: + print(completion.desc_with_module) + if os.path.abspath(completion.module_path) == os.path.abspath(self.path): + self.show_location(completion.line, completion.column) + + show_goto_assignments = show_goto_definitions + def show_errors(self): print(self.traceback) print(("Error with running Script(...).{operation}() with\n" @@ -175,7 +210,7 @@ def main(arguments): elif arguments['run']: TestCase(arguments[''], arguments[''], int(arguments['']), int(arguments['']) - ).run(debugger, print_completions=True) + ).run(debugger, print_result=True) else: for _ in range(int(arguments['--maxtries'])): t = TestCase.generate(arguments[''] or '.')