From e5c69411c695ebd668c51c5bff329c0d1c077df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Kr=C3=A1sa?= Date: Sun, 17 Jan 2021 16:58:44 +0100 Subject: [PATCH] Find Usages performance optimization --- META-INF/plugin.xml | 7 ++++- .../idea/grammar/PumlItemReference.java | 2 ++ .../plantuml/idea/grammar/PumlPsiUtil.java | 31 ++----------------- .../psi/impl/PumlNamedElementImpl.java | 6 ++++ .../grammar/psi/impl/PumlPsiImplUtil.java | 9 ++---- .../structure/PumlStructureViewElement.java | 16 +++++++--- 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 75af6363..95e468e0 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -1,6 +1,6 @@ PlantUML integration - 3.2.0 + 3.3.0 Eugene Steinberg @@ -9,6 +9,11 @@ 3.3

+
    +
  • Find Usages performance optimization
  • +
  • Small fixes
  • +

3.2

  • Added: ctrl+click - Go to Declaration/Usages, Rename...
  • diff --git a/src/org/plantuml/idea/grammar/PumlItemReference.java b/src/org/plantuml/idea/grammar/PumlItemReference.java index 05203666..600af2c0 100644 --- a/src/org/plantuml/idea/grammar/PumlItemReference.java +++ b/src/org/plantuml/idea/grammar/PumlItemReference.java @@ -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; @@ -17,6 +18,7 @@ public class PumlItemReference extends PsiReferenceBase { + private static final Logger LOG = Logger.getInstance(PumlItemReference.class); private final String key; diff --git a/src/org/plantuml/idea/grammar/PumlPsiUtil.java b/src/org/plantuml/idea/grammar/PumlPsiUtil.java index 1b1135a9..d797a658 100644 --- a/src/org/plantuml/idea/grammar/PumlPsiUtil.java +++ b/src/org/plantuml/idea/grammar/PumlPsiUtil.java @@ -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 { @@ -33,7 +31,7 @@ public static List 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; @@ -57,7 +55,7 @@ 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; } } @@ -65,29 +63,6 @@ public static PumlItem findDeclarationInFile(PsiFile containingFile, @NotNull Pu 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 findDeclarationInAllFiles(Project project, String key) { List result = new ArrayList<>(); Collection virtualFiles = FileTypeIndex.getFiles(PlantUmlFileType.INSTANCE, GlobalSearchScope.allScope(project)); @@ -97,7 +72,7 @@ public static List 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; } diff --git a/src/org/plantuml/idea/grammar/psi/impl/PumlNamedElementImpl.java b/src/org/plantuml/idea/grammar/psi/impl/PumlNamedElementImpl.java index 730cda6a..dae25574 100644 --- a/src/org/plantuml/idea/grammar/psi/impl/PumlNamedElementImpl.java +++ b/src/org/plantuml/idea/grammar/psi/impl/PumlNamedElementImpl.java @@ -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; @@ -11,4 +13,8 @@ public PumlNamedElementImpl(@NotNull ASTNode node) { super(node); } + @NotNull + public SearchScope getUseScope() { + return new LocalSearchScope(this.getContainingFile()); + } } diff --git a/src/org/plantuml/idea/grammar/psi/impl/PumlPsiImplUtil.java b/src/org/plantuml/idea/grammar/psi/impl/PumlPsiImplUtil.java index b7f690bd..da465e7b 100644 --- a/src/org/plantuml/idea/grammar/psi/impl/PumlPsiImplUtil.java +++ b/src/org/plantuml/idea/grammar/psi/impl/PumlPsiImplUtil.java @@ -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; @@ -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 @@ -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 diff --git a/src/org/plantuml/idea/grammar/structure/PumlStructureViewElement.java b/src/org/plantuml/idea/grammar/structure/PumlStructureViewElement.java index 4d5afe71..796e7edc 100644 --- a/src/org/plantuml/idea/grammar/structure/PumlStructureViewElement.java +++ b/src/org/plantuml/idea/grammar/structure/PumlStructureViewElement.java @@ -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; @@ -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 @@ -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(); @@ -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 treeElements = new ArrayList<>(); List list = PsiTreeUtil.getChildrenOfTypeAsList(myElement, PumlItemImpl.class); @@ -89,7 +98,6 @@ public TreeElement[] getChildren() { } } - return treeElements.toArray(new TreeElement[0]); } return EMPTY_ARRAY;