Skip to content

Commit

Permalink
Resolve included module in dot completion
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud committed Aug 29, 2023
1 parent cbd5550 commit 9161ab0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ private static void addExpressions(@NotNull CompletionResultSet resultSet, @NotN
}

private static void addChildren(@Nullable PsiElement body, @NotNull Collection<PsiNamedElement> expressions) {
List<RPsiInclude> includes = PsiTreeUtil.getStubChildrenOfTypeAsList(body, RPsiInclude.class);
for (RPsiInclude include : includes) {
RPsiUpperSymbol referenceIdentifier = include.getModuleReference();
RPsiUpperSymbolReference reference = referenceIdentifier == null ? null : referenceIdentifier.getReference();
PsiElement resolvedResult = reference == null ? null : reference.resolveInterface();
if (resolvedResult instanceof RPsiModule resolvedModule) {
PsiElement resolvedBody = resolvedModule instanceof RPsiInnerModule ? ((RPsiInnerModule) resolvedModule).getModuleSignature() : null;
if (resolvedBody == null) {
resolvedBody = resolvedModule.getBody();
}
addChildren(resolvedBody, expressions);
}
}

expressions.addAll(PsiTreeUtil.getStubChildrenOfTypeAsList(body, RPsiType.class));
expressions.addAll(PsiTreeUtil.getStubChildrenOfTypeAsList(body, RPsiLet.class));
expressions.addAll(PsiTreeUtil.getStubChildrenOfTypeAsList(body, RPsiVal.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,16 @@ private ORReferenceAnalyzer() {

// Add all globally opened elements (implicit open)
if (openedModules != null) {
LOG.trace("Processing globally opened modules");
for (String openedModuleName : openedModules) {
List<RPsiModule> modules = getTopModules(openedModuleName, psiManager, scope);
RPsiModule module = modules.isEmpty() ? null : modules.get(0);
if (module != null) {
resolutions.add(new ResolutionElement(module, true));
ResolutionElement resolutionElement = new ResolutionElement(module, true);
if (LOG.isTraceEnabled()) {
LOG.trace(" > global module, add [" + resolutionElement + "]");
}
resolutions.add(resolutionElement);
}
}
}
Expand Down Expand Up @@ -436,6 +441,10 @@ else if (instruction instanceof RPsiUpperSymbol foundUpper) {
} else {
ORModuleResolutionPsiGist.Data data = ORModuleResolutionPsiGist.getData(foundInnerModule.getContainingFile());
Collection<String> alternateNames = data.getValues(foundInnerModule);
if (LOG.isTraceEnabled()) {
LOG.trace(" > alias found [" + foundAlias + "], alternateNames: [" + Joiner.join(", ", alternateNames) + "]");
}

if (alternateNames.isEmpty()) {
if (foundInnerModule.isFunctorCall()) {
// Resolve using qName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public PsiLowerSymbolReference(@NotNull RPsiLowerSymbol element, @NotNull ORLang
}

if (LOG.isTraceEnabled()) {
LOG.trace(" Instructions: ", Joiner.join(" -> ", instructions));
LOG.trace(" Instructions: [" + Joiner.join(" -> ", instructions) + "]");
}

long endInstructions = System.currentTimeMillis();
Expand All @@ -73,7 +73,7 @@ public PsiLowerSymbolReference(@NotNull RPsiLowerSymbol element, @NotNull ORLang
List<RPsiQualifiedPathElement> resolvedInstructions = ORReferenceAnalyzer.resolveInstructions(instructions, openedModules, project, searchScope);

if (LOG.isTraceEnabled()) {
LOG.trace(" Resolved instructions: " + Joiner.join(" -> ", resolvedInstructions));
LOG.trace(" Resolved instructions: [" + Joiner.join(" -> ", resolvedInstructions) + "]");
}

long endResolvedInstructions = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public RPsiIncludeImpl(@NotNull ORLangTypes types, @NotNull PsiIncludeStub stub,
return firstChild == null ? "" : ORUtil.getTextUntilClass(firstChild, RPsiConstraints.class);
}

// deprecate ?
@Override
public @Nullable RPsiUpperSymbol getModuleReference() {
// Latest element in path
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/reason/ide/completion/DotCompletionOCLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public void test_multiple_alias() {
assertEquals("key", elements.get(0));
}

@Test
public void test_alias() {
configureCode("A.ml", "module A1 = struct end");
configureCode("B.ml", "module B1 = struct include A end");
configureCode("C.ml", "module C1 = B.B1.<caret>");

myFixture.complete(CompletionType.BASIC, 1);
List<String> elements = myFixture.getLookupElementStrings();

assertSize(1, elements);
assertEquals("A1", elements.get(0));
}

@Test
public void test_no_pervasives() {
configureCode("pervasives.mli", "val int_of_string : str -> int");
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/reason/ide/completion/DotCompletionRESTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public void test_alias_in_file() {
assertEquals("alias", elements.get(0));
}

@Test
public void test_alias() {
configureCode("A.res", "module A1 = {}");
configureCode("B.res", "module B1 = { include A }");
configureCode("C.res", "module C1 = B.B1.<caret>");

myFixture.complete(CompletionType.BASIC, 1);
List<String> elements = myFixture.getLookupElementStrings();

assertSize(1, elements);
assertEquals("A1", elements.get(0));
}

@Test
public void test_uncurried() {
configureCode("A.res", "let x = 1");
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/reason/ide/completion/DotCompletionRMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public void test_alias_in_file() {
assertEquals("alias", elements.get(0));
}

@Test
public void test_alias() {
configureCode("A.re", "module A1 = {};");
configureCode("B.re", "module B1 = { include A; };");
configureCode("C.re", "module C1 = B.B1.<caret>");

myFixture.complete(CompletionType.BASIC, 1);
List<String> elements = myFixture.getLookupElementStrings();

assertSize(1, elements);
assertEquals("A1", elements.get(0));
}

@Test
public void test_uncurried() {
configureCode("A.re", "let x = 1;");
Expand Down

0 comments on commit 9161ab0

Please sign in to comment.