davsclaus commented on code in PR #25408:
URL: https://github.com/apache/camel/pull/25408#discussion_r3746981526


##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceViewer.java:
##########
@@ -601,7 +710,13 @@ private boolean handleEditKeyEvent(KeyEvent ke) {
             return true;
         }
         if (ke.isHome() || ke.isKey(KeyCode.HOME)) {
-            editState.moveCursorToLineStart();
+            String line = editState.getLine(editState.cursorRow());
+            int contentStart = YamlBlockEditor.leadingSpaces(line);
+            if (editState.cursorCol() > contentStart) {
+                SourceEditHistory.positionCursor(editState, 
editState.cursorRow(), contentStart);
+            } else {
+                SourceEditHistory.positionCursor(editState, 
editState.cursorRow(), 0);
+            }

Review Comment:
   Nit: this inlines the same smart-home logic that 
`SourceEditorNavigation.smartHome()` provides. Could call 
`SourceEditorNavigation.smartHome(editState, false)` here instead to avoid the 
duplication.
   
   (Non-blocking — the behavior is correct either way.)



##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceEditHistory.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.tui;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+
+import dev.tamboui.widgets.input.TextAreaState;
+
+/**
+ * Undo/redo stack for {@link SourceViewer} plain-text edit mode.
+ */
+final class SourceEditHistory {
+
+    private static final int MAX_DEPTH = 100;
+
+    record Snapshot(String text, int row, int col) {
+    }
+
+    private final Deque<Snapshot> undo = new ArrayDeque<>();
+    private final Deque<Snapshot> redo = new ArrayDeque<>();
+
+    void clear() {
+        undo.clear();
+        redo.clear();
+    }
+
+    void seedInitial(TextAreaState state) {
+        clear();
+        undo.push(capture(state));
+    }
+
+    void beforeChange(TextAreaState state) {
+        Snapshot snap = capture(state);
+        undo.push(snap);
+        trim(undo);
+        redo.clear();
+    }
+
+    boolean undo(TextAreaState state) {
+        if (undo.size() <= 1) {
+            return false;
+        }
+        redo.push(capture(state));
+        undo.pop();
+        restore(state, undo.peek());
+        return true;
+    }
+
+    boolean redo(TextAreaState state) {
+        if (redo.isEmpty()) {
+            return false;
+        }
+        Snapshot next = redo.pop();
+        undo.push(capture(state));
+        trim(undo);
+        restore(state, next);
+        return true;
+    }
+
+    static Snapshot capture(TextAreaState state) {
+        return new Snapshot(state.text(), state.cursorRow(), 
state.cursorCol());
+    }
+
+    private static void restore(TextAreaState state, Snapshot snapshot) {
+        state.setText(snapshot.text());
+        positionCursor(state, snapshot.row(), snapshot.col());
+    }

Review Comment:
   Observation: `positionCursor` is `static` here but is used as a general 
cursor utility from `SourceEditorNavigation`, `YamlBlockEditor`, and 
`SourceViewer` — none of those uses are related to undo/redo. It might be a 
better fit on `SourceEditorNavigation` or a small shared utility. Non-blocking, 
just a note for future tidying.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to