optimize full-text-search

This commit is contained in:
Marcel Haßlinger 2021-11-09 23:00:34 +01:00
parent 47fde38168
commit 130b5ab897

View File

@ -4,6 +4,7 @@ import com.intellij.ide.projectView.PresentationData;
import com.intellij.ui.JBColor; import com.intellij.ui.JBColor;
import de.marhali.easyi18n.model.SettingsState; import de.marhali.easyi18n.model.SettingsState;
import de.marhali.easyi18n.model.Translation;
import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.model.TranslationNode; import de.marhali.easyi18n.model.TranslationNode;
import de.marhali.easyi18n.model.bus.SearchQueryListener; import de.marhali.easyi18n.model.bus.SearchQueryListener;
@ -34,34 +35,53 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
this.state = state; this.state = state;
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
this.generateNodes(rootNode, this.data.getRootNode(), null); this.generateNodes(rootNode, this.data.getRootNode());
super.setRoot(rootNode); super.setRoot(rootNode);
} }
@Override @Override
public void onSearchQuery(@Nullable String query) { public void onSearchQuery(@Nullable String query) {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
this.generateNodes(rootNode, this.data.getRootNode(), query); TranslationData shadow = new TranslationData(this.state.isSortKeys(), this.state.isNestedKeys());
if(query == null) {
this.generateNodes(rootNode, this.data.getRootNode());
super.setRoot(rootNode);
return;
}
query = query.toLowerCase();
for(String currentKey : this.data.getFullKeys()) {
Translation translation = this.data.getTranslation(currentKey);
String loweredKey = currentKey.toLowerCase();
if(query.contains(loweredKey) || loweredKey.contains(query)) {
shadow.setTranslation(currentKey, translation);
continue;
}
for(String currentContent : translation.values()) {
if(currentContent.toLowerCase().contains(query)) {
shadow.setTranslation(currentKey, translation);
break;
}
}
}
this.generateNodes(rootNode, shadow.getRootNode());
super.setRoot(rootNode); super.setRoot(rootNode);
} }
private void generateNodes(@NotNull DefaultMutableTreeNode parent, private void generateNodes(@NotNull DefaultMutableTreeNode parent, @NotNull TranslationNode translationNode) {
@NotNull TranslationNode translationNode, @Nullable String searchQuery) {
for(Map.Entry<String, TranslationNode> entry : translationNode.getChildren().entrySet()) { for(Map.Entry<String, TranslationNode> entry : translationNode.getChildren().entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
TranslationNode childTranslationNode = entry.getValue(); TranslationNode childTranslationNode = entry.getValue();
if(searchQuery != null) {
searchQuery = searchQuery.toLowerCase();
if(!this.isApplicable(key, childTranslationNode, searchQuery)) {
continue;
}
}
if(!childTranslationNode.isLeaf()) { if(!childTranslationNode.isLeaf()) {
// Nested node - run recursively // Nested node - run recursively
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(key); DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(key);
this.generateNodes(childNode, childTranslationNode, searchQuery); this.generateNodes(childNode, childTranslationNode);
parent.add(childNode); parent.add(childNode);
} else { } else {
String previewLocale = this.state.getPreviewLocale(); String previewLocale = this.state.getPreviewLocale();
@ -80,36 +100,6 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
} }
} }
/**
* Checks if the provided tree (@node) is applicable for the search string.
* A full-text-search is applied and section keys and every value will be evaluated.
* @param key Section key
* @param node Node which has @key as key
* @param searchQuery Search query to search for
* @return True if this node or ANY child is relevant for the search context
*/
private boolean isApplicable(@NotNull String key, @NotNull TranslationNode node, @NotNull String searchQuery) {
if(key.toLowerCase().contains(searchQuery)) {
return true;
}
if(!node.isLeaf()) {
for(Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
if(this.isApplicable(entry.getKey(), entry.getValue(), searchQuery)) {
return true;
}
}
} else {
for(String content : node.getValue().values()) {
if(content.toLowerCase().contains(searchQuery)) {
return true;
}
}
}
return false;
}
public @NotNull TreePath findTreePath(@NotNull String fullPath) { public @NotNull TreePath findTreePath(@NotNull String fullPath) {
List<String> sections = new PathUtil(this.state.isNestedKeys()).split(fullPath); List<String> sections = new PathUtil(this.state.isNestedKeys()).split(fullPath);
List<Object> nodes = new ArrayList<>(); List<Object> nodes = new ArrayList<>();