Skip to content

Commit 0946cf3

Browse files
committed
added a few selection handlers
1 parent 67bcb1a commit 0946cf3

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
151151
.applyShadowing()
152152
if (matchingActions.isNotEmpty()) {
153153
if (matchingActions.size == 1 && matchingActions.first().getMatchingText() == typedText) {
154-
matchingActions.first().execute()
154+
matchingActions.first().execute(editor)
155155
return
156156
}
157157
editor.showCodeCompletionMenu(

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ abstract class CellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(val co
8282

8383
fun getReference() = reference ?: throw IllegalStateException("reference isn't set yet")
8484

85-
fun createCellReference(node: ITypedNode) = TemplateCellReference(getReference(), node.untypedReference())
85+
fun createCellReference(node: INode) = TemplateCellReference(getReference(), node.reference)
86+
fun createCellReference(node: ITypedNode) = createCellReference(node.untyped())
8687

8788
private fun applyTextReplacement(cellData: CellData, editorState: EditorState) {
8889
if (cellData is TextCellData) {
@@ -139,9 +140,14 @@ class ConstantCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(concept
139140
inner class SideTransformWrapper(val nodeToWrap: INonExistingNode, val wrappingLink: IChildLink) : ICodeCompletionAction {
140141
override fun getMatchingText(): String = text
141142
override fun getDescription(): String = concept.getShortName()
142-
override fun execute() {
143+
override fun execute(editor: EditorComponent) {
143144
val wrapper = nodeToWrap.getParent()!!.getOrCreateNode(null).addNewChild(nodeToWrap.getContainmentLink()!!, nodeToWrap.index(), concept)
144145
wrapper.moveChild(wrappingLink, 0, nodeToWrap.getOrCreateNode(null))
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())
150+
}
145151
}
146152

147153
override fun shadows(shadowed: ICodeCompletionAction): Boolean {
@@ -166,7 +172,7 @@ class ConstantCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(concept
166172
return concept.getShortName()
167173
}
168174

169-
override fun execute() {
175+
override fun execute(editor: EditorComponent) {
170176
location.replaceNode(concept)
171177
}
172178
}
@@ -263,8 +269,14 @@ open class PropertyCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept>(co
263269
return concept.getShortName()
264270
}
265271

266-
override fun execute() {
267-
location.getOrCreateNode(concept).setPropertyValue(property, value)
272+
override fun execute(editor: EditorComponent) {
273+
val node = location.getOrCreateNode(concept)
274+
node.setPropertyValue(property, value)
275+
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())
279+
}
268280
}
269281
}
270282

@@ -306,9 +318,14 @@ class ReferenceCellTemplate<NodeT : ITypedNode, ConceptT : ITypedConcept, Target
306318
return concept.getShortName()
307319
}
308320

309-
override fun execute() {
321+
override fun execute(editor: EditorComponent) {
310322
val sourceNode = location.getOrCreateNode(concept)
311323
sourceNode.setReferenceTarget(link, target)
324+
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())
328+
}
312329
}
313330
}
314331
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CodeCompletionMenu(
5454
KnownKeys.ArrowRight -> patternEditor.moveCaret(1)
5555
KnownKeys.Escape -> editor.closeCodeCompletionMenu()
5656
KnownKeys.Enter -> {
57-
getSelectedEntry()?.execute()
57+
getSelectedEntry()?.execute(editor)
5858
editor.closeCodeCompletionMenu()
5959
}
6060
KnownKeys.Backspace -> patternEditor.deleteText(true)
@@ -101,7 +101,7 @@ class CodeCompletionMenu(
101101

102102
fun executeIfSingleAction() {
103103
if (entries.size == 1 && entries.first().getMatchingText() == patternEditor.pattern) {
104-
entries.first().execute()
104+
entries.first().execute(editor)
105105
editor.closeCodeCompletionMenu()
106106
}
107107
}
@@ -200,14 +200,14 @@ private fun IActionOrProvider.flatten(parameters: CodeCompletionParameters): Seq
200200
interface ICodeCompletionAction : IActionOrProvider {
201201
fun getMatchingText(): String
202202
fun getDescription(): String
203-
fun execute()
203+
fun execute(editor: EditorComponent)
204204
fun shadows(shadowed: ICodeCompletionAction) = false
205205
fun shadowedBy(shadowing: ICodeCompletionAction) = false
206206
}
207207

208208
class CodeCompletionActionWithPostprocessor(val action: ICodeCompletionAction, val after: () -> Unit) : ICodeCompletionAction by action {
209-
override fun execute() {
210-
action.execute()
209+
override fun execute(editor: EditorComponent) {
210+
action.execute(editor)
211211
after()
212212
}
213213
}

0 commit comments

Comments
 (0)