diff --git a/CodingEasier.jar b/CodingEasier.jar index a7640859720a8e482da5abe0e45e3bed8b6fe0b9..94d30679fc484826a201df7e402b8c4ba057fa49 100644 Binary files a/CodingEasier.jar and b/CodingEasier.jar differ diff --git a/README.md b/README.md index 214b7f376edff960b1db838145d1ac1690bbb098..ef4205b8d430e1c605362238897d582d28a6283a 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,31 @@ # CodingEasier -#### 介绍 -一个提升开发效率的idea插件,初衷就是将一些常用的功能写入到这个插件,提高编码效率。但是因为这方面的资料过少加上需要工作的原因, -所以新功能更新可能会相对缓慢,但是一定会慢慢加入更多的功能,我会在代码中加入注释,尽量让代码简单易懂,也算是为了尽量让它成为让一个更多人 -能自己编写插件的参考资料吧。当然也欢迎提交反馈,有想要实现的功能也可以建议,要是建议确实不错且时间允许,我就会尝试添加。 +## 介绍 +一个提升开发效率的idea插件,初衷就是将一些常用的功能写入到这个插件,减少开发过程中一些可以避免且不必要的操作,以提高编码效率。 +但是因为这方面的资料有点少,所以新功能更新可能会相对缓慢,但是一定会慢慢加入更多的功能,我会在代码中加入注释,尽量让代码简单易懂, +也算是为了尽量让它成为让一个更多人能自己编写插件的参考资料吧。 -## 分支说明 -develop分支为开发分支,有些功能可能是正在开发中,不完善的,master分支是相对稳定的代码。 +## 反馈说明 +使用过程中有问题、建议或者想要加的功能,欢迎提交issues(推荐),或者邮件到chave_z@163.com(邮箱不常看,回复可能随缘),要是新功能或者建议确实不错且时间允许,我就会尝试加入。 + +注: +反馈问题请提供操作系统、编译器版本、错误截图、如何操作导致的、还有对应的数据,以便我更好的查找错误。 + +## 更新历史 +### v1.1 +1. 修复在翻译后弹出的界面上选中文字,直接翻译报空指针的问题。 +2. 之前翻译替换和字符传格式替换增加了按键操作,不需要翻译以后,切换鼠标替换了。(强烈建议在用这个插件的朋友更新体验🤣) +3. 减小插件体积 +### 2019-09-13 +插件已经通过审核,可以在线安装了,安装步骤请查看安装教程中的在线安装部分 +### 2019-09-11 +修改翻译结果的显示界面,优化长文本翻译的功能 #### 当前功能 - 谷歌中英互译(根据选中的内容自动辨别)`alt shift Q` - 翻译及便捷替换(中英互译,多词备选,方便快捷) `alt shift A` - 快捷的创建一个常量 `alt shift Z` +- json格式化,支付json与json数组 - 字符串相关操作 为了方便见名识意,这里命名基本都按照转化后的格式命名 - all case 显示所有支持的格式 `alt shift W` @@ -26,14 +40,22 @@ develop分支为开发分支,有些功能可能是正在开发中,不完善 - to LowerCase 转化为全小写形式 #### 说明 -已经测试了2018.1-2019.2的idea,都没有发现兼容性相关的问题,字符串部分的转化部分暂时就考虑了一些正常命名和我见过的变量格式, +已经测试了windows系统下2017.3-2019.2的idea,以及mac、ubuntu上的最新版idea2019.2,都没有发现兼容性相关的问题,字符串部分的转化部分暂时就考虑了一些正常命名和我见过的变量格式, 一些奇怪的命名很可能没有涉及到,使用过程中要是有问题,欢迎提交对应的字符串、遇见的问题及idea版本进行反馈。 #### 安装教程 -1. 本地安装 -`File-->Settings-->Plugins--> Install Plugin From Disk... -->选择CodingEasier.jar-->确定` -2. 在线安装(等初版差不多了,有时间就去提交一下) +##### 本地安装 +- windows、linux下: +`File --> Settings --> Plugins --> Install Plugin From Disk... --> 选择CodingEasier.jar --> 确定` +- mac下: +`IntelliJ IDEA --> Preferences --> Settings --> Plugins --> Install Plugin From Disk... --> 选择CodingEasier.jar --> 确定` +##### 在线安装(因为是新插件,可能比较靠后,往下翻翻就行) +- windows、linux下: +`File --> Settings --> Plugins --> 在Marketplace中输入 Coding Easier然后搜索,点击安装` +- mac下: +`IntelliJ IDEA --> Preferences --> Settings --> Plugins --> 在Marketplace中输入 Coding Easier然后搜索,点击安装` + 安装完毕以后重启即可 @@ -41,9 +63,11 @@ develop分支为开发分支,有些功能可能是正在开发中,不完善 本插件暂时大部分还都是字符相关的处理,所以在没选择字符串的情况下,插件默认是置灰的,选中要处理的字符串,然后再右键就能正常使用了。 为了节省动图大小,字符串都预先输入了,没有看到清楚的点击步骤的均为快捷键操作。 - 中英互译与替换(一键操作,多词候选,再也不用担心词汇量不足,每次都得临时翻译的尴尬局面了) -![translate](/images/translate.gif) +![translate](https://cdn.jsdelivr.net/gh/Chave-Z/picture@master/data/translate.gif) - 字符选择 -![switchCase](/images/switchCase.gif) +![switchCase](https://cdn.jsdelivr.net/gh/Chave-Z/picture@master/data/switchCase.gif) - 创建常量 -![createConstant](/images/createConstant.gif) +![createConstant](https://cdn.jsdelivr.net/gh/Chave-Z/picture@master/data/createConstant.gif) +- json格式化 +![json](https://cdn.jsdelivr.net/gh/Chave-Z/picture@master/data/json.gif) diff --git a/images/createConstant.gif b/images/createConstant.gif deleted file mode 100644 index 6eb70f451a0414fbe64ae99c13c556073f0814ba..0000000000000000000000000000000000000000 Binary files a/images/createConstant.gif and /dev/null differ diff --git a/images/switchCase.gif b/images/switchCase.gif deleted file mode 100644 index e6320e9347a302c8e2e0f959f4be583e37e45abc..0000000000000000000000000000000000000000 Binary files a/images/switchCase.gif and /dev/null differ diff --git a/images/translate.gif b/images/translate.gif deleted file mode 100644 index 5280078f4ecfca365f47f068cd102a5d891c5d39..0000000000000000000000000000000000000000 Binary files a/images/translate.gif and /dev/null differ diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index dd5de218aad0a1be9154a88462992f5e10d63591..64b3ccc06ef01398cd703be324762e80199e27a4 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -1,126 +1,165 @@ - - com.cheng.plugin.coding.easier - Coding Easier - 0.0.1 - D丶Cheng + + com.cheng.plugin.coding.easier + Coding Easier + 1.1 + Cheng - >Coding Easier +

+ + Gitee | + Issues | + Doc + +

+

+

Features: +

    +
  • Google translate +
      +
    • Google translate.
    • +
    • Google translate and replace words.
    • +
    +
  • +
  • json format.
  • +
  • Create constant.
  • +
  • Switch case +
      +
    • to CONSTANT.
    • +
    • to camelCase.
    • +
    • to underscore_case.
    • +
    • to PascalCase.
    • +
    • to Kebab-case.
    • +
    • to kebab-case.
    • +
    • to words.
    • +
    • to UpperCase.
    • +
    • to LowerCase.
    • +
    +
  • +
+

+

Google translate


translate.gif

+

json format


json.gif

+

Create constant


createConstant.gif

+

Switch case


