Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff5042a

Browse files
fix compilation + bug fix + some refactoring
1 parent c49e419 commit ff5042a

File tree

108 files changed

+4218
-3742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+4218
-3742
lines changed

OCamlSources/src/ocaml/entity/OCamlModule.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import ocaml.lang.feature.resolving.OCamlResolvedReference;
3333
import ocaml.lang.fileType.ml.MLFileType;
3434
import ocaml.lang.fileType.mli.MLIFileType;
35+
import ocaml.lang.processing.parser.psi.OCamlElement;
3536
import ocaml.lang.processing.parser.psi.OCamlPsiUtil;
3637
import ocaml.lang.processing.parser.psi.element.OCamlFile;
3738
import ocaml.lang.processing.parser.psi.element.OCamlModuleName;
39+
import ocaml.lang.processing.parser.psi.element.OCamlStructuredBinding;
3840
import ocaml.util.OCamlFileUtil;
3941
import ocaml.util.OCamlStringUtil;
4042
import ocaml.util.TreeNode;
@@ -116,7 +118,9 @@ public PsiFile compute() {
116118
}
117119
});
118120
if (psiFile == null || !(psiFile instanceof OCamlFile)) return;
119-
final List<OCamlModuleName> moduleReferences = OCamlPsiUtil.collectModuleReferences((OCamlFile) psiFile);
121+
final OCamlElement binding = ((OCamlFile) psiFile).getModuleBinding(OCamlStructuredBinding.class);
122+
if (binding == null) return;
123+
final List<OCamlModuleName> moduleReferences = OCamlPsiUtil.collectModuleReferences(binding);
120124
for (final OCamlModuleName moduleReference : moduleReferences) {
121125
final OCamlResolvedReference resolvedReference = moduleReference.resolve();
122126
if (resolvedReference == null) continue;
@@ -185,7 +189,7 @@ public VirtualFile getSourcesDir() {
185189

186190
@NotNull
187191
public String getName() {
188-
return myName.length() > 0 ? OCamlStringUtil.capitalize(myName) : "";
192+
return myName.length() > 0 ? OCamlStringUtil.firstLetterToUpperCase(myName) : "";
189193
}
190194

191195
@NotNull

OCamlSources/src/ocaml/lang/feature/findUsages/OCamlFindUsagesProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,23 @@ public String getType(@NotNull final PsiElement element) {
6464

6565
@NotNull
6666
public String getDescriptiveName(@NotNull final PsiElement element) {
67-
return getCanonicalName(element);
67+
return getName(element);
6868
}
6969

7070
@NotNull
7171
public String getNodeText(@NotNull final PsiElement element, final boolean useFullName) {
72-
final StringBuilder sb = new StringBuilder(OCamlStringUtil.capitalize(getType(element)));
72+
final StringBuilder sb = new StringBuilder(OCamlStringUtil.firstLetterToUpperCase(getType(element)));
7373
if (sb.length() > 0) {
7474
sb.append(" ");
7575
}
76-
sb.append(useFullName ? getCanonicalPath(element) : getCanonicalName(element));
76+
sb.append(useFullName ? getCanonicalPath(element) : getName(element));
7777
return sb.toString();
7878
}
7979

8080
@NotNull
81-
private String getCanonicalName(@NotNull final PsiElement element) {
81+
private String getName(@NotNull final PsiElement element) {
8282
if (element instanceof OCamlNamedElement) {
83-
return OCamlStringUtil.getNotNull(((OCamlNamedElement) element).getCanonicalName());
83+
return OCamlStringUtil.getNotNull(((OCamlNamedElement) element).getName());
8484
}
8585
return "";
8686
}

OCamlSources/src/ocaml/lang/feature/resolving/OCamlNamedElement.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public interface OCamlNamedElement extends OCamlElement, PsiNamedElement, Naviga
3333
@Nullable
3434
ASTNode getNameElement();
3535

36-
@Nullable
37-
String getCanonicalName();
38-
3936
@Nullable
4037
String getCanonicalPath();
4138

OCamlSources/src/ocaml/lang/feature/resolving/impl/BaseOCamlNamedElement.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,15 @@ protected BaseOCamlNamedElement(@NotNull final ASTNode node) {
4444
super(node);
4545
}
4646

47-
@Nullable
48-
public String getCanonicalName() {
49-
return getName();
50-
}
51-
5247
@Nullable
5348
public String getCanonicalPath() {
54-
final StringBuilder sb = new StringBuilder(OCamlStringUtil.getNotNull(getCanonicalName()));
49+
final StringBuilder sb = new StringBuilder(OCamlStringUtil.getNotNull(getName()));
5550

5651
final OCamlElementProcessorAdapter processor = new OCamlElementProcessorAdapter() {
5752
public void process(@NotNull final OCamlElement psiElement) {
5853
if (psiElement instanceof OCamlModuleDefinitionBinding || psiElement instanceof OCamlModuleSpecificationBinding) {
5954
sb.insert(0, ".");
60-
sb.insert(0, OCamlStringUtil.getNotNull(((OCamlNamedElement) psiElement).getCanonicalName()));
55+
sb.insert(0, OCamlStringUtil.getNotNull(((OCamlNamedElement) psiElement).getName()));
6156
}
6257
}
6358
};
@@ -79,17 +74,18 @@ public String getName() {
7974
return nameElement == null ? null : nameElement.getText();
8075
}
8176

82-
@NotNull
83-
public PsiElement setName(@NotNull final String name) throws IncorrectOperationException {
77+
@Override
78+
public int getTextOffset() {
8479
final ASTNode nameElement = getNameElement();
85-
if (nameElement == null) {
86-
throw new IncorrectOperationException("Incorrect " + getDescription() + " name");
87-
}
80+
return nameElement == null ? 0 : nameElement.getStartOffset();
81+
}
8882

83+
@NotNull
84+
public PsiElement setName(@NotNull final String name) throws IncorrectOperationException {
8985
checkNameIsNotAKeyword(name);
9086
getNameType().checkNameIsCorrect(this, name);
9187

92-
OCamlASTNodeUtil.replaceLeafText(nameElement.getFirstChildNode(), name);
88+
doSetName(name);
9389

9490
return this;
9591
}
@@ -100,15 +96,18 @@ private void checkNameIsNotAKeyword(@NotNull final String name) throws Incorrect
10096
}
10197
}
10298

103-
@Override
104-
public int getTextOffset() {
99+
protected void doSetName(@NotNull final String name) throws IncorrectOperationException {
105100
final ASTNode nameElement = getNameElement();
106-
return nameElement == null ? 0 : nameElement.getStartOffset();
101+
if (nameElement == null) {
102+
throw new IncorrectOperationException("Incorrect " + getDescription() + " name element");
103+
}
104+
105+
OCamlASTNodeUtil.replaceLeafText(nameElement.getFirstChildNode(), name);
107106
}
108107

109108
@Override
110109
public String toString() {
111-
return getDescription();
110+
return getDescription() + " " + OCamlStringUtil.getNotNull(getName());
112111
}
113112

114113
@Override

OCamlSources/src/ocaml/lang/feature/resolving/impl/BaseOCamlReference.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public PsiElement handleElementRename(@NotNull final String newElementName) thro
6767
return setName(newElementName);
6868
}
6969

70-
@NotNull
71-
public PsiElement bindToElement(@NotNull final PsiElement element) throws IncorrectOperationException {
72-
return null; //To change body of implemented methods use File | Settings | File Templates. todo
70+
@Nullable
71+
public PsiElement bindToElement(@NotNull final PsiElement element) {
72+
return null;
7373
}
7474

7575
public boolean isReferenceTo(@NotNull final PsiElement element) {
7676
return element instanceof OCamlResolvedReference
77-
&& Comparing.equal(getCanonicalName(), ((OCamlResolvedReference) element).getCanonicalName())
77+
&& Comparing.equal(getName(), ((OCamlResolvedReference) element).getName())
7878
&& resolve() == element;
7979
}
8080

OCamlSources/src/ocaml/lang/feature/resolving/util/BaseOCamlResolvedReferencesProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public boolean process(@NotNull final OCamlResolvedReference psiElement) {
5757

5858
@Nullable
5959
protected String getSourceElementName() {
60-
return myBuilder == null ? null : myBuilder.getContext().getSourceElement().getCanonicalName();
60+
return myBuilder == null ? null : myBuilder.getContext().getSourceElement().getName();
6161
}
6262

6363
protected abstract boolean doProcess(@NotNull final OCamlResolvedReference psiElement);

OCamlSources/src/ocaml/lang/feature/resolving/util/OCamlDeclarationsUtil.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,40 @@ public static boolean processDeclarationsInChildren(@NotNull final ResolvingBuil
4545
return false;
4646
}
4747

48-
public static boolean processDeclarationsInModuleBinding(@NotNull final ResolvingBuilder builder,
49-
@NotNull final OCamlStructuredBinding binding) {
48+
public static boolean processDeclarationsInStructuredElement(@NotNull final ResolvingBuilder builder,
49+
@Nullable final OCamlStructuredElement psiElement) {
50+
if (psiElement == null || builder.childWasAlreadyProcessed(psiElement)) return false;
51+
final List<OCamlStructuredElement> actualDefinitions = psiElement.findActualDefinitions();
52+
for (final OCamlStructuredElement actualDefinition : actualDefinitions) {
53+
if (actualDefinition.processDeclarations(builder)) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
60+
public static boolean processDeclarationsInStructuredBinding(@NotNull final ResolvingBuilder builder,
61+
@NotNull final OCamlStructuredBinding binding) {
5062
final OCamlStructuredElement expression = binding.getExpression();
5163
if (expression != null && builder.childWasAlreadyProcessed(expression)) return false;
64+
65+
final OCamlStructuredElement typeExpression = binding.getTypeExpression();
66+
if (typeExpression != null && builder.childWasAlreadyProcessed(typeExpression)) return false;
67+
5268
if (builder.getProcessor().process(binding)) return true;
53-
final String moduleName = binding.getCanonicalName();
54-
final ResolvingBuilder.ModuleProcessor moduleProcessor = new ResolvingBuilder.ModuleProcessor() {
69+
70+
final String moduleName = binding.getName();
71+
return moduleName != null && builder.tryProcessModule(moduleName,
72+
createModuleProcessor(builder, expression),
73+
createModuleProcessor(builder, typeExpression));
74+
}
75+
76+
private static ResolvingBuilder.ModuleProcessor createModuleProcessor(@NotNull final ResolvingBuilder builder,
77+
@Nullable final OCamlStructuredElement moduleElement) {
78+
return new ResolvingBuilder.ModuleProcessor() {
5579
public boolean process() {
56-
return processDeclarationsInStructuredElement(builder, expression);
80+
return processDeclarationsInStructuredElement(builder, moduleElement);
5781
}
5882
};
59-
ResolvingBuilder.ModuleProcessor additionalModuleProcessor = null;
60-
if (binding instanceof OCamlModuleDefinitionBinding) {
61-
final OCamlModuleType moduleType = ((OCamlModuleDefinitionBinding) binding).getModuleType();
62-
if (moduleType != null) {
63-
if (builder.childWasAlreadyProcessed(moduleType)) return false;
64-
additionalModuleProcessor = new ResolvingBuilder.ModuleProcessor() {
65-
public boolean process() {
66-
return processDeclarationsInStructuredElement(builder, moduleType);
67-
}
68-
};
69-
}
70-
}
71-
return moduleName != null && builder.tryProcessModule(moduleName, moduleProcessor, additionalModuleProcessor);
72-
}
73-
74-
public static boolean processDeclarationsInStructuredElement(@NotNull final ResolvingBuilder builder, @Nullable final OCamlStructuredElement psiElement) {
75-
if (psiElement == null || builder.childWasAlreadyProcessed(psiElement)) return false;
76-
final OCamlStructuredElement actualDefinition = psiElement.findActualDefinition();
77-
return actualDefinition != null && actualDefinition.processDeclarations(builder);
7883
}
7984
}

OCamlSources/src/ocaml/lang/feature/resolving/util/OCamlResolvingUtil.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434
import ocaml.lang.processing.parser.psi.OCamlElement;
3535
import ocaml.lang.processing.parser.psi.OCamlPsiUtil;
3636
import ocaml.lang.processing.parser.psi.element.*;
37+
import ocaml.util.OCamlCollectionsUtil;
3738
import ocaml.util.OCamlFileUtil;
3839
import ocaml.util.OCamlStringUtil;
3940
import org.jetbrains.annotations.NotNull;
4041
import org.jetbrains.annotations.Nullable;
4142

43+
import java.util.ArrayList;
44+
import java.util.Collections;
4245
import java.util.List;
4346

4447
import static com.intellij.psi.search.GlobalSearchScope.getScopeRestrictedByFileTypes;
@@ -51,12 +54,23 @@
5154
public class OCamlResolvingUtil {
5255
@NotNull public static final String PERVASIVES = "Pervasives";
5356

54-
@Nullable
55-
public static OCamlStructuredElement findActualDefinitionOfStructuredElement(@NotNull final OCamlReference reference) {
57+
@NotNull
58+
public static List<OCamlStructuredElement> findActualDefinitionsOfStructuredElementReference(@NotNull final OCamlReference reference) {
5659
final OCamlResolvedReference resolvedReference = reference.resolve();
57-
if (resolvedReference == null || !(resolvedReference instanceof OCamlStructuredBinding)) return null;
58-
final OCamlStructuredElement expression = ((OCamlStructuredBinding) resolvedReference).getExpression();
59-
return expression == null ? null : expression.findActualDefinition();
60+
if (resolvedReference == null || !(resolvedReference instanceof OCamlStructuredBinding)) return Collections.emptyList();
61+
final OCamlStructuredBinding binding = (OCamlStructuredBinding) resolvedReference;
62+
return OCamlCollectionsUtil.createNotNullValuesList(binding.getExpression(), binding.getTypeExpression());
63+
}
64+
65+
@NotNull
66+
public static List<OCamlStructuredElement> collectActualDefinitionsOfStructuredElements(final OCamlStructuredElement... elements) {
67+
final List<OCamlStructuredElement> result = new ArrayList<OCamlStructuredElement>();
68+
for (final OCamlStructuredElement element : elements) {
69+
if (element != null) {
70+
result.addAll(element.findActualDefinitions());
71+
}
72+
}
73+
return result;
6074
}
6175

6276
@NotNull
@@ -102,9 +116,9 @@ else if (builder.getContext().getSourceElement() instanceof OCamlModuleName) {
102116
moduleName = builder.getContext().getSourceElement().getName();
103117
}
104118
if (moduleName != null) {
105-
final OCamlElement targetFile = findFileModule(sourceFile, moduleName);
106-
if (targetFile != null) {
107-
processSibling(targetFile, builder);
119+
final OCamlElement targetModule = findFileModule(sourceFile, moduleName);
120+
if (targetModule != null) {
121+
processSibling(targetModule, builder);
108122
}
109123
}
110124
}
@@ -121,16 +135,16 @@ private static boolean processSibling(@NotNull final OCamlElement sibling, @NotN
121135
}
122136

123137
private static boolean tryProcessPervasives(@NotNull final ResolvingBuilder builder, @NotNull final PsiFile sourceFile) {
124-
final OCamlElement pervasivesFile = findFileModule(sourceFile, PERVASIVES);
125-
return pervasivesFile != null && processSibling(pervasivesFile, builder);
138+
final OCamlElement pervasivesModule = findFileModule(sourceFile, PERVASIVES);
139+
return pervasivesModule != null && processSibling(pervasivesModule, builder);
126140
}
127141

128142
@Nullable
129143
private static OCamlElement findFileModule(@NotNull final PsiFile sourceFile, @NotNull final String moduleName) {
130144
if (OCamlFileUtil.isImplementationFile(sourceFile)) {
131-
final OCamlElement targetFile = findFileModuleDefinition(sourceFile, moduleName);
132-
if (targetFile != null) {
133-
return targetFile;
145+
final OCamlElement targetModule = findFileModuleDefinition(sourceFile, moduleName);
146+
if (targetModule != null) {
147+
return targetModule;
134148
}
135149
}
136150

@@ -140,50 +154,58 @@ private static OCamlElement findFileModule(@NotNull final PsiFile sourceFile, @N
140154
@Nullable
141155
public static OCamlModuleDefinitionBinding findFileModuleDefinition(@NotNull final PsiFile sourceFile,
142156
@NotNull final String moduleName) {
143-
return doFindFile(sourceFile, moduleName, MLFileType.INSTANCE, OCamlModuleDefinitionBinding.class);
157+
return doFindFileModule(sourceFile, moduleName, MLFileType.INSTANCE, OCamlModuleDefinitionBinding.class);
144158
}
145159

146160
@Nullable
147161
public static OCamlModuleSpecificationBinding findFileModuleSpecification(@NotNull final PsiFile sourceFile,
148162
@NotNull final String moduleName) {
149-
return doFindFile(sourceFile, moduleName, MLIFileType.INSTANCE, OCamlModuleSpecificationBinding.class);
163+
return doFindFileModule(sourceFile, moduleName, MLIFileType.INSTANCE, OCamlModuleSpecificationBinding.class);
150164
}
151165

152166
@SuppressWarnings({"unchecked"})
153167
@Nullable
154-
private static <T extends OCamlElement> T doFindFile(@NotNull final PsiFile sourceFile,
155-
@NotNull final String moduleName,
156-
@NotNull final FileType fileType,
157-
@NotNull final Class<T> type) {
168+
private static <T extends OCamlStructuredBinding> T doFindFileModule(@NotNull final PsiFile sourceFile,
169+
@NotNull final String moduleName,
170+
@NotNull final FileType fileType,
171+
@NotNull final Class<T> type) {
158172
final Project project = sourceFile.getProject();
159173

160174
final Module module = ModuleUtil.findModuleForPsiElement(sourceFile);
161175
if (module == null) return null;
162176

163177
final String fileName = OCamlFileUtil.getFileName(moduleName, fileType);
164178
final GlobalSearchScope scope = getScopeRestrictedByFileTypes(moduleWithDependenciesAndLibrariesScope(module), fileType);
165-
final T file = findFileByName(project, scope, type, fileName);
166-
return file == null ? findFileByName(project, scope, type, OCamlStringUtil.changeFirstLetterCase(fileName)) : file;
179+
final T file = findFileModuleByFileName(project, scope, type, fileName);
180+
return file == null ? findFileModuleByFileName(project, scope, type, OCamlStringUtil.changeFirstLetterCase(fileName)) : file;
167181
}
168182

169183
@Nullable
170-
private static <T extends OCamlElement> T findFileByName(@NotNull final Project project,
171-
@NotNull final GlobalSearchScope scope,
172-
@NotNull final Class<T> type,
173-
@NotNull final String fileName) {
184+
private static <T extends OCamlStructuredBinding> T findFileModuleByFileName(@NotNull final Project project,
185+
@NotNull final GlobalSearchScope scope,
186+
@NotNull final Class<T> type,
187+
@NotNull final String fileName) {
174188
final PsiFile[] files = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile[]>() {
175189
public PsiFile[] compute() {
176190
return FilenameIndex.getFilesByName(project, fileName, scope);
177191
}
178192
});
179193

180194
for (final PsiFile file : files) {
181-
if (type.isInstance(file)) {
182-
//noinspection unchecked
183-
return (T) file;
195+
if (file instanceof OCamlFile) {
196+
final T moduleBinding = ((OCamlFile) file).getModuleBinding(type);
197+
if (moduleBinding != null) {
198+
return moduleBinding;
199+
}
184200
}
185201
}
186202

187203
return null;
188204
}
205+
206+
private static void addIfNotNull(@NotNull final List<OCamlStructuredElement> list, @Nullable final OCamlStructuredElement item) {
207+
if (item != null) {
208+
list.add(item);
209+
}
210+
}
189211
}

0 commit comments

Comments
 (0)