Finalize i18n key completion feature
This commit is contained in:
parent
f75848e6ad
commit
62a809b514
@ -5,6 +5,10 @@ import com.intellij.codeInsight.completion.CompletionType;
|
|||||||
import com.intellij.patterns.*;
|
import com.intellij.patterns.*;
|
||||||
import com.intellij.psi.PsiLiteralValue;
|
import com.intellij.psi.PsiLiteralValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show i18n key completion for literal values.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
public class I18nCompletionContributor extends CompletionContributor {
|
public class I18nCompletionContributor extends CompletionContributor {
|
||||||
|
|
||||||
public I18nCompletionContributor() {
|
public I18nCompletionContributor() {
|
||||||
|
@ -8,36 +8,59 @@ import com.intellij.ide.DataManager;
|
|||||||
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.util.ProcessingContext;
|
import com.intellij.util.ProcessingContext;
|
||||||
|
|
||||||
import de.marhali.easyi18n.model.LocalizedNode;
|
import de.marhali.easyi18n.model.LocalizedNode;
|
||||||
import de.marhali.easyi18n.service.DataStore;
|
import de.marhali.easyi18n.service.DataStore;
|
||||||
|
import de.marhali.easyi18n.service.SettingsService;
|
||||||
|
import de.marhali.easyi18n.util.TranslationsUtil;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* I18n translation key completion provider.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
public class I18nCompletionProvider extends CompletionProvider<CompletionParameters> {
|
public class I18nCompletionProvider extends CompletionProvider<CompletionParameters> {
|
||||||
|
|
||||||
Project project;
|
private Project project;
|
||||||
|
private String previewLocale;
|
||||||
|
|
||||||
public I18nCompletionProvider() {
|
public I18nCompletionProvider() {
|
||||||
DataManager.getInstance().getDataContextFromFocusAsync().onSuccess(data -> {
|
DataManager.getInstance().getDataContextFromFocusAsync().onSuccess(data -> {
|
||||||
project = PlatformDataKeys.PROJECT.getData(data);
|
project = PlatformDataKeys.PROJECT.getData(data);
|
||||||
|
previewLocale = SettingsService.getInstance(project).getState().getPreviewLocale();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
|
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
|
||||||
String query = result.getPrefixMatcher().getPrefix();
|
String query = result.getPrefixMatcher().getPrefix();
|
||||||
|
List<String> sections = TranslationsUtil.getSections(query);
|
||||||
|
String lastSection = null;
|
||||||
|
|
||||||
LocalizedNode node = query.contains(".") ?
|
if(!sections.isEmpty() && !query.endsWith(".")) {
|
||||||
DataStore.getInstance(project).getTranslations().getNode(query)
|
lastSection = sections.remove(sections.size() - 1);
|
||||||
: DataStore.getInstance(project).getTranslations().getNodes();
|
|
||||||
|
|
||||||
if(node == null) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String path = TranslationsUtil.sectionsToFullPath(sections);
|
||||||
|
|
||||||
|
LocalizedNode node = sections.isEmpty() ? DataStore.getInstance(project).getTranslations().getNodes()
|
||||||
|
: DataStore.getInstance(project).getTranslations().getNode(path);
|
||||||
|
|
||||||
for(LocalizedNode children : node.getChildren()) {
|
for(LocalizedNode children : node.getChildren()) {
|
||||||
result.addElement(LookupElementBuilder.create(children.getKey()).appendTailText("I18n", true));
|
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() ? "" : ".");
|
||||||
|
|
||||||
System.out.println(result.getPrefixMatcher().getPrefix()); // Debug
|
result.addElement(LookupElementBuilder.create(fullKey).appendTailText(getTailText(children), true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getTailText(LocalizedNode node) {
|
||||||
|
return !node.isLeaf() ? " I18n([])"
|
||||||
|
: " I18n(" + previewLocale + ": " + node.getValue().get(previewLocale) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user