當前位置: 首頁>>代碼示例>>Python>>正文


Python document.Document方法代碼示例

本文整理匯總了Python中prompt_toolkit.document.Document方法的典型用法代碼示例。如果您正苦於以下問題:Python document.Document方法的具體用法?Python document.Document怎麽用?Python document.Document使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在prompt_toolkit.document的用法示例。


在下文中一共展示了document.Document方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_completion

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_completion():
    @click.group()
    def foo_group():
        pass

    @foo_group.command()
    def foo_cmd():
        pass

    @click.group()
    def foobar_group():
        pass

    @foobar_group.command()
    def foobar_cmd():
        pass

    c = ClickCompleter(click.CommandCollection(sources=[foo_group, foobar_group]))
    completions = list(c.get_completions(Document(u"foo")))

    assert set(x.text for x in completions) == set([u"foo_cmd", u"foobar_cmd"]) 
開發者ID:click-contrib,項目名稱:click-repl,代碼行數:23,代碼來源:test_command_collection.py

示例2: test_completion

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_completion():
    @click.group()
    def root_command():
        pass

    @root_command.group()
    def first_level_command():
        pass

    @first_level_command.command()
    def second_level_command_one():
        pass

    @first_level_command.command()
    def second_level_command_two():
        pass

    c = ClickCompleter(root_command)
    completions = list(c.get_completions(Document(u"first_level_command ")))

    assert set(x.text for x in completions) == set(
        [u"second_level_command_one", u"second_level_command_two"]
    ) 
開發者ID:click-contrib,項目名稱:click-repl,代碼行數:25,代碼來源:test_basic.py

示例3: get_new_document

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def get_new_document(self, cursor_pos=None):
        """
        Create a `Document` instance that contains the resulting text.
        """
        lines = []

        # Original text, before cursor.
        if self.original_document.text_before_cursor:
            lines.append(self.original_document.text_before_cursor)

        # Selected entries from the history.
        for line_no in sorted(self.selected_lines):
            lines.append(self.history_lines[line_no])

        # Original text, after cursor.
        if self.original_document.text_after_cursor:
            lines.append(self.original_document.text_after_cursor)

        # Create `Document` with cursor at the right position.
        text = "\n".join(lines)
        if cursor_pos is not None and cursor_pos > len(text):
            cursor_pos = len(text)
        return Document(text, cursor_pos) 
開發者ID:prompt-toolkit,項目名稱:ptpython,代碼行數:25,代碼來源:history_browser.py

示例4: _get_expression_completions

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def _get_expression_completions(
        self,
        document: Document,
        complete_event: CompleteEvent,
        temp_locals: Dict[str, Any],
    ) -> Iterable[Completion]:
        """
        Complete the [ or . operator after an object.
        """
        match = self.expression_pattern.search(document.text_before_cursor)
        if match is not None:
            object_var = match.groups()[0]
            result = self._lookup(object_var, temp_locals)

            if isinstance(result, (list, tuple, dict)):
                yield Completion("[", 0)
            elif result:
                yield Completion(".", 0) 
開發者ID:prompt-toolkit,項目名稱:ptpython,代碼行數:20,代碼來源:completer.py

示例5: _get_attribute_completions

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def _get_attribute_completions(
        self,
        document: Document,
        complete_event: CompleteEvent,
        temp_locals: Dict[str, Any],
    ) -> Iterable[Completion]:
        """
        Complete attribute names.
        """
        match = self.attribute_lookup_pattern.search(document.text_before_cursor)
        if match is not None:
            object_var, attr_name = match.groups()

            # Do lookup of `object_var` in the context.
            result = self._lookup(object_var, temp_locals)

            for name in dir(result):
                if name.startswith(attr_name):
                    yield Completion(
                        name, -len(attr_name),
                    ) 
開發者ID:prompt-toolkit,項目名稱:ptpython,代碼行數:23,代碼來源:completer.py

