Skip to content

Commit

Permalink
#167 add parsing for deconstruction let [193]
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud committed Feb 18, 2020
1 parent 90b1864 commit dd1fe03
Show file tree
Hide file tree
Showing 100 changed files with 861 additions and 537 deletions.
6 changes: 3 additions & 3 deletions jps-plugin/src/com/reason/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.ResolveResult;
import com.reason.lang.core.psi.PsiQualifiedElement;

public class Log {

Expand Down Expand Up @@ -130,13 +130,13 @@ public void trace(String comment, @Nullable Collection<?> t) {
}
}

public void debug(String comment, @NotNull PsiQualifiedNamedElement element) {
public void debug(String comment, @NotNull PsiQualifiedElement element) {
if (m_log.isDebugEnabled()) {
m_log.debug(comment + SEP + element.getQualifiedName() + " (" + element.getContainingFile().getVirtualFile().getPath() + ")");
}
}

public void debug(String comment, @NotNull PsiQualifiedNamedElement element, int position) {
public void debug(String comment, @NotNull PsiQualifiedElement element, int position) {
if (m_log.isDebugEnabled()) {
m_log.debug(comment + SEP + element.getQualifiedName() + " (" + element.getContainingFile().getVirtualFile().getPath() + ") pos=" + position);
}
Expand Down
12 changes: 12 additions & 0 deletions jps-plugin/src/com/reason/lang/core/psi/PsiQualifiedElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.reason.lang.core.psi;

import org.jetbrains.annotations.NotNull;
import com.intellij.psi.PsiNamedElement;

public interface PsiQualifiedElement extends PsiNamedElement {
@NotNull
String getPath();

@NotNull
String getQualifiedName();
}
14 changes: 6 additions & 8 deletions src/com/reason/ide/docs/DocumentationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
Expand All @@ -26,6 +24,7 @@
import com.reason.lang.core.psi.PsiLet;
import com.reason.lang.core.psi.PsiLowerSymbol;
import com.reason.lang.core.psi.PsiModule;
import com.reason.lang.core.psi.PsiQualifiedElement;
import com.reason.lang.core.psi.PsiSignatureElement;
import com.reason.lang.core.psi.PsiType;
import com.reason.lang.core.psi.PsiTypeConstrName;
Expand Down Expand Up @@ -183,20 +182,19 @@ public String getQuickNavigateInfo(@NotNull PsiElement element, @NotNull PsiElem
ORSignature signature = ((PsiSignatureElement) resolvedElement).getORSignature();
if (!signature.isEmpty()) {
String sig = DocFormatter.escapeCodeForHtml(signature.asString(originalElement.getLanguage()));
if (resolvedElement instanceof PsiQualifiedNamedElement) {
String path = ORUtil.getQualifiedPath((PsiNameIdentifierOwner) resolvedElement);
if (resolvedElement instanceof PsiQualifiedElement) {
PsiQualifiedElement qualifiedElement = (PsiQualifiedElement) resolvedElement;
String elementType = PsiTypeElementProvider.getType(resolvedElement);
String desc = ((PsiNamedElement) resolvedElement).getName();
return createQuickDocTemplate(path, elementType, desc, sig);
return createQuickDocTemplate(qualifiedElement.getPath(), elementType, qualifiedElement.getName(), sig);
}
return sig;
}
}

// No signature found, but resolved
if (resolvedElement instanceof PsiQualifiedNamedElement) {
if (resolvedElement instanceof PsiNameIdentifierOwner) {
String elementType = PsiTypeElementProvider.getType(resolvedElement);
String desc = ((PsiQualifiedNamedElement) resolvedElement).getName();
String desc = ((PsiNameIdentifierOwner) resolvedElement).getName();
String path = ORUtil.getQualifiedPath((PsiNameIdentifierOwner) resolvedElement);

if (inferredType == null) {
Expand Down
8 changes: 7 additions & 1 deletion src/com/reason/ide/files/FileBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ public PsiType getTypeExpression(@Nullable String name) {
return expressions.isEmpty() ? null : expressions.iterator().next();
}

@Nullable
@NotNull
@Override
public String getPath() {
return getModuleName();
}

@NotNull
@Override
public String getQualifiedName() {
return getModuleName();
Expand Down
6 changes: 3 additions & 3 deletions src/com/reason/ide/go/ORModuleContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.navigation.GotoClassContributor;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Processor;
import com.intellij.util.indexing.FindSymbolParameters;
Expand All @@ -16,6 +15,7 @@
import com.reason.ide.search.index.ModuleIndex;
import com.reason.lang.core.ORFileType;
import com.reason.lang.core.psi.PsiModule;
import com.reason.lang.core.psi.PsiQualifiedElement;

// Implements the goto class
public class ORModuleContributor implements GotoClassContributor, ChooseByNameContributorEx {
Expand Down Expand Up @@ -43,8 +43,8 @@ public void processElementsWithName(@NotNull String name, @NotNull Processor<Nav
public String getQualifiedName(NavigationItem item) {
if (item instanceof FileBase) {
return ((FileBase) item).getModuleName();
} else if (item instanceof PsiQualifiedNamedElement) {
return ((PsiQualifiedNamedElement) item).getQualifiedName();
} else if (item instanceof PsiQualifiedElement) {
return ((PsiQualifiedElement) item).getQualifiedName();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.reason.lang.core.psi.PsiAnnotation;
import com.reason.lang.core.psi.PsiException;
import com.reason.lang.core.psi.PsiExternal;
import com.reason.lang.core.psi.PsiInclude;
import com.reason.lang.core.psi.PsiInnerModule;
import com.reason.lang.core.psi.PsiLet;
import com.reason.lang.core.psi.PsiModule;
Expand Down Expand Up @@ -114,28 +113,23 @@ public static void addCompletions(@NotNull QNameFinder qnameFinder, @NotNull Str
}

while (item != null) {
if (item instanceof PsiInclude) {
//Set<PsiModule> modulesFromQn = psiFinder.findModulesFromQn(((PsiInclude) item).getQualifiedName(), interfaceOrImplementation, scope);
//for (PsiModule module : modulesFromQn) {
// for (PsiNamedElement expression : module.getExpressions()) {
// resultSet.addElement(LookupElementBuilder.
// create(expression).
// withTypeText(PsiSignatureUtil.getSignature(expression, element.getLanguage())).
// withIcon(PsiIconUtil.getProvidersIcon(element, 0)));
// if (item instanceof PsiType) {
// expandType((PsiType) item, resultSet);
// }
// }
//}
} else if (item instanceof PsiInnerModule || item instanceof PsiLet || item instanceof PsiType || item instanceof PsiExternal
if (item instanceof PsiInnerModule || item instanceof PsiLet || item instanceof PsiType || item instanceof PsiExternal
|| item instanceof PsiException || item instanceof PsiVal) {
PsiNamedElement expression = (PsiNamedElement) item;
resultSet.addElement(LookupElementBuilder.
create(expression).
withTypeText(PsiSignatureUtil.getSignature(expression, element.getLanguage())).
withIcon(PsiIconUtil.getProvidersIcon(expression, 0)));
if (item instanceof PsiType) {
expandType((PsiType) item, resultSet);
if (item instanceof PsiLet && ((PsiLet) item).isDeconsruction()) {
for (PsiElement deconstructedElement : ((PsiLet) item).getDeconstructedElements()) {
resultSet.addElement(LookupElementBuilder.create(deconstructedElement.getText()).
withTypeText(PsiSignatureUtil.getSignature(item, element.getLanguage())).
withIcon(Icons.LET));
}
} else {
PsiNamedElement expression = (PsiNamedElement) item;
resultSet.addElement(LookupElementBuilder.
create(expression).
withTypeText(PsiSignatureUtil.getSignature(expression, element.getLanguage())).
withIcon(PsiIconUtil.getProvidersIcon(expression, 0)));
if (item instanceof PsiType) {
expandType((PsiType) item, resultSet);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.tree.IElementType;
Expand All @@ -20,6 +19,7 @@
import com.reason.ide.search.PsiFinder;
import com.reason.lang.core.ModulePath;
import com.reason.lang.core.psi.PsiInnerModule;
import com.reason.lang.core.psi.PsiModule;
import com.reason.lang.core.psi.PsiUpperSymbol;
import com.reason.lang.core.type.ORTypes;

Expand Down Expand Up @@ -54,7 +54,8 @@ public static void addCompletions(@NotNull ORTypes types, @NotNull PsiElement el
}
} else {
PsiFinder psiFinder = PsiFinder.getInstance(project);
PsiQualifiedNamedElement foundModule = psiFinder.findModulesFromQn(modulePath.toString(), interfaceOrImplementation, scope).iterator().next();
Set<PsiModule> modulesFromQn = psiFinder.findModulesFromQn(modulePath.toString(), interfaceOrImplementation, scope);
PsiModule foundModule = modulesFromQn.isEmpty() ? null : modulesFromQn.iterator().next();
if (foundModule != null) {
LOG.debug(" Found module", foundModule);
Collection<PsiInnerModule> modules = foundModule instanceof FileBase ? ((FileBase) foundModule).getModules() :
Expand Down
27 changes: 14 additions & 13 deletions src/com/reason/ide/insight/provider/ObjectCompletionProvider.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.reason.ide.insight.provider;

import java.util.*;
import org.jetbrains.annotations.NotNull;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.PsiIconUtil;
import com.reason.Log;
import com.reason.ide.search.PsiFinder;
import com.reason.lang.core.psi.*;
import com.reason.lang.core.psi.PsiLet;
import com.reason.lang.core.psi.PsiLowerSymbol;
import com.reason.lang.core.psi.PsiObjectField;
import com.reason.lang.core.psi.PsiRecordField;
import com.reason.lang.core.type.ORTypes;
import org.jetbrains.annotations.NotNull;

import java.util.*;

import static com.reason.lang.core.ORFileType.interfaceOrImplementation;

Expand All @@ -35,7 +36,8 @@ public static void addCompletions(@NotNull ORTypes types, @NotNull PsiElement el
if (previousLeaf != null) {
IElementType previousElementType = previousLeaf.getNode().getElementType();

while (previousLeaf != null && previousElementType == types.LIDENT || previousElementType == types.SHARPSHARP || previousElementType == types.SHARP) {
while (previousLeaf != null && previousElementType == types.LIDENT || previousElementType == types.SHARPSHARP
|| previousElementType == types.SHARP) {
if (previousElementType == types.LIDENT) {
//noinspection ConstantConditions
LeafPsiElement node = (LeafPsiElement) previousLeaf.getNode();
Expand All @@ -52,16 +54,17 @@ public static void addCompletions(@NotNull ORTypes types, @NotNull PsiElement el
return;
}

PsiLet let = null;
String lowerName = path.remove(0);

Collection<? extends PsiQualifiedNamedElement> lets = PsiFinder.getInstance(project).findLets(lowerName, interfaceOrImplementation);

PsiLet let = null;
Set<PsiLet> lets = PsiFinder.getInstance(project).findLets(lowerName, interfaceOrImplementation);
if (!lets.isEmpty()) {
let = (PsiLet) lets.iterator().next();
let = lets.iterator().next();
}

if (let == null) return;
if (let == null) {
return;
}

if (let.isRecord()) {
Collection<PsiRecordField> fields = let.getRecordFields();
Expand All @@ -79,8 +82,6 @@ public static void addCompletions(@NotNull ORTypes types, @NotNull PsiElement el
resultSet.addElement(LookupElementBuilder.create(name).withIcon(PsiIconUtil.getProvidersIcon(field, 0)));
}
}

}

}
}
4 changes: 2 additions & 2 deletions src/com/reason/ide/intentions/ExpandLocalOpenIntention.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.reason.lang.core.ORElementFactory;
import com.reason.lang.core.ORCodeFactory;
import com.reason.lang.core.psi.PsiLocalOpen;
import com.reason.lang.reason.RmlTypes;
import org.jetbrains.annotations.Nls;
Expand Down Expand Up @@ -56,7 +56,7 @@ void runInvoke(@NotNull Project project, @NotNull PsiLocalOpen parentElement) {
}

String text = parentElement.getText();
PsiElement newOpen = ORElementFactory.createExpression(project, "{ open " + modulePath + "; " + text.substring(1, text.length() - 1) + "; }");
PsiElement newOpen = ORCodeFactory.createExpression(project, "{ open " + modulePath + "; " + text.substring(1, text.length() - 1) + "; }");
if (newOpen != null) {
grandParentElement.getNode().replaceChild(parentElement.getNode(), newOpen.getNode());
}
Expand Down
8 changes: 4 additions & 4 deletions src/com/reason/ide/intentions/FunctionBracesIntention.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.reason.ide.intentions;

import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.reason.lang.core.ORElementFactory;
import com.reason.lang.core.ORCodeFactory;
import com.reason.lang.core.psi.PsiFunction;
import com.reason.lang.core.psi.PsiFunctionBody;
import com.reason.lang.core.psi.PsiLet;
import com.reason.lang.core.psi.PsiScopedExpr;
import com.reason.lang.reason.RmlTypes;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

public class FunctionBracesIntention extends AbstractBaseIntention<PsiFunction> {

Expand Down Expand Up @@ -54,7 +54,7 @@ public boolean isAvailable(@NotNull Project project, @NotNull PsiFunction parent
void runInvoke(@NotNull Project project, @NotNull PsiFunction oldFunction) {
String text = oldFunction.getText();
String[] tokens = text.split("=>", 2);
PsiLet newSyntax = (PsiLet) ORElementFactory.createExpression(project, "let x = " + tokens[0] + "=> {" + tokens[1] + "; };");
PsiLet newSyntax = (PsiLet) ORCodeFactory.createExpression(project, "let x = " + tokens[0] + "=> {" + tokens[1] + "; };");

if (newSyntax != null) {
PsiFunction newFunction = newSyntax.getFunction();
Expand Down
Loading

0 comments on commit dd1fe03

Please sign in to comment.