Skip to content
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 @@ -145,6 +145,8 @@ private void calculateCompletions(List<CompletionItem> completions) {

WurstType leftType = e.getLeft().attrTyp();

addArrayLengthCompletion(completions, e, leftType);

if (leftType instanceof WurstTypeNamedScope) {
WurstTypeNamedScope ct = (WurstTypeNamedScope) leftType;
for (DefLink nameLink : ct.nameLinks().values()) {
Expand Down Expand Up @@ -229,6 +231,28 @@ private void calculateCompletions(List<CompletionItem> completions) {

}

private void addArrayLengthCompletion(List<CompletionItem> completions, ExprMember member, WurstType leftType) {
boolean hasConstArrayInitializer = false;
if (member.getLeft() instanceof NameRef) {
NameDef nameDef = ((NameRef) member.getLeft()).tryGetNameDef();
if (nameDef instanceof GlobalOrLocalVarDef) {
GlobalOrLocalVarDef varDef = (GlobalOrLocalVarDef) nameDef;
hasConstArrayInitializer = varDef.getInitialExpr() instanceof ArrayInitializer;
}
}

if (!hasConstArrayInitializer || !isSuitableCompletion("length")) {
return;
}

CompletionItem completion = new CompletionItem("length");
completion.setKind(CompletionItemKind.Property);
completion.setDetail("int length");
completion.setInsertText("length");
completion.setSortText(ratingToString(calculateRating("length", WurstTypeInt.instance())));
completions.add(completion);
}

private void addKeywordCompletions(List<CompletionItem> completions) {
for (String keyword : WurstKeywords.KEYWORDS) {
if (keyword.startsWith(alreadyEntered)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.*;

/**
* tests the autocomplete functionality.
Expand Down Expand Up @@ -416,6 +415,42 @@ public void testInnerClasses() {
testCompletions(testData, "Banana", "Blue", "Boris");
}

@Test
public void constantArrayLengthCompletion() {
CompletionTestData testData = input(
"package test",
"const ints = [1, 2, 3]",
"init",
" ints.|",
"endpackage"
);

CompletionList completions = calculateCompletions(testData);

assertTrue(
completions.getItems().stream().anyMatch(c -> "length".equals(c.getLabel())),
"Expected to suggest the synthetic array length property"
);
}

@Test
public void nonConstantArrayDoesNotSuggestLength() {
CompletionTestData testData = input(
"package test",
"int array ints",
"init",
" ints.|",
"endpackage"
);

CompletionList completions = calculateCompletions(testData);

assertFalse(
completions.getItems().stream().anyMatch(c -> "length".equals(c.getLabel())),
"Did not expect to suggest length for non-constant arrays"
);
}

private void testCompletions(CompletionTestData testData, String... expectedCompletions) {
testCompletions(testData, Arrays.asList(expectedCompletions));
}
Expand Down
Loading