Skip to content

Commit

Permalink
Excessive CPU use (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud committed Jul 21, 2023
1 parent e6a715c commit 2ecd554
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/reason/ide/IconProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public Icon getIcon(@NotNull PsiElement element, int flags) {
return ORIcons.EXCEPTION;
} else if (element instanceof RPsiInnerModule innerModule) {
return innerModule.isModuleType() ? ORIcons.INNER_MODULE_INTF : ORIcons.INNER_MODULE;
} else if (element instanceof RPsiModuleSignature) {
return ORIcons.MODULE_TYPE;
} else if (element instanceof RPsiFunctor) {
return ORIcons.FUNCTOR;
} else if (element instanceof RPsiType) {
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/reason/ide/go/ORLineMarkerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ private void collectLetNavigationMarkers(@NotNull RPsiLet element, @NotNull Proj
boolean inInterface = module != null && module.isInterfaceFile();

if (module instanceof RPsiInnerModule innerModule) {
inInterface = innerModule.isModuleType();
inInterface = innerModule.isModuleType() || inInterface;
String letName = element.getName();
if (letName != null) {
targets = inInterface
? findTargetFromInterfaceModule((RPsiInnerModule) module, letName, RPsiVar.class, scope)
: findTargetFromImplementationModule((RPsiInnerModule) module, letName, RPsiVar.class);
? findTargetFromInterfaceModule(innerModule, letName, RPsiVar.class, scope)
: findTargetFromImplementationModule(innerModule, letName, RPsiVar.class);
}
} else if (module != null) {
// Top module navigation
Expand All @@ -68,7 +68,7 @@ private void collectLetNavigationMarkers(@NotNull RPsiLet element, @NotNull Proj
} else {
resolvedElements = qName == null ? null : LetFqnIndex.getElements(qName, project, scope);
}
targets = resolveTargetFromIndex(module.isInterfaceFile(), resolvedElements);
targets = resolveTargetFromIndex(inInterface, resolvedElements);
}

RelatedItemLineMarkerInfo<PsiElement> marker = createMarkerInfo(element, inInterface, "let/val", targets);
Expand All @@ -78,17 +78,20 @@ private void collectLetNavigationMarkers(@NotNull RPsiLet element, @NotNull Proj
}

private void collectValNavigationMarkers(@NotNull RPsiVal element, @NotNull Project project, @NotNull GlobalSearchScope scope, @NotNull Collection<? super RelatedItemLineMarkerInfo<PsiElement>> result) {
List<? extends RPsiVar> targets = null;

RPsiModule module = PsiTreeUtil.getStubOrPsiParentOfType(element, RPsiModule.class);
boolean inInterface = module != null && module.isInterfaceFile();
List<? extends RPsiQualifiedPathElement> targets = null;

if (module instanceof RPsiInnerModule) {
String elementName = element.getName();
if (elementName != null) {
targets = inInterface ? findTargetFromInterfaceModule((RPsiInnerModule) module, elementName, RPsiVar.class, scope)
: findTargetFromImplementationModule((RPsiInnerModule) module, elementName, RPsiVar.class);
if (module instanceof RPsiInnerModule innerModule) {
inInterface = innerModule.isModuleType() || inInterface;
String valName = element.getName();
if (valName != null) {
targets = inInterface
? findTargetFromInterfaceModule(innerModule, valName, RPsiVar.class, scope)
: findTargetFromImplementationModule(innerModule, valName, RPsiVar.class);
}
} else {
} else if (module != null) {
// Top module navigation
String qName = element.getQualifiedName();
Collection<? extends RPsiVar> resolvedElements = qName == null ? null : LetFqnIndex.getElements(qName, project, scope);
Expand Down
52 changes: 29 additions & 23 deletions src/main/java/com/reason/ide/structure/StructureViewElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ public Icon getIcon(boolean unused) {
return new ItemPresentation() {
@Override
public @NotNull String getPresentableText() {
if (myElement instanceof PsiNamedElement namedElement) {
String name = namedElement.getName();
if (name != null) {
return name;
}
}
return "Unknown presentation for element " + myElement.getText();
}

@Nullable
@Override
public String getLocationString() {
return null;
public @NotNull String getLocationString() {
return "";
}

@Nullable
@Override
public Icon getIcon(boolean unused) {
return null;
public @Nullable Icon getIcon(boolean unused) {
return PsiIconUtil.getProvidersIcon(myElement, 0);
}
};
}
Expand Down Expand Up @@ -171,19 +175,21 @@ else if (myElement instanceof RPsiDuneStanza) {
private @NotNull List<TreeElement> buildModuleStructure(@NotNull RPsiInnerModule moduleElement) {
List<TreeElement> treeElements = new ArrayList<>();

RPsiModuleSignature moduleType = moduleElement.getModuleSignature();
PsiElement rootElement = moduleType;
if (rootElement == null) {
rootElement = moduleElement.getBody();
RPsiModuleSignature moduleSignature = moduleElement.getModuleSignature();
if (moduleSignature != null) {
treeElements.add(new StructureViewElement(moduleSignature, myLevel + 1));
}

if (rootElement != null) {
rootElement.acceptChildren(new ElementVisitor(treeElements, myLevel));
if (moduleSignature == null) {
PsiElement body = moduleElement.getBody();
if (body != null) {
body.acceptChildren(new ElementVisitor(treeElements, myLevel));
}
}

// Process body if there is a signature
if (moduleType != null) {
rootElement = moduleElement.getBody();
if (moduleSignature != null) {
PsiElement rootElement = moduleElement.getBody();
if (rootElement != null) {
treeElements.add(new StructureModuleImplView(rootElement));
}
Expand Down Expand Up @@ -256,12 +262,12 @@ else if (myElement instanceof RPsiDuneStanza) {
}

static class ElementVisitor extends PsiElementVisitor {
private final List<TreeElement> m_treeElements;
private final int m_elementLevel;
private final List<TreeElement> myTreeElements;
private final int myElementLevel;

ElementVisitor(List<TreeElement> elements, int elementLevel) {
m_treeElements = elements;
m_elementLevel = elementLevel;
myTreeElements = elements;
myElementLevel = elementLevel;
}

@Override
Expand All @@ -273,23 +279,23 @@ public void visitElement(@NotNull PsiElement element) {
// it's a tuple! add each element of the tuple separately.
for (PsiElement child : let.getScopeChildren()) {
if (child instanceof RPsiLowerSymbol) {
m_treeElements.add(new StructureViewElement(child, element, true, m_elementLevel));
myTreeElements.add(new StructureViewElement(child, element, true, myElementLevel));
}
}
return;
}
}
m_treeElements.add(new StructureViewElement(element, m_elementLevel));
myTreeElements.add(new StructureViewElement(element, myElementLevel));
}
} else if (element instanceof RPsiRecord) {
for (RPsiRecordField field : ((RPsiRecord) element).getFields()) {
m_treeElements.add(new StructureViewElement(field, m_elementLevel));
myTreeElements.add(new StructureViewElement(field, myElementLevel));
}
} else if (element instanceof RPsiScopedExpr && m_elementLevel < 2) {
} else if (element instanceof RPsiScopedExpr && myElementLevel < 2) {
List<RPsiStructuredElement> children = ORUtil.findImmediateChildrenOfClass(element, RPsiStructuredElement.class);
for (RPsiStructuredElement child : children) {
if (child.canBeDisplayed()) {
m_treeElements.add(new StructureViewElement(child, m_elementLevel));
myTreeElements.add(new StructureViewElement(child, myElementLevel));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public boolean isFunctorCall() {
}

public ItemPresentation getPresentation() {
//RPsiModuleSignature moduleSignature = getModuleSignature();

return new ItemPresentation() {
@Override
public @Nullable String getPresentableText() {
Expand All @@ -192,6 +194,7 @@ public ItemPresentation getPresentation() {

@Override
public @NotNull String getLocationString() {
//return moduleSignature != null ? moduleSignature.getQualifiedName() : getQualifiedName();
return getQualifiedName();
}

Expand All @@ -206,6 +209,6 @@ public ItemPresentation getPresentation() {

@Override
public String toString() {
return "RPsiModule:" + getModuleName();
return super.toString() + ":" + getModuleName();
}
}
8 changes: 3 additions & 5 deletions src/main/java/com/reason/lang/ocaml/OclParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ private void parseExternal() {

@SuppressWarnings("StatementWithEmptyBody")
private void parseType() {
if (is(myTypes.C_MODULE_DECLARATION)) {
if (is(myTypes.C_MODULE_DECLARATION) || is(myTypes.C_MODULE_SIGNATURE)) {
// module |>type<| M = ...
} else if (is(myTypes.C_TYPE_VARIABLE)) {
// let x : |>type<| ...
Expand Down Expand Up @@ -1349,10 +1349,8 @@ private void parseLet() {
private void parseModule() {
if (is(myTypes.C_LET_DECLARATION)) {
updateComposite(myTypes.C_MODULE_DECLARATION);
} else if (!is(myTypes.C_MACRO_NAME)) {
if (!is(myTypes.C_MODULE_SIGNATURE)) {
popEndUntilScope();
}
} else if (!is(myTypes.C_MACRO_NAME) && !is(myTypes.C_MODULE_SIGNATURE)) {
popEndUntilScope();
mark(myTypes.C_MODULE_DECLARATION);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/reason/ide/structure/StructureOCLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void test_module_type_extraction() {
TreeElement e = model.getRoot().getChildren()[0];
assertPresentation("X", "B.X", ORIcons.INNER_MODULE, e.getPresentation());
TreeElement ee = e.getChildren()[0];
assertPresentation("A.S", "", ORIcons.MODULE_TYPE, ee.getPresentation());
assertPresentation("S", "", ORIcons.MODULE_TYPE, ee.getPresentation());
}

@Test
Expand All @@ -99,7 +99,7 @@ public void test_module_type_extraction_functor() {
TreeElement e = model.getRoot().getChildren()[0];
assertPresentation("X", "B.X", ORIcons.INNER_MODULE, e.getPresentation());
TreeElement ee = e.getChildren()[0];
assertPresentation("A.Vcs.Branch", "", ORIcons.MODULE_TYPE, ee.getPresentation());
assertPresentation("Branch", "", ORIcons.MODULE_TYPE, ee.getPresentation());
}

// https://github.com/giraud/reasonml-idea-plugin/issues/274
Expand Down Expand Up @@ -140,7 +140,11 @@ public void test_GH_407() {
// https://github.com/giraud/reasonml-idea-plugin/issues/408
@Test
public void test_GH_408() {
FileBase e = configureCode("A.ml", "let a =\n let x = 0 in\n x");
FileBase e = configureCode("A.ml", """
let a =
let x = 0 in
x
""");
StructureViewModel model = new ORStructureViewModel(e);

TreeElement fn = model.getRoot().getChildren()[0];
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/reason/lang/ocaml/ModuleParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ public void test_GH_91() {
assertEquals(2, expressions(file).size());
assertEquals("Branch", first(moduleExpressions(file)).getName());
RPsiInnerModule e = (RPsiInnerModule) expressions(file).iterator().next();

RPsiModuleSignature modType = e.getModuleSignature();
assertEquals("module type of Vcs_.Branch", modType.getText());
assertNull(modType.getName());
assertNull(PsiTreeUtil.findChildOfType(modType, RPsiInnerModule.class));
assertEquals("Branch", modType.getName());
//assertEquals("Vcs_.Branch", modType.getQualifiedName());
}
}

0 comments on commit 2ecd554

Please sign in to comment.