From 26cef9eac64cdf3838b9208bdbbec1a48906e5c3 Mon Sep 17 00:00:00 2001 From: HuangYongliang Date: Thu, 18 Jan 2024 23:24:46 +0800 Subject: [PATCH] 1.support ignore variable which at other scope --- .../skykoma/plugin/idea/SkykomaConstants.java | 2 + .../GenLogBeforeCurrentLineMacro.java | 62 ++++++++++++------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/main/java/cn/hylstudio/skykoma/plugin/idea/SkykomaConstants.java b/src/main/java/cn/hylstudio/skykoma/plugin/idea/SkykomaConstants.java index b6c9f14..637b550 100644 --- a/src/main/java/cn/hylstudio/skykoma/plugin/idea/SkykomaConstants.java +++ b/src/main/java/cn/hylstudio/skykoma/plugin/idea/SkykomaConstants.java @@ -2,6 +2,7 @@ import com.google.common.collect.Sets; +import java.util.Collections; import java.util.Set; public class SkykomaConstants { @@ -33,6 +34,7 @@ private SkykomaConstants() { public static final String JPA_TABLE_CLASS = "javax.persistence.Table"; public static final String JPA_COLUMN_CLASS = "javax.persistence.Column"; public static final String JPA_ENTITY_ANNOTATION_CLASS = "javax.persistence.Entity"; + public static final Set LOMBOK_SLF4J = Collections.singleton("lombok.extern.slf4j.Slf4j"); public static final Set CONTROLLER_ANNOTATIONS = Sets.newHashSet( SkykomaConstants.CONTROLLER_ANNOTATION_CLASS, SkykomaConstants.REST_CONTROLLER_ANNOTATION_CLASS diff --git a/src/main/java/cn/hylstudio/skykoma/plugin/idea/livetmpl/GenLogBeforeCurrentLineMacro.java b/src/main/java/cn/hylstudio/skykoma/plugin/idea/livetmpl/GenLogBeforeCurrentLineMacro.java index efb76ee..569e35f 100644 --- a/src/main/java/cn/hylstudio/skykoma/plugin/idea/livetmpl/GenLogBeforeCurrentLineMacro.java +++ b/src/main/java/cn/hylstudio/skykoma/plugin/idea/livetmpl/GenLogBeforeCurrentLineMacro.java @@ -8,6 +8,11 @@ import com.intellij.openapi.editor.Editor; import com.intellij.ide.util.PropertiesComponent; import com.intellij.psi.*; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.scope.processor.VariablesProcessor; +import com.intellij.psi.scope.util.PsiScopesUtil; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.search.SearchScope; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; import org.apache.commons.lang3.StringUtils; @@ -16,12 +21,14 @@ import java.util.stream.Collectors; import static cn.hylstudio.skykoma.plugin.idea.SkykomaConstants.*; + /** * Generate LOGGER before current line */ public class GenLogBeforeCurrentLineMacro extends MacroBase { private static final Logger LOGGER = Logger.getInstance(GenLogBeforeCurrentLineMacro.class); private final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(); + public GenLogBeforeCurrentLineMacro() { this("genLogBeforeCurrentLine", "genLogBeforeCurrentLine()"); } @@ -35,10 +42,6 @@ protected Result calculateResult(Expression @NotNull [] params, ExpressionContex boolean useSelection) { boolean generateCurrentMethodName = propertiesComponent.getBoolean(GENERATE_CURRENT_METHOD_NAME_ENABLED, GENERATE_CURRENT_METHOD_NAME_ENABLED_DEFAULT); - String logVariableName = propertiesComponent.getValue(GENERATE_LOG_VARIABLE_NAME, GENERATE_LOG_VARIABLE_NAME_DEFAULT); - if(StringUtils.isEmpty(logVariableName)){ - logVariableName = "LOGGER"; - } Editor editor = expressionContext.getEditor(); if (editor == null) { return new TextResult("//genAllSetterByParam error, editor empty"); @@ -55,57 +58,70 @@ protected Result calculateResult(Expression @NotNull [] params, ExpressionContex if (currentClass == null) { return new TextResult("//genAllSetterByParam error, currentClass empty"); } + String logVariableName = propertiesComponent.getValue(GENERATE_LOG_VARIABLE_NAME, GENERATE_LOG_VARIABLE_NAME_DEFAULT); + if (PsiUtils.classHasAnnotation(currentClass, SkykomaConstants.LOMBOK_SLF4J)) { + logVariableName = "log"; + } + if (StringUtils.isEmpty(logVariableName)) { + logVariableName = "LOGGER"; + } String methodName = currentMethod.getName(); - List parameters = Arrays.asList(currentMethod.getParameterList().getParameters()); - List logParams = parameters.stream().map(PsiParameter::getName).collect(Collectors.toList()); - //TODO 处理作用域 - List localVariables = getLocalVariables(editor, currentMethod); +// List parameters = Arrays.asList(currentMethod.getParameterList().getParameters()); +// List parameters = Arrays.asList(currentMethod.getParameterList().getParameters()); + //TODO 去掉前面打印过的变量 + List logParams = new ArrayList<>();// parameters.stream().map(PsiParameter::getName).collect(Collectors.toList()); + List localVariables = getLocalVariables(editor, currentMethod); boolean isController = PsiUtils.classHasAnnotation(currentClass, SkykomaConstants.CONTROLLER_ANNOTATIONS); mergeLocalVariables(logParams, isController, localVariables); String paramsPattern = logParams.stream() .map(v -> String.format("%s = [{}]", v)) .collect(Collectors.joining(", ")); String logStmtPattern; - if(generateCurrentMethodName) { + if (generateCurrentMethodName) { logStmtPattern = String.format("%s, %s", methodName, paramsPattern); - }else{ + } else { logStmtPattern = paramsPattern; } String logStmtParams = String.join(", ", logParams); + //TODO 支持log.error("Xxx, e = [{}]",e.getMessage(),e); String result = String.format("%s.info(\"%s\", %s);", logVariableName, logStmtPattern, logStmtParams); return new TextResult(result); } - private static void mergeLocalVariables(List logParams, boolean isController, List localVariables) { + private static void mergeLocalVariables(List logParams, boolean isController, List localVariables) { Set currentParamSet = new HashSet<>(logParams.size() + localVariables.size()); currentParamSet.addAll(logParams); localVariables.forEach(v -> { - if (!currentParamSet.contains(v)) { - if ("uid".equalsIgnoreCase(v) && isController) { - logParams.add(0, v); + String name = v.getName(); + if (!currentParamSet.contains(name)) { + if ("uid".equalsIgnoreCase(name) && isController) { + logParams.add(0, name); } else { - logParams.add(v); + logParams.add(name); } } }); } - private List getLocalVariables(Editor editor, PsiMethod containingMethod) { - List result = new ArrayList<>(); + private List getLocalVariables(Editor editor, PsiMethod containingMethod) { + List result = new ArrayList<>(); int offset = editor.getCaretModel().getOffset(); PsiFile psiFile = containingMethod.getContainingFile(); PsiElement element = psiFile.findElementAt(offset); if (element == null) { return result; } - containingMethod.accept(new JavaRecursiveElementVisitor() { + VariablesProcessor variablesProcessor = new VariablesProcessor(false) { @Override - public void visitLocalVariable(PsiLocalVariable variable) { - if (variable.getTextOffset() <= offset) { - result.add(variable.getName()); - } + protected boolean check(PsiVariable variable, ResolveState state) { + return true; } - }); + }; + PsiScopesUtil.treeWalkUp(variablesProcessor, element, containingMethod); + for (int i = variablesProcessor.size() - 1; i >= 0; i--) { + PsiVariable variable = variablesProcessor.getResult(i); + result.add(variable); + } return result; }