Skip to content

Commit

Permalink
add sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
VISTALL committed Oct 12, 2024
1 parent 92e5d07 commit ba55df3
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesFileType;
import com.intellij.lang.properties.psi.PropertiesFile;
import consulo.devkit.localize.LocalizeYamlUtil;
import consulo.language.editor.CommonDataKeys;
import consulo.language.psi.PsiFile;
import consulo.ui.annotation.RequiredUIAccess;
Expand Down Expand Up @@ -47,14 +48,7 @@ public void actionPerformed(@Nonnull AnActionEvent event) {
messages.put(property.getUnescapedKey(), messageInfo);
}

DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setPrettyFlow(true);
dumperOptions.setAllowUnicode(true);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setWidth(1024);
dumperOptions.setMaxSimpleKeyLength(1024);
Yaml yaml = new Yaml(dumperOptions);
Yaml yaml = LocalizeYamlUtil.create();

File ioFile = VirtualFileUtil.virtualToIoFile(file.getVirtualFile());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package consulo.devkit.localize;

import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.component.ExtensionImpl;
import consulo.application.WriteAction;
import consulo.application.progress.Task;
import consulo.document.Document;
import consulo.fileEditor.EditorNotificationBuilder;
import consulo.fileEditor.EditorNotificationProvider;
import consulo.fileEditor.FileEditor;
import consulo.language.psi.PsiDocumentManager;
import consulo.language.psi.PsiFile;
import consulo.language.psi.PsiManager;
import consulo.localize.LocalizeValue;
import consulo.project.Project;
import consulo.ui.Alerts;
import consulo.ui.UIAccess;
import consulo.undoRedo.CommandProcessor;
import consulo.util.io.CharSequenceReader;
import consulo.virtualFileSystem.VirtualFile;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.inject.Inject;
import org.jetbrains.yaml.psi.YAMLFile;
import org.yaml.snakeyaml.Yaml;

import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

