mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-22091 Consider "self" references in f-strings inside potentially static methods
This commit is contained in:
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.jetbrains.python.codeInsight.fstrings;
|
||||
|
||||
import com.intellij.lang.injection.InjectedLanguageManager;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import com.jetbrains.python.psi.PyRecursiveElementVisitor;
|
||||
import com.jetbrains.python.psi.PyStringLiteralExpression;
|
||||
import com.jetbrains.python.psi.PyUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.jetbrains.python.psi.PyUtil.as;
|
||||
|
||||
/**
|
||||
* Implementation of {@link PyRecursiveElementVisitor} that recursively visits Python files injected into f-strings.
|
||||
* You can check whether you are inside injected fragment using {@link #myContainingFString} field.
|
||||
*/
|
||||
public class PyFStringAwareRecursiveVisitor extends PyRecursiveElementVisitor {
|
||||
protected PyStringLiteralExpression myContainingFString;
|
||||
|
||||
@Override
|
||||
public void visitPyStringLiteralExpression(PyStringLiteralExpression pyString) {
|
||||
final List<TextRange> formatNodeRanges = ContainerUtil.mapNotNull(pyString.getStringNodes(), node -> {
|
||||
final PyUtil.StringNodeInfo nodeInfo = new PyUtil.StringNodeInfo(node);
|
||||
return nodeInfo.isFormatted() ? nodeInfo.getAbsoluteContentRange().shiftRight(-pyString.getTextOffset()) : null;
|
||||
});
|
||||
if (!formatNodeRanges.isEmpty()) {
|
||||
final InjectedLanguageManager injectionManager = InjectedLanguageManager.getInstance(pyString.getProject());
|
||||
for (Pair<PsiElement, TextRange> pair : ContainerUtil.notNullize(injectionManager.getInjectedPsiFiles(pyString))) {
|
||||
final PyFile pyFile = as(pair.getFirst(), PyFile.class);
|
||||
if (pyFile != null && ContainerUtil.exists(formatNodeRanges, range -> range.contains(pair.getSecond()))) {
|
||||
myContainingFString = pyString;
|
||||
try {
|
||||
pyFile.accept(this);
|
||||
}
|
||||
finally {
|
||||
myContainingFString = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
super.visitPyStringLiteralExpression(pyString);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.codeInsight.fstrings.PyFStringAwareRecursiveVisitor;
|
||||
import com.jetbrains.python.inspections.quickfix.PyMakeFunctionFromMethodQuickFix;
|
||||
import com.jetbrains.python.inspections.quickfix.PyMakeMethodStaticQuickFix;
|
||||
import com.jetbrains.python.psi.*;
|
||||
@@ -92,7 +93,7 @@ public class PyMethodMayBeStaticInspection extends PyInspection {
|
||||
}
|
||||
|
||||
final boolean[] mayBeStatic = {true};
|
||||
PyRecursiveElementVisitor visitor = new PyRecursiveElementVisitor() {
|
||||
PyRecursiveElementVisitor visitor = new PyFStringAwareRecursiveVisitor() {
|
||||
@Override
|
||||
public void visitPyRaiseStatement(PyRaiseStatement node) {
|
||||
super.visitPyRaiseStatement(node);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
class C:
|
||||
def method(self):
|
||||
print(r'foobar' f'{self}')
|
||||
@@ -95,6 +95,11 @@ public class PyMethodMayBeStaticInspectionTest extends PyTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-22091
|
||||
public void testFString() {
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
|
||||
}
|
||||
|
||||
// PY-18866
|
||||
public void testSuperSamePy3() {
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON30, () -> doTest());
|
||||
|
||||
Reference in New Issue
Block a user