Skip to content

Commit

Permalink
Add MypyBatchInspection for "Inspect code" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
intgr committed Nov 29, 2021
1 parent 2a0d431 commit dff3fff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/leinardi/pycharm/mypy/MypyAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.leinardi.pycharm.mypy.checker.Problem;
Expand Down Expand Up @@ -78,6 +79,11 @@ public Results(List<Problem> issues) {
}
}

@Override
public String getPairedBatchInspectionShortName() {
return MypyBatchInspection.INSPECTION_SHORT_NAME;
}

@Nullable
@Override
public State collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) {
Expand All @@ -92,6 +98,20 @@ public State collectInformation(@NotNull PsiFile file, @NotNull Editor editor, b
return new State(file, editor.getProject());
}

// FIXME DUPLICATED - called from MypyBatchInspection
public State collectInformation(@NotNull PsiFile file) {
// FIXME HACK: how to get Project?
Project[] projects = ProjectManager.getInstance().getOpenProjects();
boolean unsaved = documentIsModifiedAndUnsaved(file);
LOG.debug("collectInformation " + file.getVirtualFile().getPresentableUrl() // XXX may be null
+ " modified=" + file.getModificationStamp()
+ " saved=" + !unsaved
+ " thread=" + Thread.currentThread().getName()
);

return new State(file, projects[0]);
}

@Nullable
@Override
public Results doAnnotate(State state) {
Expand All @@ -111,6 +131,7 @@ public Results doAnnotate(State state) {
return new Results(issues);
}

@Override
public void apply(@NotNull PsiFile file, Results annotationResult, @NotNull AnnotationHolder holder) {
if (annotationResult == null || !file.isValid()) {
return;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/leinardi/pycharm/mypy/MypyBatchInspection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.leinardi.pycharm.mypy;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ex.ExternalAnnotatorBatchInspection;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;

public class MypyBatchInspection extends LocalInspectionTool implements ExternalAnnotatorBatchInspection {
private static final Logger LOG = Logger.getInstance(MypyAnnotator.class);
public static final String INSPECTION_SHORT_NAME = "MypyInspection";

@Override
public @NotNull String getShortName() {
return INSPECTION_SHORT_NAME;
}
}
10 changes: 10 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
<projectConfigurable instance="com.leinardi.pycharm.mypy.MypyConfigurable"/>

<externalAnnotator language="Python" implementationClass="com.leinardi.pycharm.mypy.MypyAnnotator"/>
<!-- TODO is suppressId needed? -->
<!-- language="Python"-->
<localInspection implementationClass="com.leinardi.pycharm.mypy.MypyBatchInspection"
bundle="com.leinardi.pycharm.mypy.MypyBundle"
key="inspection.display-name"
groupKey="inspection.group"
shortName="MypyInspection"
level="WARNING"
unfair="true"
enabledByDefault="true"/>

<checkinHandlerFactory id="CheckStyleIDEACheckInHandlerFactory"
implementation="com.leinardi.pycharm.mypy.handlers.ScanFilesBeforeCheckinHandlerFactory"/>
Expand Down

0 comments on commit dff3fff

Please sign in to comment.