示例6: test_cursor_position

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_cursor_position(self, mock_session):
        mock_document = Document(text='cat bar --foo bar', cursor_position=6)
        comp = ShellCompleter()
        Resource('bar', uuid='4c6d3711-61f1-4505-b8df-189d32b52872')
        Resource('foo', uuid='7413a49b-4f17-4340-b93f-7e03b29b5a9d')
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 1)

        mock_document = Document(text='ls -P  foo', cursor_position=6)
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 2)

        mock_document = Document(text='ln  foo', cursor_position=3)
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 2)

        mock_document = Document(text='ln  foo bar', cursor_position=3)
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 0)

        mock_document = Document(text='ln foo bar', cursor_position=6)
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 1) 
開發者ID:eonpatapon,項目名稱:contrail-api-cli,代碼行數:25,代碼來源:test_completer.py

示例7: test_fq_name_completion

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_fq_name_completion(self, mock_session):
        mock_document = Document(text='cat bar/default-dom')

        comp = ShellCompleter()
        r1 = Resource('bar', fq_name='default-domain:project:resource')
        r2 = Resource('bar', fq_name='foo:foo:foo')

        completions = list(comp.get_completions(mock_document, None))
        self.assertEqual(len(completions), 1)
        self.assertTrue(str(r1.path.relative_to(Context().shell.current_path)) in
                        [c.text for c in completions])

        r1.delete()
        r2.delete()
        completions = comp.get_completions(mock_document, None)
        self.assertEqual(len(list(completions)), 0) 
開發者ID:eonpatapon,項目名稱:contrail-api-cli,代碼行數:18,代碼來源:test_completer.py

示例8: get_completions

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def get_completions(self, document, complete_event):
        """Yield completions"""
        # Detect a file handle
        m = HSYM_RE.match(document.text_before_cursor)
        if m:
            text = m.group(1)
            doc = Document(text, len(text))
            for c in self.path_completer.get_completions(doc, complete_event):
                yield c
        else:
            # Get word/text before cursor.
            word_before_cursor = document.get_word_before_cursor(False)
            for words, meta in self.words_info:
                for a in words:
                    if a.startswith(word_before_cursor):
                        yield Completion(a, -len(word_before_cursor),
                                         display_meta=meta) 
開發者ID:KxSystems,項目名稱:pyq,代碼行數:19,代碼來源:ptk.py

示例9: test_undo_capitalize

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_undo_capitalize(self):
        buffer = Buffer()

        text = 'Selec'
        buffer.set_document(Document(text, len(text)))
        self.capitalizer.apply_capitalization(buffer)
        self.assertEqual('Selec', buffer.text)

        text = buffer.text + 't'
        buffer.set_document(Document(text, len(text)))
        self.capitalizer.apply_capitalization(buffer)
        self.assertEqual('SELECT', buffer.text)

        text = buffer.text + 'i'
        buffer.set_document(Document(text, len(text)))
        self.capitalizer.apply_capitalization(buffer)
        self.assertEqual('Selecti', buffer.text)

        text = buffer.text + 'on'
        buffer.set_document(Document(text, len(text)))
        self.capitalizer.apply_capitalization(buffer)
        self.assertEqual('Selection', buffer.text) 
開發者ID:crate,項目名稱:crash,代碼行數:24,代碼來源:test_repl.py

示例10: test_second_completion

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def test_second_completion(self):
        self.init3()
        doc = Document(u'crea ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --fun ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'command d ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --funtimes "life" --hello')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion(
            "--helloworld", -7)) 
開發者ID:Azure,項目名稱:azure-cli-shell,代碼行數:23,代碼來源:test_completion.py

示例11: command_prompt

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def command_prompt(pymux, variables):
    """
    Enter command prompt.
    """
    client_state = pymux.get_client_state()

    if variables['<command>']:
        # When a 'command' has been given.
        client_state.prompt_text = variables['<message>'] or '(%s)' % variables['<command>'].split()[0]
        client_state.prompt_command = variables['<command>']

        client_state.prompt_mode = True
        client_state.prompt_buffer.reset(Document(
            format_pymux_string(pymux, variables['<default>'] or '')))

        get_app().layout.focus(client_state.prompt_buffer)
    else:
        # Show the ':' prompt.
        client_state.prompt_text = ''
        client_state.prompt_command = ''

        get_app().layout.focus(client_state.command_buffer)

    # Go to insert mode.
    get_app().vi_state.input_mode = InputMode.INSERT 