/**
* @author VISTALL
* @since 2024-10-12
*/
@ExtensionImpl
public class LocalizeEditorNotificationProvider implements EditorNotificationProvider {
private final Project myProject;
private final PsiDocumentManager myDocumentManager;
private final PsiManager myPsiManager;
private final CommandProcessor myCommandProcessor;

@Inject
public LocalizeEditorNotificationProvider(Project project,
PsiDocumentManager documentManager,
PsiManager psiManager,
CommandProcessor commandProcessor) {
myProject = project;
myDocumentManager = documentManager;
myPsiManager = psiManager;
myCommandProcessor = commandProcessor;
}

@Nonnull
@Override
public String getId() {
return "localize-editor-notification";
}

@RequiredReadAction
@Nullable
@Override
public EditorNotificationBuilder buildNotification(@Nonnull VirtualFile virtualFile,
@Nonnull FileEditor fileEditor,
@Nonnull Supplier<EditorNotificationBuilder> supplier) {
Locale locale = LocalizeUtil.extractLocaleFromFile(virtualFile);
if (locale == null) {
return null;
}

EditorNotificationBuilder builder = supplier.get();
builder.withText(LocalizeValue.localizeTODO("Locale: " + locale.getDisplayName()));

builder.withAction(LocalizeValue.localizeTODO("Sort"), e -> {
PsiFile file = myPsiManager.findFile(virtualFile);
if (!(file instanceof YAMLFile yamlFile)) {
return;
}

Document document = myDocumentManager.getDocument(yamlFile);
if (document == null) {
return;
}

// since we use document text - there no sense, but all data must be in file before rewrite
myDocumentManager.commitDocument(document);

UIAccess uiAccess = UIAccess.current();

Task.Backgroundable.queue(myProject, "Reordering...", indicator -> reorder(document, uiAccess));
});

return builder;
}

@SuppressWarnings("unchecked")
private void reorder(Document document, UIAccess uiAccess) {
String newText;
try {
Yaml yaml = LocalizeYamlUtil.create();

Map<String, Object> data;
try (CharSequenceReader reader = new CharSequenceReader(document.getImmutableCharSequence())) {
data = yaml.loadAs(reader, Map.class);
}

TreeMap<String, Object> sortedData = new TreeMap<>(data);

newText = yaml.dump(sortedData);
}
catch (Exception error) {
uiAccess.give(() -> Alerts.okError(LocalizeValue.of(error.getLocalizedMessage())).showAsync(myProject));
return;
}

WriteAction.runAndWait(() -> {
myCommandProcessor.executeCommand(myProject, () -> {
document.setText(newText);
}, "Sorting Keys", null);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean canFindUsages(@Nonnull PsiElement element) {
if (element instanceof YAMLKeyValue yamlKeyValue) {
YAMLFile yamlFile = ObjectUtil.tryCast(yamlKeyValue.getContainingFile(), YAMLFile.class);
if (yamlFile != null) {
return LocalizeUtil.isLocalizeFile(yamlFile.getVirtualFile());
return LocalizeUtil.isDefaultLocalizeFile(yamlFile.getVirtualFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class LocalizeFindUsagesProvider implements FindUsagesProvider {
@Override
public boolean canFindUsagesFor(@Nonnull PsiElement psiElement) {
if (psiElement instanceof YAMLKeyValue keyValue && LocalizeUtil.isLocalizeFile(psiElement.getContainingFile().getVirtualFile())) {
if (psiElement instanceof YAMLKeyValue keyValue && LocalizeUtil.isDefaultLocalizeFile(psiElement.getContainingFile().getVirtualFile())) {
return true;
}
return false;
Expand Down
41 changes: 37 additions & 4 deletions plugin/src/main/java/consulo/devkit/localize/LocalizeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.intellij.java.language.psi.PsiMethod;
import com.intellij.java.language.psi.PsiNameHelper;
import consulo.annotation.access.RequiredReadAction;
import consulo.localize.LocalizeManager;
import consulo.project.Project;
import consulo.util.lang.StringUtil;
import consulo.virtualFileSystem.VirtualFile;
import jakarta.annotation.Nullable;
import org.jetbrains.yaml.YAMLFileType;
import org.jetbrains.yaml.psi.YAMLKeyValue;

import java.util.Locale;
Expand All @@ -18,6 +20,9 @@
* @since 2024-09-08
*/
public class LocalizeUtil {
private static final String DEFAULT_LOCALE = "en_US";
private static final String LOCALIZE_LIB_DIR = "LOCALIZE-LIB";

private static final String ZERO_PREFIX = "zero";
private static final String ONE_PREFIX = "one";
private static final String TWO_PREFIX = "two";
Expand Down Expand Up @@ -45,18 +50,46 @@ public static PsiMethod findMethodByYAMLKey(YAMLKeyValue element) {
return null;
}

public static boolean isLocalizeFile(@Nullable VirtualFile file) {
if (file == null) {
@Nullable
public static Locale extractLocaleFromFile(@Nullable VirtualFile file) {
if (file == null || file.getFileType() != YAMLFileType.YML) {
return null;
}

CharSequence nameSequence = file.getNameSequence();

if (StringUtil.endsWith(nameSequence, "Localize.yaml")) {
VirtualFile parentDir = file.getParent();
if (parentDir != null) {
Locale locale = null;
try {
locale = LocalizeManager.get().parseLocale(parentDir.getName());
}
catch (Exception e) {
return null;
}

VirtualFile localizeLibParent = parentDir.getParent();
if (localizeLibParent != null && LOCALIZE_LIB_DIR.equals(localizeLibParent.getName())) {
return locale;
}
}
}
return null;
}

public static boolean isDefaultLocalizeFile(@Nullable VirtualFile file) {
if (file == null || file.getFileType() != YAMLFileType.YML) {
return false;
}

CharSequence nameSequence = file.getNameSequence();

if (StringUtil.endsWith(nameSequence, "Localize.yaml")) {
VirtualFile parentDir = file.getParent();
if (parentDir != null && "en_US".equals(parentDir.getName())) {
if (parentDir != null && DEFAULT_LOCALE.equals(parentDir.getName())) {
VirtualFile localizeLibParent = parentDir.getParent();
if (localizeLibParent != null && "LOCALIZE-LIB".equals(localizeLibParent.getName())) {
if (localizeLibParent != null && LOCALIZE_LIB_DIR.equals(localizeLibParent.getName())) {
return true;
}
}
Expand Down
21 changes: 21 additions & 0 deletions plugin/src/main/java/consulo/devkit/localize/LocalizeYamlUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package consulo.devkit.localize;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

/**
* @author VISTALL
* @since 2024-10-12
*/
public class LocalizeYamlUtil {
public static Yaml create() {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setPrettyFlow(true);
dumperOptions.setAllowUnicode(true);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setWidth(1024);
dumperOptions.setMaxSimpleKeyLength(1024);
return new Yaml(dumperOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DataIndexer<String, Void, FileContent> getIndexer() {
return fileContent -> {
VirtualFile file = fileContent.getFile();

if (LocalizeUtil.isLocalizeFile(file)) {
if (LocalizeUtil.isDefaultLocalizeFile(file)) {
String fileName = file.getNameWithoutExtension();
String packageName = StringUtil.getPackageName(fileName);
String id = packageName + ".localize." + StringUtil.getShortName(fileName);
Expand Down Expand Up @@ -67,7 +67,7 @@ public int getVersion() {
@Override
public FileBasedIndex.InputFilter getInputFilter() {
return (project, file) -> {
return file.getFileType() == YAMLFileType.YML && LocalizeUtil.isLocalizeFile(file);
return file.getFileType() == YAMLFileType.YML && LocalizeUtil.isDefaultLocalizeFile(file);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public PsiElementVisitor buildVisitor(@Nonnull ProblemsHolder holder, boolean is
return PsiElementVisitor.EMPTY_VISITOR;
}

if (!LocalizeUtil.isLocalizeFile(file.getVirtualFile())) {
if (!LocalizeUtil.isDefaultLocalizeFile(file.getVirtualFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}

Expand Down

0 comments on commit ba55df3

Please sign in to comment.