def get_completions(self, document: Document,
                        complete_event: CompleteEvent) -> Iterable[Completion]:
        text = document.get_word_under_cursor()

        # Complete only when we have at least the minimal input length,
        # otherwise, we can too many results and autocompletion will become too
        # heavy.
        if len(text) < self.min_input_len:
            return

        try:
            # Do tilde expansion.
            if self.expanduser:
                text = os.path.expanduser(text)

            # Directories where to look.
            dirname = os.path.dirname(text)
            if dirname:
                directories = [
                    os.path.dirname(os.path.join(p, text))
                    for p in self.get_paths()
                ]
            else:
                directories = self.get_paths()

            # Start of current file.
            prefix = os.path.basename(text)

            # Get all filenames.
            filenames = []
            for directory in directories:
                # Look for matches in this directory.
                if os.path.isdir(directory):
                    for filename in os.listdir(directory):
                        if filename.startswith(prefix):
                            filenames.append((directory, filename))

            # Sort
            filenames = sorted(filenames, key=lambda k: k[1])

            # Yield them.
            for directory, filename in filenames:
                completion = filename[len(prefix):]
                full_name = os.path.join(directory, filename)

                if os.path.isdir(full_name):
                    # For directories, add a slash to the filename.
                    # (We don't add them to the `completion`. Users can type it
                    # to trigger the autocompletion themselves.)
                    filename += '/'
                elif self.only_directories:
                    continue

                if not self.file_filter(full_name):
                    continue

                yield Completion(completion, 0, display=filename)
        except OSError:
            pass
Esempio n. 2
0
    def get_completions(self, document: Document, complete_event):
        try:
            line = document.current_line
            args = shlex.split(document.current_line)
            if len(args) > 0:
                curr_word = document.get_word_under_cursor(WORD=True)
                first_word = shlex.split(document.current_line)[0]
                previous_word = shlex.split(
                    document.current_line_before_cursor)[
                        len(shlex.split(document.current_line_before_cursor)) -
                        1]

                for i in sql_completions(document):
                    if i.text.startswith(
                            curr_word.upper()) or i.text.startswith(curr_word):
                        yield i

        except (ValueError, NameError):
            pass
Esempio n. 3
0
 def get_completions(self, document : Document, complete_event : CompleteEvent) -> Completion:
     for cmd, color in zip((MyCompleter.CMD + MyCompleter.OTHER), (('bg:ansiblack fg:ansired',) * len(MyCompleter.CMD)) + (('bg:ansiyellow fg:ansiblack',) * len(MyCompleter.OTHER))):
         last_word = document.get_word_under_cursor()
         if cmd.startswith(last_word) and not last_word == '':
             yield Completion(cmd, start_position=-len(last_word), style=color)