開發者ID:prompt-toolkit,項目名稱:pymux,代碼行數:27,代碼來源:commands.py

示例12: get_completions

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def get_completions(
        self, document: Document, complete_event: CompleteEvent
    ) -> Iterable[Completion]:
        """
        This should be a generator that yields :class:`.Completion` instances.

        If the generation of completions is something expensive (that takes a
        lot of time), consider wrapping this `Completer` class in a
        `ThreadedCompleter`. In that case, the completer algorithm runs in a
        background thread and completions will be displayed as soon as they
        arrive.

        :param document: :class:`~prompt_toolkit.document.Document` instance.
        :param complete_event: :class:`.CompleteEvent` instance.
        """
        while False:
            yield 
開發者ID:prompt-toolkit,項目名稱:python-prompt-toolkit,代碼行數:19,代碼來源:base.py

示例13: get_common_complete_suffix

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def get_common_complete_suffix(
    document: Document, completions: Sequence[Completion]
) -> str:
    """
    Return the common prefix for all completions.
    """
    # Take only completions that don't change the text before the cursor.
    def doesnt_change_before_cursor(completion: Completion) -> bool:
        end = completion.text[: -completion.start_position]
        return document.text_before_cursor.endswith(end)

    completions2 = [c for c in completions if doesnt_change_before_cursor(c)]

    # When there is at least one completion that changes the text before the
    # cursor, don't return any common part.
    if len(completions2) != len(completions):
        return ""

    # Return the common prefix.
    def get_suffix(completion: Completion) -> str:
        return completion.text[-completion.start_position :]

    return _commonprefix([get_suffix(c) for c in completions2]) 
開發者ID:prompt-toolkit,項目名稱:python-prompt-toolkit,代碼行數:25,代碼來源:base.py

示例14: insert_comment

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def insert_comment(event: E) -> None:
    """
    Without numeric argument, comment all lines.
    With numeric argument, uncomment all lines.
    In any case accept the input.
    """
    buff = event.current_buffer

    # Transform all lines.
    if event.arg != 1:

        def change(line: str) -> str:
            return line[1:] if line.startswith("#") else line

    else:

        def change(line: str) -> str:
            return "#" + line

    buff.document = Document(
        text="\n".join(map(change, buff.text.splitlines())), cursor_position=0
    )

    # Accept input.
    buff.validate_and_handle() 
開發者ID:prompt-toolkit,項目名稱:python-prompt-toolkit,代碼行數:27,代碼來源:named_commands.py

示例15: get_sync_start_position

# 需要導入模塊: from prompt_toolkit import document [as 別名]
# 或者: from prompt_toolkit.document import Document [as 別名]
def get_sync_start_position(
        self, document: Document, lineno: int
    ) -> Tuple[int, int]:
        """
        Scan backwards, and find a possible position to start.
        """
        pattern = self._compiled_pattern
        lines = document.lines

        # Scan upwards, until we find a point where we can start the syntax
        # synchronisation.
        for i in range(lineno, max(-1, lineno - self.MAX_BACKWARDS), -1):
            match = pattern.match(lines[i])
            if match:
                return i, match.start()

        # No synchronisation point found. If we aren't that far from the
        # beginning, start at the very beginning, otherwise, just try to start
        # at the current line.
        if lineno < self.FROM_START_IF_NO_SYNC_POS_FOUND:
            return 0, 0
        else:
            return lineno, 0 
開發者ID:prompt-toolkit,項目名稱:python-prompt-toolkit,代碼行數:25,代碼來源:pygments.py


注:本文中的prompt_toolkit.document.Document方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。