Exemple #1
0
class PromptToolkitCompleter(Completer):
    """Simple prompt_toolkit Completer object.

    It just redirects requests to normal Xonsh completer.
    """
    def __init__(self, completer, ctx):
        """Takes instance of xonsh.completer.Completer and dict with context."""
        self.completer = completer
        self.completer_gitsome = CompleterGitsome()
        self.ctx = ctx

    def get_completions(self, document, complete_event):
        """Returns a generator for list of completions."""
        line = document.current_line
        endidx = document.cursor_position_col
        space_pos = document.find_backwards(' ')
        if space_pos is None:
            begidx = 0
        else:
            begidx = space_pos + endidx + 1
        prefix = line[begidx:endidx]
        completions = self.completer.complete(prefix, line, begidx, endidx,
                                              self.ctx)
        completions_gitsome = \
            self.completer_gitsome.get_completions(document,
                                                   complete_event)
        completions_with_meta = \
            self.completer_gitsome.build_completions_with_meta(line,
                                                               prefix,
                                                               completions)
        completions_gitsome.extend(completions_with_meta)
        for comp in completions_gitsome:
            yield comp
Exemple #2
0
class PromptToolkitCompleter(Completer):
    """Simple prompt_toolkit Completer object.

    It just redirects requests to normal Xonsh completer.
    """

    def __init__(self, completer, ctx, shell):
        """Takes instance of xonsh.completer.Completer, the xonsh execution
        context, and the shell instance itself.
        """
        self.completer = completer
        self.ctx = ctx
        self.shell = shell
        self.completer_gitsome = CompleterGitsome()

    def get_completions(self, document, complete_event):
        """Returns a generator for list of completions."""
        line = document.current_line.lstrip()
        endidx = document.cursor_position_col
        space_pos = document.find_backwards(' ')
        if space_pos is None:
            begidx = 0
        else:
            begidx = space_pos + endidx + 1
        prefix = line[begidx:endidx]
        completions, dummy = self.completer.complete(prefix,
                                                     line,
                                                     begidx,
                                                     endidx,
                                                     self.ctx)
        completions_gitsome = \
            self.completer_gitsome.get_completions(document,
                                                   complete_event)
        completions_with_meta = \
            self.completer_gitsome.build_completions_with_meta(line,
                                                               prefix,
                                                               completions)
        completions_gitsome.extend(completions_with_meta)
        for comp in completions_gitsome:
            yield comp
class CompleterTest(unittest.TestCase):
    def setUp(self):
        self.completer = CompleterGitsome()
        self.completer_event = self.create_completer_event()

    def create_completer_event(self):
        return mock.Mock()

    def _get_completions(self, command):
        position = len(command)
        result = self.completer.get_completions(
            Document(text=command, cursor_position=position),
            self.completer_event)
        return result

    def verify_completions(self, commands, expected):
        result = []
        for command in commands:
            # Call the AWS CLI autocompleter
            result.extend(self._get_completions(command))
        result_texts = []
        for item in result:
            # Each result item is a Completion object,
            # we are only interested in the text portion
            result_texts.append(item.text)
        assert result_texts
        if len(expected) == 1:
            assert expected[0] in result_texts
        else:
            for item in expected:
                assert item in result_texts

    def test_blank(self):
        text = ''
        expected = []
        result = self._get_completions(text)
        assert result == expected

    def test_no_completions(self):
        text = 'foo'
        expected = []
        result = self._get_completions(text)
        assert result == expected

    def test_command(self):
        text = ['g']
        expected = ['gh']
        self.verify_completions(text, expected)

    def test_subcommand(self):
        self.verify_completions(
            ['gh c'],
            ['configure', 'create-comment', 'create-issue', 'create-repo'])
        self.verify_completions(['gh e'], ['emails', 'emojis'])
        self.verify_completions(['gh f'], ['feed', 'followers', 'following'])
        self.verify_completions(['gh g'],
                                ['gitignore-template', 'gitignore-templates'])
        self.verify_completions(['gh i'], ['issue', 'issues'])
        self.verify_completions(['gh l'], ['license', 'licenses'])
        self.verify_completions(['gh m'], ['me'])
        self.verify_completions(['gh n'], ['notifications'])
        self.verify_completions(['gh o'], ['octo'])
        self.verify_completions(['gh p'], ['pull-request', 'pull-requests'])
        self.verify_completions(['gh r'], ['rate-limit', 'repo', 'repos'])
        self.verify_completions(['gh s'],
                                ['search-issues', 'search-repos', 'starred'])
        self.verify_completions(['gh t'], ['trending'])
        self.verify_completions(['gh u'], ['user'])
        self.verify_completions(['gh v'], ['view'])

    def test_args(self):
        self.verify_completions(['gh octo '], ['"Keep it logically awesome"'])

    def test_no_args_with_options(self):
        self.verify_completions(['gh octo '], ['"Keep it logically awesome"'])

    def test_options(self):
        self.verify_completions(['gh emojis '], ['-p', '--pager'])

    def test_multiple_options(self):
        self.verify_completions(['gh feed -pr --pa'], ['--pager'])
        self.verify_completions(['gh feed foo -pr --pa'], ['--pager'])

    def test_fuzzy(self):
        self.completer.fuzzy_match = True
        self.verify_completions(['gh ot'], ['octo'])

    def test_build_completions_with_meta(self):
        result = self.completer.build_completions_with_meta(
            'git ad', 'ad', ['add'])
        assert result[0].display_meta_text == 'Add file contents to the index.'
        result = self.completer.build_completions_with_meta(
            'git-alia', 'git-alia', ['git-alias'])
        assert result[
            0].display_meta_text == 'Define, search and show aliases.'
