PY-33254 Complete file paths in string literals assigned to variables that has something about file paths in their names

GitOrigin-RevId: 01a4dfc57348608597fbc13ce17fecdaa7c08e38
This commit is contained in:
Andrey Vlasovskikh
2019-10-15 20:16:55 +03:00
committed by intellij-monorepo-bot
parent 19700c4bfc
commit c355b9b11c
8 changed files with 30 additions and 0 deletions

View File

@@ -1465,6 +1465,16 @@ public class PythonCompletionTest extends PyTestCase {
doMultiFileTest();
}
// PY-33254
public void testAssignmentPatternStringPath() {
doMultiFileTest();
}
// PY-33254
public void testQualifiedAssignmentPatternStringPath() {
doMultiFileTest();
}
// PY-8302
public void testUndeclaredFunction() {
myFixture.configureByFile("uninitialized/fun.py");

View File

@@ -28,11 +28,23 @@ class PySoftFileReferenceContributor : PsiReferenceContributor() {
val stringLiteral = psiElement(PyStringLiteralExpression::class.java)
val pattern = psiElement()
.andOr(stringLiteral.with(HardCodedCalleeName),
stringLiteral.with(AssignmentMatchingNamePattern),
stringLiteral.with(KeywordArgumentMatchingNamePattern),
stringLiteral.with(CallArgumentMatchingParameterNamePattern))
registrar.registerReferenceProvider(pattern, PySoftFileReferenceProvider)
}
/**
* Matches string literals used in assignment statements where the assignment target has a name that has something about files or paths.
*/
private object AssignmentMatchingNamePattern : PatternCondition<PyStringLiteralExpression>("assignmentMatchingPattern") {
override fun accepts(expr: PyStringLiteralExpression, context: ProcessingContext?): Boolean {
val assignment = expr.parent as? PyAssignmentStatement ?: return false
val targetNames = assignment.targets.filterIsInstance<PyTargetExpression>().mapNotNull { it.name }
return targetNames.any(::matchesPathNamePattern)
}
}
/**
* Matches string literals used as function arguments where the corresponding function parameter has a name that has something about
* files or paths.

View File

@@ -0,0 +1 @@
config_path = "foobar.txt"

View File

@@ -0,0 +1 @@
config_path = "foo<caret>"

View File

@@ -0,0 +1,3 @@
class C:
def __init__(self):
self.path = "foobar.txt"

View File

@@ -0,0 +1,3 @@
class C:
def __init__(self):
self.path = "foo<caret>"