@@ -144,9 +144,10 @@ class ConstantCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(concept
144
144
val wrapper = nodeToWrap.getParent()!! .getOrCreateNode(null ).addNewChild(nodeToWrap.getContainmentLink()!! , nodeToWrap.index(), concept)
145
145
wrapper.moveChild(wrappingLink, 0 , nodeToWrap.getOrCreateNode(null ))
146
146
editor.selectAfterUpdate {
147
- val cell = editor.resolveCell(createCellReference(wrapper)).firstOrNull() ? : return @selectAfterUpdate null
148
- val layoutable = cell.layoutable() ? : return @selectAfterUpdate null
149
- CaretSelection (layoutable, layoutable.getLength())
147
+ CaretPositionPolicy (wrapper)
148
+ .avoid(ChildNodeCellReference (wrapper.reference, wrappingLink))
149
+ .avoid(createCellReference(wrapper))
150
+ .getBestSelection(editor)
150
151
}
151
152
}
152
153
@@ -173,7 +174,11 @@ class ConstantCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(concept
173
174
}
174
175
175
176
override fun execute (editor : EditorComponent ) {
176
- location.replaceNode(concept)
177
+ val newNode = location.replaceNode(concept)
178
+ editor.selectAfterUpdate {
179
+ CaretPositionPolicy (newNode)
180
+ .getBestSelection(editor)
181
+ }
177
182
}
178
183
}
179
184
}
@@ -273,9 +278,8 @@ open class PropertyCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(co
273
278
val node = location.getOrCreateNode(concept)
274
279
node.setPropertyValue(property, value)
275
280
editor.selectAfterUpdate {
276
- val cell = editor.resolveCell(createCellReference(node)).firstOrNull() ? : return @selectAfterUpdate null
277
- val layoutable = cell.layoutable() ? : return @selectAfterUpdate null
278
- CaretSelection (layoutable, layoutable.getLength())
281
+ CaretPositionPolicy (createCellReference(node))
282
+ .getBestSelection(editor)
279
283
}
280
284
}
281
285
}
@@ -298,7 +302,11 @@ class ReferenceCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept, Target
298
302
val link : GeneratedReferenceLink <TargetNodeT , TargetConceptT >,
299
303
val presentation : TargetNodeT .() -> String?
300
304
) : CellTemplate<NodeT, ConceptT>(concept), IGrammarSymbol {
301
- override fun createCell (context : CellCreationContext , node : NodeT ): CellData = TextCellData (getText(node), " <no ${link.name} >" )
305
+ 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
309
+ }
302
310
private fun getText (node : NodeT ): String = getTargetNode(node)?.let (presentation) ? : " "
303
311
private fun getTargetNode (sourceNode : NodeT ): TargetNodeT ? {
304
312
return sourceNode.unwrap().getReferenceTarget(link)?.typedUnsafe()
@@ -322,9 +330,8 @@ class ReferenceCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept, Target
322
330
val sourceNode = location.getOrCreateNode(concept)
323
331
sourceNode.setReferenceTarget(link, target)
324
332
editor.selectAfterUpdate {
325
- val cell = editor.resolveCell(createCellReference(sourceNode)).firstOrNull() ? : return @selectAfterUpdate null
326
- val layoutable = cell.layoutable() ? : return @selectAfterUpdate null
327
- CaretSelection (layoutable, layoutable.getLength())
333
+ CaretPositionPolicy (createCellReference(sourceNode))
334
+ .getBestSelection(editor)
328
335
}
329
336
}
330
337
}
@@ -351,13 +358,17 @@ class ChildCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(
351
358
val substitutionPlaceholder = context.editorState.substitutionPlaceholderPositions[createCellReference(node)]
352
359
val placeholderIndex = substitutionPlaceholder?.index?.coerceIn(0 .. childNodes.size) ? : 0
353
360
val addSubstitutionPlaceholder: (Int ) -> Unit = { index ->
354
- val placeholderText = if (childNodes.isEmpty()) " <no ${link.name} >" else " <choose ${link.name} >"
361
+ val isDefaultPlaceholder = childNodes.isEmpty()
362
+ val placeholderText = if (isDefaultPlaceholder) " <no ${link.name} >" else " <choose ${link.name} >"
355
363
val placeholder = TextCellData (" " , placeholderText)
356
364
placeholder.properties[CellActionProperties .substitute] =
357
365
ReplaceNodeActionProvider (NonExistingChild (node.untyped().toNonExisting(), link, index)).after {
358
366
context.editorState.substitutionPlaceholderPositions.remove(createCellReference(node))
359
367
}
360
368
placeholder.cellReferences.add(PlaceholderCellReference (createCellReference(node)))
369
+ if (isDefaultPlaceholder) {
370
+ placeholder.cellReferences + = ChildNodeCellReference (node.untypedReference(), link, index)
371
+ }
361
372
cell.addChild(placeholder)
362
373
}
363
374
val addInsertActionCell: (Int ) -> Unit = { index ->
@@ -382,6 +393,7 @@ class ChildCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(
382
393
// child.parent?.removeChild(child) // child may be cached and is still attached to the old parent
383
394
val wrapper = CellData () // allow setting properties by the parent, because the cell is already frozen
384
395
wrapper.addChild(child)
396
+ wrapper.cellReferences + = ChildNodeCellReference (node.untypedReference(), link, index)
385
397
cell.addChild(wrapper)
386
398
}
387
399
if (substitutionPlaceholder != null && placeholderIndex == childNodes.size) {
0 commit comments