Exemple #4
0
class CompleterTest(unittest.TestCase):

    def setUp(self):
        self.completer = CompleterGitsome()
        self.completer_event = self.create_completer_event()

    def create_completer_event(self):
        return mock.Mock()

    def _get_completions(self, command):
        position = len(command)
        result = set(self.completer.get_completions(
            Document(text=command, cursor_position=position),
            self.completer_event))
        return result

    def verify_completions(self, commands, expected):
        result = set()
        for command in commands:
            # Call the AWS CLI autocompleter
            result.update(self._get_completions(command))
        result_texts = []
        for item in result:
            # Each result item is a Completion object,
            # we are only interested in the text portion
            result_texts.append(item.text)
        assert result_texts
        if len(expected) == 1:
            assert expected[0] in result_texts
        else:
            for item in expected:
                assert item in result_texts

    def test_blank(self):
        text = ''
        expected = set([])
        result = self._get_completions(text)
        assert result == expected

    def test_no_completions(self):
        text = 'foo'
        expected = set([])
        result = self._get_completions(text)
        assert result == expected

    def test_command(self):
        text = ['g']
        expected = ['gh']
        self.verify_completions(text, expected)

    def test_subcommand(self):
        self.verify_completions(['gh c'], ['configure', 'create-comment',
                                           'create-issue', 'create-repo'])
        self.verify_completions(['gh e'], ['emails', 'emojis'])
        self.verify_completions(['gh f'], ['feed', 'followers', 'following'])
        self.verify_completions(['gh g'], ['gitignore-template',
                                           'gitignore-templates'])
        self.verify_completions(['gh i'], ['issue', 'issues'])
        self.verify_completions(['gh l'], ['license', 'licenses'])
        self.verify_completions(['gh m'], ['me'])
        self.verify_completions(['gh n'], ['notifications'])
        self.verify_completions(['gh o'], ['octo'])
        self.verify_completions(['gh p'], ['pull-request', 'pull-requests'])
        self.verify_completions(['gh r'], ['rate-limit', 'repo', 'repos'])
        self.verify_completions(['gh s'], ['search-issues', 'search-repos',
                                           'starred'])
        self.verify_completions(['gh t'], ['trending'])
        self.verify_completions(['gh u'], ['user'])
        self.verify_completions(['gh v'], ['view'])

    def test_args(self):
        self.verify_completions(['gh octo '], ['"Keep it logically awesome"'])

    def test_no_args_with_options(self):
        self.verify_completions(['gh octo '], ['"Keep it logically awesome"'])

    def test_options(self):
        self.verify_completions(['gh emojis '],
                                ['-p', '--pager'])

    def test_multiple_options(self):
        self.verify_completions(['gh feed -pr --pa'], ['--pager'])
        self.verify_completions(['gh feed foo -pr --pa'], ['--pager'])

    def test_fuzzy(self):
        self.completer.fuzzy_match = True
        self.verify_completions(['gh ot'], ['octo'])

    def test_build_completions_with_meta(self):
        result = self.completer.build_completions_with_meta('git ad',
                                                            'ad',
                                                            ['add'])
        assert result[0].display_meta == 'Add file contents to the index.'
        result = self.completer.build_completions_with_meta('git-alia',
                                                            'git-alia',
                                                            ['git-alias'])
        assert result[0].display_meta == 'Define, search and show aliases.'