帮我写一个idea插件,可以把鼠标选中的代码块中的两个数组String[] keyArr={"key1","kye2"};Objject[] valueArr={"value1","value2"};转为map的Map写法Map<String,Object> kvMap = new HashMap<>(); kvMap.put("key1","value1"); kvMap.put("key2","value2");
时间: 2023-07-05 08:27:38 浏览: 141
ASP多条件查询功能实现代码(多关键词查询)
好的,下面是一个简单的Idea插件,可以将鼠标选中的代码块中的两个数组转换为Map写法:
```
package com.example.mapconverter;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.DocumentUtil;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class MapConverterAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
Project project = e.getProject();
if (editor == null || psiFile == null || project == null) {
return;
}
String selectedText = editor.getSelectionModel().getSelectedText();
if (selectedText == null) {
return;
}
PsiElement element = psiFile.findElementAt(editor.getSelectionModel().getSelectionStart());
if (element == null) {
return;
}
PsiElement parent = PsiTreeUtil.findFirstParent(element, true, el -> PsiUtilCore.getElementType(el) == PsiUtilCore.getElementType(element));
if (parent == null) {
return;
}
String[] keyArr = null;
Object[] valueArr = null;
if (PsiUtilCore.getElementType(parent) == PsiUtilCore.getElementType(element)) {
String[] lines = selectedText.split("\n");
keyArr = new String[lines.length];
valueArr = new Object[lines.length];
for (int i = 0; i < lines.length; i++) {
String[] split = lines[i].split("=");
if (split.length == 2) {
keyArr[i] = split[0].trim();
valueArr[i] = split[1].trim().substring(1, split[1].length() - 2);
}
}
}
if (keyArr == null || valueArr == null || keyArr.length == 0 || valueArr.length == 0) {
return;
}
Map<String, Object> kvMap = new HashMap<>();
for (int i = 0; i < keyArr.length; i++) {
kvMap.put(keyArr[i], valueArr[i]);
}
DocumentUtil.writeInRunUndoTransparentAction(() -> {
editor.getDocument().replaceString(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd(), mapToString(kvMap));
});
}
private String mapToString(Map<String, Object> map) {
StringBuilder sb = new StringBuilder();
sb.append("Map<String, Object> kvMap = new HashMap<>();\n");
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(String.format("kvMap.put(\"%s\", %s);\n", entry.getKey(), entry.getValue()));
}
return sb.toString();
}
}
```
这个插件会获取当前选中的文本,并将其转换为两个数组。然后,它会将这两个数组转换为一个Map,并将其写入到当前编辑器中。在转换完成后,编辑器中的代码将被替换为新的Map代码。
安装插件后,在Idea中使用快捷键Ctrl + Shift + A,然后输入“Map Converter”即可找到这个插件。选中代码块后,按下快捷键即可将其转换为Map。
阅读全文