Repository navigation
Example Language Server
examples/lsp/ puts a language server behind TurboFrame's editor: completion as you type, a call's signature, hovers, go to definition, references, go back, and the server's diagnostics drawn in the text and listed in a Problems window. It needs the lsp extra (pygls), and nothing else: the server is a toy one, beside the example, written with pygls in about 150 lines.
pip install "turboframe[lsp] @ git+https://github.com/divengine/turboframe@v0.3.0"
pip install "textual[syntax]" # optional: syntax colours
python examples/lsp/lsp_editor.py # opens examples/lsp/sample/shop.py
python examples/lsp/lsp_editor.py my.py # or a Python file of yours
| Key | Does |
|---|---|
| typing, Ctrl+Space | the completion list, with the selected item's documentation |
( and , in a call |
the call's signature, its parameter marked |
| F12 or Ctrl+click | go to the definition |
| Shift+F12 | the References window; Enter goes to one |
| F1 | the symbol's information (the hover) |
| Alt+Left | go back to where the last jump started |
| F8 | the Problems window |
| F2, Alt+X | save, exit (the server stops with the application) |
| File | What it is |
|---|---|
| lsp_editor.py | the application: LspMixin and EditorMixin on a DesktopApp
|
| toy_server.py | a language server for Python files, with regular expressions instead of a parser: completion of the names a file defines and of Python's keywords, signature help, hovers, definitions, references, and a warning on each TODO
|
| sample/shop.py | a file to try it on |
BOTTOM = 10 # lines for the References and Problems windows
TOY = ServerSpec("toy", (sys.executable, str(HERE / "toy_server.py")), language="python")
class PythonEditor(CodeEditor):
SMART_LANGUAGES = frozenset({"python"}) # a server knows it: completion, signatures, Ctrl+click
INDENT_AFTER = (":",) # Enter indents one level more after a colon
class PythonWindow(EditorWindow):
EDITOR = PythonEditor
class LspEditor(LspMixin, EditorMixin, DesktopApp):
TITLE = "Language server"
EDITOR_WINDOW = PythonWindow
LSP_SERVERS = (TOY,)- A
ServerSpecnames a server, its command and the language it serves. The first server ofLSP_SERVERSwhose command is found serves the files of its language. A real server plugs in the same way:ServerSpec("pylsp", ("pylsp",), language="python"). See LSP Helpers. -
SMART_LANGUAGEStells the editor which languages a server knows, so that typing opens the completion list and Ctrl+click goes to a definition. -
LspMixinfollows the editors (after_open,editor_changed,after_save,after_close): the server starts when the first Python file opens and hears of every change. It answers the editor's requests and fills its diagnostics.
The commands are constants of turboframe.ui.lsp (CM_LSP_DEFINITION, CM_LSP_REFERENCES, CM_LSP_HOVER, CM_LSP_BACK, CM_LSP_PROBLEMS, CM_LSP_RESTART), dispatched by lsp_command:
@work(group="command")
async def run_command(self, command: int, info: object = None) -> None:
if command == Command.QUIT:
if await self.close_all():
await self.lsp_shutdown() # every server stops before the application exits
self.exit()
elif not await self.lsp_command(command, info):
await self.editor_command(command, info)Two placement hooks keep the editor above and the References and Problems windows along the bottom:
def place_editor(self, window) -> None:
"""EditorMixin: an editor takes the desktop above the bottom ten lines (references, problems)."""
super().place_editor(window)
size = self.desktop.size
window.styles.offset = (0, 0)
window.styles.width, window.styles.height = size.width, size.height - BOTTOM
def place_lsp_window(self, window) -> None:
"""LspMixin: the References and Problems windows go along the bottom, the editor stays above."""
super().place_lsp_window(window)
size = self.desktop.size
window.styles.offset = (0, size.height - BOTTOM)
window.styles.width = size.width
window.styles.height = BOTTOM![]() |
![]() |
| Ctrl+Space: the completion list and the documentation |
(: the call's signature |
![]() |
![]() |
Shift+F12: every use of subtotal
|
F8: the server's warning, also drawn in the text |
A language server is any program that speaks the Language Server Protocol on its standard input and output. The toy one declares what it can do with pygls' decorators:
server = LanguageServer("toy-python", "1.0")
@server.feature(types.TEXT_DOCUMENT_COMPLETION, types.CompletionOptions(trigger_characters=["."], resolve_provider=True))
def completion(params: types.CompletionParams) -> types.CompletionList:
...
@server.feature(types.TEXT_DOCUMENT_DEFINITION)
def definition(params: types.DefinitionParams) -> types.Location | None:
...
if __name__ == "__main__":
server.start_io()TurboFrame's LspSession starts it and stops it with the application. On Linux the server runs in a process group of its own, so whatever it starts (a real server's helpers) ends with it too. The real-terminal test of this example checks that no process is left.
- The editor, its hooks and
LspMixin's hooks for a language's details (lsp_trigger,lsp_in_call,lsp_quiet_here,lsp_diagnostics_filter): The Editor. - The session alone, without the UI (
LspSession,find_command,diagnostics_in): LSP Helpers. - The completion list and signature box on their own: 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



