Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case translation postfix snippets (toLowerCase, toUpperCase) #459

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private static void addPostfixCompletionItems(
if (variableInvokedOn instanceof ITypedVariableNode typedVar && typedVar.type().emptyValue() != null)
{
addIsDefaultPostfix(completionItems, typedVar, identifierName, rangeToInsert, deleteEdit);
addCaseTranslationPostfix(completionItems, typedVar, identifierName, rangeToInsert, deleteEdit);
}

if (variableInvokedOn.scope().isParameter() && variableInvokedOn.findDescendantToken(SyntaxKind.OPTIONAL) != null)
Expand All @@ -181,6 +182,34 @@ private static void addPostfixCompletionItems(
}
}

private static void addCaseTranslationPostfix(
ArrayList<CompletionItem> completionItems, ITypedVariableNode typedVar,
String identifierName, Range rangeToInsert, TextEdit deleteEdit
)
{
if (!typedVar.type().isAlphaNumericFamily())
{
return;
}

var upperEdit = new TextEdit(rangeToInsert, "*TRANSLATE(%s, UPPER)".formatted(identifierName));
var lowerEdit = new TextEdit(rangeToInsert, "*TRANSLATE(%s, LOWER)".formatted(identifierName));

var upperItem = new CompletionItem("toUpperCase");
upperItem.setTextEdit(Either.forLeft(upperEdit));
upperItem.setKind(CompletionItemKind.Snippet);
upperItem.setInsertTextFormat(InsertTextFormat.PlainText);
upperItem.setAdditionalTextEdits(List.of(deleteEdit));
completionItems.add(upperItem);

var lowerItem = new CompletionItem("toLowerCase");
lowerItem.setTextEdit(Either.forLeft(lowerEdit));
lowerItem.setKind(CompletionItemKind.Snippet);
lowerItem.setInsertTextFormat(InsertTextFormat.PlainText);
lowerItem.setAdditionalTextEdits(List.of(deleteEdit));
completionItems.add(lowerItem);
}

private static void addOccPostfix(ArrayList<CompletionItem> completionItems, String identifierName, IVariableNode variableInvokedOn, Range rangeToInsert, TextEdit deleteEdit)
{
var occVar = variableInvokedOn instanceof IGroupNode group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.amshove.natls.completion;

import org.eclipse.lsp4j.CompletionItemKind;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -574,4 +575,118 @@ void createAnOccInvocationForArraysWhenInvokedOnGroup()
""");
}
}

@Nested
class TheToLowerCaseSnippetShould
{
@ParameterizedTest
@ValueSource(strings =
{
"N2", "P4", "I4", "L", "C"
})
void notBeApplicableOnVariablesThatAreNotAlphanumericFamily(String type)
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (%s)
END-DEFINE
#VAR.${}$
END
""".formatted(type))
.assertDoesNotContain("toLowerCase");
}

@ParameterizedTest
@ValueSource(strings =
{
"A10", "U10", "B4"
})
void beApplicableOnVariablesThatAreAlphanumericFamily(String type)
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (%s)
END-DEFINE
#VAR.${}$
END
""".formatted(type))
.assertContains("toLowerCase", CompletionItemKind.Snippet);
}

@Test
void createATranslateLowerCall()
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (A10)
END-DEFINE
#VAR.${}$
END
""")
.assertContainsCompletionResultingIn("toLowerCase", """
DEFINE DATA
LOCAL 1 #VAR (A10)
END-DEFINE
*TRANSLATE(#VAR, LOWER)
END
""");
}
}

@Nested
class TheToUpperCaseSnippetShould
{
@ParameterizedTest
@ValueSource(strings =
{
"N2", "P4", "I4", "L", "C"
})
void notBeApplicableOnVariablesThatAreAlphanumericFamily(String type)
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (%s)
END-DEFINE
#VAR.${}$
END
""".formatted(type))
.assertDoesNotContain("toUpperCase");
}

@ParameterizedTest
@ValueSource(strings =
{
"A10", "U10", "B4"
})
void beApplicableOnVariablesThatAreAlphanumericFamily(String type)
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (%s)
END-DEFINE
#VAR.${}$
END
""".formatted(type))
.assertContains("toUpperCase", CompletionItemKind.Snippet);
}

@Test
void createATranslateUpperCall()
{
assertCompletions("LIBONE", "SUB.NSN", ".", """
DEFINE DATA
LOCAL 1 #VAR (A10)
END-DEFINE
#VAR.${}$
END
""")
.assertContainsCompletionResultingIn("toUpperCase", """
DEFINE DATA
LOCAL 1 #VAR (A10)
END-DEFINE
*TRANSLATE(#VAR, UPPER)
END
""");
}
}
}
Loading