-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split parameter inspections into separate ones for missing required a…
…nd type
- Loading branch information
1 parent
6ccbe12
commit 4a11889
Showing
6 changed files
with
130 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/io/github/tandemdude/hklbsupport/CommandRequiredParametersInspector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.tandemdude.hklbsupport; | ||
|
||
import com.intellij.codeInspection.LocalInspectionToolSession; | ||
import com.intellij.codeInspection.ProblemHighlightType; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.openapi.module.ModuleUtilCore; | ||
import com.intellij.openapi.util.Pair; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.jetbrains.python.documentation.PythonDocumentationProvider; | ||
import com.jetbrains.python.inspections.PyInspection; | ||
import com.jetbrains.python.inspections.PyInspectionVisitor; | ||
import com.jetbrains.python.psi.PyClass; | ||
import com.jetbrains.python.psi.PyExpression; | ||
import com.jetbrains.python.psi.PyKeywordArgument; | ||
import com.jetbrains.python.psi.types.PyType; | ||
import com.jetbrains.python.psi.types.PyTypeChecker; | ||
import com.jetbrains.python.psi.types.PyTypeParser; | ||
import com.jetbrains.python.psi.types.TypeEvalContext; | ||
import io.github.tandemdude.hklbsupport.utils.Utils; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Local inspection provider allowing problem reporting within Lightbulb command metaclass parameters. | ||
*/ | ||
public class CommandRequiredParametersInspector extends PyInspection { | ||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor( | ||
@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { | ||
return new Visitor(holder, PyInspectionVisitor.getContext(session)); | ||
} | ||
|
||
/** | ||
* Visitor that checks for the presence of problems within Lightbulb command definitions. | ||
*/ | ||
static final class Visitor extends PyInspectionVisitor { | ||
Visitor(@NotNull ProblemsHolder holder, @NotNull TypeEvalContext context) { | ||
super(holder, context); | ||
} | ||
|
||
@Override | ||
public void visitPyClass(@NotNull PyClass node) { | ||
var module = ModuleUtilCore.findModuleForFile(node.getContainingFile()); | ||
if (module == null) { | ||
return; | ||
} | ||
|
||
var service = module.getProject().getService(CommandParameterCompletionLoader.class); | ||
if (service == null) { | ||
return; | ||
} | ||
|
||
var lbData = service.getLightbulbData(module); | ||
if (lbData == null) { | ||
return; | ||
} | ||
|
||
var lbSuperclass = Utils.getLightbulbSuperclass(myTypeEvalContext, node, lbData); | ||
if (lbSuperclass == null) { | ||
return; | ||
} | ||
|
||
var existingParameters = Arrays.stream(node.getSuperClassExpressions()) | ||
.filter(expr -> expr instanceof PyKeywordArgument) | ||
// I had a null pointer exception from this previously so probably good to just make sure | ||
.filter(expr -> expr.getName() != null) | ||
.map(expr -> Pair.create( | ||
((PyKeywordArgument) expr).getKeyword(), ((PyKeywordArgument) expr).getValueExpression())) | ||
.collect(Collectors.toMap(p -> p.getFirst(), p -> p.getSecond())); | ||
|
||
var requiredParams = | ||
lbData.paramData().get(lbSuperclass.getQualifiedName()).required(); | ||
requiredParams.forEach((name, type) -> { | ||
if (!existingParameters.containsKey(name)) { | ||
registerProblem( | ||
node.getSuperClassExpressionList(), | ||
"Command missing required parameter '" + name + "'", | ||
ProblemHighlightType.GENERIC_ERROR); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/main/resources/inspectionDescriptions/CommandParameterInspector.html
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
src/main/resources/inspectionDescriptions/CommandParameterTypeInspector.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Reports items of incorrect type being passed to Lightbulb command definition parameters. | ||
</body> | ||
</html> |
5 changes: 5 additions & 0 deletions
5
src/main/resources/inspectionDescriptions/CommandRequiredParametersInspector.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Reports missing required parameters from Lightbulb command definitions. | ||
</body> | ||
</html> |