Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.jetbrains.python.impl.inspections;

import consulo.annotation.access.RequiredReadAction;
import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import consulo.language.editor.intention.HintAction;
Expand All @@ -36,116 +38,129 @@
/**
* @author dcheryasov
*/
public abstract class PyInspectionVisitor extends PyElementVisitor
{
@Nullable
private final ProblemsHolder myHolder;
@Nonnull
private final LocalInspectionToolSession mySession;
protected final TypeEvalContext myTypeEvalContext;
public abstract class PyInspectionVisitor extends PyElementVisitor {
@Nullable
private final ProblemsHolder myHolder;
@Nonnull
private final LocalInspectionToolSession mySession;
protected final TypeEvalContext myTypeEvalContext;

public static final Key<TypeEvalContext> INSPECTION_TYPE_EVAL_CONTEXT = Key.create("PyInspectionTypeEvalContext");
public static final Key<TypeEvalContext> INSPECTION_TYPE_EVAL_CONTEXT = Key.create("PyInspectionTypeEvalContext");

public PyInspectionVisitor(@Nullable ProblemsHolder holder, @Nonnull LocalInspectionToolSession session)
{
myHolder = holder;
mySession = session;
TypeEvalContext context;
synchronized(INSPECTION_TYPE_EVAL_CONTEXT)
{
context = session.getUserData(INSPECTION_TYPE_EVAL_CONTEXT);
if(context == null)
{
PsiFile file = session.getFile();
context = TypeEvalContext.codeAnalysis(file.getProject(), file);
session.putUserData(INSPECTION_TYPE_EVAL_CONTEXT, context);
}
}
myTypeEvalContext = context;
}
public PyInspectionVisitor(@Nullable ProblemsHolder holder, @Nonnull LocalInspectionToolSession session) {
myHolder = holder;
mySession = session;
TypeEvalContext context;
synchronized (INSPECTION_TYPE_EVAL_CONTEXT) {
context = session.getUserData(INSPECTION_TYPE_EVAL_CONTEXT);
if (context == null) {
PsiFile file = session.getFile();
context = TypeEvalContext.codeAnalysis(file.getProject(), file);
session.putUserData(INSPECTION_TYPE_EVAL_CONTEXT, context);
}
}
myTypeEvalContext = context;
}

protected PyResolveContext getResolveContext()
{
return PyResolveContext.noImplicits().withTypeEvalContext(myTypeEvalContext);
}
protected PyResolveContext getResolveContext() {
return PyResolveContext.noImplicits().withTypeEvalContext(myTypeEvalContext);
}

@Nullable
protected ProblemsHolder getHolder()
{
return myHolder;
}
@Nullable
protected ProblemsHolder getHolder() {
return myHolder;
}

@Nonnull
public LocalInspectionToolSession getSession()
{
return mySession;
}
@Nonnull
public LocalInspectionToolSession getSession() {
return mySession;
}

protected final void registerProblem(final PsiElement element, final String message)
{
if(element == null || element.getTextLength() == 0)
{
return;
}
if(myHolder != null)
{
myHolder.registerProblem(element, message);
}
}
@RequiredReadAction
protected final void registerProblem(PsiElement element, String message) {
if (element == null || element.getTextLength() == 0) {
return;
}
if (myHolder != null) {
myHolder.newProblem(LocalizeValue.of(message))
.range(element)
.create();
}
}

protected final void registerProblem(@Nullable final PsiElement element, @Nonnull final String message, @Nonnull final LocalQuickFix... quickFixes)
{
if(element == null || element.getTextLength() == 0)
{
return;
}
if(myHolder != null)
{
myHolder.registerProblem(element, message, quickFixes);
}
}
@RequiredReadAction
protected final void registerProblem(
@Nullable PsiElement element,
@Nonnull String message,
@Nonnull LocalQuickFix... quickFixes
) {
if (element == null || element.getTextLength() == 0) {
return;
}
if (myHolder != null) {
myHolder.newProblem(LocalizeValue.of(message))
.range(element)
.withFixes(quickFixes)
.create();
}
}

protected final void registerProblem(final PsiElement element, final String message, final ProblemHighlightType type)
{
if(element == null || element.getTextLength() == 0)
{
return;
}
if(myHolder != null)
{
myHolder.registerProblem(myHolder.getManager().createProblemDescriptor(element, message, (LocalQuickFix) null, type, myHolder.isOnTheFly()));
}
}
@RequiredReadAction
protected final void registerProblem(PsiElement element, String message, ProblemHighlightType type) {
if (element == null || element.getTextLength() == 0) {
return;
}
if (myHolder != null) {
myHolder.registerProblem(myHolder.getManager().newProblemDescriptor(LocalizeValue.of(message))
.range(element)
.highlightType(type)
.onTheFly(myHolder.isOnTheFly())
.create());
}
}

/**
* The most full-blown version.
*
* @see com.intellij.codeInspection.ProblemDescriptor
*/
protected final void registerProblem(@Nonnull final PsiElement psiElement,
@Nonnull final String descriptionTemplate,
final ProblemHighlightType highlightType,
@Nullable final HintAction hintAction,
final LocalQuickFix... fixes)
{
registerProblem(psiElement, descriptionTemplate, highlightType, hintAction, null, fixes);
}
/**
* The most full-blown version.
*
* @see ProblemDescriptor
*/
@RequiredReadAction
protected final void registerProblem(
@Nonnull PsiElement psiElement,
@Nonnull String descriptionTemplate,
ProblemHighlightType highlightType,
@Nullable HintAction hintAction,
LocalQuickFix... fixes
) {
registerProblem(psiElement, descriptionTemplate, highlightType, hintAction, null, fixes);
}

/**
* The most full-blown version.
*
* @see ProblemDescriptor
*/
protected final void registerProblem(@Nonnull final PsiElement psiElement,
@Nonnull final String descriptionTemplate,
final ProblemHighlightType highlightType,
@Nullable final HintAction hintAction,
@Nullable final TextRange rangeInElement,
final LocalQuickFix... fixes)
{
if(myHolder != null && !(psiElement instanceof PsiErrorElement))
{
myHolder.registerProblem(new ProblemDescriptorImpl(psiElement, psiElement, descriptionTemplate, fixes, highlightType, false, rangeInElement, hintAction, myHolder.isOnTheFly()));
}
}
/**
* The most full-blown version.
*
* @see ProblemDescriptor
*/
@RequiredReadAction
protected final void registerProblem(
@Nonnull PsiElement psiElement,
@Nonnull String descriptionTemplate,
ProblemHighlightType highlightType,
@Nullable HintAction hintAction,
@Nullable TextRange rangeInElement,
LocalQuickFix... fixes
) {
if (myHolder != null && !(psiElement instanceof PsiErrorElement)) {
myHolder.registerProblem(new ProblemDescriptorImpl(
psiElement,
psiElement,
LocalizeValue.of(descriptionTemplate),
fixes,
highlightType,
false,
rangeInElement,
hintAction,
myHolder.isOnTheFly()
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyElement;
import consulo.annotation.access.RequiredReadAction;
import consulo.document.util.TextRange;
import consulo.ide.impl.idea.codeInspection.ex.ProblemDescriptorImpl;
import consulo.language.editor.inspection.LocalQuickFix;
import consulo.language.editor.inspection.ProblemHighlightType;
import consulo.language.editor.intention.IntentionAction;
import consulo.language.editor.intention.QuickFixWrapper;
import consulo.language.psi.PsiElement;
import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

Expand All @@ -34,58 +36,70 @@
* @author Alexey.Ivanov
*/
public class UnsupportedFeatures extends CompatibilityVisitor {
public UnsupportedFeatures() {
super(new ArrayList<>());
}

public UnsupportedFeatures() {
super(new ArrayList<>());
}
@Override
public void visitPyElement(PyElement node) {
setVersionsToProcess(Arrays.asList(LanguageLevel.forElement(node)));
}

@Override
public void visitPyElement(PyElement node) {
setVersionsToProcess(Arrays.asList(LanguageLevel.forElement(node)));
}
@Override
@RequiredReadAction
protected void registerProblem(
@Nonnull PsiElement node,
@Nonnull TextRange range,
@Nonnull String message,
@Nullable LocalQuickFix localQuickFix,
boolean asError
) {
if (range.isEmpty()) {
return;
}

@Override
protected void registerProblem(@Nonnull PsiElement node,
@Nonnull TextRange range,
@Nonnull String message,
@Nullable LocalQuickFix localQuickFix,
boolean asError) {
if (range.isEmpty()) {
return;
if (localQuickFix != null) {
if (asError) {
getHolder().createErrorAnnotation(range, message)
.registerFix(createIntention(node, LocalizeValue.of(message), localQuickFix));
}
else {
getHolder().createWarningAnnotation(range, message)
.registerFix(createIntention(node, LocalizeValue.of(message), localQuickFix));
}
}
else {
if (asError) {
getHolder().createErrorAnnotation(range, message);
}
else {
getHolder().createWarningAnnotation(range, message);
}
}
}

if (localQuickFix != null) {
if (asError) {
getHolder().createErrorAnnotation(range, message).registerFix(createIntention(node, message, localQuickFix));
}
else {
getHolder().createWarningAnnotation(range, message).registerFix(createIntention(node, message, localQuickFix));
}
}
else {
if (asError) {
getHolder().createErrorAnnotation(range, message);
}
else {
getHolder().createWarningAnnotation(range, message);
}
@Nonnull
@RequiredReadAction
private static IntentionAction createIntention(
@Nonnull PsiElement node,
@Nonnull LocalizeValue message,
@Nonnull LocalQuickFix localQuickFix
) {
return createIntention(node, node.getTextRange(), message, localQuickFix);
}
}

@Nonnull
private static IntentionAction createIntention(@Nonnull PsiElement node, @Nonnull String message, @Nonnull LocalQuickFix localQuickFix) {
return createIntention(node, node.getTextRange(), message, localQuickFix);
}
@Nonnull
@RequiredReadAction
private static IntentionAction createIntention(
@Nonnull PsiElement node,
@Nullable TextRange range,
@Nonnull LocalizeValue message,
@Nonnull LocalQuickFix localQuickFix
) {
LocalQuickFix[] quickFixes = {localQuickFix};
ProblemDescriptorImpl descr =
new ProblemDescriptorImpl(node, node, message, quickFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true, range, true);

@Nonnull
private static IntentionAction createIntention(@Nonnull PsiElement node,
@Nullable TextRange range,
@Nonnull String message,
@Nonnull LocalQuickFix localQuickFix) {
final LocalQuickFix[] quickFixes = {localQuickFix};
final ProblemDescriptorImpl descr =
new ProblemDescriptorImpl(node, node, message, quickFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true, range, true);

return QuickFixWrapper.wrap(descr, 0);
}
return QuickFixWrapper.wrap(descr, 0);
}
}
Loading