Skip to content

Commit 4a371a7

Browse files
authored
Add completion for constant array 'length' attribute (#1119)
1 parent c58def0 commit 4a371a7

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/GetCompletions.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ private void calculateCompletions(List<CompletionItem> completions) {
145145

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

148+
addArrayLengthCompletion(completions, e, leftType);
149+
148150
if (leftType instanceof WurstTypeNamedScope) {
149151
WurstTypeNamedScope ct = (WurstTypeNamedScope) leftType;
150152
for (DefLink nameLink : ct.nameLinks().values()) {
@@ -229,6 +231,28 @@ private void calculateCompletions(List<CompletionItem> completions) {
229231

230232
}
231233

234+
private void addArrayLengthCompletion(List<CompletionItem> completions, ExprMember member, WurstType leftType) {
235+
boolean hasConstArrayInitializer = false;
236+
if (member.getLeft() instanceof NameRef) {
237+
NameDef nameDef = ((NameRef) member.getLeft()).tryGetNameDef();
238+
if (nameDef instanceof GlobalOrLocalVarDef) {
239+
GlobalOrLocalVarDef varDef = (GlobalOrLocalVarDef) nameDef;
240+
hasConstArrayInitializer = varDef.getInitialExpr() instanceof ArrayInitializer;
241+
}
242+
}
243+
244+
if (!hasConstArrayInitializer || !isSuitableCompletion("length")) {
245+
return;
246+
}
247+
248+
CompletionItem completion = new CompletionItem("length");
249+
completion.setKind(CompletionItemKind.Property);
250+
completion.setDetail("int length");
251+
completion.setInsertText("length");
252+
completion.setSortText(ratingToString(calculateRating("length", WurstTypeInt.instance())));
253+
completions.add(completion);
254+
}
255+
232256
private void addKeywordCompletions(List<CompletionItem> completions) {
233257
for (String keyword : WurstKeywords.KEYWORDS) {
234258
if (keyword.startsWith(alreadyEntered)) {

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/AutoCompleteTests.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import java.util.List;
1515
import java.util.stream.Collectors;
1616

17-
import static org.testng.Assert.assertEquals;
18-
import static org.testng.Assert.assertFalse;
17+
import static org.testng.Assert.*;
1918

2019
/**
2120
* tests the autocomplete functionality.
@@ -416,6 +415,42 @@ public void testInnerClasses() {
416415
testCompletions(testData, "Banana", "Blue", "Boris");
417416
}
418417

418+
@Test
419+
public void constantArrayLengthCompletion() {
420+
CompletionTestData testData = input(
421+
"package test",
422+
"const ints = [1, 2, 3]",
423+
"init",
424+
" ints.|",
425+
"endpackage"
426+
);
427+
428+
CompletionList completions = calculateCompletions(testData);
429+
430+
assertTrue(
431+
completions.getItems().stream().anyMatch(c -> "length".equals(c.getLabel())),
432+
"Expected to suggest the synthetic array length property"
433+
);
434+
}
435+
436+
@Test
437+
public void nonConstantArrayDoesNotSuggestLength() {
438+
CompletionTestData testData = input(
439+
"package test",
440+
"int array ints",
441+
"init",
442+
" ints.|",
443+
"endpackage"
444+
);
445+
446+
CompletionList completions = calculateCompletions(testData);
447+
448+
assertFalse(
449+
completions.getItems().stream().anyMatch(c -> "length".equals(c.getLabel())),
450+
"Did not expect to suggest length for non-constant arrays"
451+
);
452+
}
453+
419454
private void testCompletions(CompletionTestData testData, String... expectedCompletions) {
420455
testCompletions(testData, Arrays.asList(expectedCompletions));
421456
}

0 commit comments

Comments
 (0)