This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 4b87ef0deed2 CAMEL-26933: camel-jbang-plugin-edit - a completion of
camel edit changes only the text it replaces (#30032)
4b87ef0deed2 is described below
commit 4b87ef0deed23faf15a05cde848a71b7f6add790
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 20:22:05 2026 +0100
CAMEL-26933: camel-jbang-plugin-edit - a completion of camel edit changes
only the text it replaces (#30032)
CamelNanoLspEditor.insertHelp (found by reading the code in the CLI scan):
- a completion without a text edit dropped the rest of the line after the
cursor;
- a text edit over several lines indexed the new text with the positions of
the old lines, which lost text or threw;
- an insert/replace edit (the right side of the Either) threw a
NullPointerException, as getLeft() was called without
isLeft().
The range of an edit is now replaced by the new text, which may span lines,
and an insert keeps the rest of the line.
The module gets its first tests.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
dsl/camel-jbang/camel-jbang-plugin-edit/pom.xml | 11 ++++
.../core/commands/edit/CamelNanoLspEditor.java | 68 +++++++++++-----------
.../core/commands/edit/CamelNanoLspEditorTest.java | 65 +++++++++++++++++++++
3 files changed, 111 insertions(+), 33 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-plugin-edit/pom.xml
b/dsl/camel-jbang/camel-jbang-plugin-edit/pom.xml
index a661cd3ba0c7..6849d5cb3c44 100644
--- a/dsl/camel-jbang/camel-jbang-plugin-edit/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-plugin-edit/pom.xml
@@ -87,6 +87,17 @@
<artifactId>juniversalchardet</artifactId>
<version>${juniversalchardet-version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-edit/src/main/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditor.java
b/dsl/camel-jbang/camel-jbang-plugin-edit/src/main/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditor.java
index f79d00a6eeca..d43c7ad5d7fa 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-edit/src/main/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditor.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-edit/src/main/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditor.java
@@ -142,48 +142,50 @@ public class CamelNanoLspEditor extends Nano {
CompletionItem item = lines.get(selected);
Either<TextEdit, InsertReplaceEdit> textEdit = item.getTextEdit();
if (textEdit != null) {
- TextEdit left = textEdit.getLeft();
- Range range = left.getRange();
+ // a text edit, or an insert/replace edit, which replaces its
range (CAMEL-26933: the latter threw a
+ // NullPointerException, as it is the right side of the Either)
+ Range range = textEdit.isLeft() ? textEdit.getLeft().getRange() :
textEdit.getRight().getReplace();
+ String newText = textEdit.isLeft() ?
textEdit.getLeft().getNewText() : textEdit.getRight().getNewText();
Position start = range.getStart();
- int startLine = start.getLine();
Position end = range.getEnd();
- int endLine = end.getLine();
- if (startLine == endLine) {
- StringBuilder newLine = new StringBuilder();
- String line = this.buffer.getLines().get(startLine);
- newLine.append(line, 0, start.getCharacter());
- newLine.append(left.getNewText());
- newLine.append(line.substring(end.getCharacter()));
- this.buffer.getLines().set(startLine, newLine.toString());
- this.buffer.moveRight(left.getNewText().length());
- } else {
- // first line
- StringBuilder newFirstLine = new StringBuilder();
- String oldFirstLine = this.buffer.getLines().get(startLine);
- String newText = left.getNewText();
- newFirstLine.append(oldFirstLine, 0, start.getCharacter());
- newFirstLine.append(newText, 0, oldFirstLine.length() -
start.getCharacter());
- this.buffer.getLines().set(startLine, newFirstLine.toString());
- // second line
- StringBuilder newSecondLine = new StringBuilder();
- String oldSecondLine = this.buffer.getLines().get(endLine);
- newSecondLine.append(newText, oldFirstLine.length() -
start.getCharacter(), end.getCharacter());
-
newSecondLine.append(oldSecondLine.substring(end.getCharacter()));
- this.buffer.getLines().set(endLine, newSecondLine.toString());
- this.buffer.moveRight(left.getNewText().length());
- }
+ applyEdit(this.buffer.getLines(), start.getLine(),
start.getCharacter(), end.getLine(), end.getCharacter(),
+ newText);
+ this.buffer.moveRight(newText.length());
} else if (!Strings.isEmpty(item.getInsertText()) ||
!Strings.isEmpty(item.getLabel())) {
String insert = Strings.isEmpty(item.getInsertText()) ?
item.getLabel() : item.getInsertText();
- StringBuilder newLine = new StringBuilder();
- String line = this.buffer.getLines().get(buffer.getLine());
- newLine.append(line, 0, buffer.getOffsetInLine() +
buffer.getColumn());
- newLine.append(insert);
- this.buffer.getLines().set(buffer.getLine(), newLine.toString());
+ int line = buffer.getLine();
+ this.buffer.getLines().set(line,
+ insertAt(this.buffer.getLines().get(line),
buffer.getOffsetInLine() + buffer.getColumn(), insert));
this.buffer.moveRight(insert.length());
}
this.buffer.setDirty(true);
}
+ /**
+ * Replaces the text from the start to the end position with the new text,
which may span several lines: the lines
+ * of the range are replaced by the lines of the result. The text of the
edit was indexed with the positions of the
+ * old lines, which lost text or threw (CAMEL-26933).
+ */
+ static void applyEdit(List<String> lines, int startLine, int startChar,
int endLine, int endChar, String newText) {
+ String first = lines.get(startLine);
+ String last = lines.get(endLine);
+ String result = first.substring(0, Math.min(startChar,
first.length())) + newText
+ + last.substring(Math.min(endChar, last.length()));
+ List<String> replaced = List.of(result.split("\n", -1));
+ for (int i = endLine; i >= startLine; i--) {
+ lines.remove(i);
+ }
+ lines.addAll(startLine, replaced);
+ }
+
+ /**
+ * The line with the text inserted at the position, keeping the rest of
the line, which was dropped (CAMEL-26933).
+ */
+ static String insertAt(String line, int pos, String insert) {
+ int at = Math.min(pos, line.length());
+ return line.substring(0, at) + insert + line.substring(at);
+ }
+
private static class LocalCamelDocumentService extends
CamelTextDocumentService {
private final EndpointDiagnosticService endpointDiagnosticService;
private final ConfigurationPropertiesDiagnosticService
configurationPropertiesDiagnosticService;
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-edit/src/test/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditorTest.java
b/dsl/camel-jbang/camel-jbang-plugin-edit/src/test/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditorTest.java
new file mode 100644
index 000000000000..0b08b3aa2c62
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-plugin-edit/src/test/java/org/apache/camel/dsl/jbang/core/commands/edit/CamelNanoLspEditorTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.edit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * A completion of camel edit changes only the text it replaces (CAMEL-26933).
+ */
+class CamelNanoLspEditorTest {
+
+ @Test
+ void anInsertKeepsTheRestOfTheLine() {
+ // without a text edit the rest of the line was dropped
+ assertThat(CamelNanoLspEditor.insertAt("uri: \"timer:tick\"", 5,
"x")).isEqualTo("uri: x\"timer:tick\"");
+ assertThat(CamelNanoLspEditor.insertAt("uri", 10,
"x")).isEqualTo("urix");
+ }
+
+ @Test
+ void anEditOnOneLine() {
+ List<String> lines = new ArrayList<>(List.of("- from:", " uri:
tim?period=1", " steps:"));
+
+ CamelNanoLspEditor.applyEdit(lines, 1, 9, 1, 12, "timer:tick");
+
+ assertThat(lines).containsExactly("- from:", " uri:
timer:tick?period=1", " steps:");
+ }
+
+ @Test
+ void anEditOverSeveralLines() {
+ // the new text was indexed with the positions of the old lines, which
lost text or threw
+ List<String> lines = new ArrayList<>(List.of("a: 1", "b: old", "c:
old", "d: 4"));
+
+ CamelNanoLspEditor.applyEdit(lines, 1, 3, 2, 6, "new\nc: new");
+
+ assertThat(lines).containsExactly("a: 1", "b: new", "c: new", "d: 4");
+ }
+
+ @Test
+ void anEditThatAddsLines() {
+ List<String> lines = new ArrayList<>(List.of("steps:", " - lo",
"end"));
+
+ CamelNanoLspEditor.applyEdit(lines, 1, 4, 1, 6, "log:\n message:
hello");
+
+ assertThat(lines).containsExactly("steps:", " - log:", "
message: hello", "end");
+ }
+}