gnodet-bot commented on code in PR #27521: URL: https://github.com/apache/camel/pull/27521#discussion_r4213301001
########## dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/RouteTreePanel.java: ########## @@ -0,0 +1,157 @@ +/* + * 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; +import java.util.Locale; +import java.util.Map; + +import dev.tamboui.layout.Rect; +import dev.tamboui.style.Style; +import dev.tamboui.terminal.Frame; +import dev.tamboui.text.Line; +import dev.tamboui.text.Span; +import dev.tamboui.text.Text; +import dev.tamboui.widgets.Clear; +import dev.tamboui.widgets.block.Block; +import dev.tamboui.widgets.block.BorderType; +import dev.tamboui.widgets.block.Borders; +import dev.tamboui.widgets.paragraph.Paragraph; +import org.apache.camel.dsl.jbang.core.commands.tui.diagram.DiagramColors; + +/** + * The mini panel of the source editor: the tree of the route the cursor is in (from, then each step, the branches + * indented), with the step under the cursor marked, so the structure of a large route stays in view while editing it. + * It is built from the text as it is (the route need not run), with the scanners of Go to Node (Ctrl+G), for the YAML, + * XML and Java DSLs. Ctrl+T shows or hides it. + */ +final class RouteTreePanel { + + static final int MIN_EDITOR_WIDTH = 90; + static final int MIN_EDITOR_HEIGHT = 12; + + private int scannedHash; + private String scannedName; + private List<YamlRouteNodeScanner.NodeEntry> entries = List.of(); + + /** The nodes of the route the cursor is in, and which of them is under the cursor (-1 for none). */ + record RouteTree(String routeId, String fromUri, List<YamlRouteNodeScanner.NodeEntry> nodes, int current) { + } + + /** + * Draws the panel at the top right of the editor area, when it is wide enough and the file has a route. + * + * @param cursorRow the line of the cursor, 0-based + */ + void render(Frame frame, Rect area, List<String> lines, String fileName, int cursorRow) { + if (area.width() < MIN_EDITOR_WIDTH || area.height() < MIN_EDITOR_HEIGHT || fileName == null) { + return; + } + RouteTree tree = treeAt(scan(lines, fileName), cursorRow); + if (tree == null || tree.nodes().isEmpty()) { + return; + } + int w = Math.min(46, area.width() / 3); + int maxRows = Math.max(3, area.height() * 2 / 3 - 2); + int rows = Math.min(tree.nodes().size(), maxRows); + // keep the step under the cursor in view + int top = 0; + if (tree.current() >= rows) { + top = Math.min(tree.current() - rows / 2, tree.nodes().size() - rows); + } + List<Line> out = new ArrayList<>(); + // where the route starts + String from = tree.fromUri() != null ? "from " + tree.fromUri() : "from"; + Style fromStyle = tree.current() < 0 + ? Theme.selectionBg().bold() + : Style.EMPTY.fg(DiagramColors.getEipColor("from")); + out.add(Line.from(Span.styled(TuiHelper.truncate((tree.current() < 0 ? "▶" : " ") + from, w - 2), fromStyle))); + rows = Math.min(rows, maxRows - 1); + for (int i = top; i < top + rows; i++) { Review Comment: **Off-by-one in scroll logic** — the cursor node is invisible at the boundary. `rows` is set to `min(nodes.size(), maxRows)` (e.g. 10), then the scroll guard checks `if (tree.current() >= rows)`. When `current == rows - 1` (e.g. current=9, rows=10), the check evaluates false (9 < 10) and `top` stays 0. But then `rows` is immediately reduced to `maxRows - 1` (e.g. 9) to account for the "from" line. The loop renders indices 0..8 — node 9, the one under the cursor, is invisible. Fix: compute `rows` as `maxRows - 1` upfront so the scroll guard uses the same value as the loop. ```suggestion int rows = Math.min(tree.nodes().size(), maxRows - 1); // keep the step under the cursor in view int top = 0; if (tree.current() >= rows) { top = Math.min(tree.current() - rows / 2, tree.nodes().size() - rows); } List<Line> out = new ArrayList<>(); // where the route starts String from = tree.fromUri() != null ? "from " + tree.fromUri() : "from"; Style fromStyle = tree.current() < 0 ? Theme.selectionBg().bold() : Style.EMPTY.fg(DiagramColors.getEipColor("from")); out.add(Line.from(Span.styled(TuiHelper.truncate((tree.current() < 0 ? "▶" : " ") + from, w - 2), fromStyle))); for (int i = top; i < top + rows; i++) { ``` -- 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]
