Skip to content

Commit

Permalink
Prefer to place new statements before ON ERROR blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusAmshove committed Jan 28, 2025
1 parent 9d29056 commit b192db4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
{
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public static boolean containsTokenWithKind(ISyntaxNode node, SyntaxKind kind)
return false;
}

@Nullable
public static <T extends IStatementNode> T findFirstStatementOfType(Class<T> statementType, ISyntaxTree tree)
{
if (statementType.isInstance(tree))
Expand Down

0 comments on commit b192db4

Please sign in to comment.