Skip to content

Commit 0f5e4a4

Browse files
committed
TAB handler
It places the caret into the next editable cell.
1 parent d8a2348 commit 0f5e4a4

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CaretPositionPolicy.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ data class CaretPositionPolicy(
3737
.mapNotNull { editor.resolveLayoutable(it) }
3838

3939
val best = candidates
40-
.sortedByDescending { it.cell.ancestors(true).any { isPropertyCell(it) } }
40+
.sortedByDescending { it.cell.isTabTarget() }
4141
.sortedBy { it.cell.ancestors(true).filter { isAvoided(it) }.count() }
4242
.firstOrNull() ?: return null
4343

4444
return CaretSelection(best, (best.cell.getSelectableText() ?: "").length)
4545
}
4646

4747
private fun isAvoided(cell: Cell) = cell.data.cellReferences.intersect(avoidedCellRefs).isNotEmpty()
48-
private fun isPropertyCell(cell: Cell) = cell.data.cellReferences.any { it is PropertyCellReference }
4948
}
5049

5150
enum class CaretPositionType {

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CaretSelection.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
8080
createNextPreviousLineSelection(false, desiredXPosition ?: getAbsoluteX())
8181
?.let { editor.changeSelection(it) }
8282
}
83+
KnownKeys.Tab -> {
84+
val target = layoutable
85+
.getSiblingsInText(!event.modifiers.shift)
86+
.filterIsInstance<LayoutableCell>()
87+
.firstOrNull { it.cell.isTabTarget() }
88+
if (target != null) {
89+
editor.changeSelection(CaretSelection(target, 0))
90+
}
91+
}
8392
KnownKeys.Delete, KnownKeys.Backspace -> {
8493
if (start == end) {
8594
val posToDelete = when (knownKey) {

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CellProperties.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ object CommonCellProperties {
4646
val placeholderTextColor = CellPropertyKey<String?>("placeholder-text-color", "lightGray", inherits = true)
4747
val backgroundColor = CellPropertyKey<String?>("background-color", null)
4848
val textReplacement = CellPropertyKey<String?>("text-replacement", null)
49+
val tabTarget = CellPropertyKey<Boolean>("tab-target", false) // caret is placed into the cell when navigating via TAB
4950
}
51+
52+
fun Cell.isTabTarget() = getProperty(CommonCellProperties.tabTarget)

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CellTemplate.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ open class PropertyCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(co
248248
val value = node.getPropertyValue(property)
249249
val data = TextCellData(value ?: "", if (value == null) placeholderText else "")
250250
data.properties[CellActionProperties.replaceText] = ChangePropertyAction(node)
251+
data.properties[CommonCellProperties.tabTarget] = true
251252
data.cellReferences += PropertyCellReference(property, node.untypedReference())
252253
return data
253254
}
@@ -303,9 +304,10 @@ class ReferenceCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept, Target
303304
val presentation: TargetNodeT.() -> String?
304305
) : CellTemplate<NodeT, ConceptT>(concept), IGrammarSymbol {
305306
override fun createCell(context: CellCreationContext, node: NodeT): CellData {
306-
val cell = TextCellData(getText(node), "<no ${link.name}>")
307-
cell.cellReferences += ReferencedNodeCellReference(node.untypedReference(), link)
308-
return cell
307+
val data = TextCellData(getText(node), "<no ${link.name}>")
308+
data.cellReferences += ReferencedNodeCellReference(node.untypedReference(), link)
309+
data.properties[CommonCellProperties.tabTarget] = true
310+
return data
309311
}
310312
private fun getText(node: NodeT): String = getTargetNode(node)?.let(presentation) ?: ""
311313
private fun getTargetNode(sourceNode: NodeT): TargetNodeT? {
@@ -369,6 +371,7 @@ class ChildCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(
369371
if (isDefaultPlaceholder) {
370372
placeholder.cellReferences += ChildNodeCellReference(node.untypedReference(), link, index)
371373
}
374+
placeholder.properties[CommonCellProperties.tabTarget] = true
372375
cell.addChild(placeholder)
373376
}
374377
val addInsertActionCell: (Int) -> Unit = { index ->

0 commit comments

Comments
 (0)