From 0428956e2cfa218826e664f0849bfd43f54c348a Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 16 Aug 2012 14:38:20 +0200 Subject: [PATCH] some PSI for template languages in python-psi-api; common superinterface for string literals in Python code and templates --- .../psi/TemplateFunctionCall.java | 29 +++++++++++++++++++ .../psi/TemplateStringLiteral.java | 29 +++++++++++++++++++ .../python/psi/PyStringLiteralExpression.java | 4 +-- .../python/psi/StringLiteralExpression.java | 14 +++++++++ .../impl/PyStringLiteralExpressionImpl.java | 12 ++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateFunctionCall.java create mode 100644 python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateStringLiteral.java create mode 100644 python/psi-api/src/com/jetbrains/python/psi/StringLiteralExpression.java diff --git a/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateFunctionCall.java b/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateFunctionCall.java new file mode 100644 index 000000000000..5daaade764e6 --- /dev/null +++ b/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateFunctionCall.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2012 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.templateLanguages.psi; + +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.Nullable; + +/** + * A function call in a template file. + * + * @author yole + */ +public interface TemplateFunctionCall extends PsiElement { + @Nullable + PsiElement getCallee(); +} diff --git a/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateStringLiteral.java b/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateStringLiteral.java new file mode 100644 index 000000000000..d506047fc388 --- /dev/null +++ b/python/openapi/src/com/jetbrains/python/templateLanguages/psi/TemplateStringLiteral.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2012 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.templateLanguages.psi; + +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import com.jetbrains.python.psi.StringLiteralExpression; + +/** + * A string literal in a template file. + * + * @author yole + */ +public interface TemplateStringLiteral extends PsiElement, StringLiteralExpression { + TextRange getStringValueTextRange(); +} diff --git a/python/psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java b/python/psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java index c2ac5bb835a6..1737d874f8c2 100644 --- a/python/psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java +++ b/python/psi-api/src/com/jetbrains/python/psi/PyStringLiteralExpression.java @@ -6,9 +6,7 @@ import com.intellij.psi.PsiLanguageInjectionHost; import java.util.List; -public interface PyStringLiteralExpression extends PyLiteralExpression, PsiLanguageInjectionHost { - String getStringValue(); - +public interface PyStringLiteralExpression extends PyLiteralExpression, StringLiteralExpression, PsiLanguageInjectionHost { List getStringValueTextRanges(); List getStringNodes(); diff --git a/python/psi-api/src/com/jetbrains/python/psi/StringLiteralExpression.java b/python/psi-api/src/com/jetbrains/python/psi/StringLiteralExpression.java new file mode 100644 index 000000000000..689d2f683edf --- /dev/null +++ b/python/psi-api/src/com/jetbrains/python/psi/StringLiteralExpression.java @@ -0,0 +1,14 @@ +package com.jetbrains.python.psi; + +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; + +/** + * A generic string literal interface (used both in Python code and template files). + * + * @author yole + */ +public interface StringLiteralExpression extends PsiElement { + String getStringValue(); + TextRange getStringValueTextRange(); +} diff --git a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java index 2bd4c0496ad3..b70c85fdbdfd 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionImpl.java @@ -167,6 +167,18 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt return stringValue; } + @Override + public TextRange getStringValueTextRange() { + List allRanges = getStringValueTextRanges(); + if (allRanges.size() == 1) { + return allRanges.get(0); + } + if (allRanges.size() > 1) { + return new TextRange(allRanges.get(0).getStartOffset(), allRanges.get(allRanges.size()-1).getEndOffset()); + } + return new TextRange(0, getTextLength()); + } + private static boolean iterateCharacterRanges(TextRangeConsumer consumer, String undecoded, int off, boolean raw, boolean unicode) { if (raw) { return iterateRawCharacterRanges(consumer, undecoded, off, unicode);