Skip to content

Commit

Permalink
Find Usages performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
krasa committed Jan 17, 2021
1 parent b596637 commit e5c6941
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
7 changes: 6 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<idea-plugin version="2">
<name>PlantUML integration</name>
<version>3.2.0</version>
<version>3.3.0</version>
<vendor url="https://github.com/esteinberg/plantuml4idea/">Eugene Steinberg</vendor>
<!-- IJ 12+ -->
<idea-version since-build="193"/>
Expand All @@ -9,6 +9,11 @@

<change-notes>
<![CDATA[
<p>3.3</p>
<ul>
<li>Find Usages performance optimization</li>
<li>Small fixes</li>
</ul>
<p>3.2</p>
<ul>
<li>Added: ctrl+click - Go to Declaration/Usages, Rename...</li>
Expand Down
2 changes: 2 additions & 0 deletions src/org/plantuml/idea/grammar/PumlItemReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.util.PsiTreeUtil;
Expand All @@ -17,6 +18,7 @@

public class PumlItemReference extends PsiReferenceBase<PumlItem> {

private static final Logger LOG = Logger.getInstance(PumlItemReference.class);

private final String key;

Expand Down
31 changes: 3 additions & 28 deletions src/org/plantuml/idea/grammar/PumlPsiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PumlPsiUtil {

Expand All @@ -33,7 +31,7 @@ public static List<PumlItem> findDeclarationOrUsagesInFile(PsiFile containingFil

if (items != null) {
for (PumlItem item : items) {
if (isSame(key, item.getText())) {
if (key.equals(item.getText())) {
if (firstMatch && item == element) {
returnFirst = false;
continue;
Expand All @@ -57,37 +55,14 @@ public static PumlItem findDeclarationInFile(PsiFile containingFile, @NotNull Pu
if (item == element) {
return null;
}
if (isSame(key, item.getText())) {
if (key.equals(item.getText())) {
return item;
}
}
}
return null;
}

static final Pattern SANITIZER = Pattern.compile("([a-zA-Z0-9].*[a-zA-Z0-9])");

public static boolean isSame(String p1, String p2) {
if (p1.equals(p2)) {
return true;
}
Matcher m = SANITIZER.matcher(p1);
String key = null;
if (m.find()) {
key = m.group();
}
if (key == null) {
return false;
}

String key2 = null;
m.reset(p2);
if (m.find()) {
key2 = m.group();
}
return key.equals(key2);
}

public static List<PumlItem> findDeclarationInAllFiles(Project project, String key) {
List<PumlItem> result = new ArrayList<>();
Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(PlantUmlFileType.INSTANCE, GlobalSearchScope.allScope(project));
Expand All @@ -97,7 +72,7 @@ public static List<PumlItem> findDeclarationInAllFiles(Project project, String k
PumlItem[] properties = PsiTreeUtil.getChildrenOfType(simpleFile, PumlItem.class);
if (properties != null) {
for (PumlItem property : properties) {
if (isSame(key, property.getText())) {
if (key.equals(property.getText())) {
result.add(property);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import org.jetbrains.annotations.NotNull;
import org.plantuml.idea.grammar.psi.PumlNamedElement;

Expand All @@ -11,4 +13,8 @@ public PumlNamedElementImpl(@NotNull ASTNode node) {
super(node);
}

@NotNull
public SearchScope getUseScope() {
return new LocalSearchScope(this.getContainingFile());
}
}
9 changes: 2 additions & 7 deletions src/org/plantuml/idea/grammar/psi/impl/PumlPsiImplUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.lang.ASTNode;
import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
Expand Down Expand Up @@ -56,7 +55,7 @@ public Icon getIcon(boolean unused) {
};
}

public static ItemPresentation getPresentation2(final PumlItem element, Document document) {
public static ItemPresentation getPresentation2(final PumlItem element, String line) {
return new ItemPresentation() {
@Nullable
@Override
Expand All @@ -67,11 +66,7 @@ public String getPresentableText() {
@Nullable
@Override
public String getLocationString() {
if (document == null) {
return null;
}
int lineNumber = document.getLineNumber(element.getTextOffset());
return "line: " + lineNumber;
return line;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.intellij.ide.util.treeView.smartTree.SortableTreeElement;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.NavigatablePsiElement;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.plantuml.idea.grammar.psi.PumlItem;
Expand All @@ -21,13 +21,23 @@
import java.util.List;

public class PumlStructureViewElement implements StructureViewTreeElement, SortableTreeElement {
private static final Logger LOG = Logger.getInstance(PumlStructureViewElement.class);

private final NavigatablePsiElement myElement;
private final Document document;
private String line;

public PumlStructureViewElement(NavigatablePsiElement element, Document document) {
this.myElement = element;
this.document = document;
try {
if (element.isValid()) {
line = "line: " + document.getLineNumber(element.getTextOffset());
}
} catch (Throwable e) {
//old document, ignore, (maybe #isValid solves it, dunno)
LOG.debug(e);
}
}

@Override
Expand Down Expand Up @@ -61,7 +71,7 @@ public String getAlphaSortKey() {
@Override
public ItemPresentation getPresentation() {
if (myElement instanceof PumlItem) {
return PumlPsiImplUtil.getPresentation2((PumlItem) myElement, document);
return PumlPsiImplUtil.getPresentation2((PumlItem) myElement, line);
}
ItemPresentation presentation = myElement.getPresentation();
return presentation != null ? presentation : new PresentationData();
Expand All @@ -70,7 +80,6 @@ public ItemPresentation getPresentation() {
@NotNull
@Override
public TreeElement[] getChildren() {
Document document = PsiDocumentManager.getInstance(myElement.getProject()).getDocument(myElement.getContainingFile());
if (myElement instanceof PlantUmlFileImpl) {
List<TreeElement> treeElements = new ArrayList<>();
List<PumlItemImpl> list = PsiTreeUtil.getChildrenOfTypeAsList(myElement, PumlItemImpl.class);
Expand All @@ -89,7 +98,6 @@ public TreeElement[] getChildren() {
}
}


return treeElements.toArray(new TreeElement[0]);
}
return EMPTY_ARRAY;
Expand Down

0 comments on commit e5c6941

Please sign in to comment.