本文整理汇总了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"])
示例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"]
)
示例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)
示例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)
示例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),
)
示例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)
示例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)
示例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)
示例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)
示例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))
示例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
示例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
示例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])
示例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()
示例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