Better prefix support

This commit is contained in:
sunarya-thito 2021-09-14 18:53:50 +07:00
parent 7b8d50f5fa
commit d61cd57979
2 changed files with 40 additions and 55 deletions

View File

@ -1,20 +1,14 @@
package de.marhali.easyi18n.editor; package de.marhali.easyi18n.editor;
import com.intellij.codeInsight.completion.CompletionParameters; import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.completion.CompletionProvider; import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.openapi.project.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.util.*;
import com.intellij.openapi.project.Project; import de.marhali.easyi18n.model.*;
import com.intellij.util.ProcessingContext; import de.marhali.easyi18n.service.*;
import org.jetbrains.annotations.*;
import de.marhali.easyi18n.model.LocalizedNode; import java.util.*;
import de.marhali.easyi18n.service.DataStore;
import de.marhali.easyi18n.service.SettingsService;
import de.marhali.easyi18n.util.TranslationsUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* I18n translation key completion provider. * I18n translation key completion provider.
@ -36,56 +30,46 @@ public class KeyCompletionProvider extends CompletionProvider<CompletionParamete
String previewLocale = SettingsService.getInstance(project).getState().getPreviewLocale(); String previewLocale = SettingsService.getInstance(project).getState().getPreviewLocale();
String prefix = SettingsService.getInstance(project).getState().getPrefix(); String prefix = SettingsService.getInstance(project).getState().getPrefix();
String query = result.getPrefixMatcher().getPrefix(); String path = result.getPrefixMatcher().getPrefix();
List<String> sections = TranslationsUtil.getSections(query);
String path = TranslationsUtil.sectionsToFullPath(sections); if (path.endsWith(".")) {
if (prefix != null && path.startsWith(prefix)) { path = path.substring(0, path.length() - 1);
path = path.substring(prefix.length()); }
if (path.startsWith(".")) {
path = path.substring(1); DataStore instance = DataStore.getInstance(project);
Map<String, String> map = new HashMap<>();
collect(map, instance.getTranslations().getNodes(), null, previewLocale, prefix);
Map<String, String> containedPath = new HashMap<>();
StringBuilder prefixedKey = new StringBuilder();
while (containedPath.isEmpty()) {
for (Map.Entry<String, String> e : map.entrySet()) {
if (e.getKey().startsWith(path)) {
containedPath.put(e.getKey(), e.getValue());
}
} }
if (path.isEmpty()) break;
if (containedPath.isEmpty()) {
prefixedKey.append(path.charAt(0));
}
path = path.substring(1);
} }
containedPath.forEach((key, value) -> {
LocalizedNode node = sections.isEmpty() ? DataStore.getInstance(project).getTranslations().getNodes() result.addElement(LookupElementBuilder.create(prefixedKey + key).appendTailText(" I18n("+previewLocale+": "+value+")", true));
: DataStore.getInstance(project).getTranslations().getNode(path); });
if(node == null) { // Unknown translation
return;
}
append(node, previewLocale, path, prefix, result);
// for(LocalizedNode children : node.getChildren()) {
// if(lastSection == null || children.getKey().startsWith(lastSection)) {
// // Construct full key path / Fore nested objects add '.' to indicate deeper level
// String fullKey = (path.isEmpty() ? children.getKey() : path + "." + children.getKey()) + (children.isLeaf() ? "" : ".");
//
// if (prefix != null && !fullKey.isEmpty()) {
// fullKey = prefix + "." + fullKey;
// }
//
// result.addElement(LookupElementBuilder.create(fullKey)
// .appendTailText(getTailText(children, previewLocale), true));
// }
// }
} }
private void append(LocalizedNode node, String locale, String path, String prefix, CompletionResultSet result) { private void collect(Map<String, String> map, LocalizedNode node, String path, String locale, String prefix) {
if (node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) { if (node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) {
String value = node.getValue().get(locale);
map.put(path, value);
if (prefix != null) { if (prefix != null) {
path = prefix + "." + path; map.put(prefix + "." + path, value);
} }
result.addElement(LookupElementBuilder.create(path)
.appendTailText(getTailText(node, locale), true));
} else { } else {
for (LocalizedNode children : node.getChildren()) { for (LocalizedNode child : node.getChildren()) {
append(children, locale, path == null || path.isEmpty() ? children.getKey() : path + "." + children.getKey(), prefix, result); collect(map, child, path == null || path.isEmpty() ? child.getKey() : path + "." + child.getKey(), locale, prefix);
} }
} }
} }
private String getTailText(LocalizedNode node, String previewLocale) {
return !node.isLeaf() ? " I18n([])"
: " I18n(" + previewLocale + ": " + node.getValue().get(previewLocale) + ")";
}
} }

View File

@ -13,7 +13,8 @@ import de.marhali.easyi18n.editor.KeyCompletionProvider;
public class GenericKeyCompletionContributor extends CompletionContributor { public class GenericKeyCompletionContributor extends CompletionContributor {
public GenericKeyCompletionContributor() { public GenericKeyCompletionContributor() {
extend(CompletionType.BASIC, PlatformPatterns.psiElement().inside(PsiLiteralValue.class), // extend(CompletionType.BASIC, PlatformPatterns.psiElement().inside(PsiLiteralValue.class),
new KeyCompletionProvider()); // new KeyCompletionProvider());
extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new KeyCompletionProvider());
} }
} }