Tab Handling¶
By default, the Tab key moves focus to the next UI element (standard Compose behavior). You can override this to indent code instead.
Tab extension¶
The demo defines a function that returns either an indent-with-tab extension or a literal-tab-insert extension:
fun tabExtension(m: TabMode): Extension = when (m) {
TabMode.INDENT -> keymapOf(indentWithTab)
TabMode.INSERT -> keymapOf(
KeyBinding(
key = "Tab",
run = { session ->
session.insertAt(session.state.selection.main.head, "\t")
true
}
)
)
}
TabMode.INDENTusesindentWithTabfrom the:commandsmodule, which maps Tab toindentMoreand Shift-Tab toindentLess.TabMode.INSERTinserts a literal tab character at the cursor.
Accessibility note¶
Capturing Tab prevents keyboard-only users from tabbing out of the editor. Consider providing an alternative escape mechanism (e.g., Escape to release focus) if your editor is part of a larger form.
Related API¶
KeyBinding— key binding data classkeymapOf— create keymap extensionindentWithTab— tab indentation binding
Based on the CodeMirror Tab Handling example.