switchCase.gif

]]>
- - 2. String utils
- ]]> -
- +
  • 优化长文本翻译的功能,优化界面组件,兼容2016.2
  • ]]>
    + - - + + - - - com.intellij.modules.lang + + + com.intellij.modules.lang - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    \ No newline at end of file diff --git a/src/com/coding/easier/enums/FormatEnum.java b/src/com/coding/easier/enums/FormatEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..3caa27b6d14cc5dc6ef277ed599e02d2fe05ef96 --- /dev/null +++ b/src/com/coding/easier/enums/FormatEnum.java @@ -0,0 +1,33 @@ +package com.coding.easier.enums; + +/** + * @author D丶Cheng + * @description: 格式化类型枚举 + * @create: 2019-09-06 16:10 + */ +public enum FormatEnum { + + /** + * json + */ + JSON("json"), + + /** + * xml + */ + XML("xml"); + + FormatEnum(String value) { + this.value = value; + } + + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/com/coding/easier/format/FormatAction.java b/src/com/coding/easier/format/FormatAction.java new file mode 100644 index 0000000000000000000000000000000000000000..41ff61eec00181e2ed7ad19502f95a5a99d5ec6e --- /dev/null +++ b/src/com/coding/easier/format/FormatAction.java @@ -0,0 +1,36 @@ +package com.coding.easier.format; + +import com.coding.easier.ui.FormatDialog; +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 java.util.regex.Pattern; + +import static java.util.regex.Pattern.compile; + +/** + * @author: D丶Cheng + * @description: + * @create: 2019-08-29 15:46 + **/ +public class FormatAction extends AnAction { + + public static final Pattern p = compile("[\u4e00-\u9fa5]"); + + @Override + public void actionPerformed(AnActionEvent e) { +// final Project project = e.getData(CommonDataKeys.PROJECT); + final Editor editor = e.getData(CommonDataKeys.EDITOR); + showDialog(editor); + } + + private void showDialog(Editor editor) { + final FormatDialog dialog = new FormatDialog(this, editor); + dialog.setSize(880, 515); + dialog.setTitle("CodingEasier Format"); + dialog.setLocationRelativeTo(null); + dialog.setVisible(true); + } +} diff --git a/src/com/coding/easier/string/AbstractStringAction.java b/src/com/coding/easier/string/AbstractStringAction.java index a19e157a131585cf32ce9273d084ca0fb9f4eff4..91f4190740758fdb8d4b6d0b597e5d92308dba4c 100644 --- a/src/com/coding/easier/string/AbstractStringAction.java +++ b/src/com/coding/easier/string/AbstractStringAction.java @@ -8,7 +8,7 @@ import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.project.Project; -import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang.StringUtils; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,7 +30,7 @@ public abstract class AbstractStringAction extends AnAction { final Editor editor = e.getData(CommonDataKeys.EDITOR); SelectionModel selectionModel = editor.getSelectionModel(); String selectedText = selectionModel.getSelectedText(); - if (StringUtils.isBlank(selectedText)) { + if (StringUtils.isEmpty(selectedText)) { NoticeUtil.error("请选择要转换的字符"); } Matcher m = p.matcher(selectedText.trim()); diff --git a/src/com/coding/easier/string/AllCaseAction.java b/src/com/coding/easier/string/AllCaseAction.java index 6536e1eab0b793e686d7c960c45bc7b84125d329..b996d3e2685e70bedbeb4396dd314787be975dd4 100644 --- a/src/com/coding/easier/string/AllCaseAction.java +++ b/src/com/coding/easier/string/AllCaseAction.java @@ -10,17 +10,19 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopupFactory; -import org.apache.commons.lang3.StringUtils; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; +import com.intellij.openapi.ui.popup.ListPopup; +import com.intellij.openapi.ui.popup.PopupStep; +import com.intellij.openapi.ui.popup.util.BaseListPopupStep; +import com.intellij.openapi.util.NlsContexts; +import org.apache.commons.lang.StringUtils; +import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.LinkedHashSet; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static com.coding.easier.util.StringUtil.*; +import static com.coding.easier.util.StringUtil.getAllCase; import static java.util.regex.Pattern.compile; /** @@ -31,13 +33,15 @@ public class AllCaseAction extends AnAction { public static final Pattern p = compile("[\u4e00-\u9fa5]"); + public static Editor editor; + public static Project project; + public static SelectionModel selectionModel; + @Override public void actionPerformed(AnActionEvent e) { - final Project project = e.getData(CommonDataKeys.PROJECT); - final Editor editor = e.getData(CommonDataKeys.EDITOR); - SelectionModel selectionModel = editor.getSelectionModel(); + selectionModel = editor.getSelectionModel(); String selectedText = selectionModel.getSelectedText(); - if (StringUtils.isBlank(selectedText)) { + if (StringUtils.isEmpty(selectedText)) { NoticeUtil.error("请选择要转换的字符"); return; } @@ -46,13 +50,13 @@ public class AllCaseAction extends AnAction { NoticeUtil.error("所选内容不能带有中文"); return; } - showPopupBalloon(project, editor, selectedText); + showPopupBalloon(selectedText); } @Override public void update(AnActionEvent e) { - final Project project = e.getData(CommonDataKeys.PROJECT); - final Editor editor = e.getData(CommonDataKeys.EDITOR); + project = e.getData(CommonDataKeys.PROJECT); + editor = e.getData(CommonDataKeys.EDITOR); e.getPresentation().setVisible(project != null && editor != null && editor.getSelectionModel().hasSelection()); } @@ -61,54 +65,49 @@ public class AllCaseAction extends AnAction { /** * 气泡显示 * - * @param project - * @param editor * @param selectedText */ - protected void showPopupBalloon(Project project, Editor editor, String selectedText) { - ApplicationManager.getApplication().invokeLater((Runnable) new Runnable() { - @Override - public void run() { - final JBPopupFactory factory = JBPopupFactory.getInstance(); - LinkedHashSet set = getAllCase(selectedText); - JLabel jLabel = new JLabel("请选择需要的格式 "); -// java.util.List list = new ArrayList(); -// list.addAll(set); -// factory.createPopupChooserBuilder(list).createPopup().show(factory.guessBestPopupLocation(editor)); - JList jList = new JList(set.toArray()); - JPanel panel = new JPanel(new BorderLayout()); - panel.add(jLabel, BorderLayout.NORTH); - panel.add(jList, BorderLayout.CENTER); - factory.createComponentPopupBuilder(panel, jList).createPopup().show(factory.guessBestPopupLocation(editor)); - jList.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - System.out.println(jList.getSelectedValue()); - final SelectionModel selectionModel = editor.getSelectionModel(); - replaceStr(project, editor, selectionModel, jList.getSelectedValue().toString().trim()); - } - } - }); - } + protected void showPopupBalloon(String selectedText) { + ApplicationManager.getApplication().invokeLater(() -> { + final JBPopupFactory factory = JBPopupFactory.getInstance(); + LinkedHashSet set = getAllCase(selectedText); + SelectListStep step = new SelectListStep("", new ArrayList(set)); + step.setDefaultOptionIndex(0); + ListPopup popup = factory.createListPopup(step, 20); + popup.setRequestFocus(true); + popup.show(factory.guessBestPopupLocation(editor)); }); } + class SelectListStep extends BaseListPopupStep { + + public SelectListStep(@Nullable @NlsContexts.PopupTitle String title, List values) { + super(title, values); + } + + @Nullable + @Override + public PopupStep onChosen(Object selectedValue, boolean finalChoice) { + final SelectionModel selectionModel = editor.getSelectionModel(); + replaceStr(selectionModel, selectedValue.toString()); + System.out.println(selectedValue.toString()); + return super.onChosen(selectedValue, finalChoice); + } + + @Override + public boolean isSpeedSearchEnabled() { + return true; + } + } + /** * 替换选中的字符串 * - * @param editor * @param selectionModel - * @param project * @param newText */ - public static void replaceStr(Project project, Editor editor, SelectionModel selectionModel, String newText) { - Runnable runnable = new Runnable() { - @Override - public void run() { - editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), newText); - } - }; + public static void replaceStr(SelectionModel selectionModel, String newText) { + Runnable runnable = () -> editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), newText); WriteCommandAction.runWriteCommandAction(project, runnable); selectionModel.removeSelection(); } diff --git a/src/com/coding/easier/translate/AbstractTranslateAction.java b/src/com/coding/easier/translate/AbstractTranslateAction.java index c16263aee5c25e2ce07a13845ff4720b48c4eba8..30774e0ad9007880e31f53f1bfee9919f81389a0 100644 --- a/src/com/coding/easier/translate/AbstractTranslateAction.java +++ b/src/com/coding/easier/translate/AbstractTranslateAction.java @@ -1,26 +1,25 @@ package com.coding.easier.translate; -import com.coding.easier.util.NoticeUtil; -import com.coding.easier.util.GsonUtil; import com.coding.easier.constant.TranslateConstant; +import com.coding.easier.util.GsonUtil; +import com.coding.easier.util.NoticeUtil; import com.coding.easier.util.StringUtil; -import com.coding.easier.util.TkTools; +import com.coding.easier.util.TkUtil; 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.editor.SelectionModel; import com.intellij.openapi.project.Project; -import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang.StringUtils; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.jetbrains.annotations.NotNull; -import java.io.PrintWriter; -import java.io.StringWriter; import java.net.URLEncoder; import java.util.regex.Pattern; @@ -35,24 +34,30 @@ public abstract class AbstractTranslateAction extends AnAction { public static final Pattern p = compile("[\u4e00-\u9fa5]"); - private final static CharSequence UNDERSCORE = "_"; - public static Editor editor; public static Project project; + public static SelectionModel selectionModel; @Override - public void actionPerformed(AnActionEvent event) { + public void actionPerformed(@NotNull AnActionEvent event) { try { - SelectionModel selectionModel = editor.getSelectionModel(); - String selectedText = selectionModel.getSelectedText(); - if (StringUtils.isBlank(selectedText)) { + String selectedText = ""; + if (editor == null){ + selectedText = TranslateBalloon.origEditorPane.getSelectedText(); + selectedText = StringUtils.isEmpty(selectedText) ? TranslateBalloon.transEditorPane.getSelectedText(): + selectedText; + }else{ + selectionModel = editor.getSelectionModel(); + selectedText = selectionModel.getSelectedText(); + } + if (StringUtils.isEmpty(selectedText)) { NoticeUtil.error("请选择要翻译的字符"); return; } NoticeUtil.init(this.getClass().getSimpleName(), 1); - executeTranslate(event); + doTranslate(StringUtil.textToWords(selectedText)); } catch (Exception e) { - NoticeUtil.error(getStackTrace(e)); + NoticeUtil.error(e); } } @@ -64,19 +69,6 @@ public abstract class AbstractTranslateAction extends AnAction { && editor.getSelectionModel().hasSelection()); } - /** - * 执行翻译操作 - * - * @param event - */ - public void executeTranslate(AnActionEvent event) { - final SelectionModel selectionModel = editor.getSelectionModel(); - String selectText = StringUtil.textToWords(selectionModel.getSelectedText()); - if (null != selectText && !"".equals(selectText.trim())) { - doTranslate(selectText); - } - } - /** * 调用接口,翻译并返回值 * @@ -90,7 +82,7 @@ public abstract class AbstractTranslateAction extends AnAction { CloseableHttpClient client = HttpClients.createDefault(); //replace填坑参数地址值 - String url = getTranslateUrl(translateType, TkTools.tk(word), URLEncoder.encode(word, "utf-8")); + String url = getTranslateUrl(translateType, TkUtil.tk(word), URLEncoder.encode(word, "utf-8")); HttpGet get = new HttpGet(url); get.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"); get.setHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9"); @@ -106,26 +98,6 @@ public abstract class AbstractTranslateAction extends AnAction { return null; } - /** - * 用e.getMessage()来获取异常信息, - * 但是这样获取到的信息内容并不全,而且有时候为空。 - * 我们可以用下面方法来获取 - * - * @param throwable - * @return - */ - public static String getStackTrace(Throwable throwable) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - - try { - throwable.printStackTrace(pw); - return sw.toString(); - } finally { - pw.close(); - } - } - /** * 根据类型选择接口地址 @@ -153,6 +125,7 @@ public abstract class AbstractTranslateAction extends AnAction { * 文本弹出显示 * * @param result + * @param translateType */ protected abstract void showPopupBalloon(GoogleTranslateResult result, String translateType); } diff --git a/src/com/coding/easier/translate/GoogleReplaceAction.java b/src/com/coding/easier/translate/GoogleReplaceAction.java index 6501f537a174de0ec0954d2fc7e6a9dcd12307e7..177a1dbed50acacce600105447d9f955f39870f5 100644 --- a/src/com/coding/easier/translate/GoogleReplaceAction.java +++ b/src/com/coding/easier/translate/GoogleReplaceAction.java @@ -1,7 +1,8 @@ package com.coding.easier.translate; -import com.coding.easier.util.NoticeUtil; import com.coding.easier.constant.TranslateConstant; +import com.coding.easier.ui.Popups; +import com.coding.easier.util.NoticeUtil; import com.coding.easier.util.StringUtil; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; @@ -9,12 +10,23 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopupFactory; +import com.intellij.openapi.ui.popup.ListPopup; +import com.intellij.openapi.ui.popup.PopupStep; +import com.intellij.openapi.ui.popup.util.BaseListPopupStep; +import com.intellij.openapi.util.NlsContexts; +import com.intellij.ui.components.JBList; +import com.intellij.ui.components.JBPanel; +import com.intellij.ui.components.JBScrollPane; +import org.jetbrains.annotations.Nullable; -import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.util.ArrayList; import java.util.LinkedHashSet; +import java.util.List; import java.util.regex.Matcher; /** @@ -47,40 +59,41 @@ public class GoogleReplaceAction extends AbstractTranslateAction { public void run() { final JBPopupFactory factory = JBPopupFactory.getInstance(); LinkedHashSet set = new LinkedHashSet<>(); - String text; + StringBuilder text = new StringBuilder(); + boolean addFlag = true; if (result.getDict() == null) { - text = result.getSentences().get(0).getTrans(); - set.add(text + " "); + if (result.getSentences().size() == 1 || result.getSentences().size() == 2) { + text = new StringBuilder(result.getSentences().get(0).getTrans()); + } else { + addFlag = false; + for (GoogleTranslateResult.SentencesBean sentence : result.getSentences()) { + if (sentence.getTrans() != null) { + text.append(sentence.getTrans()); + } + } + } + set.add(text.toString()); } else { for (GoogleTranslateResult.DictBean dictBean : result.getDict()) { for (GoogleTranslateResult.DictBean.EntryBean entryBean : dictBean.getEntry()) { - text = entryBean.getWord(); - set.add(text + " "); + text = new StringBuilder(entryBean.getWord()); + set.add(text.toString()); } } } - LinkedHashSet candidateWords = new LinkedHashSet<>(); - // 填充结果 - for (String s : set) { - addWord(candidateWords, s.trim(), translateType); - } - set.addAll(candidateWords); - JLabel jLabel = new JLabel("请选择需要的结果 "); - JList jList = new JList(set.toArray()); - JPanel panel = new JPanel(new BorderLayout()); - panel.add(jLabel, BorderLayout.NORTH); - panel.add(jList, BorderLayout.CENTER); - factory.createComponentPopupBuilder(panel, jList).createPopup().show(factory.guessBestPopupLocation(editor)); - jList.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - System.out.println(jList.getSelectedValue()); - final SelectionModel selectionModel = editor.getSelectionModel(); - replaceStr(project, editor, selectionModel, jList.getSelectedValue().toString().trim()); - } + if (addFlag) { + LinkedHashSet candidateWords = new LinkedHashSet<>(); + // 填充结果 + for (String s : set) { + addWord(candidateWords, s.trim(), translateType); } - }); + set.addAll(candidateWords); + } + SelectListStep step = new SelectListStep("", new ArrayList(set)); + step.setDefaultOptionIndex(0); + ListPopup popup = factory.createListPopup(step, 20); + popup.setRequestFocus(true); + popup.show(factory.guessBestPopupLocation(editor)); } }); } @@ -102,19 +115,34 @@ public class GoogleReplaceAction extends AbstractTranslateAction { /** * 替换选中的字符串 * - * @param editor * @param selectionModel - * @param project * @param newText */ - public static void replaceStr(Project project, Editor editor, SelectionModel selectionModel, String newText) { - Runnable runnable = new Runnable() { - @Override - public void run() { - editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), newText); - } - }; + public static void replaceStr(SelectionModel selectionModel, String newText) { + Runnable runnable = () -> editor.getDocument().replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), newText); WriteCommandAction.runWriteCommandAction(project, runnable); selectionModel.removeSelection(); } + + + class SelectListStep extends BaseListPopupStep { + + public SelectListStep(@Nullable @NlsContexts.PopupTitle String title, List values) { + super(title, values); + } + + @Nullable + @Override + public PopupStep onChosen(Object selectedValue, boolean finalChoice) { + final SelectionModel selectionModel = editor.getSelectionModel(); + replaceStr(selectionModel, selectedValue.toString()); + System.out.println(selectedValue.toString()); + return super.onChosen(selectedValue, finalChoice); + } + + @Override + public boolean isSpeedSearchEnabled() { + return true; + } + } } diff --git a/src/com/coding/easier/translate/GoogleTranslateAction.java b/src/com/coding/easier/translate/GoogleTranslateAction.java index 7387cc0b07d17a15196d663a5d88a004fa9d0e5e..d63f187001745121393470d4ebc6ad10f780a9b9 100644 --- a/src/com/coding/easier/translate/GoogleTranslateAction.java +++ b/src/com/coding/easier/translate/GoogleTranslateAction.java @@ -1,14 +1,19 @@ package com.coding.easier.translate; -import com.coding.easier.util.NoticeUtil; import com.coding.easier.constant.TranslateConstant; +import com.coding.easier.ui.modules.ColorService; +import com.coding.easier.util.NoticeUtil; +import com.intellij.notification.impl.NotificationsManagerImpl; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.ui.popup.Balloon; +import com.intellij.openapi.ui.popup.BalloonBuilder; import com.intellij.openapi.ui.popup.JBPopupFactory; -import com.intellij.ui.JBColor; +import com.intellij.openapi.wm.IdeFrame; +import com.intellij.openapi.wm.WindowManager; +import com.intellij.ui.awt.RelativePoint; +import com.intellij.util.ui.JBUI; import javax.swing.*; -import javax.swing.event.HyperlinkListener; import java.awt.*; import java.util.regex.Matcher; @@ -22,7 +27,6 @@ public class GoogleTranslateAction extends AbstractTranslateAction { protected void doTranslate(String selectText) { Matcher m = p.matcher(selectText.trim()); String translateType = m.find() ? TranslateConstant.ZH_CN_TO_EN : TranslateConstant.EN_TO_ZH_CN; - String result = null; try { GoogleTranslateResult googleTranslateResult = translate(selectText, translateType, editor); if (googleTranslateResult == null) { @@ -42,8 +46,36 @@ public class GoogleTranslateAction extends AbstractTranslateAction { @Override public void run() { final JBPopupFactory factory = JBPopupFactory.getInstance(); - factory.createHtmlTextBalloonBuilder(result.toString(), (Icon) null, (Color) new JBColor(Color.GRAY, Color.DARK_GRAY), (HyperlinkListener) null).setFadeoutTime(5000L).createBalloon().show(factory.guessBestPopupLocation(editor), Balloon.Position.below); + TranslateBalloon translateBalloon = new TranslateBalloon(result); + BalloonBuilder balloonBuilder = factory.createBalloonBuilder(translateBalloon.getjPanel()); + balloonBuilder.setFillColor(ColorService.forCurrentTheme(ColorService.Background)); + balloonBuilder.setContentInsets(JBUI.insets(40, 40)); + balloonBuilder.setBorderInsets(JBUI.emptyInsets()); + balloonBuilder.setBorderColor(ColorService.forCurrentTheme(ColorService.Background)); + balloonBuilder.setShadow(true); + Balloon balloon = balloonBuilder.createBalloon(); + setBounds(translateBalloon, balloon); + IdeFrame window = (IdeFrame) NotificationsManagerImpl.findWindowForBalloon(project); + RelativePoint pointToShowPopup = null; + if (window != null) { + pointToShowPopup = RelativePoint.getSouthEastOf(window.getComponent()); + } + balloon.show(pointToShowPopup, Balloon.Position.above); } }); } + + /** + * 设置了宽高,定位就无效了... + * + * @param balloon + */ + private void setBounds(TranslateBalloon translateBalloon, Balloon balloon) { + int width = translateBalloon.getWidth(); + int height = translateBalloon.getHeight() + 80; + JFrame jFrame = WindowManager.getInstance().getFrame(project); + int x = (int) (jFrame.getBounds().getWidth() / 2 - width / 2); + int y = (int) (jFrame.getBounds().getHeight() / 2 - height / 2); + balloon.setBounds(new Rectangle(x, y, width, height)); + } } diff --git a/src/com/coding/easier/translate/GoogleTranslateResult.java b/src/com/coding/easier/translate/GoogleTranslateResult.java index a37e709e30c90535cea20cdb67aff0782c5ff08a..16abf9230cb35e1f123be1b96e191ff64be92f86 100644 --- a/src/com/coding/easier/translate/GoogleTranslateResult.java +++ b/src/com/coding/easier/translate/GoogleTranslateResult.java @@ -1,10 +1,5 @@ package com.coding.easier.translate; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import java.util.ArrayList; import java.util.List; /** @@ -155,8 +150,13 @@ public class GoogleTranslateResult { @Override public String toString() { - return String.format("%s的翻译\n%s", orig, trans); + return orig + "\n" + trans + "\n"; } + +// @Override +// public String toString() { +// return String.format("%s的翻译\n%s", orig, trans); +// } } public static class DictBean { @@ -265,40 +265,57 @@ public class GoogleTranslateResult { @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("").append(pos).append("\n"); + stringBuilder.append("").append(pos).append("
    "); for (EntryBean entryBean : entry) { - stringBuilder.append("").append(entryBean).append("\n"); + stringBuilder.append("").append(entryBean).append("
    "); } return stringBuilder.toString(); } +// @Override +// public String toString() { +// StringBuilder stringBuilder = new StringBuilder(); +// stringBuilder.append(pos).append("\n"); +// for (EntryBean entryBean : entry) { +// stringBuilder.append(entryBean).append("\n"); +// } +// return stringBuilder.toString(); +// } } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); - SentencesBean sentencesBean = sentences.get(0); - stringBuilder.append(sentencesBean).append("\n"); - if (!sentencesBean.getOrig().equals(sentencesBean.getTrans()) && dict != null) { - for (int i = 0, len = dict.size(); i < len; i++) { - stringBuilder.append(dict.get(i)); + if (sentences.size() == 1 || sentences.size() == 2) { + SentencesBean sentencesBean = sentences.get(0); + stringBuilder.append(sentencesBean).append("\n"); + if (!sentencesBean.getOrig().equals(sentencesBean.getTrans()) && dict != null) { + for (int i = 0, len = dict.size(); i < len; i++) { + stringBuilder.append(dict.get(i)); + } + } + } else { + for (SentencesBean sentence : sentences) { + if (sentence.getTrans() != null) { + stringBuilder.append(sentence.getTrans()); + } } } return stringBuilder.toString(); } - public static void main(String[] args) { - GsonBuilder gsonBuilder = new GsonBuilder(); - gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); - Gson gson = gsonBuilder.create(); - String responseText = "{\"sentences\":[{\"trans\":\"结果\",\"orig\":\"result\",\"backend\":2},{\"translit\":\"Jiéguǒ\",\"src_translit\":\"riˈzəlt\"}],\"dict\":[{\"pos\":\"名词\",\"terms\":[\"结果\",\"成果\",\"效果\",\"成绩\",\"产物\",\"成效\",\"收获\",\"功\",\"名堂\"],\"entry\":[{\"word\":\"结果\",\"reverse_translation\":[\"result\",\"outcome\",\"consequence\",\"effect\",\"consequent\",\"upshot\"],\"score\":0.67663383},{\"word\":\"成果\",\"reverse_translation\":[\"achievement\",\"result\",\"gain\",\"profit\",\"consequent\",\"sequel\"],\"score\":0.02749503},{\"word\":\"效果\",\"reverse_translation\":[\"effect\",\"result\",\"sound effects\",\"consequent\",\"sequel\"],\"score\":0.011642128},{\"word\":\"成绩\",\"reverse_translation\":[\"score\",\"achievement\",\"result\",\"mark\"],\"score\":0.0039610346},{\"word\":\"产物\",\"reverse_translation\":[\"product\",\"result\",\"outcome\"],\"score\":0.0010999396},{\"word\":\"成效\",\"reverse_translation\":[\"effect\",\"result\"],\"score\":6.074443E-4},{\"word\":\"收获\",\"reverse_translation\":[\"gain\",\"result\",\"acquisition\"],\"score\":5.921267E-5},{\"word\":\"功\",\"reverse_translation\":[\"merit\",\"achievement\",\"meritorious service\",\"accomplishment\",\"exploit\",\"result\"],\"score\":5.7390887E-5},{\"word\":\"名堂\",\"reverse_translation\":[\"variety\",\"result\",\"item\"],\"score\":2.123383E-6}],\"base_form\":\"result\",\"pos_enum\":1},{\"pos\":\"动词\",\"terms\":[\"导致\",\"致使\",\"酿\"],\"entry\":[{\"word\":\"导致\",\"reverse_translation\":[\"lead to\",\"cause\",\"result\",\"bring about\",\"create\"],\"score\":0.45783335},{\"word\":\"致使\",\"reverse_translation\":[\"cause\",\"result\",\"occasion\"],\"score\":8.174057E-4},{\"word\":\"酿\",\"reverse_translation\":[\"brew\",\"ferment\",\"lead to\",\"brew up\",\"result\",\"make wine\"],\"score\":4.6644533E-7}],\"base_form\":\"result\",\"pos_enum\":2}],\"src\":\"en\",\"ld_result\":{\"srclangs\":[\"en\"],\"srclangs_confidences\":[1.0],\"extended_srclangs\":[\"en\"]}}"; -// String responseText = "{\"sentences\":[{\"trans\":\"resul\",\"orig\":\"resul\",\"backend\":0},{\"translit\":\"Resul\"}],\"src\":\"en\",\"ld_result\":{\"srclangs\":[\"en\"],\"srclangs_confidences\":[0.6944445],\"extended_srclangs\":[\"en\"]}}"; - GoogleTranslateResult translateResult = gson.fromJson(responseText, GoogleTranslateResult.class); - System.out.println(translateResult.toString()); - List list = new ArrayList<>(); - for (DictBean dictBean : translateResult.dict) { - for (DictBean.EntryBean entryBean : dictBean.getEntry()) { - list.add(entryBean.word); - } - } - } +// public static void main(String[] args) { +// GsonBuilder gsonBuilder = new GsonBuilder(); +// gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); +// Gson gson = gsonBuilder.create(); +// String responseText = "{\"sentences\":[{\"trans\":\"结果\",\"orig\":\"result\",\"backend\":2},{\"translit\":\"Jiéguǒ\",\"src_translit\":\"riˈzəlt\"}],\"dict\":[{\"pos\":\"名词\",\"terms\":[\"结果\",\"成果\",\"效果\",\"成绩\",\"产物\",\"成效\",\"收获\",\"功\",\"名堂\"],\"entry\":[{\"word\":\"结果\",\"reverse_translation\":[\"result\",\"outcome\",\"consequence\",\"effect\",\"consequent\",\"upshot\"],\"score\":0.67663383},{\"word\":\"成果\",\"reverse_translation\":[\"achievement\",\"result\",\"gain\",\"profit\",\"consequent\",\"sequel\"],\"score\":0.02749503},{\"word\":\"效果\",\"reverse_translation\":[\"effect\",\"result\",\"sound effects\",\"consequent\",\"sequel\"],\"score\":0.011642128},{\"word\":\"成绩\",\"reverse_translation\":[\"score\",\"achievement\",\"result\",\"mark\"],\"score\":0.0039610346},{\"word\":\"产物\",\"reverse_translation\":[\"product\",\"result\",\"outcome\"],\"score\":0.0010999396},{\"word\":\"成效\",\"reverse_translation\":[\"effect\",\"result\"],\"score\":6.074443E-4},{\"word\":\"收获\",\"reverse_translation\":[\"gain\",\"result\",\"acquisition\"],\"score\":5.921267E-5},{\"word\":\"功\",\"reverse_translation\":[\"merit\",\"achievement\",\"meritorious service\",\"accomplishment\",\"exploit\",\"result\"],\"score\":5.7390887E-5},{\"word\":\"名堂\",\"reverse_translation\":[\"variety\",\"result\",\"item\"],\"score\":2.123383E-6}],\"base_form\":\"result\",\"pos_enum\":1},{\"pos\":\"动词\",\"terms\":[\"导致\",\"致使\",\"酿\"],\"entry\":[{\"word\":\"导致\",\"reverse_translation\":[\"lead to\",\"cause\",\"result\",\"bring about\",\"create\"],\"score\":0.45783335},{\"word\":\"致使\",\"reverse_translation\":[\"cause\",\"result\",\"occasion\"],\"score\":8.174057E-4},{\"word\":\"酿\",\"reverse_translation\":[\"brew\",\"ferment\",\"lead to\",\"brew up\",\"result\",\"make wine\"],\"score\":4.6644533E-7}],\"base_form\":\"result\",\"pos_enum\":2}],\"src\":\"en\",\"ld_result\":{\"srclangs\":[\"en\"],\"srclangs_confidences\":[1.0],\"extended_srclangs\":[\"en\"]}}"; +// String responseText2 = "{\"sentences\":[{\"trans\":\"An idea plugin that improves development efficiency, the original intention is to write some commonly used functions to this plugin to improve coding efficiency. \",\"orig\":\"一个提升开发效率的idea插件,初衷就是将一些常用的功能写入到这个插件,提高编码效率。\",\"backend\":3,\"translation_engine_debug_info\":[{\"model_tracking\":{\"checkpoint_md5\":\"42aef7b0a5e950954fab74776e36dabe\",\"launch_doc\":\"\"}}]},{\"trans\":\"However, because there is too little information in this area, the new feature update may be relatively slow, but I will slowly add more features. I will add comments to the code to make the code as simple as possible. \",\"orig\":\"但是因为这方面的资料过少的原因, 所以新功能更新可能会相对缓慢,但是一定会慢慢加入更多的功能,我会在代码中加入注释,尽量让代码简单易懂,也算是为了尽量\",\"backend\":3,\"translation_engine_debug_info\":[{\"model_tracking\":{\"checkpoint_md5\":\"42aef7b0a5e950954fab74776e36dabe\",\"launch_doc\":\"\"}}]},{\"trans\":\"Let it be a reference for more people to write their own plugins. \",\"orig\":\"让它成为让一个更多人能自己编写插件的参考资料吧。\",\"backend\":3,\"translation_engine_debug_info\":[{\"model_tracking\":{\"checkpoint_md5\":\"42aef7b0a5e950954fab74776e36dabe\",\"launch_doc\":\"\"}}]},{\"trans\":\"Of course, you are welcome to submit feedback. You can also suggest the features you want to implement. If the suggestions are really good and time permits, I will try to add them.\",\"orig\":\"当然也欢迎提交反馈,有想要实现的功能也可以建议,要是建议确实不错且时间允许,我就会尝试添加。\",\"backend\":3,\"translation_engine_debug_info\":[{\"model_tracking\":{\"checkpoint_md5\":\"42aef7b0a5e950954fab74776e36dabe\",\"launch_doc\":\"\"}}]},{\"src_translit\":\"Yīgè tíshēng kāifā xiàolǜ de idea chājiàn, chūzhōng jiùshì jiāng yīxiē chángyòng de gōngnéng xiě rù dào zhège chājiàn, tígāo biānmǎ xiàolǜ. Dànshì yīnwèi zhè fāngmiàn de zīliàoguò shǎo de yuányīn, suǒyǐ xīn gōngnéng gēngxīn kěnéng huì xiāngduì huǎnmàn, dànshì yīdìng huì màn man jiārù gèng duō de gōngnéng, wǒ huì zài dàimǎ zhōng jiārù zhùshì, jǐnliàng ràng dàimǎ jiǎndān yì dǒng, yě suànshì wèile jǐnliàng ràng tā chéngwéi ràng yīgè gèng duō rén néng zìjǐ biānxiě chājiàn de cānkǎo zīliào ba. Dāngrán yě huānyíng tíjiāo fǎnkuì, yǒu xiǎng yào shíxiàn de gōngnéng yě kěyǐ jiànyì, yàoshi jiànyì quèshí bùcuò qiě shíjiān yǔnxǔ, wǒ jiù huì chángshì tiānjiā.\"}],\"src\":\"zh-CN\",\"ld_result\":{\"srclangs\":[\"zh-CN\"],\"srclangs_confidences\":[1.0],\"extended_srclangs\":[\"zh-CN\"]}}"; +// GoogleTranslateResult translateResult = gson.fromJson(responseText, GoogleTranslateResult.class); +// System.out.println(translateResult.toString()); +// List list = new ArrayList<>(); +// for (DictBean dictBean : translateResult.dict) { +// for (DictBean.EntryBean entryBean : dictBean.getEntry()) { +// list.add(entryBean.word); +// } +// } +// } } diff --git a/src/com/coding/easier/translate/TranslateBalloon.java b/src/com/coding/easier/translate/TranslateBalloon.java new file mode 100644 index 0000000000000000000000000000000000000000..d81fcdba3cf0760b9d9768d31808846144ba6540 --- /dev/null +++ b/src/com/coding/easier/translate/TranslateBalloon.java @@ -0,0 +1,125 @@ +package com.coding.easier.translate; + +import com.coding.easier.ui.modules.ColorService; +import com.coding.easier.ui.modules.IdeaColorService; +import com.intellij.ui.components.JBScrollPane; + +import javax.swing.*; +import java.awt.*; + +/** + * @author: D丶Cheng + * @description: 翻译ui + * @create: 2019-09-10 10:56 + **/ +class TranslateBalloon { + + static { + ColorService.install(new IdeaColorService()); + } + + private GoogleTranslateResult result; + + private JPanel jPanel; + + static JEditorPane origEditorPane = new JEditorPane(); + + static JEditorPane transEditorPane = new JEditorPane(); + + private int origLength; + + private int transLength; + + private int height; + + private int width; + + JPanel getjPanel() { + return jPanel; + } + + int getHeight() { + return height; + } + + int getWidth() { + return width; + } + + TranslateBalloon(GoogleTranslateResult result) { + this.result = result; + init(); + } + + private void init() { + jPanel = new JPanel(new BorderLayout()); + jPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + StringBuilder origBuilder = new StringBuilder(""); + StringBuilder transBuilder = new StringBuilder(""); + if (result.getSentences().size() == 1 || result.getSentences().size() == 2) { + GoogleTranslateResult.SentencesBean sentencesBean = result.getSentences().get(0); + origBuilder.append(result.getSentences().get(0).getOrig()); + transBuilder.append(result.getSentences().get(0).getTrans()).append("
    "); + if (!sentencesBean.getOrig().equals(sentencesBean.getTrans()) && result.getDict() != null) { + for (int i = 0, len = result.getDict().size(); i < len; i++) { + transBuilder.append(result.getDict().get(i)); + } + } + } else { + for (GoogleTranslateResult.SentencesBean sentence : result.getSentences()) { + if (sentence.getTrans() != null) { + origBuilder.append(sentence.getOrig()); + transBuilder.append(sentence.getTrans()); + } + } + } + origBuilder.append(""); + transBuilder.append(""); + origLength = origBuilder.length(); + transLength = transBuilder.length(); + createPanel("原文:", origBuilder, BorderLayout.NORTH, origEditorPane); + createPanel("翻译结果:", transBuilder, BorderLayout.CENTER, transEditorPane); + } + + private void createPanel(String text, StringBuilder builder, String position, JEditorPane editorPane) { + System.out.println(builder.toString()); + JPanel panel = new JPanel(new BorderLayout()); + JLabel label = new JLabel(text); + label.setForeground(new Color(16, 187, 100)); + label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); + panel.add(label, BorderLayout.NORTH); + editorPane.setContentType("text/html"); + editorPane.setText(builder.toString()); + editorPane.setEditable(false); + editorPane.setBackground(ColorService.forCurrentTheme(ColorService.Background)); + editorPane.setSelectionStart(0); + editorPane.setSelectionEnd(0); + editorPane.getSelectedText(); + JBScrollPane scrollPane = new JBScrollPane(editorPane); + scrollPane.setBorder(null); + scrollPane.setMaximumSize(new Dimension(520, 200)); + scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); + scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + panel.add(scrollPane, BorderLayout.CENTER); + setPreferredSize(builder.length(), panel); + jPanel.add(panel, position); + } + + private void setPreferredSize(int len, JPanel panel) { + boolean flag = len * 16 > 540; + int width = flag ? 520 : (len * 16 + 80); + int height = flag ? Math.min(((len * 16 / 520 + 2) * 16), 200) : 32; + this.height += height; + this.width = Math.max(this.width, width); + panel.setPreferredSize(new Dimension(width, height)); + } + +// public static void main(String[] args) { +// TranslateBalloon translateBalloon = new TranslateBalloon(null); +//// TranslateBalloon translateBalloon = new TranslateBalloon(); +// JFrame jFrame = new JFrame(); +// jFrame.add(translateBalloon.jPanel); +// jFrame.setSize(520, 400); +// jFrame.setVisible(true); +// } +} diff --git a/src/com/coding/easier/ui/FormatDialog.form b/src/com/coding/easier/ui/FormatDialog.form new file mode 100644 index 0000000000000000000000000000000000000000..45dabec1f282f4a7c6d7ae721eab67abd932a3c2 --- /dev/null +++ b/src/com/coding/easier/ui/FormatDialog.form @@ -0,0 +1,70 @@ + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    diff --git a/src/com/coding/easier/ui/FormatDialog.java b/src/com/coding/easier/ui/FormatDialog.java new file mode 100644 index 0000000000000000000000000000000000000000..188833dd440c18582e600dd580994c431f58550f --- /dev/null +++ b/src/com/coding/easier/ui/FormatDialog.java @@ -0,0 +1,126 @@ +package com.coding.easier.ui; + +import com.coding.easier.enums.FormatEnum; +import com.coding.easier.format.FormatAction; +import com.coding.easier.util.GsonUtil; +import com.coding.easier.util.NoticeUtil; +import com.intellij.openapi.editor.Editor; +import org.apache.commons.lang.StringUtils; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +/** + * @author D丶Cheng + * @description: format + * @create: 2019-09-06 16:08 + */ +public class FormatDialog extends JDialog { + + private final FormatAction formatAction; + private final Editor editor; + + public JPanel contentPanel; + private JButton formatBtn; + private JComboBox comboBox; + private JTextPane textPanel; + private JPanel centerPanel; + + private String selectedType = FormatEnum.JSON.getValue(); + + public FormatDialog(FormatAction formatAction, Editor editor) { + this.formatAction = formatAction; + this.editor = editor; + textPanel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); + centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + setContentPane(contentPanel); + setModal(true); + getRootPane().setDefaultButton(formatBtn); + comboBox.addItem(FormatEnum.JSON.getValue()); +// comboBox.addItem(FormatEnum.XML.getValue()); + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + initListener(); + } + + public void initListener() { + comboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(final ItemEvent e) { + selectedType = comboBox.getSelectedItem().toString(); + } + }); + formatBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (StringUtils.isEmpty(textPanel.getText())) { + NoticeUtil.error("请输入数据"); + return; + } + String formattedString = ""; + if (FormatEnum.JSON.getValue().equals(selectedType)) { + formattedString = formatJson(textPanel.getText()); + } else { + NoticeUtil.error("正在开发中..."); + return; + } + if ("".equals(formattedString)) { + return; + } + textPanel.setText(formattedString); + } + }); + +// compressBtn.addActionListener(new ActionListener() { +// @Override +// public void actionPerformed(ActionEvent e) { +// +// } +// }); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPanel.registerKeyboardAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + } + + /** + * 格式化json + * + * @param text + */ + private String formatJson(String text) { + String str = ""; + try { + str = GsonUtil.toPrettyFormat(text); + } catch (Exception e) { + System.out.println(NoticeUtil.getStackTrace(e)); + NoticeUtil.error("JSON格式有误"); + } + return str; + } + + private void onCancel() { + // add your code here if necessary + dispose(); + } + + public static void main(String[] args) { + FormatDialog dialog = new FormatDialog(null, null); + dialog.setSize(870, 515); + dialog.setTitle("CodingEasier Format"); + dialog.setLocationRelativeTo(null); + dialog.setVisible(true); + dialog.pack(); + } +} diff --git a/src/com/coding/easier/ui/Popups.java b/src/com/coding/easier/ui/Popups.java new file mode 100644 index 0000000000000000000000000000000000000000..f4d4670ea62008fb05cfb70f18a8e32fb2d73a4f --- /dev/null +++ b/src/com/coding/easier/ui/Popups.java @@ -0,0 +1,50 @@ +package com.coding.easier.ui; + +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.SelectionModel; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.popup.PopupStep; +import com.intellij.openapi.ui.popup.util.BaseListPopupStep; +import com.intellij.openapi.util.NlsContexts; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + + +/** + * @author D丶Cheng + * @Description: + * @date 2020/12/29 12:18 下午 + */ +public class Popups extends BaseListPopupStep { + + public Popups(@Nullable @NlsContexts.PopupTitle String title, List values) { + super(title, values); + } + + // public Popups(@Nullable @NlsContexts.PopupTitle String title, Object[] values, Icon[] icons) { +// super(title, values, icons); +// } +// +// public Popups(@Nullable @NlsContexts.PopupTitle String title, @NotNull List aValues, Icon aSameIcon) { +// super(title, aValues, aSameIcon); +// } +// +// @NotNull +// @Override +// public String getTextFor(Object value) { +// return super.getTextFor(value); +// } +// +// @Nullable +// @Override +// public PopupStep onChosen(Object selectedValue, boolean finalChoice) { +// return super.onChosen(selectedValue, finalChoice); +// } + + + @Override + public boolean isSpeedSearchEnabled() { + return true; + } +} diff --git a/src/com/coding/easier/ui/base/FormColors.java b/src/com/coding/easier/ui/base/FormColors.java new file mode 100644 index 0000000000000000000000000000000000000000..58cc4200171397589104579363a8515039805ea0 --- /dev/null +++ b/src/com/coding/easier/ui/base/FormColors.java @@ -0,0 +1,37 @@ +package com.coding.easier.ui.base; + +import java.awt.*; + +/** + * @author: D丶Cheng + * @description: + * @create: 2019-09-10 12:03 + **/ +public class FormColors { + public final Color[] normalBorder; + public final Color[] focusBorder; + public final Color[] errorBorder; + public final Color[] background; + public final Color[] text; + public final Color[] error; + public final Color[] fieldName; + public final Color[] placeholder; + + public FormColors(Color[] normalBorder, + Color[] focusBorder, + Color[] errorBorder, + Color[] background, + Color[] text, + Color[] error, + Color[] fieldName, + Color[] placeholder) { + this.normalBorder = normalBorder; + this.focusBorder = focusBorder; + this.errorBorder = errorBorder; + this.background = background; + this.text = text; + this.error = error; + this.fieldName = fieldName; + this.placeholder = placeholder; + } +} diff --git a/src/com/coding/easier/ui/base/InteractionColors.java b/src/com/coding/easier/ui/base/InteractionColors.java new file mode 100644 index 0000000000000000000000000000000000000000..ca956887d8d32df1463902fa7721d23be99d52ea --- /dev/null +++ b/src/com/coding/easier/ui/base/InteractionColors.java @@ -0,0 +1,22 @@ +package com.coding.easier.ui.base; + +import java.awt.*; + +/** + * @author: D丶Cheng + * @description: + * @create: 2019-09-10 12:30 + **/ +public class InteractionColors { + public final Color[] normal; + public final Color[] rollover; + public final Color[] pressed; + public final Color[] disabled; + + public InteractionColors(Color[] normal, Color[] rollover, Color[] pressed, Color[] disabled) { + this.normal = normal; + this.rollover = rollover; + this.pressed = pressed; + this.disabled = disabled; + } +} diff --git a/src/com/coding/easier/ui/modules/ColorService.java b/src/com/coding/easier/ui/modules/ColorService.java new file mode 100644 index 0000000000000000000000000000000000000000..e781d9b75610e59903aa7abe3b04c34a80f4fe1b --- /dev/null +++ b/src/com/coding/easier/ui/modules/ColorService.java @@ -0,0 +1,104 @@ +package com.coding.easier.ui.modules; + +import com.coding.easier.ui.base.FormColors; +import com.coding.easier.ui.base.InteractionColors; + +import java.awt.*; + +/** + * @author: D丶Cheng + * @description: + * @create: 2019-09-10 14:59 + **/ +public abstract class ColorService { + + private static ColorService INSTANCE = null; + + public static void install(ColorService instance) { + INSTANCE = instance; + } + + public static T forCurrentTheme(T[] objects) { + return INSTANCE.internalForCurrentTheme(objects); + } + + public static T forLightTheme(T[] objects) { + if (objects == null) { + return null; + } else { + return objects[0]; + } + } + + public static T forDarkTheme(T[] objects) { + if (objects == null) { + return null; + } else if (objects.length > 1) { + return objects[1]; + } else { + return objects[0]; + } + } + + public static final Color[] Background = new Color[]{new Color(0xFFFFFF), new Color(0x3C3F41)}; + public static final Color[] EmphasizedText = new Color[]{new Color(0x000000), new Color(0xCBCDCF)}; + public static final Color[] Text = new Color[]{new Color(0x333333), new Color(0x8B8C8F)}; + public static final Color[] UnemphasizedText = new Color[]{new Color(0x88BCCE), new Color(0x8AB0D6)}; + public static final Color[] Separator = new Color[]{new Color(0xE5E5E5), new Color(0x454546)}; + public static final Color[] SelectedTab = new Color[]{new Color(0x666666), new Color(0xCBCDCF)}; + public static final Color[] Tip = new Color[]{new Color(0xE0F2F8), new Color(0x0560A2)}; + public static final Color[] TipText = new Color[]{new Color(0x086a8b), new Color(0xDBEFFF)}; + public static final Color[] ExceptionPreviewText = new Color[]{new Color(0x2985A4), new Color(0x80BEEF)}; + public static final Color[] ExceptionPreviewBackground = new Color[]{new Color(0xC1E7F3), new Color(0x094778)}; + public static final Color[] ScrollbarTrack = new Color[]{new Color(0xF5F5F5), new Color(0x2D2E2F)}; + public static final Color[] ScrollbarThumb = new Color[]{new Color(0xD5D5D5), new Color(0x484A4B)}; + public static final Color[] ErrorBar = new Color[]{new Color(0xF8F4C8), new Color(0xF2EBAB)}; + public static final Color[] LoadingArc = new Color[]{new Color(0x3379A1), new Color(0x61B6E7)}; + public static final Color[] OnlineStatus = new Color[]{new Color(0x6FF800), new Color(0x6FF800)}; + public static final Color[] OfflineStatus = new Color[]{new Color(0xFF1B1B), new Color(0xFF1B1B)}; + + public static final FormColors NormalForm = new FormColors( + new Color[]{new Color(0xB2B2B2), new Color(0x666768)}, + new Color[]{new Color(0x333333), new Color(0xC8C8C9)}, + new Color[]{new Color(0xE80D0D), new Color(0xFF1B1B)}, + new Color[]{new Color(0xFFFFFF), new Color(0x242526)}, + new Color[]{new Color(0x000000), new Color(0xCBCDCF)}, + new Color[]{new Color(0xE80D0D), new Color(0xFF1B1B)}, + new Color[]{new Color(0x333333), new Color(0x8B8C8E)}, + new Color[]{new Color(0xAAAAAA), new Color(0x666768)} + ); + + public static final FormColors TipForm = new FormColors( + new Color[]{new Color(0xB5D7E2), new Color(0x2072AE)}, + new Color[]{new Color(0x3379A1), new Color(0x61B6E7)}, + new Color[]{new Color(0xE80D0D), new Color(0xFF5644)}, + new Color[]{new Color(0xFFFFFF), new Color(0x083960)}, + new Color[]{new Color(0x000000), new Color(0xDBEFFF)}, + new Color[]{new Color(0xE80D0D), new Color(0xFF5644)}, + new Color[]{new Color(0x086a8b), new Color(0xDBEFFF)}, + new Color[]{new Color(0xAAAAAA), new Color(0x3682BE)} + ); + + public static final InteractionColors LinkInteraction = new InteractionColors( + new Color[]{new Color(0xFF8000), new Color(0xFF8000)}, + new Color[]{new Color(0xFEA144), new Color(0xFEA144)}, + new Color[]{new Color(0xED7700), new Color(0xED7700)}, + new Color[]{new Color(0xFEA144), new Color(0xFEA144)} + ); + + public static final InteractionColors SecondaryLinkInteraction = new InteractionColors( + new Color[]{new Color(0x333333), new Color(0xCBCDCF)}, + new Color[]{new Color(0xFEA144), new Color(0xFEA144)}, + new Color[]{new Color(0xED7700), new Color(0xED7700)}, + new Color[]{new Color(0xFEA144), new Color(0xFEA144)} + ); + + public static final InteractionColors MarkInteraction = new InteractionColors( + new Color[]{new Color(0x3E85DE), new Color(0x7CD2FF)}, + new Color[]{new Color(0x66A6F6), new Color(0xBBE8FF)}, + new Color[]{new Color(0x3E85DE), new Color(0x7CD2FF)}, + new Color[]{new Color(0x81B6C8), new Color(0x78A6D4)} + ); + + protected abstract T internalForCurrentTheme(T[] objects); +} diff --git a/src/com/coding/easier/ui/modules/IdeaColorService.java b/src/com/coding/easier/ui/modules/IdeaColorService.java new file mode 100644 index 0000000000000000000000000000000000000000..a3748db975d17c993b4fefdac0bdd31a2de0a436 --- /dev/null +++ b/src/com/coding/easier/ui/modules/IdeaColorService.java @@ -0,0 +1,21 @@ +package com.coding.easier.ui.modules; + +import com.intellij.util.ui.UIUtil; + +/** + * @author: D丶Cheng + * @description: + * @create: 2019-09-10 15:39 + **/ +public class IdeaColorService extends ColorService{ + @Override + protected T internalForCurrentTheme(T[] objects) { + if (objects == null) { + return null; + } else if (UIUtil.isUnderDarcula() && objects.length > 1) { + return objects[1]; + } else { + return objects[0]; + } + } +} diff --git a/src/com/coding/easier/util/GsonUtil.java b/src/com/coding/easier/util/GsonUtil.java index d2b4dd2b9aace3ed25bbc681dfa0edad3586e074..d3c7810be9d16f9ed5e6dbdd1a718302e944e8f5 100644 --- a/src/com/coding/easier/util/GsonUtil.java +++ b/src/com/coding/easier/util/GsonUtil.java @@ -1,8 +1,7 @@ package com.coding.easier.util; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import com.google.gson.*; +import org.apache.commons.lang.StringEscapeUtils; /** * @author: D丶Cheng @@ -17,4 +16,24 @@ public class GsonUtil { gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); gson = gsonBuilder.create(); } + + public static String toPrettyFormat(String json) { + json = StringEscapeUtils.unescapeJava(json.trim()); + JsonParser jsonParser = new JsonParser(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + if (json.startsWith("[")) { + JsonArray jsonArray = jsonParser.parse(json).getAsJsonArray(); + json = gson.toJson(jsonArray); + } else if (json.startsWith("{")) { + JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject(); + json = gson.toJson(jsonObject); + } + return json; + } + +// public static void main(String[] args) { +// String responseText = "{\"sentences\":[{\"trans\":\"结果\",\"orig\":\"result\",\"backend\":2},{\"translit\":\"Jiéguǒ\",\"src_translit\":\"riˈzəlt\"}],\"dict\":[{\"pos\":\"名词\",\"terms\":[\"结果\",\"成果\",\"效果\",\"成绩\",\"产物\",\"成效\",\"收获\",\"功\",\"名堂\"],\"entry\":[{\"word\":\"结果\",\"reverse_translation\":[\"result\",\"outcome\",\"consequence\",\"effect\",\"consequent\",\"upshot\"],\"score\":0.67663383},{\"word\":\"成果\",\"reverse_translation\":[\"achievement\",\"result\",\"gain\",\"profit\",\"consequent\",\"sequel\"],\"score\":0.02749503},{\"word\":\"效果\",\"reverse_translation\":[\"effect\",\"result\",\"sound effects\",\"consequent\",\"sequel\"],\"score\":0.011642128},{\"word\":\"成绩\",\"reverse_translation\":[\"score\",\"achievement\",\"result\",\"mark\"],\"score\":0.0039610346},{\"word\":\"产物\",\"reverse_translation\":[\"product\",\"result\",\"outcome\"],\"score\":0.0010999396},{\"word\":\"成效\",\"reverse_translation\":[\"effect\",\"result\"],\"score\":6.074443E-4},{\"word\":\"收获\",\"reverse_translation\":[\"gain\",\"result\",\"acquisition\"],\"score\":5.921267E-5},{\"word\":\"功\",\"reverse_translation\":[\"merit\",\"achievement\",\"meritorious service\",\"accomplishment\",\"exploit\",\"result\"],\"score\":5.7390887E-5},{\"word\":\"名堂\",\"reverse_translation\":[\"variety\",\"result\",\"item\"],\"score\":2.123383E-6}],\"base_form\":\"result\",\"pos_enum\":1},{\"pos\":\"动词\",\"terms\":[\"导致\",\"致使\",\"酿\"],\"entry\":[{\"word\":\"导致\",\"reverse_translation\":[\"lead to\",\"cause\",\"result\",\"bring about\",\"create\"],\"score\":0.45783335},{\"word\":\"致使\",\"reverse_translation\":[\"cause\",\"result\",\"occasion\"],\"score\":8.174057E-4},{\"word\":\"酿\",\"reverse_translation\":[\"brew\",\"ferment\",\"lead to\",\"brew up\",\"result\",\"make wine\"],\"score\":4.6644533E-7}],\"base_form\":\"result\",\"pos_enum\":2}],\"src\":\"en\",\"ld_result\":{\"srclangs\":[\"en\"],\"srclangs_confidences\":[1.0],\"extended_srclangs\":[\"en\"]}}"; +// String responseText2 = "{\"sentences\":[{\"trans\":\"结果\",\"orig\":\"result\",\"backend\":2},{\"translit\":\"Jiéguǒ\",\"src_translit\":\"riˈzəlt\"}],\"dict\":[{\"pos\":\"名词\",\"terms\":[\"结果\",\"成果\",\"效果\",\"成绩\",\"产物\",\"成效\",\"收获\",\"功\",\"名堂\"],\"entry\":[{\"word\":\"结果\",\"reverse_translation\":[\"result\",\"outcome\",\"consequence\",\"effect\",\"consequent\",\"upshot\"],\"score\":0.67663383},{\"word\":\"成果\",\"reverse_translation\":[\"achievement\",\"result\",\"gain\",\"profit\",\"consequent\",\"sequel\"],\"score\":0.02749503},{\"word\":\"效果\",\"reverse_translation\":[\"effect\",\"result\",\"sound effects\",\"consequent\",\"sequel\"],\"score\":0.011642128},{\"word\":\"成绩\",\"reverse_translation\":[\"score\",\"achievement\",\"result\",\"mark\"],\"score\":0.0039610346},{\"word\":\"产物\",\"reverse_translation\":[\"product\",\"result\",\"outcome\"],\"score\":0.0010999396},{\"word\":\"成效\",\"reverse_translation\":[\"effect\",\"result\"],\"score\":6.074443E-4},{\"word\":\"收获\",\"reverse_translation\":[\"gain\",\"result\",\"acquisition\"],\"score\":5.921267E-5},{\"word\":\"功\",\"reverse_translation\":[\"merit\",\"achievement\",\"meritorious service\",\"accomplishment\",\"exploit\",\"result\"],\"score\":5.7390887E-5},{\"word\":\"名堂\",\"reverse_translation\":[\"variety\",\"result\",\"item\"],\"score\":2.123383E-6}],\"base_form\":\"result\",\"pos_enum\":1},{\"pos\":\"动词\",\"terms\":[\"导致\",\"致使\",\"酿\"],\"entry\":[{\"word\":\"导致\",\"reverse_translation\":[\"lead to\",\"cause\",\"result\",\"bring about\",\"create\"],\"score\":0.45783335},{\"word\":\"致使\",\"reverse_translation\":[\"cause\",\"result\",\"occasion\"],\"score\":8.174057E-4},{\"word\":\"酿\",\"reverse_translation\":[\"brew\",\"ferment\",\"lead to\",\"brew up\",\"result\",\"make wine\"],\"score\":4.6644533E-7}],\"base_form\":\"result\",\"pos_enum\":2}],\"src\":\"en\",\"ld_result\":{\"srclangs\":[\"en\"],\"srclangs_confidences\":[1.0],\"extended_srclangs\":[\"en\"]}}"; +// System.out.println(toPrettyFormat(responseText2)); +// } } diff --git a/src/com/coding/easier/util/NoticeUtil.java b/src/com/coding/easier/util/NoticeUtil.java index 5f0554fd03b9951494008a192dfd80dee2f87f06..156d3cee2141a3915e20a22eed8899ecb7d2adef 100644 --- a/src/com/coding/easier/util/NoticeUtil.java +++ b/src/com/coding/easier/util/NoticeUtil.java @@ -4,6 +4,9 @@ import com.coding.easier.constant.TranslateConstant; import com.intellij.notification.*; import com.intellij.openapi.ui.Messages; +import java.io.PrintWriter; +import java.io.StringWriter; + /** * @description: 通知 * @create: 2019-08-30 16:22 @@ -57,4 +60,34 @@ public class NoticeUtil { Messages.getInformationIcon() ); } + + /** + * 错误信息 + * + * @param throwable + */ + public static void error(Throwable throwable) { + Messages.showMessageDialog( + getStackTrace(throwable), + "错误", + Messages.getInformationIcon() + ); + } + + + /** + * + * @param throwable + * @return + */ + public static String getStackTrace(Throwable throwable) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + try { + throwable.printStackTrace(pw); + return sw.toString(); + } finally { + pw.close(); + } + } } diff --git a/src/com/coding/easier/util/StringUtil.java b/src/com/coding/easier/util/StringUtil.java index 9ca6d35d2fbafeb88095d17880582e5b39f6f0a5..88c318b82b2dca3b45ba41cd83a4c6f30df7d031 100644 --- a/src/com/coding/easier/util/StringUtil.java +++ b/src/com/coding/easier/util/StringUtil.java @@ -1,6 +1,7 @@ package com.coding.easier.util; -import java.util.*; +import java.util.LinkedHashSet; +import java.util.Set; /** * @author: D丶Cheng @@ -132,15 +133,15 @@ public class StringUtil { */ public static LinkedHashSet getAllCase(String text) { LinkedHashSet set = new LinkedHashSet<>(); - set.add(StringUtil.textToCamelCase(text, false) + " "); - set.add(StringUtil.textToCamelCase(text, true) + " "); - set.add(StringUtil.textToConstant(text) + " "); - set.add(StringUtil.textToUnderscoreCase(text) + " "); - set.add(StringUtil.textToKebabCase(text, false) + " "); - set.add(StringUtil.textToKebabCase(text, true) + " "); - set.add(StringUtil.textToWords(text) + " "); - set.add(text.toUpperCase() + " "); - set.add(text.toLowerCase() + " "); + set.add(StringUtil.textToCamelCase(text, false)); + set.add(StringUtil.textToCamelCase(text, true)); + set.add(StringUtil.textToConstant(text)); + set.add(StringUtil.textToUnderscoreCase(text)); + set.add(StringUtil.textToKebabCase(text, false)); + set.add(StringUtil.textToKebabCase(text, true)); + set.add(StringUtil.textToWords(text)); + set.add(text.toUpperCase()); + set.add(text.toLowerCase()); return set; } @@ -152,12 +153,12 @@ public class StringUtil { */ public static Set getAllTranslateCase(String text) { LinkedHashSet set = new LinkedHashSet<>(); - set.add(StringUtil.textToCamelCase(text, false) + " "); - set.add(StringUtil.textToCamelCase(text, true) + " "); - set.add(StringUtil.textToConstant(text) + " "); - set.add(StringUtil.textToUnderscoreCase(text) + " "); - set.add(StringUtil.textToKebabCase(text, false) + " "); - set.add(StringUtil.textToKebabCase(text, true) + " "); + set.add(StringUtil.textToCamelCase(text, false)); + set.add(StringUtil.textToCamelCase(text, true)); + set.add(StringUtil.textToConstant(text)); + set.add(StringUtil.textToUnderscoreCase(text)); + set.add(StringUtil.textToKebabCase(text, false)); + set.add(StringUtil.textToKebabCase(text, true)); return set; } } diff --git a/src/com/coding/easier/util/TkTools.java b/src/com/coding/easier/util/TkUtil.java similarity index 90% rename from src/com/coding/easier/util/TkTools.java rename to src/com/coding/easier/util/TkUtil.java index 16c83542b0df394ecccfc93cc2ffcdb520efd815..8a81e0a20ab978a50975f4b913219a74fb159147 100644 --- a/src/com/coding/easier/util/TkTools.java +++ b/src/com/coding/easier/util/TkUtil.java @@ -5,7 +5,7 @@ package com.coding.easier.util; * @description: 计算tk * @create: 2019-09-04 15:31 **/ -public class TkTools { +public class TkUtil { public final static String TKK = "406398.2087938574"; @@ -28,7 +28,7 @@ public class TkTools { * @param cc * @return */ - public static int Number(Object cc) { + public static int number(Object cc) { try { long a = Long.parseLong(cc.toString()); int b = a > 2147483647 ? (int) (a - 4294967296L) : a < -2147483647 ? (int) (a + 4294967296L) : (int) a; @@ -41,11 +41,11 @@ public class TkTools { public static String b(long a, String b) { for (int d = 0; d < b.length() - 2; d += 3) { char c = b.charAt(d + 2); - int c0 = 'a' <= c ? charCodeAt(c, 0) - 87 : Number(c); + int c0 = 'a' <= c ? charCodeAt(c, 0) - 87 : number(c); long c1 = '+' == b.charAt(d + 1) ? a >> c0 : a << c0; a = '+' == b.charAt(d) ? a + c1 & 4294967295L : a ^ c1; } - a = Number(a); + a = number(a); return a + ""; } @@ -54,7 +54,7 @@ public class TkTools { int d = 0; int h = 0; int[] g = new int[a.length() * 3]; - h = Number(e[0]); + h = number(e[0]); for (int f = 0; f < a.length(); f++) { int c = charCodeAt(a, f); if (128 > c) { @@ -92,10 +92,10 @@ public class TkTools { long aa = h; for (d = 0; d < g0.length; d++) { aa += g0[d]; - aa = Number(b(aa, "+-a^+6")); + aa = number(b(aa, "+-a^+6")); } - aa = Number(b(aa, "+-3^+b+-f")); - long bb = aa ^ Number(e[1]); + aa = number(b(aa, "+-3^+b+-f")); + long bb = aa ^ number(e[1]); aa = bb; aa = aa + bb; bb = aa - bb;