| 39 | # This class was gotten from IPython.completer (dir2 was replaced with the completer already in pydev) |
| 40 | # ======================================================================================================================= |
| 41 | class Completer: |
| 42 | def __init__(self, namespace=None, global_namespace=None): |
| 43 | """Create a new completer for the command line. |
| 44 | |
| 45 | Completer([namespace,global_namespace]) -> completer instance. |
| 46 | |
| 47 | If unspecified, the default namespace where completions are performed |
| 48 | is __main__ (technically, __main__.__dict__). Namespaces should be |
| 49 | given as dictionaries. |
| 50 | |
| 51 | An optional second namespace can be given. This allows the completer |
| 52 | to handle cases where both the local and global scopes need to be |
| 53 | distinguished. |
| 54 | |
| 55 | Completer instances should be used as the completion mechanism of |
| 56 | readline via the set_completer() call: |
| 57 | |
| 58 | readline.set_completer(Completer(my_namespace).complete) |
| 59 | """ |
| 60 | |
| 61 | # Don't bind to namespace quite yet, but flag whether the user wants a |
| 62 | # specific namespace or to use __main__.__dict__. This will allow us |
| 63 | # to bind to __main__.__dict__ at completion time, not now. |
| 64 | if namespace is None: |
| 65 | self.use_main_ns = 1 |
| 66 | else: |
| 67 | self.use_main_ns = 0 |
| 68 | self.namespace = namespace |
| 69 | |
| 70 | # The global namespace, if given, can be bound directly |
| 71 | if global_namespace is None: |
| 72 | self.global_namespace = {} |
| 73 | else: |
| 74 | self.global_namespace = global_namespace |
| 75 | |
| 76 | def complete(self, text): |
| 77 | """Return the next possible completion for 'text'. |
| 78 | |
| 79 | This is called successively with state == 0, 1, 2, ... until it |
| 80 | returns None. The completion should begin with 'text'. |
| 81 | |
| 82 | """ |
| 83 | if self.use_main_ns: |
| 84 | # In pydev this option should never be used |
| 85 | raise RuntimeError("Namespace must be provided!") |
| 86 | self.namespace = __main__.__dict__ # @UndefinedVariable |
| 87 | |
| 88 | if "." in text: |
| 89 | return self.attr_matches(text) |
| 90 | else: |
| 91 | return self.global_matches(text) |
| 92 | |
| 93 | def global_matches(self, text): |
| 94 | """Compute matches when text is a simple name. |
| 95 | |
| 96 | Return a list of all keywords, built-in functions and names currently |
| 97 | defined in self.namespace or self.global_namespace that match. |
| 98 |
no outgoing calls
no test coverage detected