IDEA插入日志插件
作者:互联网
plugin.xml
<idea-plugin> <id>com.your.company.unique.plugin.id</id> <name>Insert Logs</name> <version>1.0</version> <vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor> <description><![CDATA[ Enter short description for your plugin here.<br> <em>most HTML tags may be used</em> ]]></description> <change-notes><![CDATA[ Add change notes here.<br> <em>most HTML tags may be used</em> ]]> </change-notes> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> <idea-version since-build="173.0"/> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products --> <depends>com.intellij.modules.java</depends> <depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <actions> <group id="insertLogsActionGroup" text="InsertLogs" popup="true"> <add-to-group group-id="EditorPopupMenu" anchor="first"/> </group> <!-- Add your actions here --> <action id="insert_logs" class="InsertLogsAction" text="前后插入日志" description="在执行方法前后插入日志"> <add-to-group group-id="insertLogsActionGroup"/> <keyboard-shortcut keymap="$default" first-keystroke="alt L" second-keystroke="G"/> </action> </actions> </idea-plugin>
InsertLogsAction.Java
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; /** * @author hujiapeng3 * @description * @date 2022/6/19 11:24 */ public class InsertLogsAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); if (!(psiFile instanceof PsiJavaFile)) { return; } PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile; final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR); //获取光标所在位置 final int offset = editor.getCaretModel().getOffset(); PsiElement element = psiFile.findElementAt(offset); final PsiElement methodCallerPsiEle = element.getParent(); if (!(element instanceof PsiIdentifier) || !(methodCallerPsiEle instanceof PsiReferenceExpression)) { return; } final Project project = e.getProject(); WriteCommandAction.runWriteCommandAction(project, () -> { final String methodExpression = methodCallerPsiEle.getParent().getText().replaceAll(" ", "").replaceAll( "\n", ""); final int left = methodExpression.indexOf("("); final int right = methodExpression.indexOf(")"); if (!(left > 0 && right > 0 && (right - left) > 1)) { return; } String paramStr = methodExpression.substring(left + 1, right).replaceAll(" ", ""); String[] params = paramStr.split(","); StringBuilder paramPosLogsSB = new StringBuilder(); StringBuilder paramValLogsSB = new StringBuilder(); for (String param : params) { if (paramPosLogsSB.length() > 0) { paramPosLogsSB.append(","); paramValLogsSB.append(","); } paramPosLogsSB.append("{}"); paramValLogsSB.append("JSON.toJSONString(" + param + ")"); } String preStr = "log.info(\"" + methodExpression + "入参:" + paramPosLogsSB.toString() + "\"," + paramValLogsSB.toString() + ");"; PsiLocalVariable localVarEle = null; PsiElement methodExpresionPsiEle = element; while (!(methodExpresionPsiEle.getParent() instanceof PsiCodeBlock)) { if (methodExpresionPsiEle instanceof PsiLocalVariable) { localVarEle = (PsiLocalVariable) methodExpresionPsiEle; } methodExpresionPsiEle = methodExpresionPsiEle.getParent(); } PsiElement codeBlock = methodExpresionPsiEle.getParent(); final PsiElementFactory psiElementFactory = PsiElementFactory.getInstance(element.getProject()); PsiElement newElement = psiElementFactory.createStatementFromText(preStr, element.getContext()); codeBlock.addBefore(newElement, methodExpresionPsiEle); if (localVarEle != null) { final String tempExp = methodCallerPsiEle.getText().replaceAll(" ", ""); String afterStr = "log.info(\"" + tempExp + "出参:{}\",JSON.toJSONString(" + localVarEle.getName() + "));"; newElement = psiElementFactory.createStatementFromText(afterStr, element.getContext()); codeBlock.addAfter(newElement, methodExpresionPsiEle); } }); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); codeStyleManager.reformat(psiJavaFile); } }
标签:插件,intellij,IDEA,methodExpresionPsiEle,import,日志,openapi,com,final 来源: https://www.cnblogs.com/hujiapeng/p/16391795.html