Fixed SQL 'select/insert/...' rule injection into indented string literals (PY-11970)

This commit is contained in:
Andrey Vlasovskikh
2014-01-29 21:53:58 +04:00
parent 2621f17634
commit 5fd0a911b0
2 changed files with 21 additions and 1 deletions

View File

@@ -9,6 +9,6 @@
</injection>
<injection language="SQL" injector-id="python">
<display-name>"SQL select/delete/insert/update/create"</display-name>
<place><![CDATA[pyLiteralExpression().withText(string().matchesBrics(".{0,5}(((SELECT|DELETE) .*FROM)|((INSERT|REPLACE) .*INTO)|(UPDATE .* SET)|((CREATE|DROP|ALTER) +(TABLE|INDEX))) .*"))]]></place>
<place><![CDATA[pyStringLiteralMatches("[ \\t\\r\\n]*(((SELECT|DELETE) .*FROM)|((INSERT|REPLACE) .*INTO)|(UPDATE .* SET)|((CREATE|DROP|ALTER) +(TABLE|INDEX))).*")]]></place>
</injection>
</component>

View File

@@ -22,11 +22,14 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.documentation.DocStringUtil;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.resolve.PyResolveContext;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
/**
* @author yole
*/
@@ -39,6 +42,23 @@ public class PythonPatterns extends PlatformPatterns {
});
}
public static PyElementPattern.Capture<PyStringLiteralExpression> pyStringLiteralMatches(final String regexp) {
final Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
return new PyElementPattern.Capture<PyStringLiteralExpression>(new InitialPatternCondition<PyStringLiteralExpression>(PyStringLiteralExpression.class) {
@Override
public boolean accepts(@Nullable Object o, ProcessingContext context) {
if (o instanceof PyStringLiteralExpression) {
final PyStringLiteralExpression expr = (PyStringLiteralExpression)o;
if (!DocStringUtil.isDocStringExpression(expr)) {
final String value = expr.getStringValue();
return pattern.matcher(value).matches();
}
}
return false;
}
});
}
public static PyElementPattern.Capture<PyExpression> pyArgument(final String functionName, final int index) {
return new PyElementPattern.Capture<PyExpression>(new InitialPatternCondition<PyExpression>(PyExpression.class) {
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {