From c9139f2cf89122696d52debefbd1457ecd5f88f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Ha=C3=9Flinger?= Date: Tue, 21 Sep 2021 14:57:34 +0200 Subject: [PATCH] optimize key shrinking and suggestion --- .../editor/KeyCompletionProvider.java | 80 +++++++++++-------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/src/main/java/de/marhali/easyi18n/editor/KeyCompletionProvider.java b/src/main/java/de/marhali/easyi18n/editor/KeyCompletionProvider.java index 52570bc..a3a10b3 100644 --- a/src/main/java/de/marhali/easyi18n/editor/KeyCompletionProvider.java +++ b/src/main/java/de/marhali/easyi18n/editor/KeyCompletionProvider.java @@ -2,10 +2,12 @@ package de.marhali.easyi18n.editor; import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.lookup.*; +import com.intellij.icons.AllIcons; import com.intellij.openapi.project.*; import com.intellij.util.*; import de.marhali.easyi18n.model.*; import de.marhali.easyi18n.service.*; +import de.marhali.easyi18n.util.TranslationsUtil; import org.jetbrains.annotations.*; import java.util.*; @@ -27,46 +29,58 @@ public class KeyCompletionProvider extends CompletionProvider map = new HashMap<>(); - collect(map, instance.getTranslations().getNodes(), null, previewLocale, pathPrefix); - Map containedPath = new HashMap<>(); - StringBuilder prefixedKey = new StringBuilder(); - int maxPrefixLookUpLength = 5; - while (containedPath.isEmpty() && maxPrefixLookUpLength-- > 0) { - for (Map.Entry e : map.entrySet()) { - if (e.getKey().startsWith(path)) { - containedPath.put(e.getKey(), e.getValue()); + if(pathPrefix == null) { + pathPrefix = ""; + } + + if(path.startsWith(pathPrefix)) { + path = path.substring(pathPrefix.length()); + + if(path.startsWith(".")) { // Remove leading dot + path = path.substring(1); + } + + } else { + path = ""; // Show suggestions for root view + } + + if(pathPrefix.length() > 0 && !pathPrefix.endsWith(".")) { + pathPrefix += "."; + } + + List fullKeys = store.getTranslations().getFullKeys(); + + int sections = path.split("\\.").length; + int maxSectionForwardLookup = 5; + + for(String key : fullKeys) { + // Path matches + if(key.startsWith(path)) { + String[] keySections = key.split("\\."); + + if(keySections.length > sections + maxSectionForwardLookup) { // Key is too deep nested + String shrinkKey = TranslationsUtil.sectionsToFullPath(Arrays.asList( + Arrays.copyOf(keySections, sections + maxSectionForwardLookup))); + + result.addElement(LookupElementBuilder.create(pathPrefix + shrinkKey) + .appendTailText(" I18n([])", true)); + + } else { + LocalizedNode node = store.getTranslations().getNode(key); + String translation = node != null ? node.getValue().get(previewLocale) : null; + + result.addElement(LookupElementBuilder.create(pathPrefix + key) + .withIcon(AllIcons.Actions.PreserveCaseHover) + .appendTailText(" I18n(" + previewLocale + ": " + translation + ")", true) + ); } } - if (path.isEmpty()) break; - if (containedPath.isEmpty()) { - prefixedKey.append(path.charAt(0)); - } - path = path.substring(1); - } - containedPath.forEach((key, value) -> { - result.addElement(LookupElementBuilder.create(prefixedKey + key).appendTailText(" I18n("+previewLocale+": "+value+")", true)); - }); - } - - private void collect(Map map, LocalizedNode node, String path, String locale, String pathPrefix) { - if (node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) { - String value = node.getValue().get(locale); - map.put(path, value); - if (pathPrefix != null && !pathPrefix.isEmpty()) { - map.put(pathPrefix + "." + path, value); - } - } else { - for (LocalizedNode child : node.getChildren()) { - collect(map, child, path == null || path.isEmpty() ? child.getKey() : path + "." + child.getKey(), locale, pathPrefix); - } } } - }