diff --git a/libs/natls/src/main/java/org/amshove/natls/codemutation/CodeInsertionPlacer.java b/libs/natls/src/main/java/org/amshove/natls/codemutation/CodeInsertionPlacer.java index ab717c1ce..0da7056db 100644 --- a/libs/natls/src/main/java/org/amshove/natls/codemutation/CodeInsertionPlacer.java +++ b/libs/natls/src/main/java/org/amshove/natls/codemutation/CodeInsertionPlacer.java @@ -88,7 +88,7 @@ public CodeInsertion findInsertionPositionForStatementAtEnd(LanguageServerFile f ); } - var lastNodePosition = findLastNodeInFileThatCantHaveStatementsAfter(file.getType(), withBody); + var lastNodePosition = findLastNodeThatWeDontWantHaveStatementsAfter(file.getType(), withBody); return new CodeInsertion( lastNodePosition.filePath(), @@ -114,7 +114,7 @@ public CodeInsertion findInsertionPositionForStatementAtStart(LanguageServerFile return new CodeInsertion(file.getPath(), "", LspUtil.toRangeBefore(firstStatement.position())); } - private static IPosition findLastNodeInFileThatCantHaveStatementsAfter(NaturalFileType type, IModuleWithBody withBody) + private static IPosition findLastNodeThatWeDontWantHaveStatementsAfter(NaturalFileType type, IModuleWithBody withBody) { if (type == NaturalFileType.FUNCTION) { @@ -123,6 +123,13 @@ private static IPosition findLastNodeInFileThatCantHaveStatementsAfter(NaturalFi return withBody.body().statements().get(index - 1).position(); } + var onErrorNode = NodeUtil.findFirstStatementOfType(IOnErrorNode.class, withBody.body()); + + if (onErrorNode != null) + { + return onErrorNode.position(); + } + return type == NaturalFileType.SUBROUTINE ? withBody.body().statements().first().descendants().last().position() : withBody.body().statements().last().position(); diff --git a/libs/natls/src/test/java/org/amshove/natls/codemutation/CodeInsertionPlacerShould.java b/libs/natls/src/test/java/org/amshove/natls/codemutation/CodeInsertionPlacerShould.java index fa0277b5e..9d6790cc4 100644 --- a/libs/natls/src/test/java/org/amshove/natls/codemutation/CodeInsertionPlacerShould.java +++ b/libs/natls/src/test/java/org/amshove/natls/codemutation/CodeInsertionPlacerShould.java @@ -296,6 +296,29 @@ void findARangeForASubroutineInASubprogramWithEnd() ); } + @Test + void findARangeForASubroutineInASubprogramWithEndAndOnError() + { + var file = createOrSaveLanguageServerFile("LIBONE", "SUBPROG.NSN", """ + DEFINE DATA LOCAL + END-DEFINE + ON ERROR + IF *ERROR-NR = 7 + IGNORE + END-IF + END-ERROR + END + """); + + assertInsertion( + sut.findInsertionPositionForStatementAtEnd(file), + "", + 2, 0, + 2, 0, + System.lineSeparator() + ); + } + @Test void findARangeForASubroutineInASubprogramWithEmptyBody() { @@ -353,6 +376,12 @@ void findARangeForAStatementInAFunction() ); } + @Test + void findARangeForAStatementPlacingItBeforeOnError() + { + + } + private void assertInsertion(CodeInsertion insertion, String prefix, int startLine, int offsetInStartLine, int endLine, int offsetInEndLine, String suffix) { var range = insertion.range(); diff --git a/libs/natparse/src/main/java/org/amshove/natparse/NodeUtil.java b/libs/natparse/src/main/java/org/amshove/natparse/NodeUtil.java index 635d41024..5a1d7f466 100644 --- a/libs/natparse/src/main/java/org/amshove/natparse/NodeUtil.java +++ b/libs/natparse/src/main/java/org/amshove/natparse/NodeUtil.java @@ -346,6 +346,7 @@ public static boolean containsTokenWithKind(ISyntaxNode node, SyntaxKind kind) return false; } + @Nullable public static T findFirstStatementOfType(Class statementType, ISyntaxTree tree) { if (statementType.isInstance(tree))