From c3d05a977a0c6fc8ae871cb0f3a142bdfc230225 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 21 Nov 2017 05:35:35 +0300 Subject: [PATCH] PY-26963: Create language for Django 2.0 url syntax --- .../jetbrains/extensions/RegExpPatternExt.kt | 7 ++++ .../extensions/StringLiteralExpressionExt.kt | 34 +++++++++++++++++++ .../com/jetbrains/env/PyEnvTestCase.java | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 python/src/com/jetbrains/extensions/RegExpPatternExt.kt create mode 100644 python/src/com/jetbrains/extensions/StringLiteralExpressionExt.kt diff --git a/python/src/com/jetbrains/extensions/RegExpPatternExt.kt b/python/src/com/jetbrains/extensions/RegExpPatternExt.kt new file mode 100644 index 000000000000..bdb5f51d5228 --- /dev/null +++ b/python/src/com/jetbrains/extensions/RegExpPatternExt.kt @@ -0,0 +1,7 @@ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.jetbrains.extensions + +import org.intellij.lang.regexp.psi.RegExpPattern +import java.util.regex.Pattern + +fun RegExpPattern.asPattern() = Pattern.compile(text)!! \ No newline at end of file diff --git a/python/src/com/jetbrains/extensions/StringLiteralExpressionExt.kt b/python/src/com/jetbrains/extensions/StringLiteralExpressionExt.kt new file mode 100644 index 000000000000..9697b9e8239f --- /dev/null +++ b/python/src/com/jetbrains/extensions/StringLiteralExpressionExt.kt @@ -0,0 +1,34 @@ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.jetbrains.extensions + +import com.jetbrains.python.FunctionParameter +import com.jetbrains.python.psi.PyArgumentList +import com.jetbrains.python.psi.PyExpression +import com.jetbrains.python.psi.PyKeywordArgument +import com.jetbrains.python.psi.StringLiteralExpression + + +/** + * If literal is argument of some function call -- return callee + */ +fun StringLiteralExpression.getCalleeIfArgument(argumentPosition: Int, argumentName: String? = null): PyExpression? { + val parent = this.parent ?: return null + + val argumentList = when (parent) { + is PyArgumentList -> parent + is PyKeywordArgument -> parent.parent as? PyArgumentList + else -> null + } ?: return null + + if ((this == argumentList.arguments.getOrNull(argumentPosition)) + || + (this == argumentName?.run { argumentList.getKeywordArgument(this@run)?.valueExpression })) { + return argumentList.callExpression?.callee + } + return null +} + +/** + * @see getCalleeIfArgument + */ +fun StringLiteralExpression.getCalleeIfArgument(argument: FunctionParameter) = this.getCalleeIfArgument(argument.position, argument.name) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/env/PyEnvTestCase.java b/python/testSrc/com/jetbrains/env/PyEnvTestCase.java index 436f9c8790fe..429b14bb9ad8 100644 --- a/python/testSrc/com/jetbrains/env/PyEnvTestCase.java +++ b/python/testSrc/com/jetbrains/env/PyEnvTestCase.java @@ -38,7 +38,7 @@ import java.util.*; /** * @author traff * -* All inhertors must be in {@link com.jetbrains.env.}.* +* All inhertors must be in {@link com.jetbrains.env}.* */ public abstract class PyEnvTestCase { private static final Logger LOG = Logger.getInstance(PyEnvTestCase.class.getName());