Repository navigation
Completion
turboframe.ui.completion has the two boxes an editor shows next to the cursor: the list of completions, and a box with a call's signature or an item's documentation. Both look like Turbo Vision menus, cast a shadow, and never take the focus: the editor keeps it and forwards the keys.
| Use it for | a completion list and a signature box in an editor: TurboFrame's CodeEditor, or a TextArea of your own |
| Import | from turboframe.ui.completion import CARET, Completion, CompletionResult, CompletionPopup, InfoPopup, signature_lines |
| Needs | nothing beyond TurboFrame |
| Extend | your own provider: any code that returns a CompletionResult
|
There are three ways to use it, from the least work to the most:
-
A language server:
LspMixinfillsCodeEditor's boxes from a server (see The Editor). Nothing to write but theServerSpec. How it behaves with a slow server is there too. -
Your own provider in
CodeEditor:CodeEditoralready owns both boxes and forwards the keys. Give your application anlsp_complete(editor, explicit)that answers witheditor.show_completion(result, explicit), as below. - Your own editor: own the popups yourself, as the last section shows.
A provider, such as a language's own completion or a language server, looks at the text before the cursor and returns a CompletionResult:
from turboframe.ui.completion import CARET, Completion, CompletionResult
def complete(text: str, cursor: int) -> CompletionResult | None:
start = cursor
while start and (text[start - 1].isalnum() or text[start - 1] == "_"):
start -= 1
prefix = text[start:cursor]
names = ["print", "property", "pow"]
return CompletionResult(start, prefix, [
Completion(name, f"{name}({CARET})", detail="builtin") # CARET: where the cursor goes
for name in names if name.startswith(prefix)
])CompletionResult(start, prefix, items=[], context=""): where the replaced text starts, what was typed, and the Completion(label, insert, detail="", after="", retrigger=False, extra=(), data=None) items. CARET is a character you put in insert where the cursor goes.
Accepting an item replaces text[start:cursor] with its insert. after is inserted after the cursor unless it is already there, extra holds other edits (an import line), retrigger completes again at once, and data keeps where the item came from, to ask for its documentation later.
In CodeEditor, the provider is a method of your application, which the editor finds (see The Editor):
class MyApp(EditorMixin, DesktopApp):
def lsp_complete(self, editor, explicit: bool) -> None: # typing, or Ctrl+Space
cursor = editor.document.get_index_from_location(editor.cursor_location)
editor.show_completion(complete(editor.text, cursor), explicit)explicit is True when the user asked (Ctrl+Space), False while typing: pass it on. A result that is None or has no items hides the list.
The editor owns the popups, shows them, and implements accept_completion(), which the list calls on a click:
from turboframe.ui.completion import CompletionPopup, InfoPopup, signature_lines
class Editor(TextArea):
def on_mount(self) -> None:
self.popup = CompletionPopup(self)
self.info = InfoPopup("signature")
self.screen.mount(self.popup, self.info)
self.popup.display = self.info.display = False
def show_completion(self, result, explicit: bool) -> None:
x, y = self.cursor_screen_offset
self.popup.show(result, explicit=explicit, at=(x, y), screen_size=self.screen.size)
def accept_completion(self) -> None:
item = self.popup.selected
... # insert item.insert, place the cursor at CARET
self.popup.display = FalseWhile the list is open, the editor forwards Up and Down to popup.move(-1) and move(1), PgUp and PgDn to page(-1) and page(1), Enter and Tab to accept_completion(), and Esc hides the list. Everything else is typed into the editor, and the editor shows the list again with the new result: if the start is the same, the selected item stays selected.
The list opens under the cursor, or above it near the bottom of the screen, and keeps room for its shadow. Past ten items its bottom border counts them (12/25).
signature_lines(label, start, end, doc, width) lays a signature out for InfoPopup.show(lines, x=, y=, mark=): it returns (lines, mark), where the parameter being typed (label[start:end]) is highlighted, a long label is cut around it, and the documentation is wrapped under it. InfoPopup.hide() closes the box. CompletionPopup.MAX_ROWS (10) and MAX_WIDTH (60), and InfoPopup.MAX_WIDTH (72) and MAX_ROWS (12), size them.
Colours come from the theme ($surface, $text, $text-muted, $accent); see Themes and Colors.
API: turboframe.ui.completion.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links