atiaomar1978-hub commented on code in PR #25408: URL: https://github.com/apache/camel/pull/25408#discussion_r3740987683
########## dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/YamlBlockEditor.java: ########## @@ -0,0 +1,269 @@ +/* + * 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.ArrayList; +import java.util.List; + +/** + * YAML-structure-aware block operations for the TUI source editor. + */ +final class YamlBlockEditor { + + record BlockRange(int startRow, int endRow) { + boolean isEmpty() { + return startRow < 0 || endRow < startRow; + } + } + + record EditResult(List<String> lines, int cursorRow, int cursorCol) { + } + + private YamlBlockEditor() { + } + + static List<String> toLines(String text) { + if (text.isEmpty()) { + return new ArrayList<>(List.of("")); + } + return new ArrayList<>(List.of(text.split("\n", -1))); + } + + static String fromLines(List<String> lines) { + return String.join("\n", lines); + } + + static BlockRange findBlock(List<String> lines, int row, boolean yamlListBlocks) { + if (lines.isEmpty() || row < 0 || row >= lines.size()) { + return new BlockRange(row, row); + } + if (!yamlListBlocks) { + return new BlockRange(row, row); + } + int startRow = findBlockStart(lines, row); + int blockIndent = leadingSpaces(lines.get(startRow)); + int endRow = findBlockEnd(lines, startRow, blockIndent); + return new BlockRange(startRow, endRow); + } + + static EditResult deleteBlock(List<String> lines, int row, boolean yamlListBlocks) { + BlockRange block = findBlock(lines, row, yamlListBlocks); + if (block.isEmpty()) { + return new EditResult(lines, row, 0); + } + List<String> answer = new ArrayList<>(lines); + answer.subList(block.startRow(), block.endRow() + 1).clear(); + if (answer.isEmpty()) { + answer.add(""); + } + int cursorRow = Math.min(block.startRow(), answer.size() - 1); + return new EditResult(answer, cursorRow, leadingSpaces(answer.get(cursorRow))); + } + + static EditResult duplicateBlock(List<String> lines, int row, boolean yamlListBlocks) { + BlockRange block = findBlock(lines, row, yamlListBlocks); + if (block.isEmpty()) { + return new EditResult(lines, row, 0); + } + List<String> copy = new ArrayList<>(lines.subList(block.startRow(), block.endRow() + 1)); + List<String> answer = new ArrayList<>(lines); + answer.addAll(block.endRow() + 1, copy); + return new EditResult(answer, block.endRow() + 1, leadingSpaces(copy.get(0))); + } + + static EditResult moveBlockUp(List<String> lines, int row, boolean yamlListBlocks) { + BlockRange block = findBlock(lines, row, yamlListBlocks); + if (block.isEmpty() || block.startRow() == 0) { + return null; + } + BlockRange previous = findPreviousSibling(lines, block, yamlListBlocks); + if (previous == null || previous.isEmpty()) { + return null; + } + EditResult swapped = swapBlocks(lines, previous, block); + List<String> answer = swapped.lines(); + int cursorRow = previous.startRow(); + int cursorCol = answer.isEmpty() ? 0 : YamlBlockEditor.leadingSpaces(answer.get(cursorRow)); + return new EditResult(answer, cursorRow, cursorCol); + } + + static EditResult moveBlockDown(List<String> lines, int row, boolean yamlListBlocks) { + BlockRange block = findBlock(lines, row, yamlListBlocks); + if (block.isEmpty() || block.endRow() >= lines.size() - 1) { + return null; + } + BlockRange next = findNextSibling(lines, block, yamlListBlocks); + if (next == null || next.isEmpty()) { + return null; + } + return swapBlocks(lines, block, next); + } + + static List<String> toggleComment(List<String> lines, BlockRange block) { + List<String> answer = new ArrayList<>(lines); + boolean uncomment = true; + for (int i = block.startRow(); i <= block.endRow() && i < answer.size(); i++) { + String line = answer.get(i); + if (line.isBlank()) { + continue; + } + if (!isCommented(line)) { + uncomment = false; + break; + } + } + for (int i = block.startRow(); i <= block.endRow() && i < answer.size(); i++) { + answer.set(i, uncomment ? uncommentLine(answer.get(i)) : commentLine(answer.get(i))); + } Review Comment: Fixed in `8559ab1`: `moveBlockDown` now computes the cursor row where the moved block lands after the swap (mirroring the explicit correction in `moveBlockUp`). Covered by `moveBlockDownCursorFollowsMovedBlock`. _AI-generated reply on behalf of atiaomar1978-hub._ ########## dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/SearchHighlighter.java: ########## @@ -312,4 +312,39 @@ void reset() { highlightTerm = null; highlightPattern = null; } + + /** Closes an active search input without clearing an existing find term. */ + void closeInputOnly() { + findInputActive = false; + highlightInputActive = false; + searchInputState = new TextInputState(""); + } + + void openFindInput() { + findInputActive = true; + highlightInputActive = false; + searchInputState = new TextInputState(findTerm != null ? findTerm : ""); + } + + /** + * Find navigation while editing plain text (Ctrl+F opens find, n/N step matches). + */ + boolean handleEditFindKeyEvent(KeyEvent ke) { + if (findInputActive || highlightInputActive) { + return handleSearchInput(ke); + } + if (ke.hasCtrl() && ke.isCharIgnoreCase('f')) { + openFindInput(); + return true; + } + if (findTerm != null && ke.isChar('n') && !ke.hasCtrl() && !ke.hasAlt()) { Review Comment: Fixed in `8559ab1`: edit mode now uses **Ctrl+N** / **Ctrl+Shift+N** for find match navigation (same modifier pattern as other edit chords). Plain `n`/`N` are no longer consumed when a find term is active, so users can type freely. Footer hint updated accordingly; covered by `plainNInsertsWhileFindTermActive`. _AI-generated reply on behalf of atiaomar1978-hub._ -- 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]
